Improve global snippets

* `todo`, `note` now do not add comment strings when in a comment line
* `fixme` added to match `todo` and `note
* `date` renamed `datetime` and updated to match the unix `date` command
* `date`, `time` added inserting only the date or time now
* `utc` updated, no longer spitting the timezone offset
This commit is contained in:
Kenneth Benzie 2019-03-30 17:07:29 +00:00
parent 9d6a9925e5
commit baff9ae3fd

View File

@ -5,69 +5,85 @@ Kenneth Benzie (Benie) <k.benzie83@gmail.com>
endsnippet endsnippet
global !p global !p
comment=vim.current.buffer.options['commentstring'].decode('utf-8').strip() class Comment(object):
def __init__(self):
# Get the commentstring.
commentstring = snip.opt('&commentstring').strip()
# Slipt it into before and after parts.
commentparts = [part.strip() for part in commentstring.split('%s')]
self.commentbefore = commentparts[0] + ' '
self.commentafter = ' ' + commentparts[1] if commentparts[1] else ''
# Determine if we are in an existing comment.
line = vim.current.buffer[vim.current.window.cursor[0] - 1].strip()
# If interpolation has not occcured and the current line starts with the
# comment leader, we are in a comment.
self.incomment = not snip.c and line.startswith(commentparts[0])
def commentbefore(): def before(self):
commentbefore = comment[0:comment.find('%s')].strip() return '' if self.incomment else self.commentbefore
row, col = vim.current.window.cursor
row -= 1
col -= 1
line = vim.current.buffer[row]
if (col - 1) >= 0 and commentbefore == line[col - 1:len(commentbefore)]:
return ''
return commentbefore + ' '
def commentafter(): def after(self):
after=comment[comment.find('%s')+2:].strip() return '' if self.incomment else self.commentafter
if 0 < len(after): endglobal
return ' ' + after
return ''
import datetime snippet todo "TODO commment"
import time `!p comment=Comment()
snip.rv=comment.before()`TODO${1/.+/(/}$1${1/.+/)/}: $0`!p snip.rv=comment.after()`
endsnippet
def date(): snippet fixme "FIXME comment"
return datetime.datetime.now().strftime('%B %d, %Y') `!p comment=Comment()
snip.rv=comment.before()`FIXME${1/.+/(/}$1${1/.+/)/}: $0`!p snip.rv=comment.after()`
endsnippet
class LocalTZ(datetime.tzinfo): snippet note "NOTE comment"
`!p comment=Comment()
snip.rv=comment.before()`NOTE: $0`!p snip.rv=comment.after()`
endsnippet
global !p
from datetime import datetime, timedelta, tzinfo
from time import daylight, gmtime, localtime, timezone, tzname
class LocalTZ(tzinfo):
"""Query OS for local timezone offset.""" """Query OS for local timezone offset."""
def __init__(self): def __init__(self):
"""Initialize LocalTZ.""" """Initialize LocalTZ."""
datetime.tzinfo.__init__(self) tzinfo.__init__(self)
self._unixEpochOrdinal = datetime.datetime.utcfromtimestamp( self.unixEpochOrdinal = datetime.utcfromtimestamp(
0).toordinal() 0).toordinal()
def dst(self, _): def dst(self, _):
return datetime.timedelta(0) return timedelta(0)
def utcoffset(self, dt): def utcoffset(self, dt):
t = ((dt.toordinal() - self._unixEpochOrdinal) * 86400 + dt.hour * 3600 t = ((dt.toordinal() - self.unixEpochOrdinal) * 86400 + dt.hour * 3600
+ dt.second + time.timezone) + dt.second + timezone)
utc = datetime.datetime(*time.gmtime(t)[:6]) utc = datetime(*gmtime(t)[:6])
local = datetime.datetime(*time.localtime(t)[:6]) local = datetime(*localtime(t)[:6])
return local - utc return local - utc
def utc(): def tzname(self, dt):
value = datetime.datetime.strftime( if daylight and localtime().tm_isdst:
datetime.datetime.now(LocalTZ()), '%Y-%m-%dT%H:%M:%S%z') return tzname[daylight]
return '%s:%s' % (value[:-2], value[-2:]) return tzname[0]
endglobal endglobal
snippet date "Today's date" snippet date "date now"
`!p snip.rv=date()` `!p snip.rv=datetime.now().strftime('%a %d %b %Y')`
endsnippet
snippet time "time now"
`!p snip.rv=datetime.now().strftime('%H:%M:%S')`
endsnippet
snippet datetime "date and time now"
`!p snip.rv=datetime.now(LocalTZ()).strftime('%a %d %b %Y %H:%M:%S %Z')`
endsnippet endsnippet
snippet utc "UTC date time now" i snippet utc "UTC date time now" i
`!p snip.rv=utc()` `!p snip.rv=datetime.now(LocalTZ()).strftime('%Y-%m-%dT%H:%M:%S%z')`
endsnippet
snippet todo "TODO commment"
`!p snip.rv=commentbefore()`TODO${1/.+/(/}$1${1/.+/)/}: $0`!p snip.rv=commentafter()`
endsnippet
snippet note "NOTE comment"
`!p snip.rv=commentbefore()`NOTE: $0`!p snip.rv=commentafter()`
endsnippet endsnippet
snippet *x "GitHub style checkbox" snippet *x "GitHub style checkbox"