jaclang 0.0.6__py3-none-any.whl → 0.0.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 (82) hide show
  1. jaclang/__init__.py +2 -1
  2. jaclang/cli/__jac_gen__/__init__.py +0 -0
  3. jaclang/cli/__jac_gen__/cli.py +175 -0
  4. jaclang/cli/__jac_gen__/cmds.py +132 -0
  5. jaclang/cli/cmds.jac +3 -0
  6. jaclang/cli/impl/__jac_gen__/__init__.py +0 -0
  7. jaclang/cli/impl/__jac_gen__/cli_impl.py +16 -0
  8. jaclang/cli/impl/__jac_gen__/cmds_impl.py +26 -0
  9. jaclang/cli/impl/cmds_impl.jac +17 -3
  10. jaclang/core/__jac_gen__/__init__.py +0 -0
  11. jaclang/core/__jac_gen__/primitives.py +567 -0
  12. jaclang/core/impl/__jac_gen__/__init__.py +0 -0
  13. jaclang/core/impl/__jac_gen__/arch_impl.py +24 -0
  14. jaclang/core/impl/__jac_gen__/element_impl.py +26 -0
  15. jaclang/core/impl/__jac_gen__/exec_ctx_impl.py +12 -0
  16. jaclang/core/impl/__jac_gen__/memory_impl.py +14 -0
  17. jaclang/core/impl/element_impl.jac +2 -2
  18. jaclang/core/primitives.jac +1 -0
  19. jaclang/jac/absyntree.py +65 -42
  20. jaclang/jac/constant.py +4 -0
  21. jaclang/jac/importer.py +18 -60
  22. jaclang/jac/langserve.py +26 -0
  23. jaclang/jac/lexer.py +9 -1
  24. jaclang/jac/parser.py +135 -123
  25. jaclang/jac/passes/blue/ast_build_pass.py +410 -353
  26. jaclang/jac/passes/blue/blue_pygen_pass.py +15 -0
  27. jaclang/jac/passes/blue/decl_def_match_pass.py +33 -21
  28. jaclang/jac/passes/blue/import_pass.py +1 -1
  29. jaclang/jac/passes/blue/pyout_pass.py +47 -12
  30. jaclang/jac/passes/blue/sym_tab_build_pass.py +38 -127
  31. jaclang/jac/passes/blue/tests/test_ast_build_pass.py +2 -2
  32. jaclang/jac/passes/blue/tests/test_blue_pygen_pass.py +9 -30
  33. jaclang/jac/passes/blue/tests/test_decl_def_match_pass.py +13 -13
  34. jaclang/jac/passes/blue/tests/test_sym_tab_build_pass.py +6 -4
  35. jaclang/jac/passes/ir_pass.py +1 -1
  36. jaclang/jac/passes/purple/__jac_gen__/__init__.py +0 -0
  37. jaclang/jac/passes/purple/__jac_gen__/analyze_pass.py +37 -0
  38. jaclang/jac/passes/purple/__jac_gen__/purple_pygen_pass.py +305 -0
  39. jaclang/jac/passes/purple/impl/__jac_gen__/__init__.py +0 -0
  40. jaclang/jac/passes/purple/impl/__jac_gen__/purple_pygen_pass_impl.py +23 -0
  41. jaclang/jac/symtable.py +12 -4
  42. jaclang/jac/tests/fixtures/__jac_gen__/__init__.py +0 -0
  43. jaclang/jac/tests/fixtures/__jac_gen__/hello_world.py +16 -0
  44. jaclang/jac/tests/fixtures/fam.jac +7 -8
  45. jaclang/jac/transform.py +4 -3
  46. jaclang/jac/transpiler.py +13 -9
  47. jaclang/utils/fstring_parser.py +2 -2
  48. jaclang/utils/helpers.py +41 -0
  49. jaclang/utils/test.py +30 -0
  50. jaclang/vendor/__init__.py +1 -0
  51. jaclang/vendor/pygls/__init__.py +25 -0
  52. jaclang/vendor/pygls/capabilities.py +502 -0
  53. jaclang/vendor/pygls/client.py +176 -0
  54. jaclang/vendor/pygls/constants.py +26 -0
  55. jaclang/vendor/pygls/exceptions.py +220 -0
  56. jaclang/vendor/pygls/feature_manager.py +241 -0
  57. jaclang/vendor/pygls/lsp/__init__.py +139 -0
  58. jaclang/vendor/pygls/lsp/client.py +2224 -0
  59. jaclang/vendor/pygls/lsprotocol/__init__.py +2 -0
  60. jaclang/vendor/pygls/lsprotocol/_hooks.py +1233 -0
  61. jaclang/vendor/pygls/lsprotocol/converters.py +17 -0
  62. jaclang/vendor/pygls/lsprotocol/types.py +12820 -0
  63. jaclang/vendor/pygls/lsprotocol/validators.py +47 -0
  64. jaclang/vendor/pygls/progress.py +79 -0
  65. jaclang/vendor/pygls/protocol.py +1184 -0
  66. jaclang/vendor/pygls/server.py +620 -0
  67. jaclang/vendor/pygls/uris.py +184 -0
  68. jaclang/vendor/pygls/workspace/__init__.py +81 -0
  69. jaclang/vendor/pygls/workspace/position.py +204 -0
  70. jaclang/vendor/pygls/workspace/text_document.py +234 -0
  71. jaclang/vendor/pygls/workspace/workspace.py +311 -0
  72. {jaclang-0.0.6.dist-info → jaclang-0.0.8.dist-info}/METADATA +1 -1
  73. jaclang-0.0.8.dist-info/RECORD +118 -0
  74. jaclang/core/jaclang.jac +0 -62
  75. jaclang-0.0.6.dist-info/RECORD +0 -76
  76. /jaclang/{utils → vendor}/sly/__init__.py +0 -0
  77. /jaclang/{utils → vendor}/sly/docparse.py +0 -0
  78. /jaclang/{utils → vendor}/sly/lex.py +0 -0
  79. /jaclang/{utils → vendor}/sly/yacc.py +0 -0
  80. {jaclang-0.0.6.dist-info → jaclang-0.0.8.dist-info}/WHEEL +0 -0
  81. {jaclang-0.0.6.dist-info → jaclang-0.0.8.dist-info}/entry_points.txt +0 -0
  82. {jaclang-0.0.6.dist-info → jaclang-0.0.8.dist-info}/top_level.txt +0 -0
