Skip to content

Commit 7207f40

Browse files
testforstephenyaohaizh
authored andcommitted
Add command to add/remove/list the project's source path (#724)
Signed-off-by: Jinbo Wang <[email protected]>
1 parent 5ceb732 commit 7207f40

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,21 @@
298298
"command": "java.project.updateSourceAttachment",
299299
"title": "Attach Source",
300300
"category": "Java"
301+
},
302+
{
303+
"command": "java.project.addToSourcePath",
304+
"title": "Add Folder to Java Source Path",
305+
"category": "Java"
306+
},
307+
{
308+
"command": "java.project.removeFromSourcePath",
309+
"title": "Remove Folder from Java Source Path",
310+
"category": "Java"
311+
},
312+
{
313+
"command": "java.project.listSourcePaths",
314+
"title": "List all Java source paths",
315+
"category": "Java"
301316
}
302317
],
303318
"keybindings": [
@@ -317,6 +332,16 @@
317332
"command": "java.projectConfiguration.update",
318333
"when": "resourceFilename =~ /(.*\\.gradle)|(pom.xml)$/",
319334
"group": "1_javaactions"
335+
},
336+
{
337+
"when": "explorerResourceIsFolder&&javaLSReady",
338+
"command": "java.project.addToSourcePath",
339+
"group": "1_javaactions@1"
340+
},
341+
{
342+
"when": "explorerResourceIsFolder&&javaLSReady",
343+
"command": "java.project.removeFromSourcePath",
344+
"group": "1_javaactions@2"
320345
}
321346
],
322347
"editor/context": [
@@ -335,6 +360,14 @@
335360
{
336361
"command": "java.project.updateSourceAttachment",
337362
"when": "false"
363+
},
364+
{
365+
"command": "java.project.addToSourcePath",
366+
"when": "false"
367+
},
368+
{
369+
"command": "java.project.removeFromSourcePath",
370+
"when": "false"
338371
}
339372
]
340373
}

src/buildpath.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
import { window, commands, Uri } from 'vscode';
4+
import { Commands } from './commands';
5+
6+
interface Result {
7+
status: boolean;
8+
message: string;
9+
}
10+
11+
interface SourcePath {
12+
path: string;
13+
displayPath: string;
14+
projectName: string;
15+
projectType: string;
16+
}
17+
18+
interface ListCommandResult extends Result {
19+
data?: SourcePath[];
20+
}
21+
22+
export function registerCommands() {
23+
commands.registerCommand(Commands.ADD_TO_SOURCEPATH, async (uri: Uri) => {
24+
const result = await <any>commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.ADD_TO_SOURCEPATH, uri.toString());
25+
if (result.status) {
26+
window.showInformationMessage(result.message ? result.message : "Successfully added the folder to the source path.");
27+
} else {
28+
window.showErrorMessage(result.message);
29+
}
30+
});
31+
32+
commands.registerCommand(Commands.REMOVE_FROM_SOURCEPATH, async (uri: Uri) => {
33+
const result = await <any>commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.REMOVE_FROM_SOURCEPATH, uri.toString());
34+
if (result.status) {
35+
window.showInformationMessage(result.message ? result.message : "Successfully removed the folder from the source path.");
36+
} else {
37+
window.showErrorMessage(result.message);
38+
}
39+
});
40+
41+
commands.registerCommand(Commands.LIST_SOURCEPATHS, async() => {
42+
const result: ListCommandResult = await commands.executeCommand<ListCommandResult>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.LIST_SOURCEPATHS);
43+
if (result.status) {
44+
if (!result.data || !result.data.length) {
45+
window.showInformationMessage("No Java source directories found in the workspace, please use the command 'Add Folder to Java Source Path' first.");
46+
} else {
47+
window.showQuickPick(result.data.map(sourcePath => {
48+
return {
49+
label: sourcePath.displayPath,
50+
detail: `$(file-directory) ${sourcePath.projectType} Project: ${sourcePath.projectName}`,
51+
};
52+
}), { placeHolder: "All Java source directories recognized by the workspace."});
53+
}
54+
} else {
55+
window.showErrorMessage(result.message);
56+
}
57+
});
58+
}

src/commands.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,16 @@ export namespace Commands {
9090
* Resolve the source attachment information for the selected class file
9191
*/
9292
export const RESOLVE_SOURCE_ATTACHMENT = 'java.project.resolveSourceAttachment';
93+
/**
94+
* Mark the folder as the source root of the closest project.
95+
*/
96+
export const ADD_TO_SOURCEPATH = 'java.project.addToSourcePath';
97+
/**
98+
* Unmark the folder as the source root of the project.
99+
*/
100+
export const REMOVE_FROM_SOURCEPATH = 'java.project.removeFromSourcePath';
101+
/**
102+
* List all recognized source roots in the workspace.
103+
*/
104+
export const LIST_SOURCEPATHS = 'java.project.listSourcePaths';
93105
}

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Commands } from './commands';
1212
import { StatusNotification, ClassFileContentsRequest, ProjectConfigurationUpdateRequest, MessageType, ActionableNotification, FeatureStatus, ActionableMessage, CompileWorkspaceRequest, CompileWorkspaceStatus, ProgressReportNotification, ExecuteClientCommandRequest, SendNotificationRequest,
1313
SourceAttachmentRequest, SourceAttachmentResult, SourceAttachmentAttribute } from './protocol';
1414
import { ExtensionAPI } from './extension.api';
15+
import * as buildpath from './buildpath';
1516
import * as net from 'net';
1617

1718
let oldConfig;
@@ -113,6 +114,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
113114
item.text = '$(thumbsup)';
114115
p.report({ message: 'Finished' });
115116
lastStatus = item.text;
117+
commands.executeCommand('setContext', 'javaLSReady', true);
116118
resolve({
117119
apiVersion: '0.1',
118120
javaRequirement: requirements,
@@ -284,6 +286,8 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
284286
}
285287
});
286288

289+
buildpath.registerCommands();
290+
287291
window.onDidChangeActiveTextEditor((editor) => {
288292
toggleItem(editor, item);
289293
});

test/extension.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ suite('Java Language Extension', () => {
3737
Commands.COMPILE_WORKSPACE,
3838
Commands.OPEN_FORMATTER,
3939
Commands.CLEAN_WORKSPACE,
40-
Commands.UPDATE_SOURCE_ATTACHMENT
40+
Commands.UPDATE_SOURCE_ATTACHMENT,
41+
Commands.ADD_TO_SOURCEPATH,
42+
Commands.REMOVE_FROM_SOURCEPATH,
43+
Commands.LIST_SOURCEPATHS,
4144
];
4245
let foundJavaCommands = commands.filter(function(value){
4346
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

0 commit comments

Comments
 (0)