jaclang 0.8.6__py3-none-any.whl → 0.8.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of jaclang might be problematic. Click here for more details.

Files changed (103) hide show
  1. jaclang/cli/cli.md +3 -3
  2. jaclang/cli/cli.py +37 -37
  3. jaclang/cli/cmdreg.py +45 -140
  4. jaclang/compiler/constant.py +0 -1
  5. jaclang/compiler/jac.lark +3 -6
  6. jaclang/compiler/larkparse/jac_parser.py +2 -2
  7. jaclang/compiler/parser.py +213 -34
  8. jaclang/compiler/passes/main/__init__.py +2 -4
  9. jaclang/compiler/passes/main/def_use_pass.py +0 -4
  10. jaclang/compiler/passes/main/predynamo_pass.py +221 -0
  11. jaclang/compiler/passes/main/pyast_gen_pass.py +83 -55
  12. jaclang/compiler/passes/main/pyast_load_pass.py +66 -40
  13. jaclang/compiler/passes/main/sym_tab_build_pass.py +1 -1
  14. jaclang/compiler/passes/main/tests/fixtures/checker/import_sym.jac +2 -0
  15. jaclang/compiler/passes/main/tests/fixtures/checker/import_sym_test.jac +6 -0
  16. jaclang/compiler/passes/main/tests/fixtures/checker/imported_sym.jac +5 -0
  17. jaclang/compiler/passes/main/tests/fixtures/checker_arg_param_match.jac +37 -0
  18. jaclang/compiler/passes/main/tests/fixtures/checker_arity.jac +18 -0
  19. jaclang/compiler/passes/main/tests/fixtures/checker_binary_op.jac +21 -0
  20. jaclang/compiler/passes/main/tests/fixtures/checker_call_expr_class.jac +12 -0
  21. jaclang/compiler/passes/main/tests/fixtures/checker_cat_is_animal.jac +18 -0
  22. jaclang/compiler/passes/main/tests/fixtures/checker_cyclic_symbol.jac +4 -0
  23. jaclang/compiler/passes/main/tests/fixtures/checker_expr_call.jac +9 -0
  24. jaclang/compiler/passes/main/tests/fixtures/checker_float.jac +7 -0
  25. jaclang/compiler/passes/main/tests/fixtures/checker_import_missing_module.jac +13 -0
  26. jaclang/compiler/passes/main/tests/fixtures/checker_magic_call.jac +17 -0
  27. jaclang/compiler/passes/main/tests/fixtures/checker_mod_path.jac +8 -0
  28. jaclang/compiler/passes/main/tests/fixtures/checker_param_types.jac +11 -0
  29. jaclang/compiler/passes/main/tests/fixtures/checker_self_type.jac +9 -0
  30. jaclang/compiler/passes/main/tests/fixtures/checker_sym_inherit.jac +42 -0
  31. jaclang/compiler/passes/main/tests/fixtures/predynamo_fix3.jac +43 -0
  32. jaclang/compiler/passes/main/tests/fixtures/predynamo_where_assign.jac +13 -0
  33. jaclang/compiler/passes/main/tests/fixtures/predynamo_where_return.jac +11 -0
  34. jaclang/compiler/passes/main/tests/test_checker_pass.py +265 -0
  35. jaclang/compiler/passes/main/tests/test_predynamo_pass.py +57 -0
  36. jaclang/compiler/passes/main/type_checker_pass.py +36 -61
  37. jaclang/compiler/passes/tool/doc_ir_gen_pass.py +204 -44
  38. jaclang/compiler/passes/tool/jac_formatter_pass.py +119 -69
  39. jaclang/compiler/passes/tool/tests/fixtures/corelib_fmt.jac +3 -3
  40. jaclang/compiler/passes/tool/tests/fixtures/general_format_checks/triple_quoted_string.jac +4 -5
  41. jaclang/compiler/passes/tool/tests/fixtures/tagbreak.jac +171 -11
  42. jaclang/compiler/passes/transform.py +12 -8
  43. jaclang/compiler/program.py +14 -6
  44. jaclang/compiler/tests/fixtures/jac_import_py_files.py +4 -0
  45. jaclang/compiler/tests/fixtures/jac_module.jac +3 -0
  46. jaclang/compiler/tests/fixtures/multiple_syntax_errors.jac +10 -0
  47. jaclang/compiler/tests/fixtures/python_module.py +1 -0
  48. jaclang/compiler/tests/test_importer.py +39 -0
  49. jaclang/compiler/tests/test_parser.py +49 -0
  50. jaclang/compiler/type_system/operations.py +104 -0
  51. jaclang/compiler/type_system/type_evaluator.py +470 -47
  52. jaclang/compiler/type_system/type_utils.py +246 -0
  53. jaclang/compiler/type_system/types.py +58 -2
  54. jaclang/compiler/unitree.py +79 -94
  55. jaclang/langserve/engine.jac +253 -230
  56. jaclang/langserve/server.jac +46 -15
  57. jaclang/langserve/tests/fixtures/circle.jac +3 -3
  58. jaclang/langserve/tests/fixtures/circle_err.jac +3 -3
  59. jaclang/langserve/tests/fixtures/circle_pure.test.jac +3 -3
  60. jaclang/langserve/tests/fixtures/completion_test_err.jac +10 -0
  61. jaclang/langserve/tests/server_test/circle_template.jac +80 -0
  62. jaclang/langserve/tests/server_test/glob_template.jac +4 -0
  63. jaclang/langserve/tests/server_test/test_lang_serve.py +154 -312
  64. jaclang/langserve/tests/server_test/utils.py +153 -116
  65. jaclang/langserve/tests/test_dev_server.py +1 -1
  66. jaclang/langserve/tests/test_server.py +30 -86
  67. jaclang/langserve/utils.jac +56 -63
  68. jaclang/runtimelib/machine.py +7 -0
  69. jaclang/runtimelib/meta_importer.py +27 -1
  70. jaclang/runtimelib/tests/fixtures/custom_access_validation.jac +1 -1
  71. jaclang/runtimelib/tests/fixtures/savable_object.jac +2 -2
  72. jaclang/settings.py +18 -14
  73. jaclang/tests/fixtures/abc_check.jac +3 -3
  74. jaclang/tests/fixtures/arch_rel_import_creation.jac +12 -12
  75. jaclang/tests/fixtures/chandra_bugs2.jac +3 -3
  76. jaclang/tests/fixtures/create_dynamic_archetype.jac +13 -13
  77. jaclang/tests/fixtures/jac_run_py_bugs.py +18 -0
  78. jaclang/tests/fixtures/jac_run_py_import.py +13 -0
  79. jaclang/tests/fixtures/lambda_arg_annotation.jac +15 -0
  80. jaclang/tests/fixtures/lambda_self.jac +18 -0
  81. jaclang/tests/fixtures/maxfail_run_test.jac +4 -4
  82. jaclang/tests/fixtures/params/param_syntax_err.jac +9 -0
  83. jaclang/tests/fixtures/params/test_complex_params.jac +42 -0
  84. jaclang/tests/fixtures/params/test_failing_kwonly.jac +207 -0
  85. jaclang/tests/fixtures/params/test_failing_posonly.jac +116 -0
  86. jaclang/tests/fixtures/params/test_failing_varargs.jac +300 -0
  87. jaclang/tests/fixtures/params/test_kwonly_params.jac +29 -0
  88. jaclang/tests/fixtures/py2jac_params.py +8 -0
  89. jaclang/tests/fixtures/run_test.jac +4 -4
  90. jaclang/tests/test_cli.py +103 -18
  91. jaclang/tests/test_language.py +74 -16
  92. jaclang/utils/helpers.py +47 -2
  93. jaclang/utils/module_resolver.py +11 -1
  94. jaclang/utils/test.py +8 -0
  95. jaclang/utils/treeprinter.py +0 -18
  96. {jaclang-0.8.6.dist-info → jaclang-0.8.8.dist-info}/METADATA +3 -3
  97. {jaclang-0.8.6.dist-info → jaclang-0.8.8.dist-info}/RECORD +99 -62
  98. {jaclang-0.8.6.dist-info → jaclang-0.8.8.dist-info}/WHEEL +1 -1
  99. jaclang/compiler/passes/main/inheritance_pass.py +0 -131
  100. jaclang/langserve/dev_engine.jac +0 -645
  101. jaclang/langserve/dev_server.jac +0 -201
  102. jaclang/langserve/tests/server_test/code_test.py +0 -0
  103. {jaclang-0.8.6.dist-info → jaclang-0.8.8.dist-info}/entry_points.txt +0 -0
