From 6db6503307e369f212c3c2562689d5867de8fed7 Mon Sep 17 00:00:00 2001 From: Shi Chen Date: Mon, 20 Feb 2023 14:20:13 +0800 Subject: [PATCH] rename reference Signed-off-by: Shi Chen --- package.json | 11 +++++++++++ src/commands.ts | 4 ++++ src/standardLanguageClient.ts | 19 +++++++++++++++++-- src/utils.ts | 17 +++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5b005cab55..501272e642 100644 --- a/package.json +++ b/package.json @@ -1080,6 +1080,17 @@ "type": "boolean", "markdownDescription": "Specify whether to replace all the occurrences of the subtype with the new extracted interface.", "default": true + }, + "java.refactoring.rename.references": { + "type": "string", + "enum": [ + "auto", + "on", + "off" + ], + "default": "auto", + "markdownDescription": "Specify whether to provide diagnostics and corresponding quick fix when a symbol name is manually changed without using Rename refactoring. When set to `auto`, the diagnotics and quick fix will be provided if `java.autobuild.enabled` is set to `false`.", + "scope": "window" } } }, diff --git a/src/commands.ts b/src/commands.ts index 371448fee9..808d96b4ff 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -218,6 +218,10 @@ export namespace Commands { * Rename Command. */ export const RENAME_COMMAND = 'java.action.rename'; + /** + * Rename references Command. + */ + export const RENAME_REFERENCES_COMMAND = 'java.action.renameReferences'; /** * Navigate To Super Method Command. */ diff --git a/src/standardLanguageClient.ts b/src/standardLanguageClient.ts index 70828f0084..3d68cfdc5b 100644 --- a/src/standardLanguageClient.ts +++ b/src/standardLanguageClient.ts @@ -4,8 +4,8 @@ import * as fse from 'fs-extra'; import { findRuntimes } from "jdk-utils"; import * as net from 'net'; import * as path from 'path'; -import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, Uri, ViewColumn, window, workspace } from "vscode"; -import { ConfigurationParams, ConfigurationRequest, LanguageClientOptions, Location as LSLocation, MessageType, Position as LSPosition, TextDocumentPositionParams } from "vscode-languageclient"; +import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, Uri, ViewColumn, window, workspace, WorkspaceEdit } from "vscode"; +import { ConfigurationParams, ConfigurationRequest, LanguageClientOptions, Location as LSLocation, MessageType, Position as LSPosition, RenameParams, RenameRequest, TextDocumentIdentifier, TextDocumentPositionParams, Range as LSRange } from "vscode-languageclient"; import { LanguageClient, StreamInfo } from "vscode-languageclient/node"; import { apiManager } from "./apiManager"; import * as buildPath from './buildpath'; @@ -585,6 +585,21 @@ export class StandardLanguageClient { upgradeGradle(projectUri, version); })); + context.subscriptions.push(commands.registerCommand(Commands.RENAME_REFERENCES_COMMAND, async (fileUri: string, originalName: string, newName: string, range: LSRange) => { + const edit = new WorkspaceEdit(); + edit.replace(Uri.parse(fileUri), this.languageClient.protocol2CodeConverter.asRange(range), originalName); + await workspace.applyEdit(edit); + await workspace.saveAll(); + const params: RenameParams = { + newName: newName, + textDocument: TextDocumentIdentifier.create(fileUri), + position: LSPosition.create(range.start.line, range.start.character), + }; + const result = await this.languageClient.sendRequest(RenameRequest.type, params); + await workspace.applyEdit(this.languageClient.protocol2CodeConverter.asWorkspaceEdit(result)); + await workspace.saveAll(); + })); + languages.registerCodeActionsProvider({ language: "xml", scheme: "file", diff --git a/src/utils.ts b/src/utils.ts index 3a22388f80..71c2ac2bfd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -205,6 +205,23 @@ export function getJavaConfig(javaHome: string) { break; } + const isAutoBuildDisabled = javaConfig.autobuild.enabled === false; + const renameReference = javaConfig.refactoring.rename.references; + switch (renameReference) { + case "auto": + javaConfig.refactoring.rename.references = isAutoBuildDisabled; + break; + case "on": + javaConfig.refactoring.rename.references = true; + break; + case "off": + javaConfig.refactoring.rename.references = false; + break; + default: + javaConfig.refactoring.rename.references = false; + break; + } + const completionCaseMatching = javaConfig.completion.matchCase; if (completionCaseMatching === "auto") { javaConfig.completion.matchCase = isInsider ? "firstLetter" : "off";