Skip to content

Commit 460c8df

Browse files
committed
KeywordMatchers now initialize lazy, some refactoring
1 parent f0a2dbe commit 460c8df

File tree

5 files changed

+54
-24
lines changed

5 files changed

+54
-24
lines changed

robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,19 @@
6868

6969
RUN_KEYWORDS_NAME = "Run Keywords"
7070

71-
RUN_KEYWORDS = [*RUN_KEYWORD_NAMES, *RUN_KEYWORD_WITH_CONDITION_NAMES, RUN_KEYWORDS_NAME, RUN_KEYWORD_IF_NAME]
71+
ALL_RUN_KEYWORDS = [*RUN_KEYWORD_NAMES, *RUN_KEYWORD_WITH_CONDITION_NAMES, RUN_KEYWORDS_NAME, RUN_KEYWORD_IF_NAME]
7272

7373
BUILTIN_LIBRARY_NAME = "BuiltIn"
7474
RESERVED_LIBRARY_NAME = "Reserved"
7575
DEFAULT_LIBRARIES = (BUILTIN_LIBRARY_NAME, RESERVED_LIBRARY_NAME, "Easter")
7676
ROBOT_LIBRARY_PACKAGE = "robot.libraries"
7777

7878
ALLOWED_LIBRARY_FILE_EXTENSIONS = [".py"]
79-
ALLOWED_RESOURCE_FILE_EXTENSIONS = [".robot", ".resource", ".rst", ".rest", ".txt"]
79+
80+
ROBOT_FILE_EXTENSION = ".robot"
81+
RESOURCE_FILE_EXTENSION = ".resource"
82+
83+
ALLOWED_RESOURCE_FILE_EXTENSIONS = [ROBOT_FILE_EXTENSION, RESOURCE_FILE_EXTENSION]
8084

8185
DEFAULT_DOC_FORMAT = "ROBOT"
8286

@@ -96,16 +100,31 @@ def is_embedded_keyword(name: str) -> bool:
96100

97101
class KeywordMatcher:
98102
def __init__(self, name: str) -> None:
103+
self.name = name
104+
self._normalized_name: Optional[str] = None
105+
self._embedded_arguments: Any = None
106+
107+
@property
108+
def normalized_name(self) -> str:
109+
from robot.utils.normalizing import normalize
110+
111+
if self._normalized_name is None:
112+
self._normalized_name = str(normalize(self.name, "_"))
113+
114+
return self._normalized_name
115+
116+
@property
117+
def embedded_arguments(self) -> Any:
99118
from robot.errors import VariableError
100119
from robot.running.arguments.embedded import EmbeddedArguments
101-
from robot.utils.normalizing import normalize
102120

103-
self.name = name
104-
self.normalized_name = str(normalize(name, "_"))
105-
try:
106-
self.embedded_arguments = EmbeddedArguments(name)
107-
except VariableError:
108-
self.embedded_arguments = ()
121+
if self._embedded_arguments is None:
122+
try:
123+
self._embedded_arguments = EmbeddedArguments(self.name)
124+
except VariableError:
125+
self._embedded_arguments = ()
126+
127+
return self._embedded_arguments
109128

110129
def __eq__(self, o: object) -> bool:
111130
from robot.utils.normalizing import normalize
@@ -131,6 +150,15 @@ def __repr__(self) -> str:
131150
return f"{type(self).__name__}(name={repr(self.name)})"
132151

133152

153+
RUN_KEYWORD_WITH_CONDITION_MATCHERS = [KeywordMatcher(e) for e in RUN_KEYWORD_WITH_CONDITION_NAMES]
154+
155+
RUN_KEYWORD_IF_MATCHER = KeywordMatcher(RUN_KEYWORD_IF_NAME)
156+
157+
RUN_KEYWORDS_MATCHER = KeywordMatcher(RUN_KEYWORDS_NAME)
158+
159+
ALL_RUN_KEYWORDS_MATCHERS = [KeywordMatcher(e) for e in ALL_RUN_KEYWORDS]
160+
161+
134162
class InvalidVariableError(Exception):
135163
pass
136164

@@ -315,7 +343,7 @@ def parameter_signature(self) -> str:
315343
return f"({', '.join(str(a) for a in self.args)})"
316344

317345
def is_any_run_keyword(self) -> bool:
318-
return self.libname == BUILTIN_LIBRARY_NAME and self.name in RUN_KEYWORDS
346+
return self.libname == BUILTIN_LIBRARY_NAME and self.name in ALL_RUN_KEYWORDS
319347

