Skip to content

Commit 2c6fe37

Browse files
committed
perf(langserver): try to use the last initialized namespace for completions to be a bit more responsive
1 parent c032814 commit 2c6fe37

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/language_server/src/robotcode/language_server/robotframework/parts/completion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def collect(
150150
position: Position,
151151
context: Optional[CompletionContext],
152152
) -> Union[List[CompletionItem], CompletionList, None]:
153-
namespace = self.parent.documents_cache.get_namespace(document)
153+
namespace = self.parent.documents_cache.get_initialized_namespace(document)
154154
model = self.parent.documents_cache.get_model(document, False)
155155

156156
config = self.get_config(document)
@@ -172,7 +172,7 @@ def resolve(self, sender: Any, completion_item: CompletionItem) -> CompletionIte
172172
if document_uri is not None:
173173
document = self.parent.documents.get(document_uri)
174174
if document is not None:
175-
namespace = self.parent.documents_cache.get_namespace(document)
175+
namespace = self.parent.documents_cache.get_initialized_namespace(document)
176176
model = self.parent.documents_cache.get_model(document, False)
177177
if namespace is not None:
178178
config = self.get_config(document)

packages/robot/src/robotcode/robot/diagnostics/document_cache_helper.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class UnknownFileTypeError(Exception):
4040
pass
4141

4242

43+
class _CacheEntry:
44+
pass
45+
46+
4347
class DocumentsCacheHelper:
4448
_logger = LoggingDescriptor()
4549

@@ -50,6 +54,8 @@ def __init__(
5054
file_watcher_manager: FileWatcherManagerBase,
5155
robot_profile: Optional[RobotBaseProfile],
5256
) -> None:
57+
self.INITIALIZED_NAMESPACE = _CacheEntry()
58+
5359
self.workspace = workspace
5460
self.documents_manager = documents_manager
5561
self.file_watcher_manager = file_watcher_manager
@@ -425,6 +431,20 @@ def __invalidate_namespace(self, sender: Namespace) -> None:
425431

426432
self.namespace_invalidated(self, sender, callback_filter=language_id_filter(document))
427433

434+
def __namespace_initialized(self, sender: Namespace) -> None:
435+
if sender.document is not None:
436+
self._logger.critical(
437+
lambda: f"Save initialized Namespace: {sender.document.uri if sender.document else None}"
438+
)
439+
sender.document.set_data(self.INITIALIZED_NAMESPACE, sender)
440+
441+
def get_initialized_namespace(self, document: TextDocument) -> Namespace:
442+
result: Optional[Namespace] = document.get_data(self.INITIALIZED_NAMESPACE)
443+
if result is None:
444+
self._logger.critical(lambda: f"There is not initialized Namespace: {document.uri if document else None}")
445+
result = self.get_namespace(document)
446+
return result
447+
428448
def __get_namespace_for_document_type(
429449
self, document: TextDocument, document_type: Optional[DocumentType]
430450
) -> Namespace:
@@ -451,6 +471,7 @@ def __get_namespace_for_document_type(
451471
workspace_languages,
452472
)
453473
result.has_invalidated.add(self.__invalidate_namespace)
474+
result.has_initialized.add(self.__namespace_initialized)
454475

455476
return result
456477

0 commit comments

Comments
 (0)