Encapsulated the translation of the langmap including caching of results into a own object
This commit is contained in:
parent
1637cbef8a
commit
e3d9896cf2
@ -368,6 +368,44 @@ class Snippet(object):
|
||||
return SnippetInstance(parent, indent, v, start,
|
||||
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):
|
||||
def __init__(self):
|
||||
self._abs_pos = None
|
||||
@ -446,30 +484,11 @@ class VimState(object):
|
||||
else:
|
||||
do_select = "%ih" % (-delta.col+1)
|
||||
|
||||
def unmap(s):
|
||||
from_chars, to_chars = "", ""
|
||||
for c in vim.eval("&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]
|
||||
move_cmd = LangMapTranslator().translate(
|
||||
r"\<Esc>%sv%s%s\<c-g>" % (move_one_right, move_lines, do_select)
|
||||
)
|
||||
|
||||
rv = ""
|
||||
for c in s:
|
||||
if c not in from_chars:
|
||||
rv += c
|
||||
continue
|
||||
if c not in to_chars:
|
||||
raise RuntimeError, "langmap overwrite %s" % c
|
||||
rv += from_chars[to_chars.index(c)]
|
||||
|
||||
return rv
|
||||
|
||||
feedkeys(unmap(r"\<Esc>%sv%s%s\<c-g>" %
|
||||
(move_one_right, move_lines, do_select)))
|
||||
feedkeys(move_cmd)
|
||||
|
||||
|
||||
def buf_changed(self):
|
||||
|
Loading…
Reference in New Issue
Block a user