From 72d986d0d656a3ecaaf2f14ad604dc16e0a274df Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Mon, 18 Oct 2021 17:30:49 +0200 Subject: [PATCH] Quickfixes should be available at the line level Signed-off-by: Snjezana Peco --- README.md | 4 ++++ package.json | 10 +++++++++ src/extension.ts | 53 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1ae186a62a..10243ac4f6 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,10 @@ The following settings are supported: * `java.settings.url` : Specifies the url or file path to the workspace Java settings. See [Setting Global Preferences](https://github.com/redhat-developer/vscode-java/wiki/Settings-Global-Preferences) * `java.symbols.includeSourceMethodDeclarations` : Include method declarations from source files in symbol search. Defaults to `false`. +New in 1.1.0: + * `java.quickfix.showAt"` : Show quickfixes at the problem or line level. + + 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). diff --git a/package.json b/package.json index b3add245a3..904b0043ae 100644 --- a/package.json +++ b/package.json @@ -817,6 +817,16 @@ "markdownDescription": "Include method declarations from source files in symbol search.", "default": false, "scope": "window" + }, + "java.quickfix.showAt": { + "type": "string", + "enum": [ + "line", + "problem" + ], + "default": "line", + "description": "Show quickfixes at the problem or line level.", + "scope": "window" } } }, diff --git a/src/extension.ts b/src/extension.ts index d9d35c6eb8..32f1a29219 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,8 +4,8 @@ import * as path from 'path'; import * as os from 'os'; import * as fs from 'fs'; import * as fse from 'fs-extra'; -import { workspace, extensions, ExtensionContext, window, commands, ViewColumn, Uri, languages, IndentAction, InputBoxOptions, EventEmitter, OutputChannel, TextDocument, RelativePattern, ConfigurationTarget, WorkspaceConfiguration, env, UIKind } from 'vscode'; -import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClientOptions, RevealOutputChannelOn, ErrorHandler, Message, ErrorAction, CloseAction, DidChangeConfigurationNotification, CancellationToken } from 'vscode-languageclient'; +import { workspace, extensions, ExtensionContext, window, commands, ViewColumn, Uri, languages, IndentAction, InputBoxOptions, EventEmitter, OutputChannel, TextDocument, RelativePattern, ConfigurationTarget, WorkspaceConfiguration, env, UIKind, Selection, CodeActionContext, Diagnostic } from 'vscode'; +import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClientOptions, RevealOutputChannelOn, ErrorHandler, Message, ErrorAction, CloseAction, DidChangeConfigurationNotification, CancellationToken, CodeActionRequest, CodeActionParams, Command } from 'vscode-languageclient'; import { LanguageClient } from 'vscode-languageclient/node'; import { collectJavaExtensions, isContributedPartUpdated } from './plugin'; import { prepareExecutable } from './javaServerStarter'; @@ -205,6 +205,55 @@ export function activate(context: ExtensionContext): Promise { } }); } + }, + // https://github.com/redhat-developer/vscode-java/issues/2130 + // include all diagnostics for the current line in the CodeActionContext params for the performance reason + provideCodeActions: (document, range, context, token, next) => { + const client: any = standardClient.getClient(); + const params: CodeActionParams = { + textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document), + range: client.code2ProtocolConverter.asRange(range), + context: client.code2ProtocolConverter.asCodeActionContext(context) + }; + const showAt = getJavaConfiguration().get("quickfix.showAt"); + if (showAt === 'line' && range.start.line === range.end.line && range.start.character === range.end.character) { + const textLine = document.lineAt(params.range.start.line); + if (textLine !== null) { + const diagnostics = client.diagnostics.get(document.uri); + const allDiagnostics: Diagnostic[] = []; + for (const diagnostic of diagnostics) { + if (textLine.range.intersection(diagnostic.range)) { + const newLen = allDiagnostics.push(diagnostic); + if (newLen > 1000) { + break; + } + } + } + const codeActionContext: CodeActionContext = { + diagnostics: allDiagnostics, + only: context.only, + }; + params.context = client.code2ProtocolConverter.asCodeActionContext(codeActionContext); + } + } + return client.sendRequest(CodeActionRequest.type, params, token).then((values) => { + if (values === null) { + return undefined; + } + const result = []; + for (const item of values) { + if (Command.is(item)) { + result.push(client.protocol2CodeConverter.asCommand(item)); + } + else { + result.push(client.protocol2CodeConverter.asCodeAction(item)); + } + } + return result; + }, (error) => { + client.logFailedRequest(CodeActionRequest.type, error); + return Promise.resolve([]); + }); } }, revealOutputChannelOn: RevealOutputChannelOn.Never,