def _file_to_edit(self, requested_ft, bang): # pylint: disable=no-self-use """Returns a file to be edited for the given requested_ft. If 'bang' is empty only private files in g:UltiSnipsSnippetsDir are considered, otherwise all files are considered and the user gets to choose. """ # This method is not using self, but is called by UltiSnips.vim and is # therefore in this class because it is the facade to Vim. potentials = set() if _vim.eval("exists('g:UltiSnipsSnippetsDir')") == '1': snippet_dir = _vim.eval('g:UltiSnipsSnippetsDir') else: home = _vim.eval('$HOME') if platform.system() == 'Windows': snippet_dir = os.path.join(home, 'vimfiles', 'UltiSnips') elif _vim.eval("has('nvim')") == '1': xdg_home_config = _vim.eval( '$XDG_CONFIG_HOME') or os.path.join(home, ".config") snippet_dir = os.path.join(xdg_home_config, 'nvim', 'UltiSnips') else: snippet_dir = os.path.join(home, '.vim', 'UltiSnips') filetypes = [] if requested_ft: filetypes.append(requested_ft) else: if bang: filetypes.extend(self._buffer_filetypes[_vim.buf.number]) else: filetypes.append(self._buffer_filetypes[_vim.buf.number][0]) for ft in filetypes: potentials.update(find_snippet_files(ft, snippet_dir)) potentials.add(os.path.join(snippet_dir, ft + '.snippets')) if bang: potentials.update(find_all_snippet_files(ft)) potentials = set( os.path.realpath(os.path.expanduser(p)) for p in potentials) if len(potentials) > 1: files = sorted(potentials) formatted = [ as_unicode('%i: %s') % (i, escape(fn, '\\')) for i, fn in enumerate(files, 1) ] file_to_edit = _ask_user(files, formatted) if file_to_edit is None: return '' else: file_to_edit = potentials.pop() dirname = os.path.dirname(file_to_edit) if not os.path.exists(dirname): os.makedirs(dirname) return file_to_edit
def _file_to_edit(self, requested_ft, bang): # pylint: disable=no-self-use """Returns a file to be edited for the given requested_ft. If 'bang' is empty only private files in g:UltiSnipsSnippetsDir are considered, otherwise all files are considered and the user gets to choose. """ # This method is not using self, but is called by UltiSnips.vim and is # therefore in this class because it is the facade to Vim. potentials = set() if _vim.eval("exists('g:UltiSnipsSnippetsDir')") == '1': snippet_dir = _vim.eval('g:UltiSnipsSnippetsDir') else: if platform.system() == 'Windows': snippet_dir = os.path.join(_vim.eval('$HOME'), 'vimfiles', 'UltiSnips') elif _vim.eval("has('nvim')") == '1': snippet_dir = os.path.join(_vim.eval('$HOME'), '.nvim', 'UltiSnips') else: snippet_dir = os.path.join(_vim.eval('$HOME'), '.vim', 'UltiSnips') filetypes = [] if requested_ft: filetypes.append(requested_ft) else: if bang: filetypes.extend(self._buffer_filetypes[_vim.buf.number]) else: filetypes.append(self._buffer_filetypes[_vim.buf.number][0]) for ft in filetypes: potentials.update(find_snippet_files(ft, snippet_dir)) potentials.add(os.path.join(snippet_dir, ft + '.snippets')) if bang: potentials.update(find_all_snippet_files(ft)) potentials = set(os.path.realpath(os.path.expanduser(p)) for p in potentials) if len(potentials) > 1: files = sorted(potentials) formatted = [as_unicode('%i: %s') % (i, escape(fn, '\\')) for i, fn in enumerate(files, 1)] file_to_edit = _ask_user(files, formatted) if file_to_edit is None: return '' else: file_to_edit = potentials.pop() dirname = os.path.dirname(file_to_edit) if not os.path.exists(dirname): os.makedirs(dirname) return file_to_edit
def _file_to_edit(self, requested_ft, bang): """Returns a file to be edited for the given requested_ft. If 'bang' is empty a reasonable first choice is opened (see docs), otherwise all files are considered and the user gets to choose. """ filetypes = [] if requested_ft: filetypes.append(requested_ft) else: if bang: filetypes.extend(self.get_buffer_filetypes()) else: filetypes.append(self.get_buffer_filetypes()[0]) potentials = set() all_snippet_directories = find_all_snippet_directories() has_storage_dir = (vim_helper.eval( "exists('g:UltiSnipsSnippetStorageDirectoryForUltiSnipsEdit')") == "1") if has_storage_dir: snippet_storage_dir = vim_helper.eval( "g:UltiSnipsSnippetStorageDirectoryForUltiSnipsEdit") full_path = os.path.expanduser(snippet_storage_dir) potentials.update( _get_potential_snippet_filenames_to_edit(full_path, filetypes)) if len(all_snippet_directories) == 1: # Most likely the user has set g:UltiSnipsSnippetDirectories to a # single absolute path. potentials.update( _get_potential_snippet_filenames_to_edit( all_snippet_directories[0], filetypes)) if (len(all_snippet_directories) != 1 and not has_storage_dir) or (has_storage_dir and bang): # Likely the array contains things like ["UltiSnips", # "mycoolsnippets"] There is no more obvious way to edit than in # the users vim config directory. dot_vim_dir = vim_helper.get_dot_vim() for snippet_dir in all_snippet_directories: if Path(dot_vim_dir) != Path(snippet_dir).parent: continue potentials.update( _get_potential_snippet_filenames_to_edit( snippet_dir, filetypes)) if bang: for ft in filetypes: potentials.update(find_all_snippet_files(ft)) else: if not potentials: _show_user_warning( "UltiSnips was not able to find a default directory for snippets. " "Do you have a .vim directory? Try :UltiSnipsEdit! instead of :UltiSnipsEdit." ) return "" return _select_and_create_file_to_edit(potentials)
def _get_file_to_edit(self, snippet_dir, requested_ft, bang, allow_empty=False): # pylint: disable=no-self-use potentials = set() filetypes = [] if requested_ft: filetypes.append(requested_ft) else: if bang: filetypes.extend(self.get_buffer_filetypes()) else: filetypes.append(self.get_buffer_filetypes()[0]) for ft in filetypes: potentials.update(find_snippet_files(ft, snippet_dir)) potentials.add(os.path.join(snippet_dir, ft + '.snippets')) potentials.add(os.path.join('', ft + '.snippets')) if bang: potentials.update(find_all_snippet_files(ft)) potentials = set( os.path.realpath(os.path.expanduser(p)) for p in potentials) if len(potentials) > 1: files = sorted(potentials) formatted = [ as_unicode('%i: %s') % (i, escape(fn, '\\')) for i, fn in enumerate(files, 1) ] file_to_edit = _ask_user(files, formatted) if file_to_edit is None: return '' else: file_to_edit = potentials.pop() if not allow_empty and not os.path.exists(file_to_edit): return '' dirname = os.path.dirname(file_to_edit) if not os.path.exists(dirname): os.makedirs(dirname) return file_to_edit
def _file_to_edit(self, requested_ft, bang): """Returns a file to be edited for the given requested_ft. If 'bang' is empty a reasonable first choice is opened (see docs), otherwise all files are considered and the user gets to choose. """ filetypes = [] if requested_ft: filetypes.append(requested_ft) else: if bang: filetypes.extend(self.get_buffer_filetypes()) else: filetypes.append(self.get_buffer_filetypes()[0]) potentials = set() all_snippet_directories = find_all_snippet_directories() if len(all_snippet_directories) == 1: # Most likely the user has set g:UltiSnipsSnippetDirectories to a # single absolute path. potentials.update( _get_potential_snippet_filenames_to_edit( all_snippet_directories[0], filetypes)) else: # Likely the array contains things like ["UltiSnips", # "mycoolsnippets"] There is no more obvious way to edit than in # the users vim config directory. dot_vim_dir = Path(vim_helper.get_dot_vim()) for snippet_dir in all_snippet_directories: snippet_dir = Path(snippet_dir) if dot_vim_dir != snippet_dir.parent: continue potentials.update( _get_potential_snippet_filenames_to_edit( snippet_dir, filetypes)) if bang: for ft in filetypes: potentials.update(find_all_snippet_files(ft)) return _select_and_create_file_to_edit(potentials)
def _file_to_edit(self, ft, bang): # pylint: disable=no-self-use """Returns a file to be edited for the given ft. If 'bang' is empty only private files in g:UltiSnipsSnippetsDir are considered, otherwise all files are considered and the user gets to choose. """ # This method is not using self, but is called by UltiSnips.vim and is # therefore in this class because it is the facade to Vim. potentials = set() if _vim.eval("exists('g:UltiSnipsSnippetsDir')") == "1": snippet_dir = _vim.eval("g:UltiSnipsSnippetsDir") else: if platform.system() == "Windows": snippet_dir = os.path.join(_vim.eval("$HOME"), "_vimfiles", "UltiSnips") else: snippet_dir = os.path.join(_vim.eval("$HOME"), ".vim", "UltiSnips") potentials.update(find_snippet_files(ft, snippet_dir)) potentials.add(os.path.join(snippet_dir, ft + '.snippets')) if bang: potentials.update(find_all_snippet_files(ft)) potentials = set(os.path.realpath(os.path.expanduser(p)) for p in potentials) if len(potentials) > 1: files = sorted(potentials) formatted = [as_unicode('%i: %s') % (i, fn) for i, fn in enumerate(files, 1)] edit = _ask_user(files, formatted) if edit is None: return "" else: edit = potentials.pop() dirname = os.path.dirname(edit) if not os.path.exists(dirname): os.makedirs(dirname) return edit
def _get_file_to_edit(self, snippet_dir, requested_ft, bang, allow_empty=False): # pylint: disable=no-self-use potentials = set() filetypes = [] if requested_ft: filetypes.append(requested_ft) else: if bang: filetypes.extend(self.get_buffer_filetypes()) else: filetypes.append(self.get_buffer_filetypes()[0]) for ft in filetypes: potentials.update(find_snippet_files(ft, snippet_dir)) potentials.add(os.path.join(snippet_dir, ft + '.snippets')) if bang: potentials.update(find_all_snippet_files(ft)) potentials = set(os.path.realpath(os.path.expanduser(p)) for p in potentials) if len(potentials) > 1: files = sorted(potentials) formatted = [as_unicode('%i: %s') % (i, escape(fn, '\\')) for i, fn in enumerate(files, 1)] file_to_edit = _ask_user(files, formatted) if file_to_edit is None: return '' else: file_to_edit = potentials.pop() if not allow_empty and not os.path.exists(file_to_edit): return '' dirname = os.path.dirname(file_to_edit) if not os.path.exists(dirname): os.makedirs(dirname) return file_to_edit