Skip to content

Commit d11e5be

Browse files
committed
add new option to enable/disable analysing unused references
1 parent fb11fcb commit d11e5be

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@
461461
"markdownDescription": "Enables 'robotidy' code formatting, if installed. See [robotidy](https://github.com/MarketSquare/robotframework-tidy)",
462462
"scope": "resource"
463463
},
464+
"robotcode.analysis.findUnusedReferences": {
465+
"type": "boolean",
466+
"default": false,
467+
"markdownDescription": "Find and reports unused keyword and variable references.",
468+
"scope": "resource"
469+
},
464470
"robotcode.analysis.diagnosticMode": {
465471
"type": "string",
466472
"enum": [

robotcode/language_server/common/parts/diagnostics.py

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def __init__(self, protocol: LanguageServerProtocol) -> None:
8282
super().__init__(protocol)
8383

8484
self.workspace_loaded_event = Event()
85+
8586
self._workspace_load_lock = Lock()
8687
self._workspace_loaded = False
8788

@@ -92,7 +93,6 @@ def __init__(self, protocol: LanguageServerProtocol) -> None:
9293
self._current_workspace_task: Optional[asyncio.Task[WorkspaceDiagnosticReport]] = None
9394

9495
self.in_get_workspace_diagnostics = Event(True)
95-
self._collect_full_diagnostics = False
9696

9797
def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
9898
if (
@@ -107,15 +107,6 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
107107
work_done_progress=True,
108108
)
109109

110-
@property
111-
def collect_full_diagnostics(self) -> bool:
112-
return self._collect_full_diagnostics
113-
114-
async def set_collect_full_diagnostics(self, value: bool) -> None:
115-
if self._collect_full_diagnostics != value:
116-
self._collect_full_diagnostics = value
117-
await self.refresh()
118-
119110
@async_tasking_event_iterator
120111
async def collect(sender, document: TextDocument, full: bool) -> DiagnosticsResult: # NOSONAR
121112
...
@@ -216,8 +207,6 @@ async def _text_document_diagnostic(
216207
self._logger.debug(lambda: f"textDocument/diagnostic for {text_document}")
217208

218209
try:
219-
# await self.ensure_workspace_loaded()
220-
221210
document = await self.parent.documents.get(text_document.uri)
222211
if document is None:
223212
raise JsonRPCErrorException(ErrorCodes.INVALID_PARAMS, f"Document {text_document!r} not found")
@@ -274,13 +263,6 @@ async def _get_diagnostics() -> WorkspaceDiagnosticReport:
274263
result: List[WorkspaceDocumentDiagnosticReport] = []
275264

276265
for doc in self.parent.documents.documents:
277-
if self._current_workspace_task is None:
278-
raise JsonRPCErrorException(
279-
ErrorCodes.SERVER_CANCELLED,
280-
"ServerCancelled",
281-
data=DiagnosticServerCancellationData(True),
282-
)
283-
284266
doc_result = await self.get_document_diagnostics(doc)
285267

286268
if doc_result.result_id is not None and any(
@@ -301,7 +283,7 @@ async def _get_diagnostics() -> WorkspaceDiagnosticReport:
301283

302284
async def _get_partial_diagnostics() -> WorkspaceDiagnosticReport:
303285
async with self.parent.window.progress(
304-
f"Analyse {'full ' if self.collect_full_diagnostics else ''} Workspace",
286+
"Analyse Workspace",
305287
progress_token=work_done_token,
306288
cancellable=False,
307289
) as progress:
@@ -315,7 +297,7 @@ async def _task(doc: TextDocument) -> None:
315297
else:
316298
name = path.relative_to(folder.uri.to_path())
317299

318-
progress.report(f"Analyse {'full ' if self.collect_full_diagnostics else ''} {name}")
300+
progress.report(f"Analyse {name}")
319301

320302
doc_result = await self.get_document_diagnostics(doc)
321303

@@ -349,12 +331,6 @@ async def _task(doc: TextDocument) -> None:
349331
for doc in self.parent.documents.documents:
350332
if await self.get_diagnostics_mode(doc.uri) == DiagnosticsMode.WORKSPACE:
351333
await _task(doc)
352-
if self._current_workspace_task is None:
353-
raise JsonRPCErrorException(
354-
ErrorCodes.SERVER_CANCELLED,
355-
"ServerCancelled",
356-
data=DiagnosticServerCancellationData(True),
357-
)
358334

359335
return WorkspaceDiagnosticReport(items=[])
360336

@@ -368,11 +344,15 @@ async def _task(doc: TextDocument) -> None:
368344
task = create_sub_task(_get_diagnostics() if partial_result_token is None else _get_partial_diagnostics())
369345
self._current_workspace_task = task
370346
try:
371-
result = await task
372-
await self.set_collect_full_diagnostics(True)
373-
return result
347+
return await task
374348
except asyncio.CancelledError:
375349
self._logger.debug("workspace/diagnostic canceled")
350+
if self._current_workspace_task is None:
351+
raise JsonRPCErrorException(
352+
ErrorCodes.SERVER_CANCELLED,
353+
"ServerCancelled",
354+
data=DiagnosticServerCancellationData(True),
355+
)
376356
raise
377357
finally:
378358
if self._current_workspace_task == task:

robotcode/language_server/robotframework/configuration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class AnalysisConfig(ConfigBase):
7979
progress_mode: AnalysisProgressMode = AnalysisProgressMode.SIMPLE
8080
max_project_file_count: int = 1000
8181
references_code_lens: bool = False
82+
find_unused_references: bool = False
8283

8384

8485
@config_section("robotcode")

robotcode/language_server/robotframework/parts/diagnostics.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717
from ...common.parts.diagnostics import DiagnosticsResult
1818
from ...common.text_document import TextDocument
19+
from ..configuration import AnalysisConfig
1920
from ..diagnostics.analyzer import Analyzer
2021
from ..utils.ast_utils import (
2122
HeaderAndBodyBlock,
@@ -49,11 +50,8 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
4950

5051
@language_id("robotframework")
5152
async def namespace_invalidated(self, sender: Any, document: TextDocument) -> None:
52-
# await asyncio.sleep(2)
53-
# await self.parent.diagnostics.cancel_workspace_diagnostics()
54-
# await self.parent.diagnostics.cancel_document_diagnostics(document)
55-
# await self.parent.diagnostics.set_collect_full_diagnostics(False)
56-
pass
53+
await self.parent.diagnostics.cancel_document_diagnostics(document)
54+
await self.parent.diagnostics.cancel_workspace_diagnostics()
5755

5856
@language_id("robotframework")
5957
@threaded()
@@ -249,8 +247,11 @@ async def _collect_model_errors(self, document: TextDocument, full: bool) -> Dia
249247
@threaded()
250248
@_logger.call
251249
async def collect_unused_references(self, sender: Any, document: TextDocument, full: bool) -> DiagnosticsResult:
252-
if not full:
250+
config = await self.parent.workspace.get_configuration(AnalysisConfig, document.uri)
251+
252+
if not full or not config.find_unused_references:
253253
return DiagnosticsResult(self.collect_unused_references, [])
254+
254255
return await document.get_cache(self._collect_unused_references, full)
255256

256257
async def _collect_unused_references(self, document: TextDocument, full: bool) -> DiagnosticsResult:

0 commit comments

Comments
 (0)