1- from __future__ import annotations
2-
3- import asyncio
41import threading
52import uuid
63import weakref
74from concurrent .futures import Future
8- from dataclasses import dataclass
95from typing import (
106 TYPE_CHECKING ,
117 Any ,
128 Callable ,
9+ ClassVar ,
1310 Coroutine ,
1411 Dict ,
1512 Final ,
1613 List ,
1714 Mapping ,
1815 NamedTuple ,
1916 Optional ,
20- Protocol ,
2117 Tuple ,
2218 Type ,
2319 TypeVar ,
2420 Union ,
2521 cast ,
26- runtime_checkable ,
2722)
2823
2924from robotcode .core .concurrent import threaded
@@ -125,6 +120,9 @@ def __init__(self, name: str, uri: Uri, document_uri: DocumentUri) -> None:
125120 self .document_uri = document_uri
126121
127122
123+ _F = TypeVar ("_F" , bound = Callable [..., Any ])
124+
125+
128126def config_section (name : str ) -> Callable [[_F ], _F ]:
129127 def decorator (func : _F ) -> _F :
130128 setattr (func , "__config_section__" , name )
@@ -133,26 +131,20 @@ def decorator(func: _F) -> _F:
133131 return decorator
134132
135133
136- @runtime_checkable
137- class HasConfigSection (Protocol ):
138- __config_section__ : str
139-
140-
141- @dataclass
134+ # @dataclass
142135class ConfigBase (CamelSnakeMixin ):
143- pass
136+ __config_section__ : ClassVar [ str ]
144137
145138
146139_TConfig = TypeVar ("_TConfig" , bound = ConfigBase )
147- _F = TypeVar ("_F" , bound = Callable [..., Any ])
148140
149141
150142class Workspace (LanguageServerProtocolPart ):
151143 _logger : Final = LoggingDescriptor ()
152144
153145 def __init__ (
154146 self ,
155- parent : LanguageServerProtocol ,
147+ parent : " LanguageServerProtocol" ,
156148 root_uri : Optional [str ],
157149 root_path : Optional [str ],
158150 workspace_folders : Optional [List [TypesWorkspaceFolder ]] = None ,
@@ -174,6 +166,7 @@ def __init__(
174166
175167 self .parent .on_shutdown .add (self .server_shutdown )
176168 self .parent .on_initialize .add (self .server_initialize )
169+ self ._settings_cache : Dict [Tuple [Optional [WorkspaceFolder ], str ], ConfigBase ] = {}
177170
178171 def server_initialize (self , sender : Any , initialization_options : Optional [Any ] = None ) -> None :
179172 if (
@@ -260,6 +253,7 @@ def did_change_configuration(sender, settings: Dict[str, Any]) -> None: # NOSON
260253 @threaded
261254 def _workspace_did_change_configuration (self , settings : Dict [str , Any ], * args : Any , ** kwargs : Any ) -> None :
262255 self .settings = settings
256+ self ._settings_cache .clear ()
263257 self .did_change_configuration (self , settings )
264258
265259 @event
@@ -333,14 +327,6 @@ def _workspace_will_delete_files(self, files: List[FileDelete], *args: Any, **kw
333327 def _workspace_did_delete_files (self , files : List [FileDelete ], * args : Any , ** kwargs : Any ) -> None :
334328 self .did_delete_files (self , [f .uri for f in files ])
335329
336- def get_configuration_async (
337- self ,
338- section : Type [_TConfig ],
339- scope_uri : Union [str , Uri , None ] = None ,
340- request : bool = True ,
341- ) -> asyncio .Future [_TConfig ]:
342- return asyncio .wrap_future (self .get_configuration_future (section , scope_uri , request ))
343-
344330 def get_configuration (
345331 self ,
346332 section : Type [_TConfig ],
@@ -357,6 +343,12 @@ def get_configuration_future(
357343 ) -> Future [_TConfig ]:
358344 result_future : Future [_TConfig ] = Future ()
359345
346+ scope = self .get_workspace_folder (scope_uri ) if scope_uri is not None else None
347+
348+ if (scope , section .__config_section__ ) in self ._settings_cache :
349+ result_future .set_result (cast (_TConfig , self ._settings_cache [(scope , section .__config_section__ )]))
350+ return result_future
351+
360352 def _get_configuration_done (f : Future [Optional [Any ]]) -> None :
361353 try :
362354 if result_future .cancelled ():
@@ -371,12 +363,14 @@ def _get_configuration_done(f: Future[Optional[Any]]) -> None:
371363 return
372364
373365 result = f .result ()
374- result_future .set_result (from_dict (result [0 ] if result else {}, section ))
366+ r = from_dict (result [0 ] if result else {}, section )
367+ self ._settings_cache [(scope , section .__config_section__ )] = r
368+ result_future .set_result (r )
375369 except Exception as e :
376370 result_future .set_exception (e )
377371
378372 self .get_configuration_raw (
379- section = cast ( HasConfigSection , section ) .__config_section__ ,
373+ section = section .__config_section__ ,
380374 scope_uri = scope_uri ,
381375 request = request ,
382376 ).add_done_callback (_get_configuration_done )
@@ -453,6 +447,10 @@ def _workspace_did_change_workspace_folders(
453447 for r in to_remove :
454448 self ._workspace_folders .remove (r )
455449
450+ settings_to_remove = [k for k in self ._settings_cache .keys () if k [0 ] == r ]
451+ for k in settings_to_remove :
452+ self ._settings_cache .pop (k , None )
453+
456454 for a in event .added :
457455 self ._workspace_folders .append (WorkspaceFolder (a .name , Uri (a .uri ), a .uri ))
458456
0 commit comments