summaryrefslogtreecommitdiff
path: root/rplugin/python3/deoplete/source/buffer.py
blob: fe73913114005270d2a37b9bd197a13d5bb71955 (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
# ============================================================================
# FILE: buffer.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================

from deoplete.source.base import Base
from deoplete.util import parse_buffer_pattern, getlines


class Source(Base):

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

        self.name = 'buffer'
        self.mark = '[B]'
        self.events = ['Init', 'BufReadPost', 'BufWritePost']
        self.vars = {
            'require_same_filetype': True,
        }

        self._limit = 1000000
        self._buffers = {}
        self._max_lines = 5000

    def on_event(self, context):
        self._make_cache(context)

        tab_bufnrs = self.vim.call('tabpagebuflist')
        self._buffers = {
            x['bufnr']: x for x in self._buffers.values()
            if x['bufnr'] in tab_bufnrs or
            self.vim.call('buflisted', x['bufnr'])
        }

    def gather_candidates(self, context):
        tab_bufnrs = self.vim.call('tabpagebuflist')
        same_filetype = self.get_var('require_same_filetype')
        return {'sorted_candidates': [
            x['candidates'] for x in self._buffers.values()
            if not same_filetype or
            x['filetype'] in context['filetypes'] or
            x['filetype'] in context['same_filetypes'] or
            x['bufnr'] in tab_bufnrs
        ]}

    def _make_cache(self, context):
        # Bufsize check
        size = self.vim.call('line2byte',
                             self.vim.call('line', '$') + 1) - 1
        if size > self._limit:
            return

        try:
            self._buffers[context['bufnr']] = {
                'bufnr': context['bufnr'],
                'filetype': self.vim.current.buffer.options['filetype'],
                'candidates': [
                    {'word': x} for x in
                    sorted(parse_buffer_pattern(getlines(self.vim),
                                                context['keyword_pattern']),
                           key=str.lower)
                ]
            }
        except UnicodeDecodeError:
            return []