Skip to content

Commit 4b9f18c

Browse files
committed
Add a common pythonx directory containing helper methods for snippets.
1 parent 8770132 commit 4b9f18c

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

UltiSnips/rst.snippets

+2-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ from string import Template
1212
import re
1313
from collections import Counter
1414
15+
from vimsnippets import complete
16+
1517
#http://docutils.sourceforge.net/docs/ref/rst/roles.html
1618
TEXT_ROLES = ['emphasis','literal','code','math',
1719
'pep-reference','rfc-reference',
@@ -130,27 +132,6 @@ def get_popular_code_type():
130132
except IndexError:
131133
popular_type = "lua" # Don't break default
132134
return popular_type
133-
134-
135-
def complete(t, opts):
136-
"""
137-
get options that start with t
138-
139-
:param t: query string
140-
:param opts: list that needs to be completed
141-
142-
:return: a string that start with t
143-
"""
144-
msg = "({0})"
145-
if t:
146-
opts = [ m[len(t):] for m in opts if m.startswith(t) ]
147-
if len(opts) == 1:
148-
return opts[0]
149-
150-
if not len(opts):
151-
msg = "{0}"
152-
return msg.format("|".join(opts))
153-
154135
endglobal
155136

156137
snippet part "Part" b

plugin/vimsnippets.vim

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
if exists("b:done_vimsnippets")
2+
finish
3+
endif
4+
let b:done_vimsnippets = 1
5+
6+
" Expanding the path is not needed on Vim 7.4
7+
if &cp || version >= 704
8+
finish
9+
endif
10+
11+
" Add pythonx to the python search path if needed (i.e. <= Vim 7.3).
12+
if !has("python") && !has("python3")
13+
finish
14+
end
15+
16+
" This will fail if UltiSnips is not installed.
17+
try
18+
call UltiSnips#bootstrap#Bootstrap()
19+
catch /E117/
20+
finish
21+
endtry
22+
23+
24+
" This should have been set by UltiSnips, otherwise something is wrong.
25+
if !exists("g:_uspy")
26+
finish
27+
end
28+
29+
30+
" Expand our path
31+
let s:SourcedFile=expand("<sfile>")
32+
exec g:_uspy "import vim, os, sys"
33+
exec g:_uspy "sourced_file = vim.eval('s:SourcedFile')"
34+
exec g:_uspy "while not os.path.exists(os.path.join(sourced_file, 'pythonx')):
35+
\ sourced_file = os.path.dirname(sourced_file)"
36+
exec g:_uspy "module_path = os.path.join(sourced_file, 'pythonx')"
37+
exec g:_uspy "sys.path.append(module_path)"

pythonx/vimsnippets.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Helper methods used in UltiSnips snippets."""
2+
3+
def complete(tab, opts):
4+
"""
5+
get options that start with tab
6+
7+
:param tab: query string
8+
:param opts: list that needs to be completed
9+
10+
:return: a string that start with tab
11+
"""
12+
msg = "({0})"
13+
if tab:
14+
opts = [m[len(tab):] for m in opts if m.startswith(tab)]
15+
if len(opts) == 1:
16+
return opts[0]
17+
18+
if not len(opts):
19+
msg = "{0}"
20+
return msg.format("|".join(opts))

0 commit comments

Comments
 (0)