Skip to content

Commit bd17a5d

Browse files
committed
feat(analyze): analyze code command now also uses the settings in the robot.toml file.
closes #347
1 parent ce794bf commit bd17a5d

File tree

10 files changed

+90
-45
lines changed

10 files changed

+90
-45
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"request": "launch",
2121
"module": "robotcode.cli",
2222
"justMyCode": false,
23-
"cwd": "${workspaceFolder}/tests/robotcode/language_server/robotframework/parts/data",
23+
//"cwd": "${workspaceFolder}/tests/robotcode/language_server/robotframework/parts/data",
2424
//"cwd": "${workspaceFolder}/..",
25-
//"cwd": "${workspaceFolder}",
25+
"cwd": "${workspaceFolder}",
2626
//"cwd": "E:/source/uvtestprj",
2727
//"cwd": "e:\\develop\\robot\\robotframework",
2828
// "env": {

packages/analyze/src/robotcode/analyze/cli.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ def __str__(self) -> str:
7575
Glob pattern to filter files to analyze. Can be specified multiple times.
7676
""",
7777
)
78+
@click.option(
79+
"-v",
80+
"--variable",
81+
metavar="name:value",
82+
type=str,
83+
multiple=True,
84+
help="Set variables in the test data. see `robot --variable` option.",
85+
)
86+
@click.option(
87+
"-V",
88+
"--variablefile",
89+
metavar="path",
90+
type=str,
91+
multiple=True,
92+
help="Python or YAML file file to read variables from. see `robot --variablefile` option.",
93+
)
94+
@click.option(
95+
"-P",
96+
"--pythonpath",
97+
metavar="path",
98+
type=str,
99+
multiple=True,
100+
help="Additional locations (directories, ZIPs, JARs) where to search test libraries"
101+
" and other extensions when they are imported. see `robot --pythonpath` option.",
102+
)
78103
@click.argument(
79104
"paths", nargs=-1, type=click.Path(exists=True, dir_okay=True, file_okay=True, readable=True, path_type=Path)
80105
)
@@ -109,7 +134,10 @@ def code(app: Application, filter: Tuple[str], paths: Tuple[Path]) -> None:
109134

110135
statistics = Statistic()
111136
for e in CodeAnalyzer(
112-
app=app, config=analyzer_config, robot_profile=robot_profile, root_folder=root_folder
137+
app=app,
138+
analysis_config=analyzer_config.to_workspace_analysis_config(),
139+
robot_profile=robot_profile,
140+
root_folder=root_folder,
113141
).run(paths=paths, filter=filter):
114142
statistics.files.add(e.document)
115143

packages/analyze/src/robotcode/analyze/code_analyzer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from robotcode.robot.config.model import RobotBaseProfile
1313
from robotcode.robot.diagnostics.workspace_config import WorkspaceAnalysisConfig
1414

15-
from .config import AnalyzeConfig
1615
from .diagnostics_context import DiagnosticHandlers, DiagnosticsContext
1716
from .robot_framework_language_provider import RobotFrameworkLanguageProvider
1817

@@ -27,12 +26,13 @@ class CodeAnalyzer(DiagnosticsContext):
2726
def __init__(
2827
self,
2928
app: Application,
30-
config: AnalyzeConfig,
29+
analysis_config: WorkspaceAnalysisConfig,
3130
robot_profile: RobotBaseProfile,
3231
root_folder: Optional[Path],
3332
):
3433
self.app = app
35-
self._config = config
34+
self._analysis_config = analysis_config or WorkspaceAnalysisConfig()
35+
3636
self._robot_profile = robot_profile
3737
self._root_folder = root_folder if root_folder is not None else Path.cwd()
3838

@@ -53,8 +53,8 @@ def __init__(
5353
handler.verbose_callback = app.verbose
5454

5555
@property
56-
def analysis_config(self) -> Optional[WorkspaceAnalysisConfig]:
57-
return None
56+
def analysis_config(self) -> WorkspaceAnalysisConfig:
57+
return self._analysis_config
5858

5959
@property
6060
def profile(self) -> RobotBaseProfile:

packages/analyze/src/robotcode/analyze/config.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
from typing import List, Optional
44

55
from robotcode.robot.config.model import BaseOptions, field
6+
from robotcode.robot.diagnostics.workspace_config import (
7+
AnalysisDiagnosticModifiersConfig,
8+
AnalysisRobotConfig,
9+
WorkspaceAnalysisConfig,
10+
)
11+
from robotcode.robot.diagnostics.workspace_config import CacheConfig as WorkspaceCacheConfig
612

713

814
@dataclass
@@ -246,3 +252,30 @@ class AnalyzeConfig(BaseOptions):
246252
extend_global_library_search_order: Optional[List[str]] = field(
247253
description="Extend the global library search order setting."
248254
)
255+
256+
def to_workspace_analysis_config(self) -> WorkspaceAnalysisConfig:
257+
return WorkspaceAnalysisConfig(
258+
exclude_patterns=self.exclude_patterns or [],
259+
cache=(
260+
WorkspaceCacheConfig(
261+
# TODO savelocation
262+
ignored_libraries=self.cache.ignored_libraries or [],
263+
ignored_variables=self.cache.ignored_variables or [],
264+
ignore_arguments_for_library=self.cache.ignore_arguments_for_library or [],
265+
)
266+
if self.cache is not None
267+
else WorkspaceCacheConfig()
268+
),
269+
robot=AnalysisRobotConfig(global_library_search_order=self.global_library_search_order or []),
270+
modifiers=(
271+
AnalysisDiagnosticModifiersConfig(
272+
ignore=self.modifiers.ignore or [],
273+
error=self.modifiers.error or [],
274+
warning=self.modifiers.warning or [],
275+
information=self.modifiers.information or [],
276+
hint=self.modifiers.hint or [],
277+
)
278+
if self.modifiers is not None
279+
else AnalysisDiagnosticModifiersConfig()
280+
),
281+
)

packages/analyze/src/robotcode/analyze/diagnostics_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def collect_diagnostics(self, document: TextDocument) -> List[Union[List[Diagnos
3737
class DiagnosticsContext(ABC):
3838
@property
3939
@abstractmethod
40-
def analysis_config(self) -> Optional[WorkspaceAnalysisConfig]: ...
40+
def analysis_config(self) -> WorkspaceAnalysisConfig: ...
4141

4242
@property
4343
@abstractmethod

packages/analyze/src/robotcode/analyze/robot_framework_language_provider.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ def collect_workspace_folder_files(self, folder: WorkspaceFolder) -> Iterable[Pa
7070

7171
extensions = self.LANGUAGE_DEFINITION.extensions
7272

73+
exclude_patterns = [
74+
*self.diagnostics_context.analysis_config.exclude_patterns,
75+
*(config.exclude_patterns or []),
76+
]
7377
return filter(
7478
lambda f: f.suffix.lower() in extensions,
7579
iter_files(
7680
folder.uri.to_path(),
7781
ignore_files=[ROBOT_IGNORE_FILE, GIT_IGNORE_FILE],
7882
include_hidden=False,
7983
parent_spec=IgnoreSpec.from_list(
80-
[*DEFAULT_SPEC_RULES, *(config.exclude_patterns or [])],
84+
[*DEFAULT_SPEC_RULES, *exclude_patterns],
8185
folder.uri.to_path(),
8286
),
8387
verbose_callback=self.verbose_callback,
@@ -90,4 +94,4 @@ def analyze_document(self, sender: Any, document: TextDocument) -> Optional[List
9094

9195
namespace.analyze()
9296

93-
return namespace.get_diagnostics()
97+
return self._document_cache.get_diagnostic_modifier(document).modify_diagnostics(namespace.get_diagnostics())

packages/language_server/src/robotcode/language_server/cli.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
from robotcode.robot.config.loader import load_robot_config_from_path
1616
from robotcode.robot.config.model import RobotBaseProfile
1717
from robotcode.robot.config.utils import get_config_files
18-
from robotcode.robot.diagnostics.workspace_config import (
19-
AnalysisDiagnosticModifiersConfig,
20-
AnalysisRobotConfig,
21-
CacheConfig,
22-
WorkspaceAnalysisConfig,
23-
)
18+
from robotcode.robot.diagnostics.workspace_config import WorkspaceAnalysisConfig
2419

2520
from .__version__ import __version__
2621

@@ -101,36 +96,13 @@ def language_server(
10196
if analyzer_config is None:
10297
analyzer_config = AnalyzeConfig()
10398

104-
analysis_config = WorkspaceAnalysisConfig(
105-
cache=(
106-
CacheConfig(
107-
# TODO savelocation
108-
ignored_libraries=analyzer_config.cache.ignored_libraries or [],
109-
ignored_variables=analyzer_config.cache.ignored_variables or [],
110-
ignore_arguments_for_library=analyzer_config.cache.ignore_arguments_for_library or [],
111-
)
112-
if analyzer_config.cache is not None
113-
else CacheConfig()
114-
),
115-
robot=AnalysisRobotConfig(global_library_search_order=analyzer_config.global_library_search_order or []),
116-
modifiers=(
117-
AnalysisDiagnosticModifiersConfig(
118-
ignore=analyzer_config.modifiers.ignore or [],
119-
error=analyzer_config.modifiers.error or [],
120-
warning=analyzer_config.modifiers.warning or [],
121-
information=analyzer_config.modifiers.information or [],
122-
hint=analyzer_config.modifiers.hint or [],
123-
)
124-
if analyzer_config.modifiers is not None
125-
else AnalysisDiagnosticModifiersConfig()
126-
),
127-
)
99+
analysis_config = analyzer_config.to_workspace_analysis_config()
128100

129101
profile = robot_config.combine_profiles(
130102
*(app.config.profiles or []), verbose_callback=app.verbose, error_callback=app.error
131103
).evaluated_with_env(verbose_callback=app.verbose, error_callback=app.error)
132104
except (TypeError, ValueError) as e:
133-
app.echo(str(e), err=True)
105+
app.error(str(e), err=True)
134106

135107
mode, port, bind, pipe_name = resolve_server_options(
136108
ctx, app, mode, port, bind, pipe_name, tcp, socket, stdio, pipe, None

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def load_workspace_documents(self, sender: Any) -> None:
6363
config = self.parent.workspace.get_configuration(RobotCodeConfig, folder.uri)
6464

6565
extensions = [ROBOT_FILE_EXTENSION, RESOURCE_FILE_EXTENSION]
66+
67+
exclude_patterns = [
68+
*self.parent.analysis_config.exclude_patterns,
69+
*(config.workspace.exclude_patterns or []),
70+
]
71+
6672
with self.parent.window.progress("Collect sources", cancellable=False):
6773
files = list(
6874
filter(
@@ -72,7 +78,7 @@ def load_workspace_documents(self, sender: Any) -> None:
7278
ignore_files=[ROBOT_IGNORE_FILE, GIT_IGNORE_FILE],
7379
include_hidden=False,
7480
parent_spec=IgnoreSpec.from_list(
75-
[*DEFAULT_SPEC_RULES, *(config.workspace.exclude_patterns or [])],
81+
[*DEFAULT_SPEC_RULES, *exclude_patterns],
7682
folder.uri.to_path(),
7783
),
7884
verbose_callback=self._logger.debug,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class WorkspaceConfig(ConfigBase):
7575

7676
@dataclass
7777
class WorkspaceAnalysisConfig:
78+
exclude_patterns: List[str] = field(default_factory=list)
7879
cache: CacheConfig = field(default_factory=CacheConfig)
7980
robot: AnalysisRobotConfig = field(default_factory=AnalysisRobotConfig)
8081
modifiers: AnalysisDiagnosticModifiersConfig = field(default_factory=AnalysisDiagnosticModifiersConfig)

robot.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ a_robot_toml_variable = "a_robot_toml_value"
1111
[tool.robotcode-analyze]
1212

1313

14-
[tool.robotcode-analyze.extend-modifiers]
15-
warning = ["user"]
14+
[tool.robotcode-analyze.modifiers]
15+
warning = ["KeywordNotFound"]
16+
#warning = ["user"]
1617
extend-hint = ["a_second_hint"]

0 commit comments

Comments
 (0)