4
"""Parses a UltiSnips snippet definition and launches it into Vim."""
6
from UltiSnips.snippet.parsing._base import tokenize_snippet_text, finalize
7
from UltiSnips.snippet.parsing._lexer import EscapeCharToken, \
8
VisualToken, TransformationToken, TabStopToken, MirrorToken, \
9
PythonCodeToken, VimLCodeToken, ShellCodeToken
10
from UltiSnips.text_objects import EscapedChar, Mirror, PythonCode, \
11
ShellCode, TabStop, Transformation, VimLCode, Visual
13
_TOKEN_TO_TEXTOBJECT = {
14
EscapeCharToken: EscapedChar,
16
ShellCodeToken: ShellCode,
17
PythonCodeToken: PythonCode,
18
VimLCodeToken: VimLCode,
22
EscapeCharToken, VisualToken, TransformationToken, TabStopToken,
23
MirrorToken, PythonCodeToken, VimLCodeToken, ShellCodeToken
26
def _resolve_ambiguity(all_tokens, seen_ts):
27
"""$1 could be a Mirror or a TabStop. This figures this out."""
28
for parent, token in all_tokens:
29
if isinstance(token, MirrorToken):
30
if token.number not in seen_ts:
31
seen_ts[token.number] = TabStop(parent, token)
33
Mirror(parent, seen_ts[token.number], token)
35
def _create_transformations(all_tokens, seen_ts):
36
"""Create the objects that need to know about tabstops."""
37
for parent, token in all_tokens:
38
if isinstance(token, TransformationToken):
39
if token.number not in seen_ts:
41
"Tabstop %i is not known but is used by a Transformation"
43
Transformation(parent, seen_ts[token.number], token)
46
def parse_and_instantiate(parent_to, text, indent):
47
"""Parses a snippet definition in UltiSnips format from 'text' assuming the
48
current 'indent'. Will instantiate all the objects and link them as
49
children to parent_to. Will also put the initial text into Vim."""
50
all_tokens, seen_ts = tokenize_snippet_text(parent_to, text, indent,
51
__ALLOWED_TOKENS, __ALLOWED_TOKENS, _TOKEN_TO_TEXTOBJECT)
52
_resolve_ambiguity(all_tokens, seen_ts)
53
_create_transformations(all_tokens, seen_ts)
54
finalize(all_tokens, seen_ts, parent_to)