Skip to content

Commit 25bdcce

Browse files
committed
Added configurations, lsp and logger modules
1 parent aaed076 commit 25bdcce

28 files changed

+2188
-971
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 { appendPrefixToCommand } from "../utils"
17+
18+
export const configKeys = {
19+
jdkHome: 'jdkhome',
20+
projectJdkHome: 'project.jdkhome',
21+
lspVmOptions: 'serverVmOptions',
22+
disableNbJavac: 'advanced.disable.nbjavac',
23+
disableProjSearchLimit: 'advanced.disable.projectSearchLimit',
24+
formatPrefs: 'format',
25+
hintPrefs: 'hints',
26+
importPrefs: 'java.imports',
27+
runConfigVmOptions: 'runConfig.vmOptions',
28+
runConfigCwd: 'runConfig.cwd',
29+
verbose: 'verbose',
30+
userdir: 'userdir',
31+
vscodeTheme: 'workbench.colorTheme'
32+
};
33+
34+
export const userConfigsListened = [
35+
appendPrefixToCommand(configKeys.jdkHome),
36+
appendPrefixToCommand(configKeys.userdir),
37+
appendPrefixToCommand(configKeys.lspVmOptions),
38+
appendPrefixToCommand(configKeys.disableNbJavac),
39+
appendPrefixToCommand(configKeys.disableProjSearchLimit),
40+
configKeys.vscodeTheme,
41+
];
42+
43+
44+
export const userConfigsListenedByServer = [
45+
appendPrefixToCommand(configKeys.hintPrefs),
46+
appendPrefixToCommand(configKeys.formatPrefs),
47+
appendPrefixToCommand(configKeys.importPrefs),
48+
appendPrefixToCommand(configKeys.projectJdkHome),
49+
appendPrefixToCommand(configKeys.runConfigVmOptions),
50+
appendPrefixToCommand(configKeys.runConfigCwd)
51+
];
52+

vscode/src/configurations/handlers.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 { extensions, workspace } from "vscode";
17+
import { configKeys } from "./configuration";
18+
import { extConstants, NODE_WINDOWS_LABEL } from "../constants";
19+
import * as os from 'os';
20+
import { globalVars, LOGGER } from "../extension";
21+
import { LogLevel } from "../logger";
22+
import * as path from 'path';
23+
import * as fs from 'fs';
24+
25+
export const getConfigurationValue = <T>(key: string, defaultValue: T | undefined = undefined): T => {
26+
const conf = workspace.getConfiguration(extConstants.COMMAND_PREFIX);
27+
return defaultValue != undefined ? conf.get(key, defaultValue) : conf.get(key) as T;
28+
}
29+
30+
export const jdkHomeValueHandler = (): string | null => {
31+
return getConfigurationValue(configKeys.jdkHome) ||
32+
process.env.JDK_HOME ||
33+
process.env.JAVA_HOME ||
34+
null;
35+
}
36+
37+
export const projectSearchRootsValueHandler = (): string => {
38+
let projectSearchRoots: string = '';
39+
const isProjectFolderSearchLimited: boolean = !getConfigurationValue(configKeys.disableProjSearchLimit, false);
40+
if (isProjectFolderSearchLimited) {
41+
try {
42+
projectSearchRoots = os.homedir() as string;
43+
} catch (err: any) {
44+
LOGGER.log(`Failed to obtain the user home directory due to: ${err}`, LogLevel.ERROR);
45+
}
46+
if (!projectSearchRoots) {
47+
projectSearchRoots = os.type() === NODE_WINDOWS_LABEL ? '%USERPROFILE%' : '$HOME'; // The launcher script may perform the env variable substitution
48+
LOGGER.log(`Using userHomeDir = "${projectSearchRoots}" as the launcher script may perform env var substitution to get its value.`);
49+
}
50+
const workspaces = workspace.workspaceFolders;
51+
if (workspaces) {
52+
workspaces.forEach(workspace => {
53+
if (workspace.uri) {
54+
try {
55+
projectSearchRoots = projectSearchRoots + path.delimiter + path.normalize(workspace.uri.fsPath);
56+
} catch (err: any) {
57+
LOGGER.log(`Failed to get the workspace path: ${err}`);
58+
}
59+
}
60+
});
61+
}
62+
}
63+
64+
return projectSearchRoots;
65+
}
66+
67+
export const lspServerVmOptionsHandler = (): string[] => {
68+
let serverVmOptions: string[] = getConfigurationValue(configKeys.lspVmOptions, []);
69+
70+
return serverVmOptions.map(el => `-J${el}`);
71+
}
72+
73+
export const isDarkColorThemeHandler = (): boolean => {
74+
// const themeName = getConfigurationValue(configKeys.vscodeTheme);
75+
const themeName = workspace.getConfiguration('workbench')?.get('colorTheme');
76+
if (!themeName) {
77+
return false;
78+
}
79+
for (const ext of extensions.all) {
80+
const themeList: object[] = ext.packageJSON?.contributes && ext.packageJSON?.contributes['themes'];
81+
if (!themeList) {
82+
continue;
83+
}
84+
let t: any;
85+
for (t of themeList) {
86+
if (t.id !== themeName) {
87+
continue;
88+
}
89+
const uiTheme = t.uiTheme;
90+
if (typeof (uiTheme) == 'string') {
91+
if (uiTheme.includes('-dark') || uiTheme.includes('-black')) {
92+
return true;
93+
}
94+
}
95+
}
96+
}
97+
return false;
98+
}
99+
100+
export const userdirHandler = (): string => {
101+
const userdirScope = process.env['nbcode_userdir'] || getConfigurationValue(configKeys.userdir, "local");
102+
const userdirParentDir = userdirScope === "local"
103+
? globalVars.extensionInfo.getWorkspaceStorage()?.fsPath
104+
: globalVars.extensionInfo.getGlobalStorage().fsPath;
105+
106+
if (!userdirParentDir) {
107+
throw new Error(`Cannot create path for ${userdirScope} directory.`);
108+
}
109+
110+
const userdir = path.join(userdirParentDir, "userdir");
111+
112+
try {
113+
if(!fs.existsSync(userdir)){
114+
fs.mkdirSync(userdir, { recursive: true });
115+
const stats = fs.statSync(userdir);
116+
if (!stats.isDirectory()) {
117+
throw new Error(`${userdir} is not a directory`);
118+
}
119+
}
120+
121+
return userdir;
122+
} catch (error) {
123+
throw new Error(`Failed to create or access ${userdir}: ${(error as Error).message}`);
124+
}
125+
}
126+
127+
export const isNbJavacDisabledHandler = (): boolean => {
128+
return getConfigurationValue(configKeys.verbose, false);
129+
}

