Skip to content

Commit 0edd91b

Browse files
committed
feat: Show reload button at editor title when there is reload required diagnostics
Signed-off-by: Sheng Chen <[email protected]>
1 parent f65f381 commit 0edd91b

8 files changed

+110
-3
lines changed

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
"command": "java.project.update",
8989
"title": "%contributes.commands.java.project.update%"
9090
},
91+
{
92+
"command": "java.project.reloadProjectFromActiveFile",
93+
"title": "%contributes.commands.java.project.reloadProjectFromActiveFile%",
94+
"category": "Java",
95+
"icon": "$(sync)"
96+
},
9197
{
9298
"command": "java.project.rebuild",
9399
"title": "%contributes.commands.java.project.rebuild%"
@@ -282,6 +288,10 @@
282288
}
283289
],
284290
"commandPalette": [
291+
{
292+
"command": "java.project.reloadProjectFromActiveFile",
293+
"when": "false"
294+
},
285295
{
286296
"command": "java.view.package.exportJar",
287297
"when": "java:serverMode == Standard && !java:noJavaProjects"
@@ -383,6 +393,13 @@
383393
"group": "navigation@100"
384394
}
385395
],
396+
"editor/title": [
397+
{
398+
"command": "java.project.reloadProjectFromActiveFile",
399+
"when": "java:reloadProjectActive && javaLSReady",
400+
"group": "navigation"
401+
}
402+
],
386403
"editor/title/context": [
387404
{
388405
"command": "java.view.package.revealInProjectExplorer",

package.nls.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"contributes.commands.java.project.clean.workspace": "Clean Workspace",
1010
"contributes.commands.java.project.rebuild": "Rebuild Project",
1111
"contributes.commands.java.project.update": "Reload Project",
12+
"contributes.commands.java.project.reloadProjectFromActiveFile": "Reload Java Project",
1213
"contributes.commands.java.view.package.revealInProjectExplorer": "Reveal in Java Project Explorer",
1314
"contributes.commands.java.view.package.changeToFlatPackageView":"Flat View",
1415
"contributes.commands.java.view.package.changeToHierarchicalPackageView":"Hierarchical View",

package.nls.zh-cn.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"contributes.commands.java.project.clean.workspace": "清理工作空间",
1010
"contributes.commands.java.project.rebuild": "重新构建项目",
1111
"contributes.commands.java.project.update": "重新加载项目",
12+
"contributes.commands.java.project.reloadProjectFromActiveFile": "重新加载 Java 项目",
1213
"contributes.commands.java.view.package.revealInProjectExplorer": "在 Java 项目视图中显示",
1314
"contributes.commands.java.view.package.changeToFlatPackageView":"平行显示",
1415
"contributes.commands.java.view.package.changeToHierarchicalPackageView":"层级显示",

package.nls.zh-tw.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"contributes.commands.java.project.clean.workspace": "清理工作區",
1010
"contributes.commands.java.project.rebuild": "重新建置專案",
1111
"contributes.commands.java.project.update": "重新載入專案",
12+
"contributes.commands.java.project.reloadProjectFromActiveFile": "重新載入 Java 專案",
1213
"contributes.commands.java.view.package.revealInProjectExplorer": "在 Java 專案視圖中顯示",
1314
"contributes.commands.java.view.package.changeToFlatPackageView":"平行顯示",
1415
"contributes.commands.java.view.package.changeToHierarchicalPackageView":"階層顯示",

src/commands.ts

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export namespace Commands {
6464

6565
export const JAVA_PROJECT_UPDATE = "java.project.update";
6666

67+
export const JAVA_PROJECT_RELOAD_ALL = "java.project.reloadProjectFromActiveFile";
68+
6769
export const JAVA_PROJECT_REBUILD = "java.project.rebuild";
6870

6971
export const JAVA_PROJECT_EXPLORER_FOCUS = "javaProjectExplorer.focus";

src/constants.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export namespace Context {
66
export const LANGUAGE_SUPPORT_INSTALLED: string = "java:languageSupportInstalled";
77
export const NO_JAVA_PROJECT: string = "java:noJavaProjects";
88
export const WORKSPACE_CONTAINS_BUILD_FILES: string = "java:workspaceContainsBuildFiles";
9+
export const RELOAD_PROJECT_ACTIVE: string = "java:reloadProjectActive";
910
}
1011

1112
export namespace Explorer {
@@ -31,3 +32,8 @@ export namespace Explorer {
3132
export namespace ExtensionName {
3233
export const JAVA_LANGUAGE_SUPPORT: string = "redhat.java";
3334
}
35+
36+
/**
37+
* The files names for all the build files we support.
38+
*/
39+
export const buildFiles = ["pom.xml", "build.gradle", "settings.gradle", "build.gradle.kts", "settings.gradle.kts"];

src/extension.ts

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import { commands, Extension, ExtensionContext, extensions, tasks, Uri, workspace } from "vscode";
5-
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation, sendInfo } from "vscode-extension-telemetry-wrapper";
4+
5+
import * as path from "path";
6+
import { commands, Diagnostic, Extension, ExtensionContext, extensions, languages, tasks, TextDocument, TextEditor, Uri, window, workspace } from "vscode";
7+
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation, instrumentOperationAsVsCodeCommand, sendInfo } from "vscode-extension-telemetry-wrapper";
68
import { Commands, contextManager } from "../extension.bundle";
79
import { BuildTaskProvider } from "./tasks/build/buildTaskProvider";
8-
import { Context, ExtensionName } from "./constants";
10+
import { buildFiles, Context, ExtensionName } from "./constants";
911
import { LibraryController } from "./controllers/libraryController";
1012
import { ProjectController } from "./controllers/projectController";
1113
import { init as initExpService } from "./ExperimentationService";
@@ -40,6 +42,28 @@ async function activateExtension(_operationId: string, context: ExtensionContext
4042
context.subscriptions.push(syncHandler);
4143
context.subscriptions.push(tasks.registerTaskProvider(ExportJarTaskProvider.exportJarType, new ExportJarTaskProvider()));
4244
context.subscriptions.push(tasks.registerTaskProvider(BuildTaskProvider.type, new BuildTaskProvider()));
45+
46+
context.subscriptions.push(window.onDidChangeActiveTextEditor((e: TextEditor | undefined) => {
47+
setContextForReloadProject(e?.document);
48+
}));
49+
context.subscriptions.push(languages.onDidChangeDiagnostics(() => {
50+
setContextForReloadProject(window.activeTextEditor?.document);
51+
}));
52+
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_RELOAD_ALL, (uri?: Uri) => {
53+
if (!uri) {
54+
const activeDocument = window.activeTextEditor?.document;
55+
if (!activeDocument) {
56+
return;
57+
}
58+
uri = activeDocument.uri;
59+
}
60+
61+
if (!buildFiles.includes(path.basename(uri.fsPath))) {
62+
return;
63+
}
64+
65+
commands.executeCommand(Commands.JAVA_PROJECT_CONFIGURATION_UPDATE, uri);
66+
});
4367
}
4468

4569
// this method is called when your extension is deactivated
@@ -61,3 +85,23 @@ function addExtensionChangeListener(context: ExtensionContext): void {
6185
context.subscriptions.push(extensionChangeListener);
6286
}
6387
}
88+
89+
/**
90+
* Set the context value when reload diagnostic is detected for the active
91+
* build file.
92+
*/
93+
function setContextForReloadProject(document: TextDocument | undefined): void {
94+
if (!document || !buildFiles.includes(path.basename(document.fileName))) {
95+
contextManager.setContextValue(Context.RELOAD_PROJECT_ACTIVE, false);
96+
return;
97+
}
98+
99+
const diagnostics: Diagnostic[] = languages.getDiagnostics(document.uri);
100+
for (const diagnostic of diagnostics) {
101+
if (diagnostic.message.startsWith("The build file has been changed")) {
102+
contextManager.setContextValue(Context.RELOAD_PROJECT_ACTIVE, true);
103+
return;
104+
}
105+
}
106+
contextManager.setContextValue(Context.RELOAD_PROJECT_ACTIVE, false);
107+
}

test/maven-suite/context.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import * as path from "path";
5+
import * as assert from "assert";
6+
import { Diagnostic, DiagnosticSeverity, languages, Position, Range, Uri, window } from "vscode";
7+
import { contextManager } from "../../extension.bundle";
8+
import { setupTestEnv, Uris } from "../shared";
9+
import { sleep } from "../util";
10+
11+
// tslint:disable: only-arrow-functions
12+
suite("Context Manager Tests", () => {
13+
14+
suiteSetup(setupTestEnv);
15+
16+
test("Can set reload project context correctly", async function() {
17+
assert.strictEqual(!!contextManager.getContextValue("java:reloadProjectActive"), false);
18+
19+
const pomUri = Uri.file(path.join(Uris.MAVEN_PROJECT_NODE, "pom.xml"));
20+
await window.showTextDocument(pomUri);
21+
assert.strictEqual(!!contextManager.getContextValue("java:reloadProjectActive"), false);
22+
23+
const collection = languages.createDiagnosticCollection("test-collection");
24+
collection.set(pomUri, [new Diagnostic(
25+
new Range(new Position(0, 0), new Position(0, 0)),
26+
"The build file has been changed and may need reload to make it effective.",
27+
DiagnosticSeverity.Information
28+
)]);
29+
await sleep(1000);
30+
assert.strictEqual(!!contextManager.getContextValue("java:reloadProjectActive"), true);
31+
32+
await window.showTextDocument(Uri.file(Uris.MAVEN_MAIN_CLASS));
33+
assert.strictEqual(!!contextManager.getContextValue("java:reloadProjectActive"), false);
34+
});
35+
});

0 commit comments

Comments
 (0)