Intermediate state. Multiple text tabstops are already working; testing it does not work, because i can't type from a script
This commit is contained in:
parent
a60e392a66
commit
1926c49f96
93
PySnipEmu.py
93
PySnipEmu.py
@ -28,8 +28,51 @@ class TabStop(object):
|
|||||||
return self._no
|
return self._no
|
||||||
number = property(number)
|
number = property(number)
|
||||||
|
|
||||||
|
class SnippetInstance(object):
|
||||||
|
def __init__(self,start,end, ts):
|
||||||
|
self._start = start
|
||||||
|
self._end = end
|
||||||
|
self._ts = ts
|
||||||
|
|
||||||
|
self._cts = 1
|
||||||
|
|
||||||
|
def select_next_tab(self):
|
||||||
|
if self._cts not in self._ts:
|
||||||
|
if 0 in self._ts:
|
||||||
|
self._cts = 0
|
||||||
|
else:
|
||||||
|
self._cts = 1
|
||||||
|
|
||||||
|
ts = self._ts[self._cts]
|
||||||
|
lineno, col = self._start
|
||||||
|
|
||||||
|
newline = lineno + ts.line_idx
|
||||||
|
if newline == lineno:
|
||||||
|
newcol = col + ts.span[0]
|
||||||
|
endcol = col + ts.span[1]
|
||||||
|
else:
|
||||||
|
newcol = ts.span[0]
|
||||||
|
endcol = ts.span[1]
|
||||||
|
|
||||||
|
self._cts += 1
|
||||||
|
|
||||||
|
vim.current.window.cursor = newline, newcol
|
||||||
|
|
||||||
|
# Select the word
|
||||||
|
|
||||||
|
# Depending on the current mode and position, we
|
||||||
|
# might need to move escape out of the mode and this
|
||||||
|
# will move our cursor one left
|
||||||
|
if newcol != 0 and vim.eval("mode()") == 'i':
|
||||||
|
move_one_right = "l"
|
||||||
|
else:
|
||||||
|
move_one_right = ""
|
||||||
|
|
||||||
|
vim.command(r'call feedkeys("\<Esc>%sv%il\<c-g>")'
|
||||||
|
% (move_one_right, endcol-newcol-1))
|
||||||
|
|
||||||
class Snippet(object):
|
class Snippet(object):
|
||||||
_TB_EXPR = re.compile(r'\$(?:(?:{(\d+):(.*)})|(\d+))')
|
_TB_EXPR = re.compile(r'\$(?:(?:{(\d+):(.*?)})|(\d+))')
|
||||||
|
|
||||||
def __init__(self,trigger,value):
|
def __init__(self,trigger,value):
|
||||||
self._t = trigger
|
self._t = trigger
|
||||||
@ -40,7 +83,7 @@ class Snippet(object):
|
|||||||
trigger = property(trigger)
|
trigger = property(trigger)
|
||||||
|
|
||||||
def _find_text_tabstops(self, lines):
|
def _find_text_tabstops(self, lines):
|
||||||
tabstops = []
|
tabstops = {}
|
||||||
|
|
||||||
for idx in range(len(lines)):
|
for idx in range(len(lines)):
|
||||||
line = lines[idx]
|
line = lines[idx]
|
||||||
@ -61,12 +104,10 @@ class Snippet(object):
|
|||||||
|
|
||||||
lines[idx] = line
|
lines[idx] = line
|
||||||
|
|
||||||
tabstops.append( (ts.number, ts) )
|
tabstops[ts.number] = ts
|
||||||
|
|
||||||
m = self._TB_EXPR.search(line)
|
m = self._TB_EXPR.search(line)
|
||||||
|
|
||||||
tabstops.sort()
|
|
||||||
|
|
||||||
return tabstops
|
return tabstops
|
||||||
|
|
||||||
def _replace_tabstops(self):
|
def _replace_tabstops(self):
|
||||||
@ -76,7 +117,7 @@ class Snippet(object):
|
|||||||
|
|
||||||
return ts, lines
|
return ts, lines
|
||||||
|
|
||||||
def put(self, before, after):
|
def launch(self, before, after):
|
||||||
lineno,col = vim.current.window.cursor
|
lineno,col = vim.current.window.cursor
|
||||||
|
|
||||||
col -= len(self._t)
|
col -= len(self._t)
|
||||||
@ -84,23 +125,15 @@ class Snippet(object):
|
|||||||
ts,lines = self._replace_tabstops()
|
ts,lines = self._replace_tabstops()
|
||||||
|
|
||||||
endcol = None
|
endcol = None
|
||||||
if len(ts):
|
newline = 1
|
||||||
zts = ts[0][1]
|
newcol = 0
|
||||||
newline = lineno + zts.line_idx
|
if not len(ts):
|
||||||
if newline == lineno:
|
|
||||||
newcol = col + zts.span[0]
|
|
||||||
endcol = col + zts.span[1]
|
|
||||||
else:
|
|
||||||
newcol = zts.span[0]
|
|
||||||
endcol = zts.span[1]
|
|
||||||
else:
|
|
||||||
newline = lineno + len(lines) - 1
|
newline = lineno + len(lines) - 1
|
||||||
if len(lines) == 1:
|
if len(lines) == 1:
|
||||||
newcol = col + len(lines[-1])
|
newcol = col + len(lines[-1])
|
||||||
else:
|
else:
|
||||||
newcol = len(lines[-1])
|
newcol = len(lines[-1])
|
||||||
|
|
||||||
|
|
||||||
lines[0] = before + lines[0]
|
lines[0] = before + lines[0]
|
||||||
lines[-1] += after
|
lines[-1] += after
|
||||||
|
|
||||||
@ -108,14 +141,21 @@ class Snippet(object):
|
|||||||
|
|
||||||
vim.current.window.cursor = newline, newcol
|
vim.current.window.cursor = newline, newcol
|
||||||
|
|
||||||
# if endcol:
|
if len(ts):
|
||||||
# # Select the word
|
s = SnippetInstance( (lineno,col), (newline,newcol), ts)
|
||||||
# vim.command("insert PyVimSnips_SelectWord(%i)" % (endcol-newcol))
|
|
||||||
|
s.select_next_tab()
|
||||||
|
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
class SnippetManager(object):
|
class SnippetManager(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.reset()
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
self.clear_snippets()
|
self.clear_snippets()
|
||||||
|
self._current_snippets = []
|
||||||
|
|
||||||
def add_snippet(self,trigger,value):
|
def add_snippet(self,trigger,value):
|
||||||
self._snippets[trigger] = Snippet(trigger,value)
|
self._snippets[trigger] = Snippet(trigger,value)
|
||||||
@ -124,6 +164,10 @@ class SnippetManager(object):
|
|||||||
self._snippets = {}
|
self._snippets = {}
|
||||||
|
|
||||||
def try_expand(self):
|
def try_expand(self):
|
||||||
|
if len(self._current_snippets):
|
||||||
|
self._current_snippets[-1].select_next_tab()
|
||||||
|
return
|
||||||
|
|
||||||
line = vim.current.line
|
line = vim.current.line
|
||||||
|
|
||||||
dummy,col = vim.current.window.cursor
|
dummy,col = vim.current.window.cursor
|
||||||
@ -136,7 +180,14 @@ class SnippetManager(object):
|
|||||||
|
|
||||||
word = before.split()[-1]
|
word = before.split()[-1]
|
||||||
if word in self._snippets:
|
if word in self._snippets:
|
||||||
self._snippets[word].put(before.rstrip()[:-len(word)], after)
|
s = self._snippets[word].launch(before.rstrip()[:-len(word)], after)
|
||||||
|
if s is not None:
|
||||||
|
self._current_snippets.append(s)
|
||||||
|
|
||||||
|
def cursor_moved(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def entered_insert_mode(self):
|
||||||
|
pass
|
||||||
|
|
||||||
PySnipSnippets = SnippetManager()
|
PySnipSnippets = SnippetManager()
|
||||||
|
@ -24,13 +24,22 @@ endfunction
|
|||||||
python from PySnipEmu import PySnipSnippets
|
python from PySnipEmu import PySnipSnippets
|
||||||
|
|
||||||
inoremap <Tab> <C-R>=PyVimSnips_ExpandSnippet()<cr>
|
inoremap <Tab> <C-R>=PyVimSnips_ExpandSnippet()<cr>
|
||||||
|
snoremap <Tab> <Esc>:call PyVimSnips_ExpandSnippet()<cr>
|
||||||
|
|
||||||
|
python PySnipSnippets.add_snippet("echo","echo ${1:Hallo}")
|
||||||
python PySnipSnippets.add_snippet("hello", "Hallo Welt!\nUnd Wie gehts?")
|
python PySnipSnippets.add_snippet("hello", "Hallo Welt!\nUnd Wie gehts?")
|
||||||
python PySnipSnippets.add_snippet("echo","$0 run")
|
python PySnipSnippets.add_snippet("hallo", "hallo ${0:End} ${1:Beginning}")
|
||||||
|
|
||||||
|
|
||||||
python PySnipSnippets.add_snippet("if", "if(${1:/* condition */})\n{\n${0:/* code */}\n}")
|
|
||||||
|
|
||||||
|
|
||||||
|
python << EOF
|
||||||
|
PySnipSnippets.add_snippet("if",
|
||||||
|
"""if(${1:/* condition */}) {
|
||||||
|
${2:/* code */}
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
EOF
|
||||||
|
|
||||||
|
au CursorMovedI * py PySnipSnippets.cursor_moved()
|
||||||
|
au InsertEnter * py PySnipSnippets.entered_insert_mode()
|
||||||
|
|
||||||
|
|
||||||
|
48
test.py
48
test.py
@ -10,6 +10,11 @@ from PySnipEmu import PySnipSnippets
|
|||||||
|
|
||||||
class _VimTest(unittest.TestCase):
|
class _VimTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
PySnipSnippets.reset()
|
||||||
|
|
||||||
|
for sv,content in self.snippets:
|
||||||
|
PySnipSnippets.add_snippet(sv,content)
|
||||||
|
|
||||||
vim.command(":new")
|
vim.command(":new")
|
||||||
try:
|
try:
|
||||||
self.cmd()
|
self.cmd()
|
||||||
@ -20,6 +25,9 @@ class _VimTest(unittest.TestCase):
|
|||||||
def insert(self,string):
|
def insert(self,string):
|
||||||
"""A helper function to type some text"""
|
"""A helper function to type some text"""
|
||||||
vim.command('normal i%s' % string)
|
vim.command('normal i%s' % string)
|
||||||
|
def change(self,string):
|
||||||
|
"""A helper function to type some text"""
|
||||||
|
vim.command('normal c%s' % string)
|
||||||
|
|
||||||
def expand(self):
|
def expand(self):
|
||||||
vim.command("call PyVimSnips_ExpandSnippet()")
|
vim.command("call PyVimSnips_ExpandSnippet()")
|
||||||
@ -36,10 +44,9 @@ class _VimTest(unittest.TestCase):
|
|||||||
# Simple Expands #
|
# Simple Expands #
|
||||||
##################
|
##################
|
||||||
class _SimpleExpands(_VimTest):
|
class _SimpleExpands(_VimTest):
|
||||||
def setUp(self):
|
snippets = (
|
||||||
PySnipSnippets.add_snippet("hallo","Hallo Welt!")
|
("hallo", "Hallo Welt!"),
|
||||||
|
)
|
||||||
_VimTest.setUp(self)
|
|
||||||
|
|
||||||
class SimpleExpand_ExceptCorrectResult(_SimpleExpands):
|
class SimpleExpand_ExceptCorrectResult(_SimpleExpands):
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
@ -85,8 +92,11 @@ class ExpandInTheMiddleOfLine_ExceptCorrectResult(_SimpleExpands):
|
|||||||
self.assertEqual(self.output,"Wie Hallo Welt! gehts?")
|
self.assertEqual(self.output,"Wie Hallo Welt! gehts?")
|
||||||
|
|
||||||
class MultilineExpand_ExceptCorrectResult(_VimTest):
|
class MultilineExpand_ExceptCorrectResult(_VimTest):
|
||||||
|
snippets = (
|
||||||
|
("hallo", "Hallo Welt!\nUnd Wie gehts?"),
|
||||||
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
PySnipSnippets.add_snippet("hallo","Hallo Welt!\nUnd Wie gehts?")
|
|
||||||
self.insert("Wie hallo gehts?")
|
self.insert("Wie hallo gehts?")
|
||||||
vim.command("normal 02f ")
|
vim.command("normal 02f ")
|
||||||
self.expand()
|
self.expand()
|
||||||
@ -94,8 +104,11 @@ class MultilineExpand_ExceptCorrectResult(_VimTest):
|
|||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output, "Wie Hallo Welt!\nUnd Wie gehts? gehts?")
|
self.assertEqual(self.output, "Wie Hallo Welt!\nUnd Wie gehts? gehts?")
|
||||||
class MultilineExpandTestTyping_ExceptCorrectResult(_VimTest):
|
class MultilineExpandTestTyping_ExceptCorrectResult(_VimTest):
|
||||||
|
snippets = (
|
||||||
|
("hallo", "Hallo Welt!\nUnd Wie gehts?"),
|
||||||
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
PySnipSnippets.add_snippet("hallo","Hallo Welt!\nUnd Wie gehts?")
|
|
||||||
self.insert("Wie hallo gehts?")
|
self.insert("Wie hallo gehts?")
|
||||||
vim.command("normal 02f ")
|
vim.command("normal 02f ")
|
||||||
self.expand()
|
self.expand()
|
||||||
@ -109,8 +122,11 @@ class MultilineExpandTestTyping_ExceptCorrectResult(_VimTest):
|
|||||||
# TabStops #
|
# TabStops #
|
||||||
############
|
############
|
||||||
class ExitTabStop_ExceptCorrectResult(_VimTest):
|
class ExitTabStop_ExceptCorrectResult(_VimTest):
|
||||||
|
snippets = (
|
||||||
|
("echo", "$0 run"),
|
||||||
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
PySnipSnippets.add_snippet("echo","$0 run")
|
|
||||||
self.insert("echo ")
|
self.insert("echo ")
|
||||||
self.expand()
|
self.expand()
|
||||||
self.insert("test")
|
self.insert("test")
|
||||||
@ -119,14 +135,29 @@ class ExitTabStop_ExceptCorrectResult(_VimTest):
|
|||||||
self.assertEqual(self.output,"test run ")
|
self.assertEqual(self.output,"test run ")
|
||||||
|
|
||||||
class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
|
class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
|
||||||
|
snippets = (
|
||||||
|
("echo", "echo ${1:Hallo}"),
|
||||||
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
PySnipSnippets.add_snippet("echo","echo ${1:Hallo}")
|
|
||||||
self.insert("echo ")
|
self.insert("echo ")
|
||||||
self.expand()
|
self.expand()
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"echo Hallo ")
|
self.assertEqual(self.output,"echo Hallo ")
|
||||||
|
|
||||||
|
class TextTabStopSimpleReplace_ExceptCorrectResult(_VimTest):
|
||||||
|
snippets = (
|
||||||
|
("hallo", "hallo ${0:End} ${1:Beginning}"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def cmd(self):
|
||||||
|
self.insert("hallo ")
|
||||||
|
self.expand()
|
||||||
|
vim.command(r'call feedkeys("na")')
|
||||||
|
|
||||||
|
def runTest(self):
|
||||||
|
self.assertEqual(self.output,"hallo End na ")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
@ -144,6 +175,7 @@ if __name__ == '__main__':
|
|||||||
MultilineExpandTestTyping_ExceptCorrectResult(),
|
MultilineExpandTestTyping_ExceptCorrectResult(),
|
||||||
ExitTabStop_ExceptCorrectResult(),
|
ExitTabStop_ExceptCorrectResult(),
|
||||||
TextTabStopNoReplace_ExceptCorrectResult(),
|
TextTabStopNoReplace_ExceptCorrectResult(),
|
||||||
|
TextTabStopSimpleReplace_ExceptCorrectResult(),
|
||||||
]
|
]
|
||||||
# suite = unittest.TestLoader(.loadTestsFromModule(__import__("test"))
|
# suite = unittest.TestLoader(.loadTestsFromModule(__import__("test"))
|
||||||
suite = unittest.TestSuite()
|
suite = unittest.TestSuite()
|
||||||
|
Loading…
Reference in New Issue
Block a user