Skip to content

Commit

Permalink
Feature discovery - Provide quick fix to 'Configure static import...' (
Browse files Browse the repository at this point in the history
  • Loading branch information
testforstephen authored Oct 16, 2023
1 parent 3ac1335 commit f598b79
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/clientCodeActionProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { CancellationToken, CodeAction, CodeActionContext, CodeActionKind, CodeActionProvider, Command, ExtensionContext, ProviderResult, Range, Selection, TextDocument, commands } from "vscode";
import { apiManager } from "./apiManager";

const configureStaticImportsCommand = "java.action.configureFavoriteStaticMembers";
const UNDEFINED_METHOD = "67108964";
export class ClientCodeActionProvider implements CodeActionProvider<CodeAction> {
constructor(readonly context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand(configureStaticImportsCommand, async () => {
commands.executeCommand("workbench.action.openSettings", "java.completion.favoriteStaticMembers");
apiManager.fireTraceEvent({
name: "java.ls.command",
properties: {
command: configureStaticImportsCommand,
},
});
}));
}

provideCodeActions(document: TextDocument, range: Range | Selection, context: CodeActionContext, token: CancellationToken): ProviderResult<(CodeAction | Command)[]> {
const codeActions = [];
if (context.diagnostics?.some(diagnostic => diagnostic.code === UNDEFINED_METHOD)
|| document.lineAt(range.start.line)?.text?.startsWith("import ")) {
const action = new CodeAction("Configure static import...", CodeActionKind.QuickFix);
action.command = {
title: "Configure static import...",
command: configureStaticImportsCommand,
};
codeActions.push(action);
}
return codeActions;
}
}
5 changes: 5 additions & 0 deletions src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { Telemetry } from "./telemetry";
import { TelemetryEvent } from "@redhat-developer/vscode-redhat-telemetry/lib";
import { registerDocumentValidationListener } from './diagnostic';
import { listJdks, sortJdksBySource, sortJdksByVersion } from './jdkUtils';
import { ClientCodeActionProvider } from './clientCodeActionProvider';

const extensionName = 'Language Support for Java';
const GRADLE_CHECKSUM = "gradle/checksum/prompt";
Expand Down Expand Up @@ -618,6 +619,10 @@ export class StandardLanguageClient {
pattern: "**/{gradle/wrapper/gradle-wrapper.properties,build.gradle,build.gradle.kts,settings.gradle,settings.gradle.kts}"
}, new GradleCodeActionProvider(), gradleCodeActionMetadata);

languages.registerCodeActionsProvider({
scheme: 'file',
language: 'java'
}, new ClientCodeActionProvider(context));
}

private showGradleCompatibilityIssueNotification(message: string, options: string[], projectUri: string, gradleVersion: string, newJavaHome: string) {
Expand Down

0 comments on commit f598b79

Please sign in to comment.