Skip to content

Commit bcae80e

Browse files
hopehadfieldrgrunber
authored andcommitted
Add keybinding & setting to perform cleanup actions manually.
Signed-off-by: Hope Hadfield <[email protected]>
1 parent c5aa100 commit bcae80e

File tree

9 files changed

+95
-11
lines changed

9 files changed

+95
-11
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ The following settings are supported:
219219
* `java.completion.chain.enabled`: Enable/disable chain completion support. Defaults to `false`.
220220
* `java.completion.matchCase`: Specify whether to match case for code completion. Defaults to `firstLetter`.
221221
* `java.compile.nullAnalysis.mode`: Specify how to enable the annotation-based null analysis. Supported values are `disabled` (disable the null analysis), `interactive` (asks when null annotation types are detected), `automatic` (automatically enable null analysis when null annotation types are detected). Defaults to `interactive`.
222-
* `java.cleanup.actionsOnSave`: The list of clean ups to be run on the current document when it's saved. Clean ups can automatically fix code style or programming mistakes. [Click here](document/_java.learnMoreAboutCleanUps.md#java-clean-ups) to learn more about what each clean up does.
222+
* `java.cleanup.actionsOnSave`: **Deprecated, please use 'java.cleanup.actions' instead.** The list of clean ups to be run on the current document when it's saved. Clean ups can automatically fix code style or programming mistakes. [Click here](document/_java.learnMoreAboutCleanUps.md#java-clean-ups) to learn more about what each clean up does.
223223
* `java.import.gradle.annotationProcessing.enabled`: Enable/disable the annotation processing on Gradle projects and delegate to JDT APT. Only works for Gradle 5.2 or higher.
224224
* `java.sharedIndexes.enabled`: [Experimental] Specify whether to share indexes between different workspaces. Defaults to `auto` and the shared indexes is automatically enabled in Visual Studio Code - Insiders.
225225
- auto
@@ -241,6 +241,10 @@ The following settings are supported:
241241
* `java.edit.smartSemicolonDetection.enabled`: Defines the `smart semicolon` detection. Defaults to `false`.
242242
* `java.configuration.detectJdksAtStart`: Automatically detect JDKs installed on local machine at startup. If you have specified the same JDK version in `java.configuration.runtimes`, the extension will use that version first. Defaults to `true`.
243243

