summaryrefslogtreecommitdiff
path: root/rplugin/python3/deoplete/source/file.py
blob: 819de374ebf4399c74bf9a053862e192c42058dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# ============================================================================
# FILE: file.py
# AUTHOR: Felipe Morales <hel.sheep at gmail.com>
#         Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================

import os
import re
from os.path import exists, dirname
from .base import Base
from deoplete.util import expand


class Source(Base):

    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'file'
        self.mark = '[F]'
        self.min_pattern_length = 0
        self.rank = 150
        self.__isfname = ''

    def on_init(self, context):
        self.__buffer_path = context['vars'].get(
            'deoplete#file#enable_buffer_path', 0)

    def on_event(self, context):
        self.__isfname = self.vim.call(
            'deoplete#util#vimoption2python_not',
            self.vim.options['isfname'])

    def get_complete_position(self, context):
        pos = context['input'].rfind('/')
        return pos if pos < 0 else pos + 1

    def gather_candidates(self, context):
        p = self.__longest_path_that_exists(context, context['input'])
        if p in (None, []) or p == '/' or re.search('//+$', p):
            return []
        complete_str = self.__substitute_path(context, dirname(p) + '/')
        if not os.path.isdir(complete_str):
            return []
        hidden = context['complete_str'].find('.') == 0
        contents = [[], []]
        try:
            for item in sorted(os.listdir(complete_str), key=str.lower):
                if not hidden and item[0] == '.':
                    continue
                contents[not os.path.isdir(complete_str + item)].append(item)
        except PermissionError:
            pass

        dirs, files = contents
        return [{'word': x, 'abbr': x + '/'} for x in dirs
                ] + [{'word': x} for x in files]

    def __longest_path_that_exists(self, context, input_str):
        data = re.split(r'((?:%s+|(?<![\w\s])(?:~|\.{1,2})?/))' %
                        self.__isfname, input_str)
        data = [''.join(data[i:]) for i in range(len(data))]
        existing_paths = sorted(filter(lambda x: exists(
            dirname(self.__substitute_path(context, x))), data))
        return existing_paths[-1] if existing_paths else None

    def __substitute_path(self, context, path):
        m = re.match(r'(\.{1,2})/+', path)
        if m:
            if self.__buffer_path and context['bufname']:
                base = context['bufname']
            else:
                base = os.path.join(context['cwd'], 'x')

            for _ in m.group(1):
                base = dirname(base)
            path = os.path.abspath(os.path.join(base, path[len(m.group(0)):]))
            return path
        return expand(path)