Skip to content

Commit c1ba746

Browse files
Re-validate current document if the focus is switched to a Java file (#3053)
* Re-validate current document if the focus is switched to a Java file Signed-off-by: Jinbo Wang <[email protected]>
1 parent 02deb55 commit c1ba746

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ The following settings are supported:
228228
* `java.configuration.maven.defaultMojoExecutionAction` : Specifies default mojo execution action when no associated metadata can be detected. Defaults to `ignore`.
229229
* `java.completion.lazyResolveTextEdit.enabled`: [Experimental] Enable/disable lazily resolving text edits for code completion. Defaults to `true`.
230230

231+
New in 1.19.0
232+
* `java.edit.validateAllOpenBuffersOnChanges`: Specifies whether to recheck all open Java files for diagnostics when editing a Java file. Defaults to `false`.
233+
231234
Semantic Highlighting
232235
===============
233236
[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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,12 @@
11081108
"usesOnlineServices",
11091109
"telemetry"
11101110
]
1111+
},
1112+
"java.edit.validateAllOpenBuffersOnChanges": {
1113+
"type": "boolean",
1114+
"default": false,
1115+
"markdownDescription": "Specifies whether to recheck all open Java files for diagnostics when editing a Java file.",
1116+
"scope": "window"
11111117
}
11121118
}
11131119
},

src/diagnostic.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ExtensionContext, window } from "vscode";
2+
import { ValidateDocumentNotification } from "./protocol";
3+
import { LanguageClient } from "vscode-languageclient/node";
4+
import { validateAllOpenBuffersOnChanges } from "./settings";
5+
6+
export function registerDocumentValidationListener(context: ExtensionContext, languageClient: LanguageClient) {
7+
context.subscriptions.push(window.onDidChangeActiveTextEditor(textEditor => {
8+
// Refresh the diagnostics when the focus is switched to a Java file.
9+
if (textEditor?.document.uri?.scheme === "file" && textEditor?.document.languageId === "java") {
10+
if (!validateAllOpenBuffersOnChanges()) {
11+
languageClient.sendNotification(ValidateDocumentNotification.type, {
12+
textDocument: {
13+
uri: textEditor.document.uri.toString(),
14+
},
15+
});
16+
}
17+
}
18+
}));
19+
}

src/protocol.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,11 @@ export interface CheckExtractInterfaceStatusResponse {
464464
export namespace CheckExtractInterfaceStatusRequest {
465465
export const type = new RequestType<CodeActionParams, CheckExtractInterfaceStatusResponse, void>('java/checkExtractInterfaceStatus');
466466
}
467+
468+
export interface ValidateDocumentParams {
469+
textDocument: TextDocumentIdentifier;
470+
}
471+
472+
export namespace ValidateDocumentNotification {
473+
export const type = new NotificationType<ValidateDocumentParams>('java/validateDocument');
474+
}

src/settings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ export function getJavaServerMode(): ServerMode {
274274
|| ServerMode.hybrid;
275275
}
276276

277+
export function validateAllOpenBuffersOnChanges(): boolean {
278+
return workspace.getConfiguration().get('java.edit.validateAllOpenBuffersOnChanges');
279+
}
280+
277281
export function setGradleWrapperChecksum(wrapper: string, sha256?: string) {
278282
const opened = gradleWrapperPromptDialogs.filter(v => (v === sha256));
279283
if (opened !== null && opened.length > 0) {

src/standardLanguageClient.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { typeHierarchyTree } from "./typeHierarchy/typeHierarchyTree";
3939
import { getAllJavaProjects, getJavaConfig, getJavaConfiguration } from "./utils";
4040
import { Telemetry } from "./telemetry";
4141
import { TelemetryEvent } from "@redhat-developer/vscode-redhat-telemetry/lib";
42+
import { registerDocumentValidationListener } from './diagnostic';
4243

4344
const extensionName = 'Language Support for Java';
4445
const GRADLE_CHECKSUM = "gradle/checksum/prompt";
@@ -146,6 +147,7 @@ export class StandardLanguageClient {
146147
});
147148
// Disable the client-side snippet provider since LS is ready.
148149
snippetCompletionProvider.dispose();
150+
registerDocumentValidationListener(context, this.languageClient);
149151
break;
150152
case 'Started':
151153
this.status = ClientStatus.started;

0 commit comments

Comments
 (0)