244+
New in 1.28.0
245+
* `java.cleanup.actions`: The list of clean ups to be run on the current document when it's saved or when the cleanup command is issued. Clean ups can automatically fix code style or programming mistakes. [Click here](document/_java.learnMoreAboutCleanUps.md#java-clean-ups) to learn more about what each clean up does.
246+
* `java.saveActions.cleanup`: Enable/disable cleanup actions on save.
247+
244248
Semantic Highlighting
245249
===============
246250
[Semantic Highlighting](https://github.com/redhat-developer/vscode-java/wiki/Semantic-Highlighting) fixes numerous syntax highlighting issues with the default Java Textmate grammar. However, you might experience a few minor issues, particularly a delay when it kicks in, as it needs to be computed by the Java Language server, when opening a new file or when typing. Semantic highlighting can be disabled for all languages using the `editor.semanticHighlighting.enabled` setting, or for Java only using [language-specific editor settings](https://code.visualstudio.com/docs/getstarted/settings#_languagespecific-editor-settings).

package.json

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,9 +1212,9 @@
12121212
"title": "Code Action",
12131213
"order": 110,
12141214
"properties": {
1215-
"java.cleanup.actionsOnSave": {
1215+
"java.cleanup.actions": {
12161216
"type": "array",
1217-
"markdownDescription": "The list of clean ups to be run on the current document when it's saved. Clean ups can automatically fix code style or programming mistakes. Click [HERE](command:_java.learnMoreAboutCleanUps) to learn more about what each clean up does.",
1217+
"markdownDescription": "The list of clean ups to be run on the current document when it's saved or when the cleanup command is issued. Clean ups can automatically fix code style or programming mistakes. Click [HERE](command:_java.learnMoreAboutCleanUps) to learn more about what each clean up does.",
12181218
"items": {
12191219
"type": "string",
12201220
"enum": [
@@ -1236,6 +1236,34 @@
12361236
"scope": "window",
12371237
"order": 10
12381238
},
1239+
"java.cleanup.actionsOnSave": {
1240+
"type": "array",
1241+
"deprecationMessage": "Deprecated, please use 'java.cleanup.actions' instead.",
1242+
"items": {
1243+
"type": "string",
1244+
"enum": [
1245+
"qualifyMembers",
1246+
"qualifyStaticMembers",
1247+
"addOverride",
1248+
"addDeprecated",
1249+
"stringConcatToTextBlock",
1250+
"invertEquals",
1251+
"addFinalModifier",
1252+
"instanceofPatternMatch",
1253+
"lambdaExpressionFromAnonymousClass",
1254+
"lambdaExpression",
1255+
"switchExpression",
1256+
"tryWithResource"
1257+
]
1258+
},
1259+
"default": [],
1260+
"scope": "window"
1261+
},
1262+
"java.saveActions.cleanup": {
1263+
"type": "boolean",
1264+
"default": true,
1265+
"description": "Enable/disable cleanup actions on save."
1266+
},
12391267
"java.saveActions.organizeImports": {
12401268
"type": "boolean",
12411269
"default": false,
@@ -1560,6 +1588,11 @@
15601588
"command": "java.action.filesExplorerPasteAction",
15611589
"title": "%java.action.filesExplorerPasteAction%",
15621590
"category": "Java"
1591+
},
1592+
{
1593+
"command": "java.action.doCleanup",
1594+
"title": "%java.action.doCleanup%",
1595+
"category": "Java"
15631596
}
15641597
],
15651598
"keybindings": [
@@ -1583,6 +1616,11 @@
15831616
"key": "ctrl+shift+v",
15841617
"mac": "cmd+shift+v",
15851618
"when": "explorerViewletFocus && config.editor.pasteAs.enabled"
1619+
},
1620+
{
1621+
"command": "java.action.doCleanup",
1622+
"key": "ctrl+shift+alt+s",
1623+
"when": "javaLSReady && editorLangId == java"
15861624
}
15871625
],
15881626
"menus": {
@@ -1705,6 +1743,10 @@
17051743
{
17061744
"command": "java.action.filesExplorerPasteAction",
17071745
"when": "false"
1746+
},
1747+
{
1748+
"command": "java.action.doCleanup",
1749+
"when": "false"
17081750
}
17091751
],
17101752
"view/title": [

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
"java.clean.sharedIndexes": "Clean Shared Indexes",
2727
"java.server.restart": "Restart Java Language Server",
2828
"java.edit.smartSemicolonDetection": "Java Smart Semicolon Detection",
29-
"java.action.filesExplorerPasteAction": "Paste clipboard text into a file"
29+
"java.action.filesExplorerPasteAction": "Paste Clipboard Text Into a File",
30+
"java.action.doCleanup": "Performs Cleanup Actions"
3031
}

package.nls.ko.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
"java.action.showSubtypeHierarchy": "Subtype 계층 구조 표시",
2424
"java.action.changeBaseType": "이 유형을 기준으로",
2525
"java.project.createModuleInfo.command": "module-info.java 생성",
26-
"java.action.filesExplorerPasteAction": "클립보드 텍스트를 파일에 붙여넣기"
26+
"java.action.filesExplorerPasteAction": "클립보드 텍스트를 파일에 붙여넣기",
27+
"java.action.doCleanup": "정리 작업을 수행합니다"
2728
}

package.nls.zh-cn.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
"java.action.changeBaseType": "基于此类型",
2525
"java.project.createModuleInfo.command": "创建 module-info.java",
2626
"java.clean.sharedIndexes": "清理共享的索引文件",
27-
"java.action.filesExplorerPasteAction": "将剪贴板文本粘贴到文件中"
27+
"java.action.filesExplorerPasteAction": "将剪贴板文本粘贴到文件中",
28+
"java.action.doCleanup": "执行清理操作"
2829
}

package.nls.zh-tw.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
"java.action.showSubtypeHierarchy": "顯示子類別階層結構",
2424
"java.action.changeBaseType": "以此型別為基礎",
2525
"java.project.createModuleInfo.command": "創建 module-info.java",
26-
"java.action.filesExplorerPasteAction": "將剪貼簿文字貼到文件中"
26+
"java.action.filesExplorerPasteAction": "將剪貼簿文字貼到文件中",
27+
"java.action.doCleanup": "執行清理操作"
2728
}

