-
Notifications
You must be signed in to change notification settings - Fork 769
/
go_completer.py
108 lines (87 loc) · 3.3 KB
/
go_completer.py
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
# Copyright (C) 2019 ycmd contributors
#
# This file is part of ycmd.
#
# ycmd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ycmd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ycmd. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
# Not installing aliases from python-future; it's unreliable and slow.
from builtins import * # noqa
import logging
import os
from ycmd import responses
from ycmd import utils
from ycmd.completers.language_server import ( simple_language_server_completer,
language_server_completer )
PATH_TO_GOPLS = os.path.abspath( os.path.join( os.path.dirname( __file__ ),
'..',
'..',
'..',
'third_party',
'go',
'src',
'golang.org',
'x',
'tools',
'cmd',
'gopls',
utils.ExecutableName( 'gopls' ) ) )
def ShouldEnableGoCompleter( user_options ):
server_exists = os.path.isfile( PATH_TO_GOPLS )
if server_exists:
return True
utils.LOGGER.info( 'No gopls executable at %s.', PATH_TO_GOPLS )
return False
class GoCompleter( simple_language_server_completer.SimpleLSPCompleter ):
def __init__( self, user_options ):
super( GoCompleter, self ).__init__( user_options )
def GetServerName( self ):
return 'gopls'
def _GetProjectDirectory( self, request_data, extra_conf_dir ):
# Without LSP workspaces support, GOPLS relies on the rootUri to detect a
# project.
# TODO: add support for LSP workspaces to allow users to change project
# without having to restart GOPLS.
for folder in utils.PathsToAllParentFolders( request_data[ 'filepath' ] ):
if os.path.isfile( os.path.join( folder, 'go.mod' ) ):
return folder
return super( GoCompleter, self )._GetProjectDirectory( request_data,
extra_conf_dir )
def GetCommandLine( self ):
cmdline = [ PATH_TO_GOPLS, '-logfile', self._stderr_file ]
if utils.LOGGER.isEnabledFor( logging.DEBUG ):
cmdline.append( '-rpc.trace' )
return cmdline
def SupportedFiletypes( self ):
return [ 'go' ]
def GetType( self, request_data ):
# Quite stupidly, gopls returns this as markdown
# Example value: "```go\\nfunc Hello\\n```"
try:
result = self.GetHoverResponse( request_data )[ 'value' ]
return responses.BuildDisplayMessageResponse( result )
except language_server_completer.ResponseFailedException:
raise RuntimeError( 'Unknown type.' )
def GetCustomSubcommands( self ):
return {
'RestartServer': (
lambda self, request_data, args: self._RestartServer( request_data )
),
'GetType': (
# In addition to type information we show declaration.
lambda self, request_data, args: self.GetType( request_data )
),
}