Skip to content

Commit af1466b

Browse files
committed
Added commands module
1 parent 25bdcce commit af1466b

File tree

15 files changed

+726
-637
lines changed

15 files changed

+726
-637
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 { LOGGER } from "../extension";
17+
import { l10n } from "../localiser";
18+
import { extCommands, nbCommands } from "./commands";
19+
import { ICommand } from "./types";
20+
import { wrapCommandWithProgress, wrapProjectActionWithProgress } from "./utils";
21+
22+
const compileWorkspaceCHandler = () => {
23+
wrapCommandWithProgress(nbCommands.buildWorkspace, l10n.value('jdk.extension.command.progress.compilingWorkSpace'), LOGGER.getOutputChannel(), true);
24+
}
25+
const cleanWorkspaceHandler = () => {
26+
wrapCommandWithProgress(nbCommands.cleanWorkspace,l10n.value('jdk.extension.command.progress.cleaningWorkSpace'), LOGGER.getOutputChannel(), true)
27+
}
28+
29+
const compileProjectHandler = (args: any) => {
30+
wrapProjectActionWithProgress('build', undefined, l10n.value('jdk.extension.command.progress.compilingProject'), LOGGER.getOutputChannel(), true, args);
31+
}
32+
33+
const cleanProjectHandler = (args: any) => {
34+
wrapProjectActionWithProgress('clean', undefined, l10n.value('jdk.extension.command.progress.cleaningProject'), LOGGER.getOutputChannel(), true, args);
35+
}
36+
37+
38+
export const registerBuildOperationCommands: ICommand[] = [
39+
{
40+
command: extCommands.compileWorkspace,
41+
handler: compileWorkspaceCHandler
42+
}, {
43+
command: extCommands.cleanWorkspace,
44+
handler: cleanWorkspaceHandler
45+
},{
46+
command: extCommands.compileProject,
47+
handler: compileProjectHandler
48+
},{
49+
command: extCommands.cleanProject,
50+
handler: cleanProjectHandler
51+
}
52+
];

vscode/src/commands/cache.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 { commands, window } from "vscode";
17+
import { globalVars } from "../extension";
18+
import { builtInCommands, extCommands } from "./commands";
19+
import { ICommand } from "./types";
20+
import { l10n } from "../localiser";
21+
import * as fs from 'fs';
22+
import * as path from 'path';
23+
24+
const deleteCache = async () => {
25+
// TODO: Change workspace path to userdir path
26+
const storagePath = globalVars.extensionInfo.getWorkspaceStorage()?.fsPath;
27+
if (!storagePath) {
28+
window.showErrorMessage(l10n.value("jdk.extenstion.cache.error_msg.cannotFindWrkSpacePath"));
29+
return;
30+
}
31+
32+
const userDir = path.join(storagePath, "userdir");
33+
if (userDir && fs.existsSync(userDir)) {
34+
const yes = l10n.value("jdk.extension.cache.label.confirmation.yes")
35+
const cancel = l10n.value("jdk.extension.cache.label.confirmation.cancel")
36+
const confirmation = await window.showInformationMessage('Are you sure you want to delete cache for this workspace and reload the window ?',
37+
yes, cancel);
38+
if (confirmation === yes) {
39+
const reloadWindowActionLabel = l10n.value("jdk.extension.cache.label.reloadWindow");
40+
try {
41+
await globalVars.clientPromise.stopClient();
42+
globalVars.deactivated = true;
43+
await globalVars.nbProcessManager?.killProcess(false);
44+
await fs.promises.rmdir(userDir, { recursive: true });
45+
await window.showInformationMessage(l10n.value("jdk.extenstion.message.cacheDeleted"), reloadWindowActionLabel);
46+
} catch (err) {
47+
await window.showErrorMessage(l10n.value("jdk.extenstion.error_msg.cacheDeletionError"), reloadWindowActionLabel);
48+
} finally {
49+
commands.executeCommand(builtInCommands.reloadWindow);
50+
}
51+
}
52+
} else {
53+
window.showErrorMessage(l10n.value("jdk.extension.cache.message.noUserDir"));
54+
}
55+
}
56+
57+
export const registerCacheCommands: ICommand[] = [{
58+
command: extCommands.deleteCache,
59+
handler: deleteCache
60+
}];