src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export namespace Commands {
183183
* Organize imports silently.
184184
*/
185185
export const ORGANIZE_IMPORTS_SILENTLY = "java.edit.organizeImports";
186+
export const MANUAL_CLEANUP = "java.action.doCleanup";
186187
/**
187188
* Handle a paste event.
188189
*/

src/protocol.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
import { Command, Range } from 'vscode';
34
import {
45
CodeActionParams,
56
ExecuteCommandParams,
@@ -13,7 +14,6 @@ import {
1314
WorkspaceEdit,
1415
WorkspaceSymbolParams,
1516
} from 'vscode-languageclient';
16-
import { Command, Range } from 'vscode';
1717

1818
/**
1919
* The message type. Copied from vscode protocol
@@ -233,6 +233,10 @@ export namespace OrganizeImportsRequest {
233233
export const type = new RequestType<CodeActionParams, WorkspaceEdit, void>('java/organizeImports');
234234
}
235235

236+
export namespace CleanupRequest {
237+
export const type = new RequestType<TextDocumentIdentifier, WorkspaceEdit, void>('java/cleanup');
238+
}
239+
236240
export interface ImportCandidate {
237241
fullyQualifiedName: string;
238242
id: string;

src/sourceAction.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
11
'use strict';
22

3-
import { commands, window, ExtensionContext, ViewColumn, Uri, Disposable, workspace, TextEditorRevealType } from 'vscode';
3+
import { Disposable, ExtensionContext, TextEditorRevealType, Uri, ViewColumn, commands, window, workspace } from 'vscode';
44
import { CodeActionParams, WorkspaceEdit } from 'vscode-languageclient';
55
import { LanguageClient } from 'vscode-languageclient/node';
66
import { Commands } from './commands';
7-
import { ListOverridableMethodsRequest, AddOverridableMethodsRequest, CheckHashCodeEqualsStatusRequest, GenerateHashCodeEqualsRequest,
8-
OrganizeImportsRequest, ImportCandidate, ImportSelection, GenerateToStringRequest, CheckToStringStatusRequest, VariableBinding, GenerateAccessorsRequest, CheckConstructorStatusRequest, GenerateConstructorsRequest, CheckDelegateMethodsStatusRequest, GenerateDelegateMethodsRequest, AccessorKind, AccessorCodeActionRequest, AccessorCodeActionParams } from './protocol';
7+
import {
8+
AccessorCodeActionParams,
9+
AccessorCodeActionRequest,
10+
AccessorKind,
11+
AddOverridableMethodsRequest,
12+
CheckConstructorStatusRequest,
13+
CheckDelegateMethodsStatusRequest,
14+
CheckHashCodeEqualsStatusRequest,
15+
CheckToStringStatusRequest,
16+
GenerateAccessorsRequest,
17+
GenerateConstructorsRequest,
18+
GenerateDelegateMethodsRequest,
19+
GenerateHashCodeEqualsRequest,
20+
GenerateToStringRequest,
21+
ImportCandidate, ImportSelection,
22+
ListOverridableMethodsRequest,
23+
CleanupRequest,
24+
OrganizeImportsRequest,
25+
VariableBinding
26+
} from './protocol';
927
import { applyWorkspaceEdit } from './standardLanguageClient';
28+
import { getActiveLanguageClient } from './extension';
1029

1130
export function registerCommands(languageClient: LanguageClient, context: ExtensionContext) {
1231
registerOverrideMethodsCommand(languageClient, context);
1332
registerHashCodeEqualsCommand(languageClient, context);
1433
registerOrganizeImportsCommand(languageClient, context);
34+
registerCleanupCommand(languageClient, context);
1535
registerChooseImportCommand(context);
1636
registerGenerateToStringCommand(languageClient, context);
1737
registerGenerateAccessorsCommand(languageClient, context);
@@ -67,6 +87,15 @@ function registerOverrideMethodsCommand(languageClient: LanguageClient, context:
6787
}));
6888
}
6989

90+
function registerCleanupCommand(languageClient: LanguageClient, context: ExtensionContext): void {
91+
// Only active when editorLangId == java
92+
context.subscriptions.push(commands.registerCommand(Commands.MANUAL_CLEANUP, async () => {
93+
const languageClient: LanguageClient | undefined = await getActiveLanguageClient();
94+
const workspaceEdit = await languageClient.sendRequest(CleanupRequest.type, languageClient.code2ProtocolConverter.asTextDocumentIdentifier(window.activeTextEditor.document));
95+
await applyWorkspaceEdit(workspaceEdit, languageClient);
96+
}));
97+
}
98+
7099
function registerHashCodeEqualsCommand(languageClient: LanguageClient, context: ExtensionContext): void {
71100
context.subscriptions.push(commands.registerCommand(Commands.HASHCODE_EQUALS_PROMPT, async (params: CodeActionParams) => {
72101
const result = await languageClient.sendRequest(CheckHashCodeEqualsStatusRequest.type, params);

0 commit comments

Comments
 (0)