Refactored parsing of Snippets files a bit to make it easier to extend the syntax
This commit is contained in:
parent
5eaed114db
commit
1236d0ab86
@ -11,6 +11,51 @@ from UltiSnips.Geometry import Position
|
|||||||
from UltiSnips.TextObjects import *
|
from UltiSnips.TextObjects import *
|
||||||
from UltiSnips.Buffer import VimBuffer
|
from UltiSnips.Buffer import VimBuffer
|
||||||
|
|
||||||
|
class _SnippetsFileParser(object):
|
||||||
|
def __init__(self, ft, fn, snip_manager):
|
||||||
|
self._sm = snip_manager
|
||||||
|
self._ft = ft
|
||||||
|
self._lines = open(fn).readlines()
|
||||||
|
|
||||||
|
self._idx = 0
|
||||||
|
|
||||||
|
def _parse_snippet(self):
|
||||||
|
line = self._lines[self._idx]
|
||||||
|
|
||||||
|
cdescr = ""
|
||||||
|
coptions = ""
|
||||||
|
|
||||||
|
cs = line.split()[1]
|
||||||
|
left = line.find('"')
|
||||||
|
if left != -1:
|
||||||
|
right = line.rfind('"')
|
||||||
|
cdescr = line[left+1:right]
|
||||||
|
coptions = line[right:].strip()
|
||||||
|
|
||||||
|
self._idx += 1
|
||||||
|
cv = ""
|
||||||
|
while 1:
|
||||||
|
line = self._lines[self._idx]
|
||||||
|
if line.startswith("endsnippet"):
|
||||||
|
cv = cv[:-1] # Chop the last newline
|
||||||
|
self._sm.add_snippet(cs, cv, cdescr, coptions, self._ft)
|
||||||
|
break
|
||||||
|
|
||||||
|
cv += line
|
||||||
|
self._idx += 1
|
||||||
|
|
||||||
|
def parse(self):
|
||||||
|
while self._idx < len(self._lines):
|
||||||
|
line = self._lines[self._idx]
|
||||||
|
|
||||||
|
if not line.startswith('#'):
|
||||||
|
if line.startswith("snippet"):
|
||||||
|
self._parse_snippet()
|
||||||
|
|
||||||
|
self._idx += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Snippet(object):
|
class Snippet(object):
|
||||||
_INDENT = re.compile(r"^[ \t]*")
|
_INDENT = re.compile(r"^[ \t]*")
|
||||||
|
|
||||||
@ -206,12 +251,12 @@ class SnippetManager(object):
|
|||||||
if not rv:
|
if not rv:
|
||||||
self._handle_failure(self.expand_trigger)
|
self._handle_failure(self.expand_trigger)
|
||||||
|
|
||||||
def add_snippet(self, trigger, value, descr, options):
|
def add_snippet(self, trigger, value, descr, options, ft = "all"):
|
||||||
if "all" not in self._snippets:
|
if ft not in self._snippets:
|
||||||
self._snippets["all"] = {}
|
self._snippets[ft] = {}
|
||||||
l = self._snippets["all"].get(trigger,[])
|
l = self._snippets[ft].get(trigger,[])
|
||||||
l.append(Snippet(trigger,value, descr, options))
|
l.append(Snippet(trigger, value, descr, options))
|
||||||
self._snippets["all"][trigger] = l
|
self._snippets[ft][trigger] = l
|
||||||
|
|
||||||
|
|
||||||
def backspace_while_selected(self):
|
def backspace_while_selected(self):
|
||||||
@ -525,8 +570,7 @@ class SnippetManager(object):
|
|||||||
"*%s.snippets" % ft
|
"*%s.snippets" % ft
|
||||||
|
|
||||||
for fn in glob.glob(pattern):
|
for fn in glob.glob(pattern):
|
||||||
self._load_snippets_from(ft, fn)
|
_SnippetsFileParser(ft, fn, self).parse()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _find_snippets(self, ft, trigger):
|
def _find_snippets(self, ft, trigger):
|
||||||
|
Loading…
Reference in New Issue
Block a user