-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
goto.py
194 lines (151 loc) · 6.89 KB
/
goto.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
############################################################################
# Copyright(c) Open Law Library. All rights reserved. #
# See ThirdPartyNotices.txt in the project root for additional notices. #
# #
# Licensed under the Apache License, Version 2.0 (the "License") #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http: // www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################
import logging
import re
from lsprotocol import types
from pygls.server import LanguageServer
from pygls.workspace import TextDocument
ARGUMENT = re.compile(r"(?P<name>\w+): (?P<type>\w+)")
FUNCTION = re.compile(r"^fn ([a-z]\w+)\(")
TYPE = re.compile(r"^type ([A-Z]\w+)\(")
class GotoLanguageServer(LanguageServer):
"""Language server demonstrating the various "Goto X" methods in the LSP
specification."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.index = {}
def parse(self, doc: TextDocument):
typedefs = {}
funcs = {}
for linum, line in enumerate(doc.lines):
if (match := TYPE.match(line)) is not None:
name = match.group(1)
start_char = match.start() + line.find(name)
typedefs[name] = types.Range(
start=types.Position(line=linum, character=start_char),
end=types.Position(line=linum, character=start_char + len(name)),
)
elif (match := FUNCTION.match(line)) is not None:
name = match.group(1)
start_char = match.start() + line.find(name)
funcs[name] = types.Range(
start=types.Position(line=linum, character=start_char),
end=types.Position(line=linum, character=start_char + len(name)),
)
self.index[doc.uri] = {
"types": typedefs,
"functions": funcs,
}
logging.info("Index: %s", self.index)
server = GotoLanguageServer("goto-server", "v1")
@server.feature(types.TEXT_DOCUMENT_DID_OPEN)
def did_open(ls: GotoLanguageServer, params: types.DidOpenTextDocumentParams):
"""Parse each document when it is opened"""
doc = ls.workspace.get_text_document(params.text_document.uri)
ls.parse(doc)
@server.feature(types.TEXT_DOCUMENT_DID_CHANGE)
def did_change(ls: GotoLanguageServer, params: types.DidOpenTextDocumentParams):
"""Parse each document when it is changed"""
doc = ls.workspace.get_text_document(params.text_document.uri)
ls.parse(doc)
@server.feature(types.TEXT_DOCUMENT_TYPE_DEFINITION)
def goto_type_definition(ls: GotoLanguageServer, params: types.TypeDefinitionParams):
"""Jump to an object's type definition."""
doc = ls.workspace.get_text_document(params.text_document.uri)
index = ls.index.get(doc.uri)
if index is None:
return
try:
line = doc.lines[params.position.line]
except IndexError:
line = ""
word = doc.word_at_position(params.position)
for match in ARGUMENT.finditer(line):
if match.group("name") == word:
if (range_ := index["types"].get(match.group("type"), None)) is not None:
return types.Location(uri=doc.uri, range=range_)
@server.feature(types.TEXT_DOCUMENT_DEFINITION)
def goto_definition(ls: GotoLanguageServer, params: types.DefinitionParams):
"""Jump to an object's definition."""
doc = ls.workspace.get_text_document(params.text_document.uri)
index = ls.index.get(doc.uri)
if index is None:
return
word = doc.word_at_position(params.position)
# Is word a type?
if (range_ := index["types"].get(word, None)) is not None:
return types.Location(uri=doc.uri, range=range_)
@server.feature(types.TEXT_DOCUMENT_DECLARATION)
def goto_declaration(ls: GotoLanguageServer, params: types.DeclarationParams):
"""Jump to an object's declaration."""
doc = ls.workspace.get_text_document(params.text_document.uri)
index = ls.index.get(doc.uri)
if index is None:
return
try:
line = doc.lines[params.position.line]
except IndexError:
line = ""
word = doc.word_at_position(params.position)
for match in ARGUMENT.finditer(line):
if match.group("name") == word:
linum = params.position.line
return types.Location(
uri=doc.uri,
range=types.Range(
start=types.Position(line=linum, character=match.start()),
end=types.Position(line=linum, character=match.end()),
),
)
@server.feature(types.TEXT_DOCUMENT_IMPLEMENTATION)
def goto_implementation(ls: GotoLanguageServer, params: types.ImplementationParams):
"""Jump to an object's implementation."""
doc = ls.workspace.get_text_document(params.text_document.uri)
index = ls.index.get(doc.uri)
if index is None:
return
word = doc.word_at_position(params.position)
# Is word a function?
if (range_ := index["functions"].get(word, None)) is not None:
return types.Location(uri=doc.uri, range=range_)
@server.feature(types.TEXT_DOCUMENT_REFERENCES)
def find_references(ls: GotoLanguageServer, params: types.ReferenceParams):
"""Find references of an object."""
doc = ls.workspace.get_text_document(params.text_document.uri)
index = ls.index.get(doc.uri)
if index is None:
return
word = doc.word_at_position(params.position)
is_object = any([word in index[name] for name in index])
if not is_object:
return
references = []
for linum, line in enumerate(doc.lines):
for match in re.finditer(f"\\b{word}\\b", line):
references.append(
types.Location(
uri=doc.uri,
range=types.Range(
start=types.Position(line=linum, character=match.start()),
end=types.Position(line=linum, character=match.end()),
),
)
)
return references
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(message)s")
server.start_io()