Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)