vscode/src/commands/commands.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
19+
export const extCommands = {
20+
configureRunSettings: appendPrefixToCommand('workspace.configureRunSettings'),
21+
newFromTemplate : appendPrefixToCommand('workspace.new'),
22+
newProject: appendPrefixToCommand('workspace.newproject'),
23+
openTest: appendPrefixToCommand('open.test'),
24+
deleteCache: appendPrefixToCommand('delete.cache'),
25+
downloadJdk: appendPrefixToCommand('download.jdk'),
26+
compileWorkspace: appendPrefixToCommand('workspace.compile'),
27+
cleanWorkspace: appendPrefixToCommand('workspace.clean'),
28+
compileProject: appendPrefixToCommand('project.compile'),
29+
cleanProject: appendPrefixToCommand('project.clean'),
30+
openType: appendPrefixToCommand('open.type'),
31+
goToSuperImpl: appendPrefixToCommand('java.goto.super.implementation'),
32+
renameElement: appendPrefixToCommand('rename.element.at'),
33+
surroundWith: appendPrefixToCommand('surround.with'),
34+
generateCode: appendPrefixToCommand('generate.code'),
35+
runTest: appendPrefixToCommand('run.test'),
36+
debugTest: appendPrefixToCommand('debug.test'),
37+
runSingle: appendPrefixToCommand('run.single'),
38+
debugSingle: appendPrefixToCommand('debug.single'),
39+
projectRun: appendPrefixToCommand('project.run'),
40+
projectDebug: appendPrefixToCommand('project.debug'),
41+
projectTest: appendPrefixToCommand('project.test'),
42+
packageTest: appendPrefixToCommand('package.test'),
43+
openStackTrace: appendPrefixToCommand('open.stacktrace'),
44+
workspaceSymbols: appendPrefixToCommand('workspace.symbols'),
45+
abstractMethodsComplete: appendPrefixToCommand('java.complete.abstract.methods'),
46+
startupCondition: appendPrefixToCommand('startup.condition'),
47+
nbEventListener: appendPrefixToCommand('addEventListener'),
48+
editNodeProps: appendPrefixToCommand('node.properties.edit'),
49+
selectEditorProjs: appendPrefixToCommand('select.editor.projects'),
50+
attachDebugger: appendPrefixToCommand("java.attachDebugger.connector"),
51+
startDebug: 'workbench.action.debug.start',
52+
}
53+
54+
export const builtInCommands = {
55+
setCustomContext: 'setContext',
56+
openFolder: 'vscode.openFolder',
57+
reloadWindow: 'workbench.action.reloadWindow',
58+
focusActiveEditorGroup: 'workbench.action.focusActiveEditorGroup',
59+
goToEditorLocations: 'editor.action.goToLocations',
60+
renameSymbol: 'editor.action.rename',
61+
quickAccess: 'workbench.action.quickOpen',
62+
openSettings: 'workbench.action.openSettings'
63+
}
64+
65+
export const nbCommands = {
66+
newFromTemplate: appendPrefixToCommand('new.from.template'),
67+
newProject: appendPrefixToCommand('new.project'),
68+
goToTest: appendPrefixToCommand('go.to.test'),
69+
quickOpen: appendPrefixToCommand('quick.open'),
70+
superImpl: appendPrefixToCommand('java.super.implementation'),
71+
resolveStackLocation: appendPrefixToCommand('resolve.stacktrace.location'),
72+
implementAbstractMethods: appendPrefixToCommand('java.implement.all.abstract.methods'),
73+
archiveFileContent: appendPrefixToCommand('get.archive.file.content'),
74+
htmlProcessCmd: appendPrefixToCommand('htmlui.process.command'),
75+
projectConfigurations: appendPrefixToCommand('project.configurations'),
76+
debuggerConfigurations: appendPrefixToCommand('java.attachDebugger.configurations'),
77+
runProjectAction: appendPrefixToCommand('project.run.action'),
78+
buildWorkspace: appendPrefixToCommand('build.workspace'),
79+
cleanWorkspace: appendPrefixToCommand('clean.workspace')
80+
}

