summaryrefslogtreecommitdiff
path: root/vim/bundle/vim-racer/rplugin/python3/deoplete/sources/racer.py
blob: f04153070317f76079966760ad5be278e129cee0 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#=============================================================================
# FILE: racer.py
# AUTHOR:  Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license  {{{
#     Permission is hereby granted, free of charge, to any person obtaining
#     a copy of this software and associated documentation files (the
#     "Software"), to deal in the Software without restriction, including
#     without limitation the rights to use, copy, modify, merge, publish,
#     distribute, sublicense, and/or sell copies of the Software, and to
#     permit persons to whom the Software is furnished to do so, subject to
#     the following conditions:
#
#     The above copyright notice and this permission notice shall be included
#     in all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#     IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
#     CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
#     TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# }}}
#=============================================================================

import re
import os
import subprocess
import tempfile
from .base import Base

class Source(Base):
    def __init__(self, vim):
        Base.__init__(self, vim)

        self.name = 'racer'
        self.mark = '[racer]'
        self.filetypes = ['rust']
        self.input_pattern = r'(\.|::)\w*'
        self.rank = 500

    def on_init(self, context):
        self.__executable_racer = self.vim.funcs.executable(
            self.vim.eval('g:racer_cmd'))
        self.__racer = self.vim.eval('g:racer_cmd')

    def get_complete_position(self, context):
        if not self.__executable_racer:
            return -1

        m = re.search('\w*$', context['input'])
        return m.start() if m else -1


    def gather_candidates(self, context):
        typeMap = {
            'Struct': 's', 'Module': 'M', 'Function': 'f',
            'Crate': 'C',  'Let': 'v',    'StructField': 'm',
            'Impl': 'i',   'Enum': 'e',   'EnumVariant': 'E',
            'Type': 't',   'FnArg': 'v',  'Trait': 'T',
            'Const': 'c'
        }

        candidates = []
        insert_paren = int(self.vim.eval('g:racer_insert_paren'))
        for line in [l[6:] for l
                     in self.get_results(context, 'complete',
                                         context['complete_position'] + 1)
                     if l.startswith('MATCH')]:
            completions = line.split(',')
            kind = typeMap.get(completions[4], '')
            completion = { 'kind': kind, 'word': completions[0], 'dup': 1 }
            if kind == 'f': # function
                completion['menu'] = ','.join(completions[5:]).replace(
                    'pub ', '').replace('fn ', '').rstrip('{')
                if ' where ' in completion['menu'] or completion[
                        'menu'].endswith(' where') :
                    where = completion['menu'].rindex(' where')
                    completion['menu'] = completion['menu'][: where]
                if insert_paren:
                    completion['abbr'] = completions[0]
                    completion['word'] += '('
            elif kind == 's' : # struct
                completion['menu'] = ','.join(completions[5:]).replace(
                    'pub ', '').replace( 'struct ', '').rstrip('{')
            candidates.append(completion)
        return candidates

    def get_results(self, context, command, col):
        with tempfile.NamedTemporaryFile(mode='w') as tf:
            tf.write("\n".join(self.vim.current.buffer))
            tf.flush()

            args = [
                self.__racer, command,
                str(self.vim.funcs.line('.')),
                str(col - 1),
                tf.name
            ] if command == 'prefix' else [
                self.__racer, command,
                str(self.vim.funcs.line('.')),
                str(col - 1),
                self.vim.current.buffer.name,
                tf.name
            ]
            try:
                results = subprocess.check_output(args).decode(
                    context['encoding']).splitlines()
            except subprocess.CalledProcessError:
                return []
            return results