-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdim_lsp.py
More file actions
306 lines (250 loc) · 9.16 KB
/
dim_lsp.py
File metadata and controls
306 lines (250 loc) · 9.16 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# dim_lsp.py — Language Server Protocol implementation for Dim
#
# Provides IDE support: diagnostics, completions, go-to-definition, hover.
import json
import sys
import os
from typing import Optional, Dict, Any, List
from pathlib import Path
from dim_lexer import Lexer
from dim_parser import Parser
from dim_semantic import SemanticAnalyzer
from dim_module_resolver import ModuleResolver
from dim_mir_lowering import lower_program
class LSPDocument:
def __init__(self, uri: str):
self.uri = uri
self.text = ""
self.version = 0
def update(self, content: str, version: int):
self.text = content
self.version = version
class DimLanguageServer:
def __init__(self):
self.documents: Dict[str, LSPDocument] = {}
self.workspace_path: Optional[str] = None
def _get_doc(self, uri: str) -> Optional[LSPDocument]:
return self.documents.get(uri)
def _parse_and_analyze(self, text: str, uri: str):
try:
tokens = Lexer(text, uri).tokenize()
parser = Parser(tokens, text, uri)
ast = parser.parse_program()
if parser.diag.has_errors:
return {"errors": parser.diag.errors, "warnings": parser.diag.warnings}
resolver = ModuleResolver(uri)
sem = SemanticAnalyzer(text, uri, resolver)
resolver.resolve_program(ast, text, uri)
if not sem.analyze(ast):
return {"errors": sem.diag.errors, "warnings": sem.diag.warnings}
return {"ast": ast, "sem": sem, "resolver": resolver}
except Exception as e:
return {"errors": [{"message": str(e), "line": 1, "column": 1}]}
def initialize(self, params: Dict[str, Any]) -> Dict[str, Any]:
root_path = params.get("rootUri") or params.get("rootPath")
if root_path:
self.workspace_path = root_path
return {
"capabilities": {
"textDocumentSync": 1,
"completionProvider": {"triggerCharacters": [".", ":"]},
"definitionProvider": True,
"hoverProvider": True,
"diagnosticsProvider": True,
},
"serverInfo": {"name": "Dim Language Server", "version": "0.5.0"},
}
def did_open(self, params: Dict[str, Any]):
uri = params["textDocument"]["uri"]
text = params["textDocument"]["text"]
version = params["textDocument"]["version"]
doc = LSPDocument(uri)
doc.update(text, version)
self.documents[uri] = doc
return self.publish_diagnostics(uri)
def did_change(self, params: Dict[str, Any]):
uri = params["textDocument"]["uri"]
version = params["textDocument"]["version"]
for change in params.get("contentChanges", []):
if uri in self.documents:
self.documents[uri].update(change.get("text", ""), version)
return self.publish_diagnostics(uri)
def did_close(self, params: Dict[str, Any]):
uri = params["textDocument"]["uri"]
if uri in self.documents:
del self.documents[uri]
return None
def publish_diagnostics(self, uri: str) -> Optional[Dict[str, Any]]:
doc = self._get_doc(uri)
if not doc:
return None
result = self._parse_and_analyze(doc.text, uri)
diagnostics = []
if "errors" in result:
for err in result["errors"]:
diagnostics.append(
{
"severity": 1,
"range": {
"start": {
"line": err.get("line", 1) - 1,
"character": err.get("column", 1) - 1,
},
"end": {
"line": err.get("line", 1) - 1,
"character": err.get("column", 1),
},
},
"message": err.get("message", "Unknown error"),
}
)
if "warnings" in result:
for warn in result["warnings"]:
diagnostics.append(
{
"severity": 2,
"range": {
"start": {
"line": warn.get("line", 1) - 1,
"character": warn.get("column", 1) - 1,
},
"end": {
"line": warn.get("line", 1) - 1,
"character": warn.get("column", 1),
},
},
"message": warn.get("message", "Warning"),
}
)
return {
"method": "textDocument/publishDiagnostics",
"params": {"uri": uri, "diagnostics": diagnostics},
}
def completion(self, params: Dict[str, Any]) -> List[Dict[str, Any]]:
uri = params["textDocument"]["uri"]
position = params["position"]
doc = self._get_doc(uri)
if not doc:
return []
text = doc.text
line = position["line"]
col = position["character"]
lines = text.split("\n")
if line >= len(lines):
return []
line_text = lines[line]
prefix = line_text[:col]
completions = []
keywords = [
"fn",
"let",
"mut",
"if",
"else",
"while",
"for",
"return",
"struct",
"enum",
"trait",
"impl",
"import",
"pub",
"async",
"match",
"loop",
"break",
"continue",
"try",
"catch",
"throw",
]
for kw in keywords:
if kw.startswith(prefix.split()[-1] if prefix.split() else prefix):
completions.append({"label": kw, "kind": 14, "detail": "keyword"})
stdlib_items = [
("print", "fn(msg: str) -> unit"),
("println", "fn(msg: str) -> unit"),
("input", "fn(prompt: str) -> str"),
("vec", "module"),
("io", "module"),
("math", "module"),
("str", "module"),
("file", "module"),
("json", "module"),
]
for name, detail in stdlib_items:
if name.startswith(prefix.split()[-1] if prefix.split() else prefix):
completions.append(
{
"label": name,
"kind": 6 if detail == "module" else 1,
"detail": detail,
}
)
return completions
def definition(self, params: Dict[str, Any]) -> Optional[Dict[str, Any]]:
uri = params["textDocument"]["uri"]
position = params["position"]
doc = self._get_doc(uri)
if not doc:
return None
return None
def hover(self, params: Dict[str, Any]) -> Optional[Dict[str, Any]]:
uri = params["textDocument"]["uri"]
position = params["position"]
doc = self._get_doc(uri)
if not doc:
return None
return None
def run_lsp():
server = DimLanguageServer()
while True:
line = sys.stdin.readline()
if not line:
break
try:
request = json.loads(line)
except json.JSONDecodeError:
continue
method = request.get("method")
params = request.get("params", {})
msg_id = request.get("id")
result = None
error = None
try:
if method == "initialize":
result = server.initialize(params)
elif method == "textDocument/didOpen":
publish = server.did_open(params)
if publish:
print(json.dumps(publish))
elif method == "textDocument/didChange":
publish = server.did_change(params)
if publish:
print(json.dumps(publish))
elif method == "textDocument/didClose":
server.did_close(params)
elif method == "textDocument/completion":
result = server.completion(params)
elif method == "textDocument/definition":
result = server.definition(params)
elif method == "textDocument/hover":
result = server.hover(params)
elif method == "shutdown":
result = None
else:
error = {"code": -32601, "message": f"Unknown method: {method}"}
except Exception as e:
error = {"code": -32603, "message": str(e)}
response = {"jsonrpc": "2.0"}
if msg_id is not None:
response["id"] = msg_id
if error:
response["error"] = error
else:
response["result"] = result
print(json.dumps(response))
sys.stdout.flush()
if __name__ == "__main__":
run_lsp()