320348
def is_run_keyword(self) -> bool:
321349
return self.libname == BUILTIN_LIBRARY_NAME and self.name in RUN_KEYWORD_NAMES

robotcode/language_server/robotframework/parts/diagnostics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _create_error_from_node(self, node: ast.AST, msg: str, source: Optional[str]
5151
def _create_error_from_token(self, token: Token, source: Optional[str] = None) -> Diagnostic:
5252
return Diagnostic(
5353
range=range_from_token(token),
54-
message=token.error if token.error is not None else "Unknown Error.",
54+
message=token.error if token.error is not None else "(No Message).",
5555
severity=DiagnosticSeverity.ERROR,
5656
source=source if source is not None else self.source_name,
5757
code="TokenError",

vscode-client/extension.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ class TerminalLink extends vscode.TerminalLink {
1010
}
1111
}
1212

13+
let languageClientManger: LanguageClientsManager;
14+
1315
export async function activateAsync(context: vscode.ExtensionContext): Promise<void> {
1416
const outputChannel = vscode.window.createOutputChannel("RobotCode");
1517

1618
outputChannel.appendLine("Activate RobotCode Extension.");
1719

1820
const pythonManager = new PythonManager(context, outputChannel);
1921

20-
const languageClientManger = new LanguageClientsManager(context, pythonManager, outputChannel);
22+
languageClientManger = new LanguageClientsManager(context, pythonManager, outputChannel);
2123

2224
const debugManager = new DebugManager(context, pythonManager, languageClientManger, outputChannel);
2325

@@ -65,17 +67,18 @@ export async function activateAsync(context: vscode.ExtensionContext): Promise<v
6567
await testControllerManger.refresh();
6668
}
6769

68-
function displayProgress(promise: Promise<unknown>) {
70+
function displayProgress<R>(promise: Promise<R>): Thenable<R> {
6971
const progressOptions: vscode.ProgressOptions = {
7072
location: vscode.ProgressLocation.Window,
7173
title: "RobotCode extension loading ...",
7274
};
73-
vscode.window.withProgress(progressOptions, () => promise);
75+
return vscode.window.withProgress(progressOptions, () => promise);
7476
}
7577

7678
export async function activate(context: vscode.ExtensionContext): Promise<void> {
77-
displayProgress(activateAsync(context));
79+
return await displayProgress(activateAsync(context));
7880
}
7981

80-
// eslint-disable-next-line @typescript-eslint/no-empty-function
81-
export async function deactivate(): Promise<void> {}
82+
export async function deactivate(): Promise<void> {
83+
return await languageClientManger.stopAllClients();
84+
}

vscode-client/languageclientsmanger.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ export class LanguageClientsManager {
6161
for (const client of this.clients.values()) {
6262
promises.push(client.stop());
6363
}
64+
this.clients.clear();
6465

6566
await Promise.all(promises);
66-
67-
this.clients.clear();
6867
});
6968
}
7069

71-
async dispose(): Promise<void> {
72-
await this.stopAllClients();
70+
dispose(): void {
71+
this.stopAllClients().then((_) => undefined);
72+
7373
this._disposables.dispose();
7474
}
7575

@@ -195,7 +195,6 @@ export class LanguageClientsManager {
195195
this.outputChannel.appendLine(
196196
`client ${result?.clientOptions.workspaceFolder?.uri ?? "unknown"} error: ${reason}`
197197
);
198-
vscode.window.showErrorMessage(reason.message ?? "Unknown error.");
199198
return undefined;
200199
}
201200
);

vscode-client/testcontrollermanager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ export class TestControllerManager {
540540

541541
if (event.failedKeywords) {
542542
for (const keyword of event.failedKeywords) {
543-
const message = new vscode.TestMessage(keyword.message ?? "unknown error");
543+
const message = new vscode.TestMessage(keyword.message ?? "");
544544

545545
if (keyword.source) {
546546
message.location = new vscode.Location(
@@ -559,7 +559,7 @@ export class TestControllerManager {
559559
!event.attributes?.message ||
560560
!event.failedKeywords?.find((v) => v.message === event.attributes?.message)
561561
) {
562-
const message = new vscode.TestMessage(event.attributes.message ?? "unknown error");
562+
const message = new vscode.TestMessage(event.attributes.message ?? "");
563563

564564
if (event.attributes.source) {
565565
message.location = new vscode.Location(

0 commit comments

Comments
 (0)