Skip to content

Commit

Permalink
Add keybinding & setting to perform cleanup actions manually.
Browse files Browse the repository at this point in the history
Signed-off-by: Hope Hadfield <[email protected]>
  • Loading branch information
hopehadfield authored and rgrunber committed Feb 3, 2024
1 parent c5aa100 commit bcae80e
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ The following settings are supported:
* `java.completion.chain.enabled`: Enable/disable chain completion support. Defaults to `false`.
* `java.completion.matchCase`: Specify whether to match case for code completion. Defaults to `firstLetter`.
* `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`.
* `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.
* `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.
* `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.
* `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.
- auto
Expand All @@ -241,6 +241,10 @@ The following settings are supported:
* `java.edit.smartSemicolonDetection.enabled`: Defines the `smart semicolon` detection. Defaults to `false`.
* `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`.

New in 1.28.0
* `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.
* `java.saveActions.cleanup`: Enable/disable cleanup actions on save.

Semantic Highlighting
===============
[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).
Expand Down
46 changes: 44 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1212,9 +1212,9 @@
"title": "Code Action",
"order": 110,
"properties": {
"java.cleanup.actionsOnSave": {
"java.cleanup.actions": {
"type": "array",
"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.",
"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.",
"items": {
"type": "string",
"enum": [
Expand All @@ -1236,6 +1236,34 @@
"scope": "window",
"order": 10
},
"java.cleanup.actionsOnSave": {
"type": "array",
"deprecationMessage": "Deprecated, please use 'java.cleanup.actions' instead.",
"items": {
"type": "string",
"enum": [
"qualifyMembers",
"qualifyStaticMembers",
"addOverride",
"addDeprecated",
"stringConcatToTextBlock",
"invertEquals",
"addFinalModifier",
"instanceofPatternMatch",
"lambdaExpressionFromAnonymousClass",
"lambdaExpression",
"switchExpression",
"tryWithResource"
]
},
"default": [],
"scope": "window"
},
"java.saveActions.cleanup": {
"type": "boolean",
"default": true,
"description": "Enable/disable cleanup actions on save."
},
"java.saveActions.organizeImports": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -1560,6 +1588,11 @@
"command": "java.action.filesExplorerPasteAction",
"title": "%java.action.filesExplorerPasteAction%",
"category": "Java"
},
{
"command": "java.action.doCleanup",
"title": "%java.action.doCleanup%",
"category": "Java"
}
],
"keybindings": [
Expand All @@ -1583,6 +1616,11 @@
"key": "ctrl+shift+v",
"mac": "cmd+shift+v",
"when": "explorerViewletFocus && config.editor.pasteAs.enabled"
},
{
"command": "java.action.doCleanup",
"key": "ctrl+shift+alt+s",
"when": "javaLSReady && editorLangId == java"
}
],
"menus": {
Expand Down Expand Up @@ -1705,6 +1743,10 @@
{
"command": "java.action.filesExplorerPasteAction",
"when": "false"
},
{
"command": "java.action.doCleanup",
"when": "false"
}
],
"view/title": [
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"java.clean.sharedIndexes": "Clean Shared Indexes",
"java.server.restart": "Restart Java Language Server",
"java.edit.smartSemicolonDetection": "Java Smart Semicolon Detection",
"java.action.filesExplorerPasteAction": "Paste clipboard text into a file"
"java.action.filesExplorerPasteAction": "Paste Clipboard Text Into a File",
"java.action.doCleanup": "Performs Cleanup Actions"
}
3 changes: 2 additions & 1 deletion package.nls.ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"java.action.showSubtypeHierarchy": "Subtype 계층 구조 표시",
"java.action.changeBaseType": "이 유형을 기준으로",
"java.project.createModuleInfo.command": "module-info.java 생성",
"java.action.filesExplorerPasteAction": "클립보드 텍스트를 파일에 붙여넣기"
"java.action.filesExplorerPasteAction": "클립보드 텍스트를 파일에 붙여넣기",
"java.action.doCleanup": "정리 작업을 수행합니다"
}
3 changes: 2 additions & 1 deletion package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
"java.action.changeBaseType": "基于此类型",
"java.project.createModuleInfo.command": "创建 module-info.java",
"java.clean.sharedIndexes": "清理共享的索引文件",
"java.action.filesExplorerPasteAction": "将剪贴板文本粘贴到文件中"
"java.action.filesExplorerPasteAction": "将剪贴板文本粘贴到文件中",
"java.action.doCleanup": "执行清理操作"
}
3 changes: 2 additions & 1 deletion package.nls.zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"java.action.showSubtypeHierarchy": "顯示子類別階層結構",
"java.action.changeBaseType": "以此型別為基礎",
"java.project.createModuleInfo.command": "創建 module-info.java",
"java.action.filesExplorerPasteAction": "將剪貼簿文字貼到文件中"
"java.action.filesExplorerPasteAction": "將剪貼簿文字貼到文件中",
"java.action.doCleanup": "執行清理操作"
}
1 change: 1 addition & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export namespace Commands {
* Organize imports silently.
*/
export const ORGANIZE_IMPORTS_SILENTLY = "java.edit.organizeImports";
export const MANUAL_CLEANUP = "java.action.doCleanup";
/**
* Handle a paste event.
*/
Expand Down
6 changes: 5 additions & 1 deletion src/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import { Command, Range } from 'vscode';
import {
CodeActionParams,
ExecuteCommandParams,
Expand All @@ -13,7 +14,6 @@ import {
WorkspaceEdit,
WorkspaceSymbolParams,
} from 'vscode-languageclient';
import { Command, Range } from 'vscode';

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

export namespace CleanupRequest {
export const type = new RequestType<TextDocumentIdentifier, WorkspaceEdit, void>('java/cleanup');
}

export interface ImportCandidate {
fullyQualifiedName: string;
id: string;
Expand Down
35 changes: 32 additions & 3 deletions src/sourceAction.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
'use strict';

import { commands, window, ExtensionContext, ViewColumn, Uri, Disposable, workspace, TextEditorRevealType } from 'vscode';
import { Disposable, ExtensionContext, TextEditorRevealType, Uri, ViewColumn, commands, window, workspace } from 'vscode';
import { CodeActionParams, WorkspaceEdit } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { Commands } from './commands';
import { ListOverridableMethodsRequest, AddOverridableMethodsRequest, CheckHashCodeEqualsStatusRequest, GenerateHashCodeEqualsRequest,
OrganizeImportsRequest, ImportCandidate, ImportSelection, GenerateToStringRequest, CheckToStringStatusRequest, VariableBinding, GenerateAccessorsRequest, CheckConstructorStatusRequest, GenerateConstructorsRequest, CheckDelegateMethodsStatusRequest, GenerateDelegateMethodsRequest, AccessorKind, AccessorCodeActionRequest, AccessorCodeActionParams } from './protocol';
import {
AccessorCodeActionParams,
AccessorCodeActionRequest,
AccessorKind,
AddOverridableMethodsRequest,
CheckConstructorStatusRequest,
CheckDelegateMethodsStatusRequest,
CheckHashCodeEqualsStatusRequest,
CheckToStringStatusRequest,
GenerateAccessorsRequest,
GenerateConstructorsRequest,
GenerateDelegateMethodsRequest,
GenerateHashCodeEqualsRequest,
GenerateToStringRequest,
ImportCandidate, ImportSelection,
ListOverridableMethodsRequest,
CleanupRequest,
OrganizeImportsRequest,
VariableBinding
} from './protocol';
import { applyWorkspaceEdit } from './standardLanguageClient';
import { getActiveLanguageClient } from './extension';

export function registerCommands(languageClient: LanguageClient, context: ExtensionContext) {
registerOverrideMethodsCommand(languageClient, context);
registerHashCodeEqualsCommand(languageClient, context);
registerOrganizeImportsCommand(languageClient, context);
registerCleanupCommand(languageClient, context);
registerChooseImportCommand(context);
registerGenerateToStringCommand(languageClient, context);
registerGenerateAccessorsCommand(languageClient, context);
Expand Down Expand Up @@ -67,6 +87,15 @@ function registerOverrideMethodsCommand(languageClient: LanguageClient, context:
}));
}

function registerCleanupCommand(languageClient: LanguageClient, context: ExtensionContext): void {
// Only active when editorLangId == java
context.subscriptions.push(commands.registerCommand(Commands.MANUAL_CLEANUP, async () => {
const languageClient: LanguageClient | undefined = await getActiveLanguageClient();
const workspaceEdit = await languageClient.sendRequest(CleanupRequest.type, languageClient.code2ProtocolConverter.asTextDocumentIdentifier(window.activeTextEditor.document));
await applyWorkspaceEdit(workspaceEdit, languageClient);
}));
}

function registerHashCodeEqualsCommand(languageClient: LanguageClient, context: ExtensionContext): void {
context.subscriptions.push(commands.registerCommand(Commands.HASHCODE_EQUALS_PROMPT, async (params: CodeActionParams) => {
const result = await languageClient.sendRequest(CheckHashCodeEqualsStatusRequest.type, params);
Expand Down

0 comments on commit bcae80e

Please sign in to comment.