vscode/src/commands/create.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 { workspace, commands, Uri, window } from "vscode";
17+
import { LanguageClient } from "vscode-languageclient/node";
18+
import { nbCommands, builtInCommands, extCommands } from "./commands";
19+
import { l10n } from "../localiser";
20+
import * as os from 'os';
21+
import * as fs from 'fs';
22+
import { ICommand } from "./types";
23+
import { globalVars } from "../extension";
24+
import { getContextUri, isNbCommandRegistered } from "./utils";
25+
import { isString } from "../typesUtil";
26+
27+
const newFromTemplate = async (ctx: any, template: any) => {
28+
const client: LanguageClient = await globalVars.clientPromise.client;
29+
if (await isNbCommandRegistered(nbCommands.newFromTemplate)) {
30+
const workspaces = workspace.workspaceFolders;
31+
32+
if (!workspaces) {
33+
const userHomeDir = os.homedir();
34+
const folderPath = await window.showInputBox({
35+
prompt: l10n.value('jdk.workspace.new.prompt'),
36+
value: `${userHomeDir}`
37+
});
38+
if (!folderPath?.trim()) return;
39+
40+
if (!fs.existsSync(folderPath)) {
41+
await fs.promises.mkdir(folderPath);
42+
}
43+
const folderPathUri = Uri.file(folderPath);
44+
await commands.executeCommand(nbCommands.newFromTemplate, folderPathUri.toString());
45+
await commands.executeCommand(builtInCommands.openFolder, folderPathUri);
46+
47+
return;
48+
}
49+
50+
// first give the template (if present), then the context, and then the open-file hint in the case the context is not specific enough
51+
const params = [];
52+
if (isString(template)) {
53+
params.push(template);
54+
}
55+
params.push(getContextUri(ctx)?.toString(), window.activeTextEditor?.document?.uri?.toString());
56+
const res = await commands.executeCommand(nbCommands.newFromTemplate, ...params);
57+
58+
if (isString(res)) {
59+
let newFile = Uri.parse(res as string);
60+
await window.showTextDocument(newFile, { preview: false });
61+
} else if (Array.isArray(res)) {
62+
for (let r of res) {
63+
if (isString(r)) {
64+
let newFile = Uri.parse(r as string);
65+
await window.showTextDocument(newFile, { preview: false });
66+
}
67+
}
68+
}
69+
} else {
70+
throw l10n.value("jdk.extenstion.error_msg.doesntSupportNewTeamplate", { client });
71+
}
72+
}
73+
74+
const newProject = async (ctx: any) => {
75+
const client: LanguageClient = await globalVars.clientPromise.client;
76+
if (await isNbCommandRegistered(nbCommands.newProject)) {
77+
const res = await commands.executeCommand(nbCommands.newProject, getContextUri(ctx)?.toString());
78+
if (isString(res)) {
79+
let newProject = Uri.parse(res as string);
80+
81+
const OPEN_IN_NEW_WINDOW = l10n.value("jdk.extension.label.openInNewWindow");
82+
const ADD_TO_CURRENT_WORKSPACE = l10n.value("jdk.extension.label.addToWorkSpace");
83+
84+
const value = await window.showInformationMessage(l10n.value("jdk.extension.message.newProjectCreated"),
85+
OPEN_IN_NEW_WINDOW,
86+
ADD_TO_CURRENT_WORKSPACE);
87+
88+
if (value === OPEN_IN_NEW_WINDOW) {
89+
await commands.executeCommand(builtInCommands.openFolder, newProject, true);
90+
} else if (value === ADD_TO_CURRENT_WORKSPACE) {
91+
workspace.updateWorkspaceFolders(workspace.workspaceFolders ? workspace.workspaceFolders.length : 0, undefined, { uri: newProject });
92+
}
93+
}
94+
} else {
95+
throw l10n.value("jdk.extenstion.error_msg.doesntSupportNewProject", { client });
96+
}
97+
};
98+
99+
100+
export const registerCreateCommands: ICommand[] = [
101+
{
102+
command: extCommands.newFromTemplate,
103+
handler: newFromTemplate
104+
}, {
105+
command: extCommands.newProject,
106+
handler: newProject
107+
}
108+
];

0 commit comments

Comments
 (0)