Ejemplo n.º 1
0
def find_all_snippet_directories():
    """Returns a list of the absolute path of all potential snippet
    directories, no matter if they exist or not."""

    if vim_helper.eval("exists('b:UltiSnipsSnippetDirectories')") == "1":
        snippet_dirs = vim_helper.eval("b:UltiSnipsSnippetDirectories")
    else:
        snippet_dirs = vim_helper.eval("g:UltiSnipsSnippetDirectories")

    if len(snippet_dirs) == 1:
        # To reduce confusion and increase consistency with
        # `UltiSnipsSnippetsDir`, we expand ~ here too.
        full_path = os.path.expanduser(snippet_dirs[0])
        if os.path.isabs(full_path):
            return [full_path]

    all_dirs = []
    check_dirs = vim_helper.eval("&runtimepath").split(",")
    for rtp in check_dirs:
        for snippet_dir in snippet_dirs:
            if snippet_dir == "snippets":
                raise RuntimeError(
                    "You have 'snippets' in UltiSnipsSnippetDirectories. This "
                    "directory is reserved for snipMate snippets. Use another "
                    "directory for UltiSnips snippets."
                )
            pth = normalize_file_path(
                os.path.expanduser(os.path.join(rtp, snippet_dir))
            )
            all_dirs.append(pth)
    return all_dirs
Ejemplo n.º 2
0
def find_snippet_files(ft, directory):
    """Returns all matching snippet files for 'ft' in 'directory'."""
    patterns = ["%s.snippets", "%s_*.snippets", os.path.join("%s", "*")]
    ret = set()
    directory = os.path.expanduser(directory)
    for pattern in patterns:
        for fn in glob.glob(os.path.join(directory, pattern % ft)):
            ret.add(normalize_file_path(fn))
    return ret
Ejemplo n.º 3
0
def get_dot_vim():
    """Returns the likely place for ~/.vim for the current setup."""
    home = vim.eval("$HOME")
    candidates = []
    if platform.system() == "Windows":
        candidates.append(os.path.join(home, "vimfiles"))
    if vim.eval("has('nvim')") == "1":
        xdg_home_config = vim.eval("$XDG_CONFIG_HOME") or os.path.join(
            home, ".config")
        candidates.append(os.path.join(xdg_home_config, "nvim"))

    candidates.append(os.path.join(home, ".vim"))

    my_vimrc = os.path.expandvars(os.environ["MYVIMRC"])
    candidates.append(normalize_file_path(os.path.dirname(my_vimrc)))
    for candidate in candidates:
        if os.path.isdir(candidate):
            return normalize_file_path(candidate)
    raise RuntimeError(
        "Unable to find user configuration directory. I tried '%s'." %
        candidates)
Ejemplo n.º 4
0
def _get_potential_snippet_filenames_to_edit(snippet_dir: str,
                                             filetypes: str) -> Set[str]:
    potentials = set()
    for ft in filetypes:
        ft_snippets_files = find_snippet_files(ft, snippet_dir)
        potentials.update(ft_snippets_files)
        if not ft_snippets_files:
            # If there is no snippet file yet, we just default to `ft.snippets`.
            fpath = os.path.join(snippet_dir, ft + ".snippets")
            fpath = normalize_file_path(fpath)
            potentials.add(fpath)
    return potentials
Ejemplo n.º 5
0
def _snipmate_files_for(ft):
    """Returns all snipMate files we need to look at for 'ft'."""
    if ft == "all":
        ft = "_"
    patterns = [
        "%s.snippets" % ft,
        os.path.join(ft, "*.snippets"),
        os.path.join(ft, "*.snippet"),
        os.path.join(ft, "*/*.snippet"),
    ]
    ret = set()
    for rtp in vim_helper.eval("&runtimepath").split(","):
        path = normalize_file_path(
            os.path.expanduser(os.path.join(rtp, "snippets")))
        for pattern in patterns:
            for fn in glob.glob(os.path.join(path, pattern)):
                ret.add(fn)
    return ret
Ejemplo n.º 6
0
def _snipmate_files_for(ft):
    """Returns all snipMate files we need to look at for 'ft'."""
    if ft == "all":
        ft = "_"
    patterns = [
        "%s.snippets" % ft,
        os.path.join(ft, "*.snippets"),
        os.path.join(ft, "*.snippet"),
        os.path.join(ft, "*/*.snippet"),
    ]

    ret = set()
    if vim_helper.eval("exists('g:UltiSnipsSnipMateAbsDirectories')") == '1':
        # specify the snipmate snippets by handle with absolute path
        paths = vim_helper.eval('g:UltiSnipsSnipMateAbsDirectories')
    else:
        paths = vim_helper.eval('&runtimepath').split(',')
    for rtp in paths:
        path = normalize_file_path(
            os.path.expanduser(os.path.join(rtp, "snippets")))
        for pattern in patterns:
            for fn in glob.glob(os.path.join(path, pattern)):
                ret.add(fn)
    return ret