jaclang/jac/transpiler.py CHANGED
@@ -6,7 +6,7 @@ from jaclang.jac.parser import JacLexer
6
6
  from jaclang.jac.parser import JacParser
7
7
  from jaclang.jac.passes import Pass
8
8
  from jaclang.jac.passes.blue import BluePygenPass, PyOutPass, pass_schedule
9
- from jaclang.jac.transform import Transform
9
+ from jaclang.jac.transform import Alert, Transform
10
10
 
11
11
 
12
12
  T = TypeVar("T", bound=Pass)
@@ -22,7 +22,7 @@ def jac_file_to_parse_tree(file_path: str, base_dir: str) -> Transform:
22
22
  return prse
23
23
 
24
24
 
25
- def transpile_jac_blue(file_path: str, base_dir: str) -> str:
25
+ def transpile_jac_blue(file_path: str, base_dir: str) -> list[Alert]:
26
26
  """Transpiler Jac file and return python code as string."""
27
27
  code = jac_file_to_pass(
28
28
  file_path=file_path,
@@ -31,13 +31,15 @@ def transpile_jac_blue(file_path: str, base_dir: str) -> str:
31
31
  schedule=pass_schedule,
32
32
  )
33
33
  if isinstance(code.ir, ast.Module):
34
- PyOutPass(mod_path=file_path, input_ir=code.ir, base_path=base_dir, prior=code)
35
- return code.ir.meta["py_code"]
34
+ print_pass = PyOutPass(
35
+ mod_path=file_path, input_ir=code.ir, base_path=base_dir, prior=code
36
+ )
36
37
  else:
37
- raise code.gen_exception("Transpilation of Jac file failed.")
38
+ return code.errors_had
39
+ return print_pass.errors_had
38
40
 
39
41
 
40
- def transpile_jac_purple(file_path: str, base_dir: str) -> str:
42
+ def transpile_jac_purple(file_path: str, base_dir: str) -> list[Alert]:
41
43
  """Transpiler Jac file and return python code as string."""
42
44
  from jaclang.jac.passes.purple import pass_schedule, PurplePygenPass
43
45
 
@@ -48,10 +50,12 @@ def transpile_jac_purple(file_path: str, base_dir: str) -> str:
48
50
  schedule=pass_schedule,
49
51
  )
50
52
  if isinstance(code.ir, ast.Module):
51
- PyOutPass(mod_path=file_path, input_ir=code.ir, base_path=base_dir, prior=code)
52
- return code.ir.meta["py_code"]
53
+ print_pass = PyOutPass(
54
+ mod_path=file_path, input_ir=code.ir, base_path=base_dir, prior=code
55
+ )
53
56
  else:
54
- raise code.gen_exception("Transpilation of Jac file failed.")
57
+ return code.errors_had
58
+ return print_pass.errors_had
55
59
 
