1+ import uuid
12from collections .abc import Iterable
23from importlib .metadata import version
3- from typing import cast
4+ from typing import cast , get_args
45
56import attr
67import cattrs
910from pygls import server
1011from pygls .workspace import TextDocument
1112
12- from auto_typing_final .transform import IMPORT_MODES_TO_IMPORT_CONFIGS , AddFinal , Edit , ImportMode , make_replacements
13+ from auto_typing_final .transform import (
14+ IMPORT_STYLES_TO_IMPORT_CONFIGS ,
15+ AddFinal ,
16+ Edit ,
17+ ImportConfig ,
18+ ImportStyle ,
19+ make_replacements ,
20+ )
1321
1422LSP_SERVER = server .LanguageServer (name = "auto-typing-final" , version = version ("auto-typing-final" ), max_workers = 5 )
15- IMPORT_CONFIG = IMPORT_MODES_TO_IMPORT_CONFIGS [ ImportMode . typing_final ]
23+ IMPORT_CONFIG : ImportConfig | None = None
1624
1725
1826@attr .define
@@ -45,15 +53,17 @@ def make_text_edit(edit: Edit) -> lsp.TextEdit:
4553
4654
4755def make_diagnostics (source : str ) -> Iterable [lsp .Diagnostic ]:
56+ if not IMPORT_CONFIG :
57+ return
4858 result = make_replacements (root = SgRoot (source , "python" ).root (), import_config = IMPORT_CONFIG )
4959
5060 for replacement in result .replacements :
5161 if replacement .operation_type == AddFinal :
52- fix_message = f"{ LSP_SERVER .name } : Add typing.Final "
53- diagnostic_message = "Missing typing.Final "
62+ fix_message = f"{ LSP_SERVER .name } : Add { IMPORT_CONFIG . value } "
63+ diagnostic_message = f "Missing { IMPORT_CONFIG . value } "
5464 else :
55- fix_message = f"{ LSP_SERVER .name } : Remove typing.Final "
56- diagnostic_message = "Unexpected typing.Final "
65+ fix_message = f"{ LSP_SERVER .name } : Remove { IMPORT_CONFIG . value } "
66+ diagnostic_message = f "Unexpected { IMPORT_CONFIG . value } "
5767
5868 fix = Fix (message = fix_message , text_edits = [make_text_edit (edit ) for edit in replacement .edits ])
5969 if result .import_text :
@@ -74,6 +84,8 @@ def make_diagnostics(source: str) -> Iterable[lsp.Diagnostic]:
7484
7585
7686def make_fixall_text_edits (source : str ) -> Iterable [lsp .TextEdit ]:
87+ if not IMPORT_CONFIG :
88+ return
7789 result = make_replacements (root = SgRoot (source , "python" ).root (), import_config = IMPORT_CONFIG )
7890
7991 for replacement in result .replacements :
@@ -98,7 +110,35 @@ def make_workspace_edit(text_document: TextDocument, text_edits: list[lsp.TextEd
98110
99111
100112@LSP_SERVER .feature (lsp .INITIALIZE )
101- def initialize (params : lsp .InitializeParams ) -> None : ... # noqa: ARG001
113+ async def initialize (_ : lsp .InitializeParams ) -> None : ...
114+
115+
116+ @LSP_SERVER .feature (lsp .INITIALIZED )
117+ async def initialized (_ : lsp .InitializedParams ) -> None :
118+ await LSP_SERVER .register_capability_async (
119+ params = lsp .RegistrationParams (
120+ registrations = [
121+ lsp .Registration (
122+ id = str (uuid .uuid4 ()),
123+ method = lsp .WORKSPACE_DID_CHANGE_CONFIGURATION ,
124+ register_options = lsp .DidChangeConfigurationRegistrationOptions (section = LSP_SERVER .name ),
125+ )
126+ ]
127+ )
128+ )
129+
130+
131+ @LSP_SERVER .feature (lsp .WORKSPACE_DID_CHANGE_CONFIGURATION )
132+ def workspace_did_change_configuration (params : lsp .DidChangeConfigurationParams ) -> None :
133+ if (
134+ isinstance (params .settings , dict )
135+ and (settings := params .settings .get (LSP_SERVER .name ))
136+ and isinstance (settings , dict )
137+ and (import_style := settings .get ("import-style" ))
138+ and (import_style in get_args (ImportStyle ))
139+ ):
140+ global IMPORT_CONFIG # noqa: PLW0603
141+ IMPORT_CONFIG = IMPORT_STYLES_TO_IMPORT_CONFIGS [import_style ]
102142
103143
104144@LSP_SERVER .feature (lsp .TEXT_DOCUMENT_DID_OPEN )
0 commit comments