@@ -1,645 +0,0 @@
1
- """Living Workspace of Jac project."""
2
-
3
- import asyncio;
4
- import logging;
5
- import time;
6
- import from concurrent.futures { ThreadPoolExecutor }
7
- import from typing { Callable, Optional }
8
-
9
- import jaclang.compiler.unitree as uni;
10
- import from jaclang { JacMachineInterface as Jac }
11
- import from jaclang.compiler.constant {SymbolType}
12
- import from jaclang.compiler.program { JacProgram }
13
- import from jaclang.compiler.type_system.types{ ClassType}
14
- import from jaclang.compiler.unitree { UniScopeNode }
15
- import from sem_manager { SemTokManager }
16
- import from jaclang.vendor.pygls { uris }
17
- import from jaclang.vendor.pygls.server { LanguageServer }
18
-
19
- import lsprotocol.types as lspt;
20
- import utils;
21
-
22
-
23
- """Handles Jac module, semantic manager, and alert management."""
24
- class ModuleManager {
25
- """Initialize ModuleManager."""
26
- def init(self: ModuleManager, program: JacProgram, sem_managers: <>dict) -> None {
27
- self.program = program;
28
- self.sem_managers = sem_managers;
29
- }
30
-
31
- """Update modules in JacProgram's hub and semantic managers."""
32
- def update(
33
- self: ModuleManager,
34
- file_path: str,
35
- build: uni.Module,
36
- update_annexed: bool = True
37
- ) -> None {
38
- file_path = file_path.removeprefix('file://');
39
- self.program.mod.hub[file_path] = build;
40
- if update_annexed {
41
- self.sem_managers[file_path] = SemTokManager(ir=build);
42
- for (p, mod) in self.program.mod.hub.items() {
43
- if p != file_path {
44
- self.sem_managers[p] = SemTokManager(ir=mod);
45
- }
46
- }
47
- }
48
- }
49
-
50
- """Remove errors and warnings for a specific file from the lists."""
51
- def clear_alerts_for_file(self: ModuleManager, file_path_fs: str) -> None {
52
- self.program.errors_had =
53
- [ e for e in self.program.errors_had if e.loc.mod_path != file_path_fs ];
54
- self.program.warnings_had =
55
- [ w for w in self.program.warnings_had if w.loc.mod_path != file_path_fs ];
56
- }
57
- }
58
-
59
-
60
- """Jac Language Server, manages JacProgram and LSP."""
61
- class JacLangServer(JacProgram , LanguageServer) {
62
- """Initialize JacLangServer."""
63
- def init(self: JacLangServerV2) -> None {
64
- LanguageServer.init(self, 'jac-lsp', 'v0.1');
65
- JacProgram.init(self);
66
- self.executor = ThreadPoolExecutor();
67
- self.tasks: <>dict[(str, asyncio.Task)] = {};
68
- self.sem_managers: <>dict[(str, SemTokManager)] = {};
69
- self.module_manager = ModuleManager(self, self.sem_managers);
70
- }
71
-
72
- """Return diagnostics for all files as a dict {uri: diagnostics}."""
73
- @ property
74
- def diagnostics(self: JacLangServer, ) -> <>dict[str, <>list] {
75
- result = {};
76
- for file_path in self.mod.hub {
77
- uri = uris.from_fs_path(file_path);
78
- result[uri] =
79
- utils.gen_diagnostics(uri, self.errors_had, self.warnings_had);
80
- }
81
- return result;
82
- }
83
-
84
- """Remove errors and warnings for a specific file from the lists."""
85
- def _clear_alerts_for_file(self: JacLangServer, file_path_fs: str) -> None {
86
- self.module_manager.clear_alerts_for_file(file_path_fs);
87
- }
88
-
89
- """Get IR for a file path."""
90
- def get_ir(self: JacLangServer, file_path: str) -> Optional[uni.Module] {
91
- file_path = file_path.removeprefix('file://');
92
- return self.mod.hub.get(file_path);
93
- }
94
-
95
- """Update modules in JacProgram's hub and semantic managers."""
96
- def update_modules(
97
- self: JacLangServer,
98
- file_path: str,
99
- build: uni.Module,
100
- need: bool = True
101
- ) -> None {
102
- self.log_py(f"'Updating modules for '{file_path}");
103
- self.module_manager.update(file_path, build, update_annexed=need);
104
- }
105
-
106
- """Rebuild a file (syntax only)."""
107
- def quick_check(self: JacLangServer, file_path: str) -> bool {
108
- try {
109
- file_path_fs = file_path.removeprefix('file://');
110
- document = self.workspace.get_text_document(file_path);
111
- self._clear_alerts_for_file(file_path_fs);
112
- build = self.compile(use_str=document.source, file_path=document.path);
113
- self.update_modules(file_path_fs, build, need=False);
114
- self.publish_diagnostics(
115
- file_path,
116
- utils.gen_diagnostics(file_path, self.errors_had, self.warnings_had)
117
- );
118
- build_errors =
119
- [ e for e in self.errors_had if e.loc.mod_path == file_path_fs ];
120
- return len(build_errors) == 0;
121
- } except Exception as e {
122
- self.log_error(f"'Error during syntax check: '{e}");
123
- return False;
124
- }
125
- }
126
-
127
- """Rebuild a file and its dependencies (typecheck)."""
128
- def deep_check(
129
- self: JacLangServer,
130
- file_path: str,
131
- annex_view: Optional[str] = None
132
- ) -> bool {
133
- try {
134
- start_time = time.time();
135
- file_path_fs = file_path.removeprefix('file://');
136
- document = self.workspace.get_text_document(file_path);
137
- self._clear_alerts_for_file(file_path_fs);
138
- build = self.build(use_str=document.source, file_path=document.path,type_check=True);
139
- self.update_modules(file_path_fs, build);
140
- if build.annexable_by {
141
- return self.deep_check(
142
- uris.from_fs_path(build.annexable_by),
143
- annex_view=file_path
144
- );
145
- }
146
- self.publish_diagnostics(
147
- annex_view if annex_view else file_path,
148
- utils.gen_diagnostics(
149
- annex_view if annex_view else file_path,
150
- self.errors_had,
151
- self.warnings_had
152
- )
153
- );
154
- if annex_view {
155
- self.publish_diagnostics(
156
- file_path,
157
- utils.gen_diagnostics(file_path, self.errors_had, self.warnings_had)
158
- );
159
- }
160
- self.log_py(
161
- f"'PROFILE: Deep check took '{(time.time() - start_time)}' seconds.'"
162
- );
163
- return len(self.errors_had) == 0;
164
- } except Exception as e {
165
- self.log_py(f"'Error during deep check: '{e}");
166
- return False;
167
- }
168
- }
169
-
170
- """Analyze and publish diagnostics."""
171
- async def launch_quick_check(self: JacLangServer, uri: str) -> bool {
172
- return await asyncio.get_event_loop().run_in_executor(
173
- self.executor,
174
- self.quick_check,
175
- uri
176
- );
177
- }
178
-
179
- """Analyze and publish diagnostics."""
180
- async def launch_deep_check(self: JacLangServer, uri: str) -> None {
181
- async def run_in_executor(
182
- func: Callable[([str, Optional[str]], bool)],
183
- file_path: str,
184
- annex_view: Optional[str] = None
185
- ) -> None {
186
- loop = asyncio.get_event_loop();
187
- await loop.run_in_executor(self.executor, func, file_path, annex_view);
188
- }
189
- if uri in self.tasks and not self.tasks[uri].done() {
190
- self.log_py(f"'Canceling '{uri}' deep check...'");
191
- self.tasks[uri].cancel();
192
- del (self.tasks[uri], ) ;
193
- }
194
- self.log_py(f"'Analyzing '{uri}'...'");
195
- task = asyncio.create_task(run_in_executor(self.deep_check, uri));
196
- self.tasks[uri] = task;
197
- await task;
198
- }
199
-
200
- """Return completion for a file."""
201
- def get_completion(
202
- self: JacLangServer,
203
- file_path: str,
204
- position: lspt.Position,
205
- completion_trigger: Optional[str]
206
- ) -> lspt.CompletionList {
207
- try {
208
- document = self.workspace.get_text_document(file_path);
209
- mod_ir = self.get_ir(file_path);
210
- if not mod_ir {
211
- return lspt.CompletionList(is_incomplete=False, items=[]);
212
- }
213
- current_line = document.lines[position.line];
214
- current_pos = position.character;
215
- current_symbol_path = utils.parse_symbol_path(current_line, current_pos);
216
- builtin_mod =
217
- next(
218
- ( mod for (name, mod) in self.mod.hub.items() if 'builtins' in name )
219
- );
220
- builtin_tab = builtin_mod.sym_tab;
221
- assert isinstance(builtin_tab, UniScopeNode) ;
222
- completion_items = [];
223
- node_selected =
224
- utils.find_deepest_symbol_node_at_pos(
225
- mod_ir,
226
- position.line,
227
- (position.character - 2)
228
- );
229
- mod_tab = mod_ir.sym_tab if not node_selected else node_selected.sym_tab;
230
- current_symbol_table = mod_tab;
231
- if completion_trigger == '.' {
232
- if current_symbol_path {
233
- temp_tab = mod_tab;
234
- for symbol in current_symbol_path {
235
- if symbol == 'self' {
236
- is_ability_def =
237
- temp_tab
238
- if isinstance(temp_tab, uni.ImplDef)
239
- else temp_tab.find_parent_of_type(uni.ImplDef);
240
- if not is_ability_def {
241
- archi_owner =
242
- mod_tab.find_parent_of_type(uni.Archetype);
243
- temp_tab =
244
- archi_owner.sym_tab
245
- if archi_owner and archi_owner.sym_tab
246
- else mod_tab;
247
- continue;
248
- } else {
249
- archi_owner =
250
- is_ability_def.decl_link.find_parent_of_type(
251
- uni.Archetype
252
- )
253
- if is_ability_def.decl_link
254
- else None;
255
- temp_tab =
256
- archi_owner.sym_tab
257
- if archi_owner and archi_owner.sym_tab
258
- else temp_tab;
259
- continue;
260
- }
261
- }
262
- symb = temp_tab.lookup(symbol);
263
- if symb {
264
- fetc_tab = symb.symbol_table;
265
- if fetc_tab {
266
- temp_tab = fetc_tab;
267
- } else {
268
- temp_tab =
269
- symb.defn[0].type_sym_tab
270
- if symb.defn[0].type_sym_tab
271
- else temp_tab;
272
- }
273
- } else {
274
- break;
275
- }
276
- }
277
- completion_items += utils.collect_all_symbols_in_scope(
278
- temp_tab,
279
- up_tree=False
280
- );
281
- if isinstance(temp_tab, uni.Archetype) and temp_tab.base_classes {
282
- base = [];
283
- for base_name in temp_tab.base_classes {
284
- if isinstance(base_name, uni.Name) and base_name.sym {
285
- base.append(base_name.sym);
286
- }
287
- }
288
- for base_class_symbol in base {
289
- if base_class_symbol.symbol_table {
290
- completion_items += utils.collect_all_symbols_in_scope(
291
- base_class_symbol.symbol_table,
292
- up_tree=False
293
- );
294
- }
295
- }
296
- }
297
- }
298
- } elif node_selected and node_selected.find_parent_of_type(uni.Archetype)
299
- or node_selected.find_parent_of_type(uni.ImplDef)
300
- {
301
- self_symbol =
302
-
303
- [lspt.CompletionItem(
304
- label='self',
305
- kind=lspt.CompletionItemKind.Variable
306
- )];
307
- } else {
308
- self_symbol = [];
309
- }
310
- return lspt.CompletionList(is_incomplete=False, items=completion_items);
311
- } except Exception as e {
312
- self.log_py(f"'Error during completion: '{e}");
313
- return lspt.CompletionList(is_incomplete=False, items=[]);
314
- }
315
- }
316
-
317
- """Rename module."""
318
- def rename_module(self: JacLangServer, old_path: str, new_path: str) -> None {
319
- if old_path in self.mod.hub and new_path != old_path {
320
- self.mod.hub[new_path] = self.mod.hub[old_path];
321
- self.sem_managers[new_path] = self.sem_managers[old_path];
322
- del (self.mod.hub[old_path], ) ;
323
- del (self.sem_managers[old_path], ) ;
324
- }
325
- }
326
-
327
- """Delete module."""
328
- def delete_module(self: JacLangServer, uri: str) -> None {
329
- if uri in self.mod.hub {
330
- del (self.mod.hub[uri], ) ;
331
- }
332
- if uri in self.sem_managers {
333
- del (self.sem_managers[uri], ) ;
334
- }
335
- }
336
-
337
- """Return formatted jac."""
338
- def formatted_jac(self: JacLangServer, file_path: str) -> <>list[lspt.TextEdit] {
339
- try {
340
- document = self.workspace.get_text_document(file_path);
341
- formatted_text =
342
- JacProgram.jac_str_formatter(
343
- source_str=document.source,
344
- file_path=document.path
345
- );
346
- } except Exception as e {
347
- self.log_error(f"'Error during formatting: '{e}");
348
- formatted_text = document.source;
349
- }
350
- return
351
- [lspt.TextEdit(
352
- range=lspt.Range(
353
- start=lspt.Position(line=0, character=0),
354
- end=lspt.Position(
355
- line=(len(document.source.splitlines()) + 1),
356
- character=0
357
- )
358
- ),
359
- new_text=formatted_text
360
- )];
361
- }
362
-
363
- """Return hover information for a file."""
364
- def get_hover_info(
365
- self: JacLangServer,
366
- file_path: str,
367
- position: lspt.Position
368
- ) -> Optional[lspt.Hover] {
369
- file_path_fs = file_path.removeprefix('file://');
370
- if file_path_fs not in self.mod.hub {
371
- return None;
372
- }
373
- sem_mgr = self.sem_managers.get(file_path_fs);
374
- if not sem_mgr {
375
- return None;
376
- }
377
- token_index =
378
- utils.find_index(sem_mgr.sem_tokens, position.line, position.character);
379
- if token_index is None {
380
- return None;
381
- }
382
- node_selected = sem_mgr.static_sem_tokens[token_index][3];
383
- value = self.get_node_info(node_selected) if node_selected else None;
384
- if value {
385
- return lspt.Hover(
386
- contents=lspt.MarkupContent(
387
- kind=lspt.MarkupKind.PlainText,
388
- value=f"{value}"
389
- )
390
- );
391
- }
392
- return None;
393
- }
394
-
395
- """Extract meaningful information from the AST node."""
396
- def get_node_info(self: JacLangServer, <>node: uni.AstSymbolNode) -> Optional[str] {
397
- try {
398
- if isinstance(<>node, uni.NameAtom) {
399
- <>node = <>node.name_of;
400
- }
401
- access = (<>node.sym.access.value + ' ') if <>node.sym else None;
402
- node_info =
403
- f"'('{access if access else ''}{<>node.sym_category.value}') '{<>node.sym_name}";
404
- if <>node.name_spec.clean_type {
405
- node_info += f"': '{<>node.name_spec.clean_type}";
406
- }
407
- if isinstance(<>node,uni.AstSymbolNode) and isinstance(<>node.name_spec.type,ClassType) {
408
- node_info += f"': '{<>node.name_spec.type.shared.class_name}";
409
- }
410
- if isinstance(<>node, uni.AstDocNode) and <>node.doc {
411
- node_info += f"'\n'{<>node.doc.value}";
412
- }
413
- if isinstance(<>node, uni.Ability) and <>node.signature {
414
- node_info += f"'\n'{<>node.signature.unparse()}";
415
- }
416
- } except AttributeError as e {
417
- self.log_warning(f"'Attribute error when accessing node attributes: '{e}");
418
- }
419
- return node_info.strip();
420
- }
421
-
422
- """Return document symbols for a file."""
423
- def get_outline(self: JacLangServer, file_path: str) -> <>list[lspt.DocumentSymbol] {
424
- file_path_fs = file_path.removeprefix('file://');
425
- if file_path_fs in self.mod.hub
426
- and (root_node := self.mod.hub[file_path_fs].sym_tab)
427
- {
428
- return utils.get_symbols_for_outline(root_node);
429
- }
430
- return [];
431
- }
432
-
433
- """Return definition location for a file."""
434
- def get_definition(
435
- self: JacLangServer,
436
- file_path: str,
437
- position: lspt.Position
438
- ) -> Optional[lspt.Location] {
439
- file_path_fs = file_path.removeprefix('file://');
440
- if file_path_fs not in self.mod.hub {
441
- return None;
442
- }
443
- sem_mgr = self.sem_managers.get(file_path_fs);
444
- if not sem_mgr {
445
- return None;
446
- }
447
- token_index =
448
- utils.find_index(sem_mgr.sem_tokens, position.line, position.character);
449
- if token_index is None {
450
- return None;
451
- }
452
- node_selected = sem_mgr.static_sem_tokens[token_index][3];
453
- if node_selected {
454
- if (node_selected.sym.sym_type == SymbolType.MODULE) {
455
- spec = node_selected.sym.decl.parent.resolve_relative_path();
456
- if spec {
457
- spec = spec[ 5 : ] if spec.startswith('File:') else spec;
458
- return lspt.Location(
459
- uri=uris.from_fs_path(spec),
460
- range=lspt.Range(
461
- start=lspt.Position(line=0, character=0),
462
- end=lspt.Position(line=0, character=0)
463
- )
464
- );
465
- } else {
466
- return None;
467
- }
468
- }
469
- if isinstance(node_selected.sym, uni.NameAtom) {
470
- node_selected = node_selected.name_of;
471
- }
472
- elif isinstance(node_selected, uni.Name)
473
- and node_selected.parent
474
- and isinstance(node_selected.parent, uni.ModulePath)
475
- {
476
- spec = node_selected.parent.parent.abs_path;
477
- if spec {
478
- spec = spec[ 5 : ] if spec.startswith('File:') else spec;
479
- return lspt.Location(
480
- uri=uris.from_fs_path(spec),
481
- range=lspt.Range(
482
- start=lspt.Position(line=0, character=0),
483
- end=lspt.Position(line=0, character=0)
484
- )
485
- );
486
- } else {
487
- return None;
488
- }
489
- } elif node_selected.parent
490
- and isinstance(node_selected.parent, uni.ModuleItem)
491
- {
492
- path =
493
- node_selected.parent.abs_path
494
- or node_selected.parent.from_mod_path.abs_path
495
- ;
496
- loc_range = (0, 0, 0, 0);
497
- if path and loc_range {
498
- path = path[ 5 : ] if path.startswith('File:') else path;
499
- return lspt.Location(
500
- uri=uris.from_fs_path(path),
501
- range=lspt.Range(
502
- start=lspt.Position(
503
- line=loc_range[0],
504
- character=loc_range[1]
505
- ),
506
- end=lspt.Position(line=loc_range[2], character=loc_range[3])
507
- )
508
- );
509
- }
510
- } elif isinstance(node_selected, uni.ElementStmt) {
511
- return None;
512
- }
513
- decl_node =
514
- node_selected.parent.body.target
515
- if node_selected.parent
516
- and isinstance(node_selected.parent, uni.AstImplNeedingNode)
517
- and isinstance(node_selected.parent.body, uni.ImplDef)
518
-
519
- else node_selected.sym.decl
520
- if node_selected.sym and node_selected.sym.decl
521
- else node_selected;
522
- if isinstance(decl_node, list) {
523
- valid_path = decl_node[0].loc.mod_path;
524
- } else {
525
- valid_path = decl_node.loc.mod_path;
526
- }
527
- decl_uri = uris.from_fs_path(valid_path);
528
- if isinstance(decl_node, list) {
529
- valid_range = decl_node[0].loc;
530
- } else {
531
- valid_range = decl_node.loc;
532
- }
533
- try {
534
- decl_range = utils.create_range(valid_range);
535
- } except ValueError {
536
- return None;
537
- }
538
- decl_location = lspt.Location(uri=decl_uri, range=decl_range);
539
- return decl_location;
540
- } else {
541
- return None;
542
- }
543
- }
544
-
545
- """Return references for a file."""
546
- def get_references(
547
- self: JacLangServer,
548
- file_path: str,
549
- position: lspt.Position
550
- ) -> <>list[lspt.Location] {
551
- file_path_fs = file_path.removeprefix('file://');
552
- if file_path_fs not in self.mod.hub {
553
- return [];
554
- }
555
- sem_mgr = self.sem_managers.get(file_path_fs);
556
- if not sem_mgr {
557
- return [];
558
- }
559
- index1 =
560
- utils.find_index(sem_mgr.sem_tokens, position.line, position.character);
561
- if index1 is None {
562
- return [];
563
- }
564
- node_selected = sem_mgr.static_sem_tokens[index1][3];
565
- if node_selected and node_selected.sym {
566
- list_of_references: <>list[lspt.Location] =
567
- [ lspt.Location(
568
- uri=uris.from_fs_path(<>node.loc.mod_path),
569
- range=utils.create_range(<>node.loc)
570
- ) for <>node in node_selected.sym.uses ];
571
- return list_of_references;
572
- }
573
- return [];
574
- }
575
-
576
- """Rename a symbol in a file."""
577
- def rename_symbol(
578
- self: JacLangServer,
579
- file_path: str,
580
- position: lspt.Position,
581
- new_name: str
582
- ) -> Optional[lspt.WorkspaceEdit] {
583
- file_path_fs = file_path.removeprefix('file://');
584
- if file_path_fs not in self.mod.hub {
585
- return None;
586
- }
587
- sem_mgr = self.sem_managers.get(file_path_fs);
588
- if not sem_mgr {
589
- return None;
590
- }
591
- index1 =
592
- utils.find_index(sem_mgr.sem_tokens, position.line, position.character);
593
- if index1 is None {
594
- return None;
595
- }
596
- node_selected = sem_mgr.static_sem_tokens[index1][3];
597
- if node_selected and node_selected.sym {
598
- changes: <>dict[(str, <>list[lspt.TextEdit])] = {};
599
- for <>node in [*node_selected.sym.uses, node_selected.sym.defn[0]] {
600
- key = uris.from_fs_path(<>node.loc.mod_path);
601
- new_edit =
602
- lspt.TextEdit(
603
- range=utils.create_range(<>node.loc),
604
- new_text=new_name
605
- );
606
- utils.add_unique_text_edit(changes, key, new_edit);
607
- }
608
- return lspt.WorkspaceEdit(changes=changes);
609
- }
610
- return None;
611
- }
612
-
613
- """Return semantic tokens for a file."""
614
- def get_semantic_tokens(self: JacLangServer, file_path: str) -> lspt.SemanticTokens {
615
- file_path_fs = file_path.removeprefix('file://');
616
- sem_mgr = self.sem_managers.get(file_path_fs);
617
- if not sem_mgr {
618
- return lspt.SemanticTokens(data=[]);
619
- }
620
- return lspt.SemanticTokens(data=sem_mgr.sem_tokens);
621
- }
622
-
623
- """Log an error message."""
624
- def log_error(self: JacLangServer, message: str) -> None {
625
- self.show_message_log(message, lspt.MessageType.Error);
626
- self.show_message(message, lspt.MessageType.Error);
627
- }
628
-
629
- """Log a warning message."""
630
- def log_warning(self: JacLangServer, message: str) -> None {
631
- self.show_message_log(message, lspt.MessageType.Warning);
632
- self.show_message(message, lspt.MessageType.Warning);
633
- }
634
-
635
- """Log an info message."""
636
- def log_info(self: JacLangServer, message: str) -> None {
637
- self.show_message_log(message, lspt.MessageType.Info);
638
- self.show_message(message, lspt.MessageType.Info);
639
- }
640
-
641
- """Log a message."""
642
- def log_py(self: JacLangServer, message: str) -> None {
643
- logging.info(message);
644
- }
645
- }