|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import ast |
| 4 | +import asyncio |
3 | 5 | import io
|
4 | 6 | from typing import TYPE_CHECKING, Any, List, Optional
|
5 | 7 |
|
6 | 8 | from ....utils.logging import LoggingDescriptor
|
7 | 9 | from ...common.language import language_id
|
8 | 10 | from ...common.parts.diagnostics import DiagnosticsResult
|
| 11 | +from ...common.parts.workspace import WorkspaceFolder |
9 | 12 | from ...common.text_document import TextDocument
|
10 | 13 | from ...common.types import Diagnostic, DiagnosticSeverity, Position, Range
|
11 | 14 |
|
@@ -46,59 +49,74 @@ async def get_config(self, document: TextDocument) -> Optional[RoboCopConfig]:
|
46 | 49 | @_logger.call
|
47 | 50 | async def collect_diagnostics(self, sender: Any, document: TextDocument) -> DiagnosticsResult:
|
48 | 51 |
|
49 |
| - from robocop.config import Config |
50 |
| - from robocop.rules import RuleSeverity |
51 |
| - from robocop.run import Robocop |
52 |
| - |
53 |
| - result: List[Diagnostic] = [] |
54 | 52 | try:
|
55 | 53 | workspace_folder = self.parent.workspace.get_workspace_folder(document.uri)
|
56 | 54 | if workspace_folder is not None:
|
57 | 55 | extension_config = await self.get_config(document)
|
58 | 56 |
|
59 | 57 | if extension_config is not None and extension_config.enabled:
|
60 | 58 |
|
61 |
| - with io.StringIO("") as output: |
62 |
| - config = Config(str(workspace_folder.uri.to_path())) |
63 |
| - |
64 |
| - config.exec_dir = str(workspace_folder.uri.to_path()) |
65 |
| - |
66 |
| - config.output = output |
67 |
| - |
68 |
| - if extension_config.include: |
69 |
| - config.include = set(extension_config.include) |
70 |
| - if extension_config.exclude: |
71 |
| - config.exclude = set(extension_config.exclude) |
72 |
| - if extension_config.configurations: |
73 |
| - config.configure = set(extension_config.configurations) |
74 |
| - |
75 |
| - analyser = Robocop(from_cli=False, config=config) |
76 |
| - analyser.reload_config() |
77 |
| - |
78 |
| - model = await self.parent.documents_cache.get_model(document) |
79 |
| - |
80 |
| - issues = analyser.run_check(model, str(document.uri.to_path()), document.text) |
81 |
| - |
82 |
| - for issue in issues: |
83 |
| - d = Diagnostic( |
84 |
| - range=Range( |
85 |
| - start=Position(line=max(0, issue.line - 1), character=issue.col), |
86 |
| - end=Position(line=max(0, issue.end_line - 1), character=issue.end_col), |
87 |
| - ), |
88 |
| - message=issue.desc, |
89 |
| - severity=DiagnosticSeverity.INFORMATION |
90 |
| - if issue.severity == RuleSeverity.INFO |
91 |
| - else DiagnosticSeverity.WARNING |
92 |
| - if issue.severity == RuleSeverity.WARNING |
93 |
| - else DiagnosticSeverity.ERROR |
94 |
| - if issue.severity == RuleSeverity.ERROR |
95 |
| - else DiagnosticSeverity.HINT, |
96 |
| - source=self.source_name, |
97 |
| - code=f"{issue.severity.value}{issue.rule_id}", |
98 |
| - ) |
99 |
| - |
100 |
| - result.append(d) |
| 59 | + model = await self.parent.documents_cache.get_model(document) |
| 60 | + result = await self.collect_threading(document, workspace_folder, extension_config, model) |
| 61 | + return DiagnosticsResult(self.collect_diagnostics, result) |
101 | 62 | except BaseException:
|
102 | 63 | pass
|
103 | 64 |
|
104 |
| - return DiagnosticsResult(self.collect_diagnostics, result) |
| 65 | + return DiagnosticsResult(self.collect_diagnostics, []) |
| 66 | + |
| 67 | + async def collect_threading( |
| 68 | + self, document: TextDocument, workspace_folder: WorkspaceFolder, extension_config: RoboCopConfig, model: ast.AST |
| 69 | + ) -> List[Diagnostic]: |
| 70 | + return await asyncio.get_event_loop().run_in_executor( |
| 71 | + None, self.collect, document, workspace_folder, extension_config, model |
| 72 | + ) |
| 73 | + |
| 74 | + def collect( |
| 75 | + self, document: TextDocument, workspace_folder: WorkspaceFolder, extension_config: RoboCopConfig, model: ast.AST |
| 76 | + ) -> List[Diagnostic]: |
| 77 | + from robocop.config import Config |
| 78 | + from robocop.rules import RuleSeverity |
| 79 | + from robocop.run import Robocop |
| 80 | + |
| 81 | + result: List[Diagnostic] = [] |
| 82 | + |
| 83 | + with io.StringIO("") as output: |
| 84 | + config = Config(str(workspace_folder.uri.to_path())) |
| 85 | + |
| 86 | + config.exec_dir = str(workspace_folder.uri.to_path()) |
| 87 | + |
| 88 | + config.output = output |
| 89 | + |
| 90 | + if extension_config.include: |
| 91 | + config.include = set(extension_config.include) |
| 92 | + if extension_config.exclude: |
| 93 | + config.exclude = set(extension_config.exclude) |
| 94 | + if extension_config.configurations: |
| 95 | + config.configure = set(extension_config.configurations) |
| 96 | + |
| 97 | + analyser = Robocop(from_cli=False, config=config) |
| 98 | + analyser.reload_config() |
| 99 | + |
| 100 | + issues = analyser.run_check(model, str(document.uri.to_path()), document.text) |
| 101 | + |
| 102 | + for issue in issues: |
| 103 | + d = Diagnostic( |
| 104 | + range=Range( |
| 105 | + start=Position(line=max(0, issue.line - 1), character=issue.col), |
| 106 | + end=Position(line=max(0, issue.end_line - 1), character=issue.end_col), |
| 107 | + ), |
| 108 | + message=issue.desc, |
| 109 | + severity=DiagnosticSeverity.INFORMATION |
| 110 | + if issue.severity == RuleSeverity.INFO |
| 111 | + else DiagnosticSeverity.WARNING |
| 112 | + if issue.severity == RuleSeverity.WARNING |
| 113 | + else DiagnosticSeverity.ERROR |
| 114 | + if issue.severity == RuleSeverity.ERROR |
| 115 | + else DiagnosticSeverity.HINT, |
| 116 | + source=self.source_name, |
| 117 | + code=f"{issue.severity.value}{issue.rule_id}", |
| 118 | + ) |
| 119 | + |
| 120 | + result.append(d) |
| 121 | + |
| 122 | + return result |
0 commit comments