Skip to content

Commit

Permalink
feat: Show reload button at editor title when there is reload require…
Browse files Browse the repository at this point in the history
…d diagnostics

Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo committed Jul 21, 2022
1 parent 755f1c1 commit 51e5d6f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
"./server/com.microsoft.jdtls.ext.core-0.20.0.jar"
],
"commands": [
{
"command": "java.project.reloadProjectFromActiveFile",
"title": "Reload Java Project",
"category": "Java",
"icon": "$(sync)"
},
{
"command": "java.project.create",
"title": "%contributes.commands.java.project.create%",
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
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";

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);
});
});

0 comments on commit 51e5d6f

Please sign in to comment.