From 0d17585bf84fd42a3da8b741dcbd11d5eb60534f Mon Sep 17 00:00:00 2001 From: Brian Mock Date: Thu, 12 Dec 2013 23:33:09 -0800 Subject: [PATCH] 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