Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Show reload button at editor title when there is reload required diagnostics #671

Merged
merged 2 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
"command": "java.project.update",
"title": "%contributes.commands.java.project.update%"
},
{
"command": "java.project.reloadProjectFromActiveFile",
"title": "%contributes.commands.java.project.reloadProjectFromActiveFile%",
"category": "Java",
"icon": "$(sync)"
},
{
"command": "java.project.rebuild",
"title": "%contributes.commands.java.project.rebuild%"
Expand Down Expand Up @@ -282,6 +288,10 @@
}
],
"commandPalette": [
{
"command": "java.project.reloadProjectFromActiveFile",
"when": "false"
},
{
"command": "java.view.package.exportJar",
"when": "java:serverMode == Standard && !java:noJavaProjects"
Expand Down Expand Up @@ -383,6 +393,13 @@
"group": "navigation@100"
}
],
"editor/title": [
{
"command": "java.project.reloadProjectFromActiveFile",
"when": "java:reloadProjectActive && javaLSReady",
"group": "navigation"
}
],
"editor/title/context": [
{
"command": "java.view.package.revealInProjectExplorer",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"contributes.commands.java.project.clean.workspace": "Clean Workspace",
"contributes.commands.java.project.rebuild": "Rebuild Project",
"contributes.commands.java.project.update": "Reload Project",
"contributes.commands.java.project.reloadProjectFromActiveFile": "Reload Java Project",
"contributes.commands.java.view.package.revealInProjectExplorer": "Reveal in Java Project Explorer",
"contributes.commands.java.view.package.changeToFlatPackageView":"Flat View",
"contributes.commands.java.view.package.changeToHierarchicalPackageView":"Hierarchical View",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"contributes.commands.java.project.clean.workspace": "清理工作空间",
"contributes.commands.java.project.rebuild": "重新构建项目",
"contributes.commands.java.project.update": "重新加载项目",
"contributes.commands.java.project.reloadProjectFromActiveFile": "重新加载 Java 项目",
"contributes.commands.java.view.package.revealInProjectExplorer": "在 Java 项目视图中显示",
"contributes.commands.java.view.package.changeToFlatPackageView":"平行显示",
"contributes.commands.java.view.package.changeToHierarchicalPackageView":"层级显示",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"contributes.commands.java.project.clean.workspace": "清理工作區",
"contributes.commands.java.project.rebuild": "重新建置專案",
"contributes.commands.java.project.update": "重新載入專案",
"contributes.commands.java.project.reloadProjectFromActiveFile": "重新載入 Java 專案",
"contributes.commands.java.view.package.revealInProjectExplorer": "在 Java 專案視圖中顯示",
"contributes.commands.java.view.package.changeToFlatPackageView":"平行顯示",
"contributes.commands.java.view.package.changeToHierarchicalPackageView":"階層顯示",
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export namespace Commands {

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

export const JAVA_PROJECT_RELOAD_ALL = "java.project.reloadProjectFromActiveFile";
jdneo marked this conversation as resolved.
Show resolved Hide resolved

export const JAVA_PROJECT_REBUILD = "java.project.rebuild";

export const JAVA_PROJECT_EXPLORER_FOCUS = "javaProjectExplorer.focus";
Expand Down
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export namespace Context {
export const LANGUAGE_SUPPORT_INSTALLED: string = "java:languageSupportInstalled";
export const NO_JAVA_PROJECT: string = "java:noJavaProjects";
export const WORKSPACE_CONTAINS_BUILD_FILES: string = "java:workspaceContainsBuildFiles";
export const RELOAD_PROJECT_ACTIVE: string = "java:reloadProjectActive";
}

export namespace Explorer {
Expand All @@ -31,3 +32,8 @@ export namespace Explorer {
export namespace ExtensionName {
export const JAVA_LANGUAGE_SUPPORT: string = "redhat.java";
}

/**
* The files names for all the build files we support.
*/
export const buildFiles = ["pom.xml", "build.gradle", "settings.gradle", "build.gradle.kts", "settings.gradle.kts"];
50 changes: 47 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { commands, Extension, ExtensionContext, extensions, tasks, Uri, workspace } from "vscode";
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation, sendInfo } from "vscode-extension-telemetry-wrapper";

import * as path from "path";
import { commands, Diagnostic, Extension, ExtensionContext, extensions, languages, tasks, TextDocument, TextEditor, Uri, window, workspace } from "vscode";
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation, instrumentOperationAsVsCodeCommand, sendInfo } from "vscode-extension-telemetry-wrapper";
import { Commands, contextManager } from "../extension.bundle";
import { BuildTaskProvider } from "./tasks/build/buildTaskProvider";
import { Context, ExtensionName } from "./constants";
import { buildFiles, Context, ExtensionName } from "./constants";
import { LibraryController } from "./controllers/libraryController";
import { ProjectController } from "./controllers/projectController";
import { init as initExpService } from "./ExperimentationService";
Expand Down Expand Up @@ -40,6 +42,28 @@ async function activateExtension(_operationId: string, context: ExtensionContext
context.subscriptions.push(syncHandler);
context.subscriptions.push(tasks.registerTaskProvider(ExportJarTaskProvider.exportJarType, new ExportJarTaskProvider()));
context.subscriptions.push(tasks.registerTaskProvider(BuildTaskProvider.type, new BuildTaskProvider()));

context.subscriptions.push(window.onDidChangeActiveTextEditor((e: TextEditor | undefined) => {
setContextForReloadProject(e?.document);
}));
context.subscriptions.push(languages.onDidChangeDiagnostics(() => {
setContextForReloadProject(window.activeTextEditor?.document);
}));
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_RELOAD_ALL, (uri?: Uri) => {
if (!uri) {
const activeDocument = window.activeTextEditor?.document;
if (!activeDocument) {
return;
}
uri = activeDocument.uri;
}

if (!buildFiles.includes(path.basename(uri.fsPath))) {
return;
}

commands.executeCommand(Commands.JAVA_PROJECT_CONFIGURATION_UPDATE, uri);
});
}

// this method is called when your extension is deactivated
Expand All @@ -61,3 +85,23 @@ function addExtensionChangeListener(context: ExtensionContext): void {
context.subscriptions.push(extensionChangeListener);
}
}

/**
* Set the context value when reload diagnostic is detected for the active
* build file.
*/
function setContextForReloadProject(document: TextDocument | undefined): void {
if (!document || !buildFiles.includes(path.basename(document.fileName))) {
contextManager.setContextValue(Context.RELOAD_PROJECT_ACTIVE, false);
return;
}

const diagnostics: Diagnostic[] = languages.getDiagnostics(document.uri);
for (const diagnostic of diagnostics) {
if (diagnostic.message.startsWith("The build file has been changed")) {
contextManager.setContextValue(Context.RELOAD_PROJECT_ACTIVE, true);
return;
}
}
contextManager.setContextValue(Context.RELOAD_PROJECT_ACTIVE, false);
}
35 changes: 35 additions & 0 deletions test/maven-suite/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as path from "path";
import * as assert from "assert";
import { Diagnostic, DiagnosticSeverity, languages, Position, Range, Uri, window } from "vscode";
import { contextManager } from "../../extension.bundle";
import { setupTestEnv, Uris } from "../shared";
import { sleep } from "../util";

// tslint:disable: only-arrow-functions
suite("Context Manager Tests", () => {

suiteSetup(setupTestEnv);

test("Can set reload project context correctly", async function() {
assert.strictEqual(!!contextManager.getContextValue("java:reloadProjectActive"), false);

const pomUri = Uri.file(path.join(Uris.MAVEN_PROJECT_NODE, "pom.xml"));
await window.showTextDocument(pomUri);
assert.strictEqual(!!contextManager.getContextValue("java:reloadProjectActive"), false);

const collection = languages.createDiagnosticCollection("test-collection");
collection.set(pomUri, [new Diagnostic(
new Range(new Position(0, 0), new Position(0, 0)),
"The build file has been changed and may need reload to make it effective.",
DiagnosticSeverity.Information
)]);
await sleep(1000);
assert.strictEqual(!!contextManager.getContextValue("java:reloadProjectActive"), true);

await window.showTextDocument(Uri.file(Uris.MAVEN_MAIN_CLASS));
assert.strictEqual(!!contextManager.getContextValue("java:reloadProjectActive"), false);
});
});