vscode/src/configurations/listener.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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, workspace } from "vscode";
17+
import { userConfigsListened } from "./configuration";
18+
import { globalVars } from "../extension";
19+
20+
let timeout: NodeJS.Timeout | undefined = undefined;
21+
export const configChangeListener = (params: ConfigurationChangeEvent) => {
22+
if (timeout) {
23+
return;
24+
}
25+
timeout = setTimeout(() => {
26+
timeout = undefined;
27+
userConfigsListened.forEach((config: string) => {
28+
const doesAffect = params.affectsConfiguration(config);
29+
if(doesAffect){
30+
globalVars.clientPromise.restartExtension(globalVars.nbProcessManager, true);
31+
}
32+
})
33+
}, 0);
34+
}

vscode/src/constants.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@
1515
*/
1616

1717

18-
export const JDK_RELEASES_TRACK_URL = `https://www.java.com/releases/releases.json`;
18+
export namespace extConstants {
19+
export const API_VERSION: string = "1.0";
20+
export const SERVER_NAME: string = "Oracle Java SE Language Server";
21+
export const NB_LANGUAGE_CLIENT_ID: string = 'Oracle Java SE';
22+
export const NB_LANGUAGE_CLIENT_NAME: string = "java";
23+
export const LANGUAGE_ID: string = "java";
24+
export const ORACLE_VSCODE_EXTENSION_ID = 'oracle.oracle-java';
25+
export const COMMAND_PREFIX = 'jdk';
26+
}
1927

20-
export const ORACLE_JDK_BASE_DOWNLOAD_URL = `https://download.oracle.com/java`;
28+
export namespace jdkDownloaderConstants {
29+
export const JDK_RELEASES_TRACK_URL = `https://www.java.com/releases/releases.json`;
2130

22-
export const ORACLE_JDK_DOWNLOAD_VERSIONS = ['23','21'];
31+
export const ORACLE_JDK_BASE_DOWNLOAD_URL = `https://download.oracle.com/java`;
2332

24-
export const OPEN_JDK_VERSION_DOWNLOAD_LINKS: { [key: string]: string } = {
25-
"23": "https://download.java.net/java/GA/jdk23.0.1/c28985cbf10d4e648e4004050f8781aa/11/GPL/openjdk-23.0.1"
26-
};
33+
export const ORACLE_JDK_DOWNLOAD_VERSIONS = ['23', '21'];
34+
35+
export const OPEN_JDK_VERSION_DOWNLOAD_LINKS: { [key: string]: string } = {
36+
"23": "https://download.java.net/java/GA/jdk23.0.1/c28985cbf10d4e648e4004050f8781aa/11/GPL/openjdk-23.0.1"
37+
};
38+
}
2739

28-
export const ORACLE_VSCODE_EXTENSION_ID = 'oracle.oracle-java';
2940
export const NODE_WINDOWS_LABEL = "Windows_NT";

vscode/src/explorer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import * as vscode from 'vscode';
2020
import { ThemeIcon } from 'vscode';
2121
import { LanguageClient } from 'vscode-languageclient/node';
22-
import { NbLanguageClient } from './extension';
22+
import { NbLanguageClient } from './lsp/nbLanguageClient';
2323
import { NodeChangedParams, NodeInfoNotification, NodeInfoRequest, GetResourceParams, NodeChangeType, NodeChangesParams } from './protocol';
2424
import { l10n } from './localiser';
2525
const doLog : boolean = false;

0 commit comments

Comments
 (0)