1
1
import ast
2
- import threading
3
2
from concurrent .futures import CancelledError
4
3
from typing import TYPE_CHECKING , Any , List , Optional
5
4
24
23
GlobalVariableDefinition ,
25
24
LibraryArgumentDefinition ,
26
25
)
26
+ from robotcode .robot .diagnostics .library_doc import LibraryDoc
27
27
from robotcode .robot .diagnostics .namespace import Namespace
28
28
from robotcode .robot .utils .ast import (
29
29
iter_nodes ,
32
32
)
33
33
from robotcode .robot .utils .stubs import HasError , HasErrors , HeaderAndBodyBlock
34
34
35
- from ...common .parts .diagnostics import DiagnosticsResult
35
+ from ...common .parts .diagnostics import DiagnosticsCollectType , DiagnosticsResult
36
36
37
37
if TYPE_CHECKING :
38
38
from ..protocol import RobotLanguageServerProtocol
@@ -58,57 +58,81 @@ def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
58
58
self .parent .diagnostics .collect .add (self .collect_unused_keyword_references )
59
59
self .parent .diagnostics .collect .add (self .collect_unused_variable_references )
60
60
61
- self ._collect_unused_references_event = threading .Event ()
62
-
63
- self .parent .diagnostics .on_workspace_diagnostics_analyze .add (self ._on_workspace_diagnostics_analyze )
64
- self .parent .diagnostics .on_workspace_diagnostics_collect .add (self ._on_workspace_diagnostics_collect )
61
+ self .parent .diagnostics .on_get_related_documents .add (self ._on_get_related_documents )
65
62
66
63
def _on_initialized (self , sender : Any ) -> None :
67
64
self .parent .diagnostics .analyze .add (self .analyze )
68
-
69
- self .parent .documents_cache .namespace_invalidated .add (self .namespace_invalidated )
70
-
71
- def _on_workspace_diagnostics_analyze (self , sender : Any ) -> None :
72
- self ._collect_unused_references_event .clear ()
73
-
74
- def _on_workspace_diagnostics_collect (self , sender : Any ) -> None :
75
- self ._collect_unused_references_event .set ()
65
+ self .parent .documents_cache .namespace_invalidated .add (self ._on_namespace_invalidated )
66
+ self .parent .documents_cache .namespace_initialized (self ._on_namespace_initialized )
67
+ self .parent .documents_cache .libraries_changed .add (self ._on_libraries_changed )
68
+ self .parent .documents_cache .variables_changed .add (self ._on_variables_changed )
69
+
70
+ def _on_libraries_changed (self , sender : Any , libraries : List [LibraryDoc ]) -> None :
71
+ for doc in self .parent .documents .documents :
72
+ namespace = self .parent .documents_cache .get_only_initialized_namespace (doc )
73
+ if namespace is not None :
74
+ lib_docs = (e .library_doc for e in namespace .get_libraries ().values ())
75
+ if any (lib_doc in lib_docs for lib_doc in libraries ):
76
+ self .parent .diagnostics .force_refresh_document (doc )
77
+
78
+ def _on_variables_changed (self , sender : Any , variables : List [LibraryDoc ]) -> None :
79
+ for doc in self .parent .documents .documents :
80
+ namespace = self .parent .documents_cache .get_only_initialized_namespace (doc )
81
+ if namespace is not None :
82
+ lib_docs = (e .library_doc for e in namespace .get_imported_variables ().values ())
83
+ if any (lib_doc in lib_docs for lib_doc in variables ):
84
+ self .parent .diagnostics .force_refresh_document (doc )
76
85
77
86
@language_id ("robotframework" )
78
87
def analyze (self , sender : Any , document : TextDocument ) -> None :
79
88
self .parent .documents_cache .get_namespace (document ).analyze ()
80
89
81
90
@language_id ("robotframework" )
82
- def namespace_invalidated (self , sender : Any , namespace : Namespace ) -> None :
83
- self ._collect_unused_references_event .clear ()
91
+ def _on_namespace_initialized (self , sender : Any , namespace : Namespace ) -> None :
92
+ if namespace .document is not None :
93
+ self .parent .diagnostics .force_refresh_document (namespace .document )
94
+
95
+ @language_id ("robotframework" )
96
+ def _on_namespace_invalidated (self , sender : Any , namespace : Namespace ) -> None :
97
+ if namespace .document is not None :
98
+ namespace .document .remove_cache_entry (self ._collect_model_errors )
99
+ namespace .document .remove_cache_entry (self ._collect_token_errors )
100
+
101
+ @language_id ("robotframework" )
102
+ def _on_get_related_documents (self , sender : Any , document : TextDocument ) -> Optional [List [TextDocument ]]:
103
+ namespace = self .parent .documents_cache .get_only_initialized_namespace (document )
104
+ if namespace is None :
105
+ return None
84
106
85
- self . _namespace_invalidated ( namespace )
107
+ result = []
86
108
87
- self .parent .diagnostics .break_workspace_diagnostics_loop ()
109
+ resources = namespace .get_resources ().values ()
110
+ for r in resources :
111
+ if r .library_doc .source :
112
+ doc = self .parent .documents .get (Uri .from_path (r .library_doc .source ).normalized ())
113
+ if doc is not None :
114
+ result .append (doc )
88
115
89
- def _namespace_invalidated (self , namespace : Namespace ) -> None :
90
- if namespace .document is not None :
91
- refresh = namespace .document .opened_in_editor
116
+ lib_doc = namespace .get_library_doc ()
117
+ for doc in self .parent .documents .documents :
118
+ if doc .language_id != "robotframework" :
119
+ continue
92
120
93
- self .parent .diagnostics .force_refresh_document (namespace .document , False )
121
+ doc_namespace = self .parent .documents_cache .get_only_initialized_namespace (doc )
122
+ if doc_namespace is None :
123
+ continue
94
124
95
- if namespace .is_initialized ():
96
- resources = namespace .get_resources ().values ()
97
- for r in resources :
98
- if r .library_doc .source :
99
- doc = self .parent .documents .get (Uri .from_path (r .library_doc .source ).normalized ())
100
- if doc is not None :
101
- refresh |= doc .opened_in_editor
102
- self .parent .diagnostics .force_refresh_document (doc , False )
125
+ if doc_namespace .is_analyzed ():
126
+ for ref in doc_namespace .get_namespace_references ():
127
+ if ref .library_doc == lib_doc :
128
+ result .append (doc )
103
129
104
- if refresh :
105
- self .parent .diagnostics .refresh ()
130
+ return result
106
131
107
132
@language_id ("robotframework" )
108
- def collect_namespace_diagnostics (self , sender : Any , document : TextDocument ) -> DiagnosticsResult :
109
- return document .get_cache (self ._collect_namespace_diagnostics )
110
-
111
- def _collect_namespace_diagnostics (self , document : TextDocument ) -> DiagnosticsResult :
133
+ def collect_namespace_diagnostics (
134
+ self , sender : Any , document : TextDocument , diagnostics_type : DiagnosticsCollectType
135
+ ) -> DiagnosticsResult :
112
136
try :
113
137
namespace = self .parent .documents_cache .get_namespace (document )
114
138
@@ -172,7 +196,9 @@ def _create_error_from_token(self, token: Token, source: Optional[str] = None) -
172
196
173
197
@language_id ("robotframework" )
174
198
@_logger .call
175
- def collect_token_errors (self , sender : Any , document : TextDocument ) -> DiagnosticsResult :
199
+ def collect_token_errors (
200
+ self , sender : Any , document : TextDocument , diagnostics_type : DiagnosticsCollectType
201
+ ) -> DiagnosticsResult :
176
202
return document .get_cache (self ._collect_token_errors )
177
203
178
204
def _collect_token_errors (self , document : TextDocument ) -> DiagnosticsResult :
@@ -238,7 +264,9 @@ def _collect_token_errors(self, document: TextDocument) -> DiagnosticsResult:
238
264
239
265
@language_id ("robotframework" )
240
266
@_logger .call
241
- def collect_model_errors (self , sender : Any , document : TextDocument ) -> DiagnosticsResult :
267
+ def collect_model_errors (
268
+ self , sender : Any , document : TextDocument , diagnostics_type : DiagnosticsCollectType
269
+ ) -> DiagnosticsResult :
242
270
return document .get_cache (self ._collect_model_errors )
243
271
244
272
def _collect_model_errors (self , document : TextDocument ) -> DiagnosticsResult :
@@ -284,13 +312,15 @@ def _collect_model_errors(self, document: TextDocument) -> DiagnosticsResult:
284
312
285
313
@language_id ("robotframework" )
286
314
@_logger .call
287
- def collect_unused_keyword_references (self , sender : Any , document : TextDocument ) -> DiagnosticsResult :
315
+ def collect_unused_keyword_references (
316
+ self , sender : Any , document : TextDocument , diagnostics_type : DiagnosticsCollectType
317
+ ) -> DiagnosticsResult :
288
318
config = self .parent .workspace .get_configuration (AnalysisConfig , document .uri )
289
319
290
320
if not config .find_unused_references :
291
321
return DiagnosticsResult (self .collect_unused_keyword_references , [])
292
322
293
- if not self . _collect_unused_references_event . is_set () :
323
+ if diagnostics_type != DiagnosticsCollectType . SLOW :
294
324
return DiagnosticsResult (self .collect_unused_keyword_references , None , True )
295
325
296
326
return self ._collect_unused_keyword_references (document )
@@ -341,13 +371,15 @@ def _collect_unused_keyword_references(self, document: TextDocument) -> Diagnost
341
371
342
372
@language_id ("robotframework" )
343
373
@_logger .call
344
- def collect_unused_variable_references (self , sender : Any , document : TextDocument ) -> DiagnosticsResult :
374
+ def collect_unused_variable_references (
375
+ self , sender : Any , document : TextDocument , diagnostics_type : DiagnosticsCollectType
376
+ ) -> DiagnosticsResult :
345
377
config = self .parent .workspace .get_configuration (AnalysisConfig , document .uri )
346
378
347
379
if not config .find_unused_references :
348
380
return DiagnosticsResult (self .collect_unused_variable_references , [])
349
381
350
- if not self . _collect_unused_references_event . is_set () :
382
+ if diagnostics_type != DiagnosticsCollectType . SLOW :
351
383
return DiagnosticsResult (self .collect_unused_variable_references , None , True )
352
384
353
385
return self ._collect_unused_variable_references (document )
0 commit comments