Merged my fix for bug 501727
This commit is contained in:
commit
9011515686
@ -24,6 +24,10 @@ def _vim_quote(s):
|
|||||||
"""Quote string s as Vim literal string."""
|
"""Quote string s as Vim literal string."""
|
||||||
return "'" + s.replace("'", "''") + "'"
|
return "'" + s.replace("'", "''") + "'"
|
||||||
|
|
||||||
|
def feedkeys(s, mode='n'):
|
||||||
|
"""Wrapper around vim's feedkeys function. Mainly for convenience."""
|
||||||
|
vim.command(r'call feedkeys("%s", "%s")' % (s, mode))
|
||||||
|
|
||||||
class _SnippetDictionary(object):
|
class _SnippetDictionary(object):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self._snippets = []
|
self._snippets = []
|
||||||
@ -135,7 +139,7 @@ class _SnippetsFileParser(object):
|
|||||||
cs = cs[1:-1]
|
cs = cs[1:-1]
|
||||||
|
|
||||||
return (snip, cs, cdescr, coptions)
|
return (snip, cs, cdescr, coptions)
|
||||||
|
|
||||||
def _parse_snippet(self):
|
def _parse_snippet(self):
|
||||||
line = self._line()
|
line = self._line()
|
||||||
|
|
||||||
@ -364,6 +368,44 @@ class Snippet(object):
|
|||||||
return SnippetInstance(parent, indent, v, start,
|
return SnippetInstance(parent, indent, v, start,
|
||||||
end, last_re = self._last_re, globals = self._globals)
|
end, last_re = self._last_re, globals = self._globals)
|
||||||
|
|
||||||
|
class LangMapTranslator(object):
|
||||||
|
"""
|
||||||
|
This object cares for the vim langmap option and basically reverses
|
||||||
|
the mappings. This was the only solution to get UltiSnips to work
|
||||||
|
nicely with langmap; other stuff I tried was using inoremap movement
|
||||||
|
commands and caching and restoring the langmap option.
|
||||||
|
|
||||||
|
Note that this will not work if the langmap overwrites a character
|
||||||
|
completely, for example if 'j' is remapped, but nothing is mapped
|
||||||
|
back to 'j', then moving one line down is no longer possible and
|
||||||
|
UltiSnips will fail.
|
||||||
|
"""
|
||||||
|
_maps = {}
|
||||||
|
|
||||||
|
def _create_translation(self, langmap):
|
||||||
|
from_chars, to_chars = "", ""
|
||||||
|
for c in langmap.split(','):
|
||||||
|
if ";" in c:
|
||||||
|
a,b = c.split(';')
|
||||||
|
from_chars += a
|
||||||
|
to_chars += b
|
||||||
|
else:
|
||||||
|
from_chars += c[::2]
|
||||||
|
to_chars += c[1::2]
|
||||||
|
|
||||||
|
self._maps[langmap] = string.maketrans(to_chars, from_chars)
|
||||||
|
|
||||||
|
def translate(self, s):
|
||||||
|
langmap = vim.eval("&langmap").strip()
|
||||||
|
if langmap == "":
|
||||||
|
return s
|
||||||
|
|
||||||
|
if langmap not in self._maps:
|
||||||
|
self._create_translation(langmap)
|
||||||
|
|
||||||
|
return s.translate(self._maps[langmap])
|
||||||
|
|
||||||
|
|
||||||
class VimState(object):
|
class VimState(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._abs_pos = None
|
self._abs_pos = None
|
||||||
@ -419,9 +461,9 @@ class VimState(object):
|
|||||||
|
|
||||||
if delta.line == delta.col == 0:
|
if delta.line == delta.col == 0:
|
||||||
if col == 0 or vim.eval("mode()") != 'i':
|
if col == 0 or vim.eval("mode()") != 'i':
|
||||||
vim.command(r'call feedkeys("\<Esc>i")')
|
feedkeys(r"\<Esc>i")
|
||||||
else:
|
else:
|
||||||
vim.command(r'call feedkeys("\<Esc>a")')
|
feedkeys(r"\<Esc>a")
|
||||||
else:
|
else:
|
||||||
if delta.line:
|
if delta.line:
|
||||||
move_lines = "%ij" % delta.line
|
move_lines = "%ij" % delta.line
|
||||||
@ -442,9 +484,11 @@ class VimState(object):
|
|||||||
else:
|
else:
|
||||||
do_select = "%ih" % (-delta.col+1)
|
do_select = "%ih" % (-delta.col+1)
|
||||||
|
|
||||||
|
move_cmd = LangMapTranslator().translate(
|
||||||
|
r"\<Esc>%sv%s%s\<c-g>" % (move_one_right, move_lines, do_select)
|
||||||
|
)
|
||||||
|
|
||||||
vim.command(r'call feedkeys("\<Esc>%sv%s%s\<c-g>")' %
|
feedkeys(move_cmd)
|
||||||
(move_one_right, move_lines, do_select))
|
|
||||||
|
|
||||||
|
|
||||||
def buf_changed(self):
|
def buf_changed(self):
|
||||||
@ -556,10 +600,10 @@ class SnippetManager(object):
|
|||||||
|
|
||||||
if self._cs and (self._span_selected is not None):
|
if self._cs and (self._span_selected is not None):
|
||||||
# This only happens when a default value is delted using backspace
|
# This only happens when a default value is delted using backspace
|
||||||
vim.command(r'call feedkeys("i")')
|
feedkeys(r"i")
|
||||||
self._chars_entered('')
|
self._chars_entered('')
|
||||||
else:
|
else:
|
||||||
vim.command(r'call feedkeys("\<BS>")')
|
feedkeys(r"\<BS>")
|
||||||
|
|
||||||
def cursor_moved(self):
|
def cursor_moved(self):
|
||||||
self._vstate.update()
|
self._vstate.update()
|
||||||
@ -709,7 +753,7 @@ class SnippetManager(object):
|
|||||||
break
|
break
|
||||||
|
|
||||||
if feedkey:
|
if feedkey:
|
||||||
vim.command(r'call feedkeys("%s", "%s")' % (feedkey, mode))
|
feedkeys(feedkey, mode)
|
||||||
|
|
||||||
def _ensure_snippets_loaded(self):
|
def _ensure_snippets_loaded(self):
|
||||||
filetypes = vim.eval("&filetype").split(".") + [ "all" ]
|
filetypes = vim.eval("&filetype").split(".") + [ "all" ]
|
||||||
|
20
test.py
20
test.py
@ -1841,6 +1841,26 @@ endsnippet
|
|||||||
wanted = "x first a bob b y"
|
wanted = "x first a bob b y"
|
||||||
|
|
||||||
|
|
||||||
|
###########################################################################
|
||||||
|
# Test for bug 501727 #
|
||||||
|
###########################################################################
|
||||||
|
class TestNonEmptyLangmap_ExceptCorrectResult(_VimTest):
|
||||||
|
snippets = ("testme",
|
||||||
|
"""my snipped ${1:some_default}
|
||||||
|
and a mirror: $1
|
||||||
|
$2...$3
|
||||||
|
$0""")
|
||||||
|
keys = "testme" + EX + "hi1" + JF + "hi2" + JF + "hi3" + JF + "hi4"
|
||||||
|
wanted ="""my snipped hi1
|
||||||
|
and a mirror: hi1
|
||||||
|
hi2...hi3
|
||||||
|
hi4"""
|
||||||
|
|
||||||
|
def _options_on(self):
|
||||||
|
self.send(":set langmap=dj,rk,nl,ln,jd,kr,DJ,RK,NL,LN,JD,KR\n")
|
||||||
|
def _options_off(self):
|
||||||
|
self.send(":set langmap=\n")
|
||||||
|
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# END OF TEST #
|
# END OF TEST #
|
||||||
###########################################################################
|
###########################################################################
|
||||||
|
Loading…
Reference in New Issue
Block a user