From 0d17585bf84fd42a3da8b741dcbd11d5eb60534f Mon Sep 17 00:00:00 2001 From: Brian Mock Date: Thu, 12 Dec 2013 23:33:09 -0800 Subject: [PATCH 1/4] Launchpad bug-1179630 Autodetect path search order --- plugin/UltiSnips/__init__.py | 54 ++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index be0b4ef..d0f6bff 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -965,8 +965,7 @@ class SnippetManager(object): paths = _vim.eval("&runtimepath").split(',') - if _vim.eval("exists('g:UltiSnipsDontReverseSearchPath')") == "0" or \ - _vim.eval("g:UltiSnipsDontReverseSearchPath") == "0": + if self._should_reverse_search_path(): paths = paths[::-1] for rtp in paths: @@ -984,6 +983,57 @@ class SnippetManager(object): return ret + def _plugin_dir(self): + def apply_n_times(f, x, n): + if n < 1: + return x + else: + return apply_n_times(f, f(x), n - 1) + + return apply_n_times(os.path.dirname, __file__, 3) + + def _snippets_dir_is_before_plugin_dir(self): + def no_slash(path): + path_dir, path_base = os.path.split(path) + + if path_base == "": + return path_dir + else: + return path + + paths = _vim.eval("&runtimepath").split(',') + paths = map(os.path.expanduser, paths) + paths = map(no_slash, paths) + + home = _vim.eval("$HOME") + + def path_index_of(p): + try: + return paths.index(p) + except ValueError: + return -1 + + def vim_path_index(suffix): + return path_index_of(no_slash(os.path.join(home, suffix))) + + plugin_dir = self._plugin_dir() + + try: + vim1 = vim_path_index(".vim") + vim2 = vim_path_index("vimfiles") + max_vim_dir_index = max(vim1, vim2) + plugin_dir_index = paths.index(plugin_dir) + return plugin_dir_index < max_vim_dir_index + except ValueError: + return False + + def _should_reverse_search_path(self): + reverse_setting = \ + _vim.eval("exists('g:UltiSnipsDontReverseSearchPath')") != "0" and \ + _vim.eval("g:UltiSnipsDontReverseSearchPath") != "0" + + return reverse_setting or not self._snippets_dir_is_before_plugin_dir() + @property def primary_filetype(self): """ Property for the primary filetype. This filetype From 9695633b21a9c3e48a7ff78572f93ed82edc9603 Mon Sep 17 00:00:00 2001 From: Brian Mock Date: Tue, 24 Dec 2013 11:28:27 -0800 Subject: [PATCH 2/4] Cleaned up code; updated docs. --- doc/UltiSnips.txt | 11 +++-- plugin/UltiSnips/__init__.py | 93 ++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 55 deletions(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index f2199d4..0f751ef 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -335,12 +335,17 @@ You can also redefine the search path on a buffer by buffer basis by setting the variable b:UltiSnipsSnippetDirectories. This variable takes precedence over the global variable. -UltiSnips searches in 'runtimepath' for snippet directories but traverses -'runtimepath' in reverse order (last item first). If you would like to have -UltiSnips traverse 'runtimepath' in the standard order, add this to your vimrc +UltiSnips searches in 'runtimepath' for snippet directories and attempts to +determine if 'runtimepath' should be traversed in normal or reverse order. If +you would like to override the order UltiSnips detected for 'runtimepath', add +this to your vimrc file: > + " Traverse in normal order let g:UltiSnipsDontReverseSearchPath="1" + " Traverse in reverse order + let g:UltiSnipsDontReverseSearchPath="0" + By default, whenever a snippet expand is triggered, UltiSnips will check for modifications to the snippet file associated with the filetype and reload it if necessary. This behavior can be disabled as follows: > diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index d0f6bff..ccdf7ca 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -16,6 +16,46 @@ from UltiSnips.text_objects import SnippetInstance from UltiSnips.util import IndentUtil import UltiSnips._vim as _vim +def _plugin_dir(): + d = __file__ + for i in xrange(3): + d = os.path.dirname(d) + + return d + +def _snippets_dir_is_before_plugin_dir(): + def no_slash(path): + path_dir, path_base = os.path.split(path) + + if path_base == "": + return path_dir + else: + return path + + paths = [ no_slash(os.path.expanduser(p)) + for p in _vim.eval("&runtimepath").split(',') ] + + home = _vim.eval("$HOME") + + def vim_path_index(suffix): + path = no_slash(os.path.join(home, suffix)) + try: + return paths.index(path) + except ValueError: + return -1 + + try: + vim_dir_index = max(vim_path_index(".vim"), vim_path_index("vimfiles")) + return paths.index(_plugin_dir()) < vim_dir_index + except ValueError: + return False + +def _should_reverse_search_path(): + if _vim.eval("exists('g:UltiSnipsDontReverseSearchPath')") != "0": + return _vim.eval("g:UltiSnipsDontReverseSearchPath") != "0" + + return not _snippets_dir_is_before_plugin_dir() + def err_to_scratch_buffer(f): @wraps(f) def wrapper(self, *args, **kwds): @@ -965,7 +1005,7 @@ class SnippetManager(object): paths = _vim.eval("&runtimepath").split(',') - if self._should_reverse_search_path(): + if _should_reverse_search_path(): paths = paths[::-1] for rtp in paths: @@ -983,57 +1023,6 @@ class SnippetManager(object): return ret - def _plugin_dir(self): - def apply_n_times(f, x, n): - if n < 1: - return x - else: - return apply_n_times(f, f(x), n - 1) - - return apply_n_times(os.path.dirname, __file__, 3) - - def _snippets_dir_is_before_plugin_dir(self): - def no_slash(path): - path_dir, path_base = os.path.split(path) - - if path_base == "": - return path_dir - else: - return path - - paths = _vim.eval("&runtimepath").split(',') - paths = map(os.path.expanduser, paths) - paths = map(no_slash, paths) - - home = _vim.eval("$HOME") - - def path_index_of(p): - try: - return paths.index(p) - except ValueError: - return -1 - - def vim_path_index(suffix): - return path_index_of(no_slash(os.path.join(home, suffix))) - - plugin_dir = self._plugin_dir() - - try: - vim1 = vim_path_index(".vim") - vim2 = vim_path_index("vimfiles") - max_vim_dir_index = max(vim1, vim2) - plugin_dir_index = paths.index(plugin_dir) - return plugin_dir_index < max_vim_dir_index - except ValueError: - return False - - def _should_reverse_search_path(self): - reverse_setting = \ - _vim.eval("exists('g:UltiSnipsDontReverseSearchPath')") != "0" and \ - _vim.eval("g:UltiSnipsDontReverseSearchPath") != "0" - - return reverse_setting or not self._snippets_dir_is_before_plugin_dir() - @property def primary_filetype(self): """ Property for the primary filetype. This filetype From e9c00a746b45a24a834d53553a741312a3c5c126 Mon Sep 17 00:00:00 2001 From: Brian Mock Date: Wed, 25 Dec 2013 10:44:23 -0800 Subject: [PATCH 3/4] More style cleanup; added docstrings --- doc/UltiSnips.txt | 1 + plugin/UltiSnips/__init__.py | 37 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 0f751ef..d15180d 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -1448,6 +1448,7 @@ Contributors listed in chronological order: Bernhard Vallant (lazerscience) Von Welch (von) Nikola Petrov (nikolavp) + Brian Mock (saikobee) Thank you for your support. diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index ccdf7ca..6fadd6d 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -17,43 +17,42 @@ from UltiSnips.util import IndentUtil import UltiSnips._vim as _vim def _plugin_dir(): + """ Calculates the plugin directory for UltiSnips. This depends on the + current file being 3 levels deep from the plugin directory, so it needs to + be updated if the code moves. + """ d = __file__ for i in xrange(3): d = os.path.dirname(d) - return d def _snippets_dir_is_before_plugin_dir(): - def no_slash(path): - path_dir, path_base = os.path.split(path) - - if path_base == "": - return path_dir - else: - return path - - paths = [ no_slash(os.path.expanduser(p)) - for p in _vim.eval("&runtimepath").split(',') ] - - home = _vim.eval("$HOME") - + """ Returns True if the snippets directory comes before the plugin + directory in Vim's runtime path. False otherwise. + """ def vim_path_index(suffix): - path = no_slash(os.path.join(home, suffix)) + path = os.path.join(home, suffix).rstrip(os.path.sep) try: return paths.index(path) except ValueError: return -1 - + paths = [ os.path.expanduser(p).rstrip(os.path.sep) + for p in _vim.eval("&runtimepath").split(',') ] + home = _vim.eval("$HOME") try: - vim_dir_index = max(vim_path_index(".vim"), vim_path_index("vimfiles")) - return paths.index(_plugin_dir()) < vim_dir_index + real_vim_path_index = max(vim_path_index(".vim"), vim_path_index("vimfiles")) + return paths.index(_plugin_dir()) < real_vim_path_index except ValueError: return False def _should_reverse_search_path(): + """ If the user defined g:UltiSnipsDontReverseSearchPath then return True + or False based on the value of that variable, else defer to + _snippets_dir_is_before_plugin_dir to determine whether this is True or + False. + """ if _vim.eval("exists('g:UltiSnipsDontReverseSearchPath')") != "0": return _vim.eval("g:UltiSnipsDontReverseSearchPath") != "0" - return not _snippets_dir_is_before_plugin_dir() def err_to_scratch_buffer(f): From bc391a330d162e5db1d00fd2e905715b3005290a Mon Sep 17 00:00:00 2001 From: Brian Mock Date: Fri, 27 Dec 2013 10:21:51 -0800 Subject: [PATCH 4/4] Fixed contributors list; saner plugin dir checking. --- doc/UltiSnips.txt | 2 +- plugin/UltiSnips/__init__.py | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index d15180d..f4295c0 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -1395,6 +1395,7 @@ Contributors listed in chronological order: John Szakmeister - jszakmeister Jonas Diemer - diemer Romain Giot - rgiot + Brian Mock - saikobee 8.2 Snippets *UltiSnips-contrisnippets* @@ -1448,7 +1449,6 @@ Contributors listed in chronological order: Bernhard Vallant (lazerscience) Von Welch (von) Nikola Petrov (nikolavp) - Brian Mock (saikobee) Thank you for your support. diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 6fadd6d..2caf000 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -22,23 +22,25 @@ def _plugin_dir(): be updated if the code moves. """ d = __file__ - for i in xrange(3): + for i in xrange(10): d = os.path.dirname(d) - return d + if os.path.isdir(os.path.join(d, "plugin")) and os.path.isdir(os.path.join(d, "doc")): + return d + raise Exception("Unable to find the plugin directory.") def _snippets_dir_is_before_plugin_dir(): """ Returns True if the snippets directory comes before the plugin directory in Vim's runtime path. False otherwise. """ + paths = [ os.path.expanduser(p).rstrip(os.path.sep) + for p in _vim.eval("&runtimepath").split(',') ] + home = _vim.eval("$HOME") def vim_path_index(suffix): path = os.path.join(home, suffix).rstrip(os.path.sep) try: return paths.index(path) except ValueError: return -1 - paths = [ os.path.expanduser(p).rstrip(os.path.sep) - for p in _vim.eval("&runtimepath").split(',') ] - home = _vim.eval("$HOME") try: real_vim_path_index = max(vim_path_index(".vim"), vim_path_index("vimfiles")) return paths.index(_plugin_dir()) < real_vim_path_index