Skip to content

Commit 0c664a4

Browse files
committed
refactored webviews, register and notification listeners
1 parent a49d15b commit 0c664a4

File tree

17 files changed

+503
-365
lines changed

17 files changed

+503
-365
lines changed

vscode/src/commands/webViews.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
import { JdkDownloaderView } from "../jdkDownloader/view";
16+
import { JdkDownloaderView } from "../webviews/jdkDownloader/view";
1717
import { extCommands } from "./commands";
1818
import { ICommand } from "./types";
1919

vscode/src/configurations/handlers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export const getConfigurationValue = <T>(key: string, defaultValue: T | undefine
2727
return defaultValue != undefined ? conf.get(key, defaultValue) : conf.get(key) as T;
2828
}
2929

30+
export const updateConfigurationValue = <T>(key: string, newValue: T): void => {
31+
workspace.getConfiguration(extConstants.COMMAND_PREFIX).update(key, newValue);
32+
}
33+
3034
export const getBuiltinConfigurationValue = <T>(key: string, defaultValue: T | undefined = undefined): T => {
3135
const splitKey = key.split('.');
3236
const selector = splitKey?.[0];

vscode/src/configurations/listener.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

vscode/src/extension.ts

Lines changed: 17 additions & 316 deletions
Large diffs are not rendered by default.

vscode/src/listener.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright (c) 2023-2024, Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
import { ConfigurationChangeEvent, Disposable, TextEditor, window, workspace } from "vscode";
17+
import { userConfigsListened } from "./configurations/configuration";
18+
import { globalVars } from "./extension";
19+
import { asRanges } from "./lsp/protocol";
20+
21+
const configChangeHandler = (params: ConfigurationChangeEvent) => {
22+
userConfigsListened.forEach((config: string) => {
23+
const doesAffect = params.affectsConfiguration(config);
24+
if (doesAffect) {
25+
globalVars.clientPromise.restartExtension(globalVars.nbProcessManager, true);
26+
}
27+
});
28+
}
29+
30+
const visibleTextEditorsChangeHandler = (editors: readonly TextEditor[]) => {
31+
editors.forEach((editor: any) => {
32+
let decorationParams = globalVars.decorationParamsByUri.get(editor.document.uri);
33+
if (decorationParams) {
34+
let decorationType = globalVars.decorations.get(decorationParams.key);
35+
if (decorationType) {
36+
editor.setDecorations(decorationType, asRanges(decorationParams.ranges));
37+
}
38+
}
39+
});
40+
}
41+
42+
const configChangeListener = workspace.onDidChangeConfiguration(configChangeHandler);
43+
const visibleTextEditorsChangeListener = window.onDidChangeVisibleTextEditors(visibleTextEditorsChangeHandler);
44+
45+
const beforeInitlisteners: Disposable[] = [configChangeListener];
46+
const afterInitlisteners: Disposable[] = [visibleTextEditorsChangeListener];
47+
48+
49+
export const registerListenersBeforeClientInit = () => {
50+
beforeInitlisteners.forEach(listener => {
51+
globalVars.extensionInfo.pushSubscription(listener);
52+
});
53+
}
54+
55+
export const registerListenersAfterClientInit = () => {
56+
afterInitlisteners.forEach(listener => {
57+
globalVars.extensionInfo.pushSubscription(listener);
58+
});
59+
}

vscode/src/lsp/clientPromise.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ export class ClientPromise {
2525
client!: Promise<NbLanguageClient>;
2626
activationPending: boolean = true;
2727

28-
public clientPromiseInitialization = (): void => {
28+
public initialize = (): void => {
2929
this.client = new Promise<NbLanguageClient>((clientOK, clientErr) => {
3030
this.setClient = [
3131
(c: NbLanguageClient) => {
32-
this.initialPromiseResolved = true;
3332
clientOK(c);
3433
},
3534
(err: any) => {
@@ -71,7 +70,7 @@ export class ClientPromise {
7170
try {
7271
await this.stopClient();
7372
await nbProcessManager.killProcess(notifyKill);
74-
this.clientPromiseInitialization();
73+
this.initialize();
7574
initializeServer();
7675
} catch (error) {
7776
LOGGER.log(`Error during activation: ${error}`, LogLevel.ERROR);

vscode/src/lsp/initializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { extConstants, NODE_WINDOWS_LABEL } from "../constants";
2424
import { l10n } from "../localiser";
2525
import { window } from "vscode";
2626
import { ChildProcess } from "child_process";
27-
import { jdkDownloaderPrompt } from "../jdkDownloader/prompt";
27+
import { jdkDownloaderPrompt } from "../webviews/jdkDownloader/prompt";
2828
import * as os from 'os';
2929
import { LogLevel } from "../logger";
3030
import { isNbJavacDisabledHandler } from "../configurations/handlers";
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
Copyright (c) 2023-2024, Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
import { LogMessageNotification, MessageType, TelemetryEventNotification } from "vscode-languageclient";
17+
import { notificationOrRequestListenerType } from "../types";
18+
import { asRanges, ShowStatusMessageParams, StatusMessageRequest, TestProgressNotification, TextEditorDecorationDisposeNotification, TextEditorDecorationSetNotification } from "../protocol";
19+
import { commands, window, workspace } from "vscode";
20+
import { isNbJavacDisabledHandler, updateConfigurationValue } from "../../configurations/handlers";
21+
import { l10n } from "../../localiser";
22+
import { configKeys } from "../../configurations/configuration";
23+
import { builtInCommands } from "../../commands/commands";
24+
import { globalVars, LOGGER } from "../../extension";
25+
26+
const checkInstallNbJavac = (msg: string) => {
27+
const NO_JAVA_SUPPORT = "Cannot initialize Java support";
28+
if (msg.startsWith(NO_JAVA_SUPPORT)) {
29+
if (isNbJavacDisabledHandler()) {
30+
const message = l10n.value("jdk.extension.nbjavac.message.supportedVersionRequired");
31+
const enable = l10n.value("jdk.extension.nbjavac.label.enableNbjavac");
32+
const settings = l10n.value("jdk.extension.nbjavac.label.openSettings");
33+
window.showErrorMessage(message, enable, settings).then(reply => {
34+
if (enable === reply) {
35+
updateConfigurationValue(configKeys.disableNbJavac, false);
36+
} else if (settings === reply) {
37+
commands.executeCommand(builtInCommands.openSettings, configKeys.jdkHome);
38+
}
39+
});
40+
}
41+
}
42+
}
43+
44+
const showStatusBarMessageHandler = (params : ShowStatusMessageParams) => {
45+
let decorated : string = params.message;
46+
let defTimeout;
47+
48+
switch (params.type) {
49+
case MessageType.Error:
50+
decorated = '$(error) ' + params.message;
51+
defTimeout = 0;
52+
checkInstallNbJavac(params.message);
53+
break;
54+
case MessageType.Warning:
55+
decorated = '$(warning) ' + params.message;
56+
defTimeout = 0;
57+
break;
58+
default:
59+
defTimeout = 10000;
60+
break;
61+
}
62+
// params.timeout may be defined but 0 -> should be used
63+
const timeout = params.timeout != undefined ? params.timeout : defTimeout;
64+
if (timeout > 0) {
65+
window.setStatusBarMessage(decorated, timeout);
66+
} else {
67+
window.setStatusBarMessage(decorated);
68+
}
69+
}
70+
71+
const logMessageHandler = (param: any) => {
72+
LOGGER.log(param.message);
73+
}
74+
75+
const testProgressHandler = (param: any) => {
76+
if (globalVars.testAdapter) {
77+
globalVars.testAdapter.testProgress(param.suite);
78+
}
79+
}
80+
81+
const textEditorSetDecorationHandler = (param: any) => {
82+
let decorationType = globalVars.decorations.get(param.key);
83+
if (decorationType) {
84+
let editorsWithUri = window.visibleTextEditors.filter(
85+
editor => editor.document.uri.toString() == param.uri
86+
);
87+
if (editorsWithUri.length > 0) {
88+
editorsWithUri[0].setDecorations(decorationType, asRanges(param.ranges));
89+
globalVars.decorationParamsByUri.set(editorsWithUri[0].document.uri, param);
90+
}
91+
}
92+
}
93+
94+
const textEditorDecorationDisposeHandler = (param: any) => {
95+
let decorationType = globalVars.decorations.get(param);
96+
if (decorationType) {
97+
globalVars.decorations.delete(param);
98+
decorationType.dispose();
99+
globalVars.decorationParamsByUri.forEach((value, key, map) => {
100+
if (value.key == param) {
101+
map.delete(key);
102+
}
103+
});
104+
}
105+
}
106+
107+
108+
const telemetryEventHandler = (param: any) => {
109+
const ls = globalVars.listeners.get(param);
110+
if (ls) {
111+
for (const listener of ls) {
112+
commands.executeCommand(listener);
113+
}
114+
}
115+
}
116+
117+
export const notificationListeners : notificationOrRequestListenerType[] = [{
118+
type: StatusMessageRequest.type,
119+
handler: showStatusBarMessageHandler
120+
}, {
121+
type: LogMessageNotification.type,
122+
handler: logMessageHandler
123+
}, {
124+
type: TestProgressNotification.type,
125+
handler: testProgressHandler
126+
},{
127+
type: TextEditorDecorationSetNotification.type,
128+
handler: textEditorSetDecorationHandler
129+
},{
130+
type: TextEditorDecorationDisposeNotification.type,
131+
handler: textEditorDecorationDisposeHandler
132+
},{
133+
type: TelemetryEventNotification.type,
134+
handler: telemetryEventHandler
135+
}];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright (c) 2023-2024, Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
import { NbLanguageClient } from "../nbLanguageClient"
17+
import { notificationOrRequestListenerType } from "../types"
18+
import { notificationListeners } from "./handlers"
19+
20+
export const registerNotificationListeners = (client: NbLanguageClient) => {
21+
notificationListeners.forEach((listener: notificationOrRequestListenerType) => {
22+
const { type, handler } = listener;
23+
client.onNotification(type, handler);
24+
})
25+
}

0 commit comments

Comments
 (0)