Skip to content

Commit 7b0e884

Browse files
snjezafbricon
authored andcommitted
Add new command to clean the server workspace
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
1 parent 95fb8d8 commit 7b0e884

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ The following commands are available:
5959
- `Java:Force Java compilation` (`Shift+Alt+B`): manually triggers compilation of the workspace.
6060
- `Java:Organize imports` (`Shift+Alt+O`): Organize imports in the currently opened Java file.
6161
- `Java:Open Java formatter settings`: Open the Eclipse formatter settings. Creates a new settings file if none exists.
62+
- `Java:Clean the Java language server workspace`: Clean the Java language server workspace.
6263

6364
Supported VS Code settings
6465
==========================
@@ -90,6 +91,9 @@ The following settings are supported:
9091
* `java.completion.guessMethodArguments` : When set to true, method arguments are guessed when a method is selected from as list of code assist proposals.
9192
* `java.completion.enabled` : Enable/disable code completion support.
9293

94+
*New in 0.33.0:*
95+
* `java.clean.workspace` : Clean the Java language server workspace.
96+
9397

9498
Troubleshooting
9599
===============

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@
283283
"command": "java.open.formatter.settings",
284284
"title": "Open Java formatter settings",
285285
"category": "Java"
286+
},
287+
{
288+
"command": "java.clean.workspace",
289+
"title": "Clean the Java language server workspace",
290+
"category": "Java"
286291
}
287292
],
288293
"keybindings": [

src/commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,8 @@ export namespace Commands {
8282
* Open Java formatter settings
8383
*/
8484
export const OPEN_FORMATTER = 'java.open.formatter.settings';
85+
/**
86+
* Clean the Java language server workspace
87+
*/
88+
export const CLEAN_WORKSPACE = 'java.clean.workspace';
8589
}

src/extension.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import * as net from 'net';
1515

1616
let oldConfig;
1717
let lastStatus;
18-
let languageClient : LanguageClient;
18+
let languageClient: LanguageClient;
19+
const cleanWorkspaceFileName = '.cleanWorkspace';
1920

2021
export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
2122

@@ -264,13 +265,25 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
264265
};
265266
workspace.registerTextDocumentContentProvider('jdt', provider);
266267
});
268+
269+
let cleanWorkspaceExists = fs.existsSync( path.join(workspacePath, cleanWorkspaceFileName));
270+
if (cleanWorkspaceExists) {
271+
try {
272+
deleteDirectory(workspacePath);
273+
} catch (error) {
274+
window.showErrorMessage('Failed to delete ' + workspacePath + ': ' + error);
275+
}
276+
}
277+
267278
languageClient.start();
268279
// Register commands here to make it available even when the language client fails
269280
commands.registerCommand(Commands.OPEN_SERVER_LOG, () => openServerLogFile(workspacePath));
270281

271282
let extensionPath = context.extensionPath;
272283
commands.registerCommand(Commands.OPEN_FORMATTER, async () => openFormatter(extensionPath));
273284

285+
commands.registerCommand(Commands.CLEAN_WORKSPACE, () => cleanWorkspace(workspacePath));
286+
274287
context.subscriptions.push(onConfigurationChange());
275288
toggleItem(window.activeTextEditor, item);
276289
});
@@ -431,6 +444,31 @@ function getJavaConfiguration(): WorkspaceConfiguration {
431444
return workspace.getConfiguration('java');
432445
}
433446

447+
async function cleanWorkspace(workspacePath) {
448+
const doIt = 'Restart and delete';
449+
window.showWarningMessage('Are you sure you want to clean the Java language server workspace?', 'Cancel', doIt).then(selection => {
450+
if (selection === doIt) {
451+
const file = path.join(workspacePath, cleanWorkspaceFileName);
452+
fs.closeSync(fs.openSync(file, 'w'));
453+
commands.executeCommand(Commands.RELOAD_WINDOW);
454+
}
455+
});
456+
}
457+
458+
function deleteDirectory(dir) {
459+
if (fs.existsSync(dir)) {
460+
fs.readdirSync(dir).forEach(function (child) {
461+
let entry = path.join(dir, child);
462+
if (fs.lstatSync(entry).isDirectory()) {
463+
deleteDirectory(entry);
464+
} else {
465+
fs.unlinkSync(entry);
466+
}
467+
});
468+
fs.rmdirSync(dir);
469+
}
470+
}
471+
434472
function openServerLogFile(workspacePath): Thenable<boolean> {
435473
let serverLogFile = path.join(workspacePath, '.metadata', '.log');
436474
if (!serverLogFile) {

test/extension.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ suite('Java Language Extension', () => {
3636
Commands.OPEN_SERVER_LOG,
3737
Commands.COMPILE_WORKSPACE,
3838
Commands.EDIT_ORGANIZE_IMPORTS,
39-
Commands.OPEN_FORMATTER
39+
Commands.OPEN_FORMATTER,
40+
Commands.CLEAN_WORKSPACE
4041
];
4142
let foundJavaCommands = commands.filter(function(value){
4243
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

0 commit comments

Comments
 (0)