56
60
 
57
61
  def jac_file_to_pass(
@@ -1,7 +1,7 @@
1
1
  # type: ignore
2
2
  """Python Like F-String Parser."""
3
- from jaclang.utils.sly.lex import Lexer
4
- from jaclang.utils.sly.yacc import Parser, YaccProduction
3
+ from jaclang.vendor.sly.lex import Lexer
4
+ from jaclang.vendor.sly.yacc import Parser, YaccProduction
5
5
 
6
6
  _ = None # For flake8 linting and sly compatibility
7
7
 
jaclang/utils/helpers.py CHANGED
@@ -1,7 +1,9 @@
1
1
  """Utility functions and classes for Jac compilation toolchain."""
2
2
  import re
3
+ import traceback
3
4
 
4
5
  import jaclang.jac.absyntree as ast
6
+ from jaclang.jac.constant import Constants as Con, Values as Val
5
7
 
6
8
 
7
9
  def pascal_to_snake(pascal_string: str) -> str:
@@ -31,6 +33,45 @@ def clip_code_section(s: str, target_line: int, line_range: int) -> str:
31
33
  return "\n".join(result)
32
34
 
33
35
 
36
+ def dedent_code_block(code: str) -> str:
37
+ """Dedent a code block."""
38
+ lines = code.splitlines()
39
+ min_indent = min(len(line) - len(line.lstrip()) for line in lines if line.strip())
40
+ dedented_lines = [line[min_indent:] for line in lines]
41
+ dedented_code = "\n".join(dedented_lines)
42
+ return dedented_code
43
+
44
+
45
+ def handle_jac_error(code_string: str, e: Exception, tb: traceback.StackSummary) -> str:
46
+ """Handle Jac Error."""
47
+ except_line = e.end_lineno if isinstance(e, SyntaxError) else list(tb)[-1].lineno
48
+ if not isinstance(except_line, int) or except_line == 0:
49
+ return ""
50
+ py_error_region = clip_code_section(
51
+ add_line_numbers(code_string), except_line, Val.JAC_ERROR_LINE_RANGE
52
+ )
53
+ try:
54
+ jac_err_line = int(code_string.splitlines()[except_line - 1].split()[-1])
55
+ mod_index = int(code_string.splitlines()[except_line - 1].split()[-2])
56
+ mod_paths = code_string.split(Con.JAC_DEBUG_SPLITTER)[1].strip().splitlines()
57
+ target_mod = mod_paths[mod_index]
58
+ with open(target_mod, "r") as file:
59
+ jac_code_string = file.read()
60
+ jac_error_region = clip_code_section(
61
+ add_line_numbers(jac_code_string), jac_err_line, Val.JAC_ERROR_LINE_RANGE
62
+ )
63
+ except Exception as e:
64
+ jac_error_region = str(e)
65
+ target_mod = ""
66
+ snippet = (
67
+ f"{Con.JAC_ERROR_PREAMBLE}\n"
68
+ f"{target_mod}\n"
69
+ f"JacCode Snippet:\n{jac_error_region}\n"
70
+ f"PyCode Snippet:\n{py_error_region}\n"
71
+ )
72
+ return snippet
73
+
74
+
34
75
  def get_ast_nodes_as_snake_case() -> list[str]:
35
76
  """Get all AST nodes as snake case."""
36
77
  import inspect
jaclang/utils/test.py CHANGED
@@ -5,6 +5,8 @@ import os
5
5
  from abc import ABC, abstractmethod
6
6
  from unittest import TestCase as _TestCase
7
7
 
8
+ from jaclang.utils.helpers import get_ast_nodes_as_snake_case as ast_snakes
9
+
8
10
 
9
11
  class TestCase(_TestCase):
10
12
  """Base test case for Jaseci."""
@@ -101,3 +103,31 @@ class TestCaseMicroSuite(ABC, TestCase):
101
103
  def micro_suite_test(self, filename: str) -> None:
102
104
  """Test micro jac file."""
103
105
  pass
106
+
107
+
108
+ class AstSyncTestMixin:
109
+ """Mixin for testing AST sync."""
110
+
111
+ TargetPass = None
112
+
113
+ def test_pass_ast_complete(self) -> None:
114
+ """Test for enter/exit name diffs with parser."""
115
+ ast_func_names = [
116
+ x
117
+ for x in ast_snakes()
118
+ if x not in ["ast_node", "o_o_p_access_node", "walker_stmt_only_node"]
119
+ ]
120
+ pygen_func_names = []
121
+ for name, value in inspect.getmembers(self.TargetPass):
122
+ if (
123
+ (name.startswith("enter_") or name.startswith("exit_"))
124
+ and inspect.isfunction(value)
125
+ and not getattr(self.TargetPass.__base__, value.__name__, False) # type: ignore
126
+ and value.__qualname__.split(".")[0]
127
+ == self.TargetPass.__name__.replace("enter_", "").replace("exit_", "") # type: ignore
128
+ ):
129
+ pygen_func_names.append(name.replace("enter_", "").replace("exit_", ""))
130
+ for name in pygen_func_names:
131
+ self.assertIn(name, ast_func_names) # type: ignore
132
+ for name in ast_func_names:
133
+ self.assertIn(name, pygen_func_names) # type: ignore
@@ -0,0 +1 @@
1
+ """Vendor packages for JACLang."""
@@ -0,0 +1,25 @@
1
+ ############################################################################
2
+ # Original work Copyright 2018 Palantir Technologies, Inc. #
3
+ # Original work licensed under the MIT License. #
4
+ # See ThirdPartyNotices.txt in the project root for license information. #
5
+ # All modifications Copyright (c) Open Law Library. All rights reserved. #
6
+ # #
7
+ # Licensed under the Apache License, Version 2.0 (the "License") #
8
+ # you may not use this file except in compliance with the License. #
9
+ # You may obtain a copy of the License at #
10
+ # #
11
+ # http: // www.apache.org/licenses/LICENSE-2.0 #
12
+ # #
13
+ # Unless required by applicable law or agreed to in writing, software #
14
+ # distributed under the License is distributed on an "AS IS" BASIS, #
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
16
+ # See the License for the specific language governing permissions and #
17
+ # limitations under the License. #
18
+ ############################################################################
19
+ import os
20
+ import sys
21
+
22
+ IS_WIN = os.name == "nt"
23
+ IS_PYODIDE = "pyodide" in sys.modules
24
+
25
+ pygls = "pygls"
@@ -0,0 +1,502 @@
1
+ ############################################################################
2
+ # Copyright(c) Open Law Library. All rights reserved. #
3
+ # See ThirdPartyNotices.txt in the project root for additional notices. #
4
+ # #
5
+ # Licensed under the Apache License, Version 2.0 (the "License") #
6
+ # you may not use this file except in compliance with the License. #
7
+ # You may obtain a copy of the License at #
8
+ # #
9
+ # http: // www.apache.org/licenses/LICENSE-2.0 #
10
+ # #
11
+ # Unless required by applicable law or agreed to in writing, software #
12
+ # distributed under the License is distributed on an "AS IS" BASIS, #
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14
+ # See the License for the specific language governing permissions and #
15
+ # limitations under the License. #
16
+ ############################################################################
17
+ from functools import reduce
18
+ from typing import Any, Dict, List, Optional, Set, Union
19
+ import logging
20
+
21
+ from jaclang.vendor.pygls.lsprotocol.types import (
22
+ INLAY_HINT_RESOLVE,
23
+ TEXT_DOCUMENT_CODE_ACTION,
24
+ TEXT_DOCUMENT_CODE_LENS,
25
+ TEXT_DOCUMENT_COMPLETION,
26
+ TEXT_DOCUMENT_DECLARATION,
27
+ TEXT_DOCUMENT_DEFINITION,
28
+ TEXT_DOCUMENT_DIAGNOSTIC,
29
+ TEXT_DOCUMENT_DOCUMENT_COLOR,
30
+ TEXT_DOCUMENT_DOCUMENT_HIGHLIGHT,
31
+ TEXT_DOCUMENT_DOCUMENT_LINK,
32
+ TEXT_DOCUMENT_DOCUMENT_SYMBOL,
33
+ TEXT_DOCUMENT_FOLDING_RANGE,
34
+ TEXT_DOCUMENT_FORMATTING,
35
+ TEXT_DOCUMENT_HOVER,
36
+ TEXT_DOCUMENT_IMPLEMENTATION,
37
+ TEXT_DOCUMENT_INLAY_HINT,
38
+ TEXT_DOCUMENT_INLINE_VALUE,
39
+ TEXT_DOCUMENT_ON_TYPE_FORMATTING,
40
+ TEXT_DOCUMENT_RANGE_FORMATTING,
41
+ TEXT_DOCUMENT_REFERENCES,
42
+ TEXT_DOCUMENT_RENAME,
43
+ TEXT_DOCUMENT_SELECTION_RANGE,
44
+ TEXT_DOCUMENT_SIGNATURE_HELP,
45
+ TEXT_DOCUMENT_PREPARE_CALL_HIERARCHY,
46
+ TEXT_DOCUMENT_PREPARE_TYPE_HIERARCHY,
47
+ TEXT_DOCUMENT_DID_CLOSE,
48
+ TEXT_DOCUMENT_DID_OPEN,
49
+ TEXT_DOCUMENT_DID_SAVE,
50
+ TEXT_DOCUMENT_LINKED_EDITING_RANGE,
51
+ TEXT_DOCUMENT_MONIKER,
52
+ TEXT_DOCUMENT_SEMANTIC_TOKENS_FULL,
53
+ TEXT_DOCUMENT_SEMANTIC_TOKENS_FULL_DELTA,
54
+ TEXT_DOCUMENT_SEMANTIC_TOKENS_RANGE,
55
+ TEXT_DOCUMENT_WILL_SAVE,
56
+ TEXT_DOCUMENT_WILL_SAVE_WAIT_UNTIL,
57
+ TEXT_DOCUMENT_TYPE_DEFINITION,
58
+ WORKSPACE_DIAGNOSTIC,
59
+ WORKSPACE_DID_CREATE_FILES,
60
+ WORKSPACE_DID_DELETE_FILES,
61
+ WORKSPACE_DID_RENAME_FILES,
62
+ WORKSPACE_SYMBOL,
63
+ WORKSPACE_SYMBOL_RESOLVE,
64
+ WORKSPACE_WILL_CREATE_FILES,
65
+ WORKSPACE_WILL_DELETE_FILES,
66
+ WORKSPACE_WILL_RENAME_FILES,
67
+ InlayHintOptions,
68
+ PositionEncodingKind,
69
+ )
70
+ from jaclang.vendor.pygls.lsprotocol.types import (
71
+ ClientCapabilities,
72
+ CodeLensOptions,
73
+ CompletionOptions,
74
+ DocumentLinkOptions,
75
+ ExecuteCommandOptions,
76
+ ImplementationOptions,
77
+ NotebookDocumentSyncOptions,
78
+ SemanticTokensOptions,
79
+ SemanticTokensRegistrationOptions,
80
+ SemanticTokensOptionsFullType1,
81
+ ServerCapabilities,
82
+ ServerCapabilitiesWorkspaceType,
83
+ SignatureHelpOptions,
84
+ TextDocumentSyncKind,
85
+ TextDocumentSyncOptions,
86
+ TypeDefinitionOptions,
87
+ FileOperationOptions,
88
+ WorkspaceFoldersServerCapabilities,
89
+ )
90
+
91
+ logger = logging.getLogger(__name__)
92
+
93
+
94
+ def get_capability(
95
+ client_capabilities: ClientCapabilities, field: str, default: Any = None
96
+ ) -> Any:
97
+ """Check if ClientCapabilities has some nested value without raising
98
+ AttributeError.
99
+ e.g. get_capability('text_document.synchronization.will_save')
100
+ """
101
+ try:
102
+ value = reduce(getattr, field.split("."), client_capabilities)
103
+ except AttributeError:
104
+ return default
105
+
106
+ # If we reach the desired leaf value but it's None, return the default.
107
+ return default if value is None else value
108
+
109
+
110
+ class ServerCapabilitiesBuilder:
111
+ """Create `ServerCapabilities` instance depending on builtin and user registered
112
+ features.
113
+ """
114
+
115
+ def __init__(
116
+ self,
117
+ client_capabilities: ClientCapabilities,
118
+ features: Set[str],
119
+ feature_options: Dict[str, Any],
120
+ commands: List[str],
121
+ text_document_sync_kind: TextDocumentSyncKind,
122
+ notebook_document_sync: Optional[NotebookDocumentSyncOptions] = None,
123
+ ):
124
+ self.client_capabilities = client_capabilities
125
+ self.features = features
126
+ self.feature_options = feature_options
127
+ self.commands = commands
128
+ self.text_document_sync_kind = text_document_sync_kind
129
+ self.notebook_document_sync = notebook_document_sync
130
+
131
+ self.server_cap = ServerCapabilities()
132
+
133
+ def _provider_options(self, feature, default=True):
134
+ if feature in self.features:
135
+ return self.feature_options.get(feature, default)
136
+ return None
137
+
138
+ def _with_text_document_sync(self):
139
+ open_close = (
140
+ TEXT_DOCUMENT_DID_OPEN in self.features
141
+ or TEXT_DOCUMENT_DID_CLOSE in self.features
142
+ )
143
+ will_save = (
144
+ get_capability(
145
+ self.client_capabilities, "text_document.synchronization.will_save"
146
+ )
147
+ and TEXT_DOCUMENT_WILL_SAVE in self.features
148
+ )
149
+ will_save_wait_until = (
150
+ get_capability(
151
+ self.client_capabilities,
152
+ "text_document.synchronization.will_save_wait_until",
153
+ )
154
+ and TEXT_DOCUMENT_WILL_SAVE_WAIT_UNTIL in self.features
155
+ )
156
+ if TEXT_DOCUMENT_DID_SAVE in self.features:
157
+ save = self.feature_options.get(TEXT_DOCUMENT_DID_SAVE, True)
158
+ else:
159
+ save = False
160
+
161
+ self.server_cap.text_document_sync = TextDocumentSyncOptions(
162
+ open_close=open_close,
163
+ change=self.text_document_sync_kind,
164
+ will_save=will_save,
165
+ will_save_wait_until=will_save_wait_until,
166
+ save=save,
167
+ )
168
+
169
+ return self
170
+
171
+ def _with_notebook_document_sync(self):
172
+ if self.client_capabilities.notebook_document is None:
173
+ return self
174
+
175
+ self.server_cap.notebook_document_sync = self.notebook_document_sync
176
+ return self
177
+
178
+ def _with_completion(self):
179
+ value = self._provider_options(
180
+ TEXT_DOCUMENT_COMPLETION, default=CompletionOptions()
181
+ )
182
+ if value is not None:
183
+ self.server_cap.completion_provider = value
184
+ return self
185
+
186
+ def _with_hover(self):
187
+ value = self._provider_options(TEXT_DOCUMENT_HOVER)
188
+ if value is not None:
189
+ self.server_cap.hover_provider = value
190
+ return self
191
+
192
+ def _with_signature_help(self):
193
+ value = self._provider_options(
194
+ TEXT_DOCUMENT_SIGNATURE_HELP, default=SignatureHelpOptions()
195
+ )
196
+ if value is not None:
197
+ self.server_cap.signature_help_provider = value
198
+ return self
199
+
200
+ def _with_declaration(self):
201
+ value = self._provider_options(TEXT_DOCUMENT_DECLARATION)
202
+ if value is not None:
203
+ self.server_cap.declaration_provider = value
204
+ return self
205
+
206
+ def _with_definition(self):
207
+ value = self._provider_options(TEXT_DOCUMENT_DEFINITION)
208
+ if value is not None:
209
+ self.server_cap.definition_provider = value
210
+ return self
211
+
212
+ def _with_type_definition(self):
213
+ value = self._provider_options(
214
+ TEXT_DOCUMENT_TYPE_DEFINITION, default=TypeDefinitionOptions()
215
+ )
216
+ if value is not None:
217
+ self.server_cap.type_definition_provider = value
218
+ return self
219
+
220
+ def _with_inlay_hints(self):
221
+ value = self._provider_options(
222
+ TEXT_DOCUMENT_INLAY_HINT, default=InlayHintOptions()
223
+ )
224
+ if value is not None:
225
+ value.resolve_provider = INLAY_HINT_RESOLVE in self.features
226
+ self.server_cap.inlay_hint_provider = value
227
+ return self
228
+
229
+ def _with_implementation(self):
230
+ value = self._provider_options(
231
+ TEXT_DOCUMENT_IMPLEMENTATION, default=ImplementationOptions()
232
+ )
233
+ if value is not None:
234
+ self.server_cap.implementation_provider = value
235
+ return self
236
+
237
+ def _with_references(self):
238
+ value = self._provider_options(TEXT_DOCUMENT_REFERENCES)
239
+ if value is not None:
240
+ self.server_cap.references_provider = value
241
+ return self
242
+
243
+ def _with_document_highlight(self):
244
+ value = self._provider_options(TEXT_DOCUMENT_DOCUMENT_HIGHLIGHT)
245
+ if value is not None:
246
+ self.server_cap.document_highlight_provider = value
247
+ return self
248
+
249
+ def _with_document_symbol(self):
250
+ value = self._provider_options(TEXT_DOCUMENT_DOCUMENT_SYMBOL)
251
+ if value is not None:
252
+ self.server_cap.document_symbol_provider = value
253
+ return self
254
+
255
+ def _with_code_action(self):
256
+ value = self._provider_options(TEXT_DOCUMENT_CODE_ACTION)
257
+ if value is not None:
258
+ self.server_cap.code_action_provider = value
259
+ return self
260
+
261
+ def _with_code_lens(self):
262
+ value = self._provider_options(
263
+ TEXT_DOCUMENT_CODE_LENS, default=CodeLensOptions()
264
+ )
265
+ if value is not None:
266
+ self.server_cap.code_lens_provider = value
267
+ return self
268
+
269
+ def _with_document_link(self):
270
+ value = self._provider_options(
271
+ TEXT_DOCUMENT_DOCUMENT_LINK, default=DocumentLinkOptions()
272
+ )
273
+ if value is not None:
274
+ self.server_cap.document_link_provider = value
275
+ return self
276
+
277
+ def _with_color(self):
278
+ value = self._provider_options(TEXT_DOCUMENT_DOCUMENT_COLOR)
279
+ if value is not None:
280
+ self.server_cap.color_provider = value
281
+ return self
282
+
283
+ def _with_document_formatting(self):
284
+ value = self._provider_options(TEXT_DOCUMENT_FORMATTING)
285
+ if value is not None:
286
+ self.server_cap.document_formatting_provider = value
287
+ return self
288
+
289
+ def _with_document_range_formatting(self):
290
+ value = self._provider_options(TEXT_DOCUMENT_RANGE_FORMATTING)
291
+ if value is not None:
292
+ self.server_cap.document_range_formatting_provider = value
293
+ return self
294
+
295
+ def _with_document_on_type_formatting(self):
296
+ value = self._provider_options(TEXT_DOCUMENT_ON_TYPE_FORMATTING)
297
+ if value is not None:
298
+ self.server_cap.document_on_type_formatting_provider = value
299
+ return self
300
+
301
+ def _with_rename(self):
302
+ value = self._provider_options(TEXT_DOCUMENT_RENAME)
303
+ if value is not None:
304
+ self.server_cap.rename_provider = value
305
+ return self
306
+
307
+ def _with_folding_range(self):
308
+ value = self._provider_options(TEXT_DOCUMENT_FOLDING_RANGE)
309
+ if value is not None:
310
+ self.server_cap.folding_range_provider = value
311
+ return self
312
+
313
+ def _with_execute_command(self):
314
+ self.server_cap.execute_command_provider = ExecuteCommandOptions(
315
+ commands=self.commands
316
+ )
317
+ return self
318
+
319
+ def _with_selection_range(self):
320
+ value = self._provider_options(TEXT_DOCUMENT_SELECTION_RANGE)
321
+ if value is not None:
322
+ self.server_cap.selection_range_provider = value
323
+ return self
324
+
325
+ def _with_call_hierarchy(self):
326
+ value = self._provider_options(TEXT_DOCUMENT_PREPARE_CALL_HIERARCHY)
327
+ if value is not None:
328
+ self.server_cap.call_hierarchy_provider = value
329
+ return self
330
+
331
+ def _with_type_hierarchy(self):
332
+ value = self._provider_options(TEXT_DOCUMENT_PREPARE_TYPE_HIERARCHY)
333
+ if value is not None:
334
+ self.server_cap.type_hierarchy_provider = value
335
+ return self
336
+
337
+ def _with_semantic_tokens(self):
338
+ providers = [
339
+ TEXT_DOCUMENT_SEMANTIC_TOKENS_FULL,
340
+ TEXT_DOCUMENT_SEMANTIC_TOKENS_FULL_DELTA,
341
+ TEXT_DOCUMENT_SEMANTIC_TOKENS_RANGE,
342
+ ]
343
+
344
+ for provider in providers:
345
+ value = self._provider_options(provider, None)
346
+ if value:
347
+ break
348
+
349
+ if value is None:
350
+ return self
351
+
352
+ if isinstance(value, SemanticTokensRegistrationOptions):
353
+ self.server_cap.semantic_tokens_provider = value
354
+ return self
355
+
356
+ full_support: Union[bool, SemanticTokensOptionsFullType1] = (
357
+ TEXT_DOCUMENT_SEMANTIC_TOKENS_FULL in self.features
358
+ )
359
+
360
+ if TEXT_DOCUMENT_SEMANTIC_TOKENS_FULL_DELTA in self.features:
361
+ full_support = SemanticTokensOptionsFullType1(delta=True)
362
+
363
+ options = SemanticTokensOptions(
364
+ legend=value,
365
+ full=full_support or None,
366
+ range=TEXT_DOCUMENT_SEMANTIC_TOKENS_RANGE in self.features or None,
367
+ )
368
+
369
+ if options.full or options.range:
370
+ self.server_cap.semantic_tokens_provider = options
371
+
372
+ return self
373
+
374
+ def _with_linked_editing_range(self):
375
+ value = self._provider_options(TEXT_DOCUMENT_LINKED_EDITING_RANGE)
376
+ if value is not None:
377
+ self.server_cap.linked_editing_range_provider = value
378
+ return self
379
+
380
+ def _with_moniker(self):
381
+ value = self._provider_options(TEXT_DOCUMENT_MONIKER)
382
+ if value is not None:
383
+ self.server_cap.moniker_provider = value
384
+ return self
385
+
386
+ def _with_workspace_symbol(self):
387
+ value = self._provider_options(WORKSPACE_SYMBOL)
388
+ if value is not None:
389
+ value.resolve_provider = self._provider_options(WORKSPACE_SYMBOL_RESOLVE)
390
+ self.server_cap.workspace_symbol_provider = value
391
+ return self
392
+
393
+ def _with_workspace_capabilities(self):
394
+ # File operations
395
+ file_operations = FileOperationOptions()
396
+ operations = [
397
+ (WORKSPACE_WILL_CREATE_FILES, "will_create"),
398
+ (WORKSPACE_DID_CREATE_FILES, "did_create"),
399
+ (WORKSPACE_WILL_DELETE_FILES, "will_delete"),
400
+ (WORKSPACE_DID_DELETE_FILES, "did_delete"),
401
+ (WORKSPACE_WILL_RENAME_FILES, "will_rename"),
402
+ (WORKSPACE_DID_RENAME_FILES, "did_rename"),
403
+ ]
404
+
405
+ for method_name, capability_name in operations:
406
+ client_supports_method = get_capability(
407
+ self.client_capabilities, f"workspace.file_operations.{capability_name}"
408
+ )
409
+
410
+ if client_supports_method:
411
+ value = self._provider_options(method_name, None)
412
+ setattr(file_operations, capability_name, value)
413
+
414
+ self.server_cap.workspace = ServerCapabilitiesWorkspaceType(
415
+ workspace_folders=WorkspaceFoldersServerCapabilities(
416
+ supported=True,
417
+ change_notifications=True,
418
+ ),
419
+ file_operations=file_operations,
420
+ )
421
+ return self
422
+
423
+ def _with_diagnostic_provider(self):
424
+ value = self._provider_options(TEXT_DOCUMENT_DIAGNOSTIC)
425
+ if value is not None:
426
+ value.workspace_diagnostics = self._provider_options(WORKSPACE_DIAGNOSTIC)
427
+ self.server_cap.diagnostic_provider = value
428
+ return self
429
+
430
+ def _with_inline_value_provider(self):
431
+ value = self._provider_options(TEXT_DOCUMENT_INLINE_VALUE)
432
+ if value is not None:
433
+ self.server_cap.inline_value_provider = value
434
+ return self
435
+
436
+ def _with_position_encodings(self):
437
+ self.server_cap.position_encoding = PositionEncodingKind.Utf16
438
+
439
+ general = self.client_capabilities.general
440
+ if general is None:
441
+ return self
442
+
443
+ encodings = general.position_encodings
444
+ if encodings is None:
445
+ return self
446
+
447
+ if PositionEncodingKind.Utf16 in encodings:
448
+ return self
449
+
450
+ if PositionEncodingKind.Utf32 in encodings:
451
+ self.server_cap.position_encoding = PositionEncodingKind.Utf32
452
+ return self
453
+
454
+ if PositionEncodingKind.Utf8 in encodings:
455
+ self.server_cap.position_encoding = PositionEncodingKind.Utf8
456
+ return self
457
+
458
+ logger.warning(f"Unknown `PositionEncoding`s: {encodings}")
459
+
460
+ return self
461
+
462
+ def _build(self):
463
+ return self.server_cap
464
+
465
+ def build(self):
466
+ return (
467
+ self._with_text_document_sync()
468
+ ._with_notebook_document_sync()
469
+ ._with_completion()
470
+ ._with_hover()
471
+ ._with_signature_help()
472
+ ._with_declaration()
473
+ ._with_definition()
474
+ ._with_type_definition()
475
+ ._with_inlay_hints()
476
+ ._with_implementation()
477
+ ._with_references()
478
+ ._with_document_highlight()
479
+ ._with_document_symbol()
480
+ ._with_code_action()
481
+ ._with_code_lens()
482
+ ._with_document_link()
483
+ ._with_color()
484
+ ._with_document_formatting()
485
+ ._with_document_range_formatting()
486
+ ._with_document_on_type_formatting()
487
+ ._with_rename()
488
+ ._with_folding_range()
489
+ ._with_execute_command()
490
+ ._with_selection_range()
491
+ ._with_call_hierarchy()
492
+ ._with_type_hierarchy()
493
+ ._with_semantic_tokens()
494
+ ._with_linked_editing_range()
495
+ ._with_moniker()
496
+ ._with_workspace_symbol()
497
+ ._with_workspace_capabilities()
498
+ ._with_diagnostic_provider()
499
+ ._with_inline_value_provider()
500
+ ._with_position_encodings()
501
+ ._build()
502
+ )