Skip to content

Commit

Permalink
Remove the compatibility check for createLanguageStatusItem() api
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <sheche@microsoft.com>
  • Loading branch information
jdneo authored and rgrunber committed Dec 8, 2023
1 parent 65a6431 commit 7ef38c6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 116 deletions.
8 changes: 4 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fs from 'fs';
import * as fse from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { CodeActionContext, commands, ConfigurationTarget, Diagnostic, env, EventEmitter, ExtensionContext, extensions, IndentAction, InputBoxOptions, languages, RelativePattern, TextDocument, UIKind, Uri, ViewColumn, window, workspace, WorkspaceConfiguration, version } from 'vscode';
import { CodeActionContext, commands, ConfigurationTarget, Diagnostic, env, EventEmitter, ExtensionContext, extensions, IndentAction, InputBoxOptions, languages, RelativePattern, TextDocument, UIKind, Uri, ViewColumn, window, workspace, WorkspaceConfiguration } from 'vscode';
import { CancellationToken, CodeActionParams, CodeActionRequest, Command, CompletionRequest, DidChangeConfigurationNotification, ExecuteCommandParams, ExecuteCommandRequest, LanguageClientOptions, RevealOutputChannelOn } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { apiManager } from './apiManager';
Expand All @@ -22,7 +22,7 @@ import { collectJavaExtensions, getBundlesToReload, isContributedPartUpdated } f
import { registerClientProviders } from './providerDispatcher';
import { initialize as initializeRecommendation } from './recommendation';
import * as requirements from './requirements';
import { runtimeStatusBarProvider } from './runtimeStatusBarProvider';
import { languageStatusBarProvider } from './runtimeStatusBarProvider';
import { serverStatusBarProvider } from './serverStatusBarProvider';
import { ACTIVE_BUILD_TOOL_STATE, cleanWorkspaceFileName, getJavaServerMode, handleTextDocumentChanges, getImportMode, onConfigurationChange, ServerMode, ImportMode } from './settings';
import { snippetCompletionProvider } from './snippetCompletionProvider';
Expand Down Expand Up @@ -399,7 +399,7 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>

context.subscriptions.push(snippetCompletionProvider.initialize());
context.subscriptions.push(serverStatusBarProvider);
context.subscriptions.push(runtimeStatusBarProvider);
context.subscriptions.push(languageStatusBarProvider);

const classEditorProviderRegistration = window.registerCustomEditorProvider(JavaClassEditorProvider.viewType, new JavaClassEditorProvider(context));
context.subscriptions.push(classEditorProviderRegistration);
Expand All @@ -410,7 +410,7 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
if (event === ServerMode.standard) {
syntaxClient.stop();
fileEventHandler.setServerStatus(true);
runtimeStatusBarProvider.initialize(context);
languageStatusBarProvider.initialize(context);
}
commands.executeCommand('setContext', 'java:serverMode', event);
});
Expand Down
50 changes: 17 additions & 33 deletions src/languageStatusItemFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ const languageServerDocumentSelector = [
{ pattern: '**/{build,settings}.gradle.kts'}
];

export function supportsLanguageStatus(): boolean {
return !!vscode.languages.createLanguageStatusItem;
}

export namespace StatusCommands {
export const switchToStandardCommand = {
title: "Load Projects",
Expand All @@ -25,12 +21,6 @@ export namespace StatusCommands {
tooltip: "LightWeight mode only provides limited features, please load projects to get full feature set"
};

export const showServerStatusCommand = {
title: "Show Build Status",
command: Commands.SHOW_SERVER_TASK_STATUS,
tooltip: "Show Build Status"
};

export const configureJavaRuntimeCommand = {
title: "Configure Java Runtime",
command: "workbench.action.openSettings",
Expand All @@ -47,19 +37,16 @@ export namespace StatusCommands {
}

export namespace RuntimeStatusItemFactory {
export function create(text: string, vmInstallPath: string): any {
if (supportsLanguageStatus()) {
const item = vscode.languages.createLanguageStatusItem("javaRuntimeStatusItem", languageServerDocumentSelector);
item.severity = vscode.LanguageStatusSeverity?.Information;
item.name = "Java Runtime";
item.text = text;
item.command = StatusCommands.configureJavaRuntimeCommand;
if (vmInstallPath) {
item.command.tooltip = `Language Level: ${text} <${vmInstallPath}>`;
}
return item;
export function create(text: string, vmInstallPath: string): vscode.LanguageStatusItem {
const item = vscode.languages.createLanguageStatusItem("javaRuntimeStatusItem", languageServerDocumentSelector);
item.severity = vscode.LanguageStatusSeverity?.Information;
item.name = "Java Runtime";
item.text = text;
item.command = StatusCommands.configureJavaRuntimeCommand;
if (vmInstallPath) {
item.command.tooltip = `Language Level: ${text} <${vmInstallPath}>`;
}
return undefined;
return item;
}

export function update(item: any, text: string, vmInstallPath: string): void {
Expand All @@ -69,17 +56,14 @@ export namespace RuntimeStatusItemFactory {
}

export namespace BuildFileStatusItemFactory {
export function create(buildFilePath: string): any {
if (supportsLanguageStatus()) {
const fileName = path.basename(buildFilePath);
const item = vscode.languages.createLanguageStatusItem("javaBuildFileStatusItem", languageServerDocumentSelector);
item.severity = vscode.LanguageStatusSeverity?.Information;
item.name = "Java Build File";
item.text = fileName;
item.command = getOpenBuildFileCommand(buildFilePath);
return item;
}
return undefined;
export function create(buildFilePath: string): vscode.LanguageStatusItem {
const fileName = path.basename(buildFilePath);
const item = vscode.languages.createLanguageStatusItem("javaBuildFileStatusItem", languageServerDocumentSelector);
item.severity = vscode.LanguageStatusSeverity?.Information;
item.name = "Java Build File";
item.text = fileName;
item.command = getOpenBuildFileCommand(buildFilePath);
return item;
}

export function update(item: any, buildFilePath: string): void {
Expand Down
24 changes: 10 additions & 14 deletions src/lombokSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import * as vscode from "vscode";
import { ExtensionContext, window, commands } from "vscode";
import { Commands } from "./commands";
import { apiManager } from "./apiManager";
import { supportsLanguageStatus } from "./languageStatusItemFactory";
import { runtimeStatusBarProvider } from './runtimeStatusBarProvider';
import { languageStatusBarProvider } from './runtimeStatusBarProvider';
import { logger } from './log';
import { getAllJavaProjects } from "./utils";

Expand Down Expand Up @@ -157,11 +156,11 @@ export async function checkLombokDependency(context: ExtensionContext) {
registerLombokConfigureCommand(context);
isLombokCommandInitialized = true;
}
runtimeStatusBarProvider.initializeLombokStatusBar();
languageStatusBarProvider.initializeLombokStatusBar();
isLombokStatusBarInitialized = true;
}
if (isLombokStatusBarInitialized && !projectLombokPath) {
runtimeStatusBarProvider.destroyLombokStatusBar();
languageStatusBarProvider.destroyLombokStatusBar();
isLombokStatusBarInitialized = false;
cleanupLombokCache(context);
}
Expand Down Expand Up @@ -245,16 +244,13 @@ export function registerLombokConfigureCommand(context: ExtensionContext) {
}

export namespace LombokVersionItemFactory {
export function create(text: string): any {
if (supportsLanguageStatus()) {
const item = vscode.languages.createLanguageStatusItem("javaLombokVersionItem", languageServerDocumentSelector);
item.severity = vscode.LanguageStatusSeverity?.Information;
item.name = "Lombok Version";
item.text = text;
item.command = getLombokChangeCommand();
return item;
}
return undefined;
export function create(text: string): vscode.LanguageStatusItem {
const item = vscode.languages.createLanguageStatusItem("javaLombokVersionItem", languageServerDocumentSelector);
item.severity = vscode.LanguageStatusSeverity?.Information;
item.name = "Lombok Version";
item.text = text;
item.command = getLombokChangeCommand();
return item;
}

export function update(item: any, text: string): void {
Expand Down
94 changes: 29 additions & 65 deletions src/runtimeStatusBarProvider.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
'use strict';

import * as fse from "fs-extra";
import { StatusBarItem, window, StatusBarAlignment, TextEditor, Uri, commands, workspace, version, languages, Command, ExtensionContext } from "vscode";
import { window, TextEditor, Uri, commands, workspace, ExtensionContext, LanguageStatusItem } from "vscode";
import { Commands } from "./commands";
import { Disposable } from "vscode-languageclient";
import * as path from "path";
import { apiManager } from "./apiManager";
import * as semver from "semver";
import { ACTIVE_BUILD_TOOL_STATE } from "./settings";
import { BuildFileStatusItemFactory, RuntimeStatusItemFactory, StatusCommands, supportsLanguageStatus } from "./languageStatusItemFactory";
import { BuildFileStatusItemFactory, RuntimeStatusItemFactory } from "./languageStatusItemFactory";
import { getAllJavaProjects, getJavaConfiguration, hasBuildToolConflicts } from "./utils";
import { LombokVersionItemFactory, getLombokVersion, isLombokImported } from "./lombokSupport";

class RuntimeStatusBarProvider implements Disposable {
private statusBarItem: StatusBarItem;
private runtimeStatusItem: any;
private buildFileStatusItem: any;
private lombokVersionItem: any;
class LanguageStatusBarProvider implements Disposable {
private runtimeStatusItem: LanguageStatusItem;
private buildFileStatusItem: LanguageStatusItem;
private lombokVersionItem: LanguageStatusItem;
private javaProjects: Map<string, IProjectInfo>;
private fileProjectMapping: Map<string, string>;
private storagePath: string | undefined;
private disposables: Disposable[];
// Adopt new API for status bar item, meanwhile keep the compatibility with Theia.
// See: https://github.com/redhat-developer/vscode-java/issues/1982
private isAdvancedStatusBarItem: boolean;

constructor() {
this.javaProjects = new Map<string, IProjectInfo>();
this.fileProjectMapping = new Map<string, string>();
this.disposables = [];
this.isAdvancedStatusBarItem = semver.gte(version, "1.57.0");
}

public async initialize(context: ExtensionContext): Promise<void> {
Expand All @@ -39,15 +33,6 @@ class RuntimeStatusBarProvider implements Disposable {
this.storagePath = Uri.file(path.join(storagePath, "..", "..")).fsPath;
}

if (!supportsLanguageStatus()) {
if (this.isAdvancedStatusBarItem) {
this.statusBarItem = (window.createStatusBarItem as any)("java.runtimeStatus", StatusBarAlignment.Right, 0);
(this.statusBarItem as any).name = "Java Runtime Configuration";
} else {
this.statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right, 0);
}
}

let projectUriStrings: string[];
try {
projectUriStrings = await getAllJavaProjects(false);
Expand All @@ -59,10 +44,6 @@ class RuntimeStatusBarProvider implements Disposable {
this.javaProjects.set(Uri.parse(uri).fsPath, undefined);
}

if (!supportsLanguageStatus()) {
this.statusBarItem.command = StatusCommands.configureJavaRuntimeCommand;
}

this.disposables.push(window.onDidChangeActiveTextEditor((textEditor) => {
this.updateItem(context, textEditor);
}));
Expand Down Expand Up @@ -111,7 +92,6 @@ class RuntimeStatusBarProvider implements Disposable {
}

public dispose(): void {
this.statusBarItem?.dispose();
this.runtimeStatusItem?.dispose();
this.buildFileStatusItem?.dispose();
this.lombokVersionItem?.dispose();
Expand Down Expand Up @@ -176,63 +156,47 @@ class RuntimeStatusBarProvider implements Disposable {
}

private async updateItem(context: ExtensionContext, textEditor: TextEditor): Promise<void> {
if (!textEditor || path.extname(textEditor.document.fileName) !== ".java" && !supportsLanguageStatus()) {
this.statusBarItem?.hide();
if (!textEditor || path.extname(textEditor.document.fileName) !== ".java") {
return;
}

const uri: Uri = textEditor.document.uri;
const projectPath: string = this.findOwnerProject(uri);
if (!projectPath) {
if (supportsLanguageStatus()) {
this.hideRuntimeStatusItem();
this.hideBuildFileStatusItem();
this.hideLombokVersionItem();
} else {
this.statusBarItem?.hide();
}
this.hideRuntimeStatusItem();
this.hideBuildFileStatusItem();
this.hideLombokVersionItem();
return;
}

const projectInfo: IProjectInfo = await this.getProjectInfo(projectPath);
if (!projectInfo) {
if (supportsLanguageStatus()) {
this.hideRuntimeStatusItem();
this.hideBuildFileStatusItem();
this.hideLombokVersionItem();
} else {
this.statusBarItem?.hide();
}
this.hideRuntimeStatusItem();
this.hideBuildFileStatusItem();
this.hideLombokVersionItem();
return;
}

const text = this.getJavaRuntimeFromVersion(projectInfo.sourceLevel);
if (supportsLanguageStatus()) {
const buildFilePath = await this.getBuildFilePath(context, projectPath);
if (!this.runtimeStatusItem) {
this.runtimeStatusItem = RuntimeStatusItemFactory.create(text, projectInfo.vmInstallPath);
if (buildFilePath) {
this.buildFileStatusItem = BuildFileStatusItemFactory.create(buildFilePath);
}
} else {
RuntimeStatusItemFactory.update(this.runtimeStatusItem, text, projectInfo.vmInstallPath);
if (buildFilePath) {
BuildFileStatusItemFactory.update(this.buildFileStatusItem, buildFilePath);
}
const buildFilePath = await this.getBuildFilePath(context, projectPath);
if (!this.runtimeStatusItem) {
this.runtimeStatusItem = RuntimeStatusItemFactory.create(text, projectInfo.vmInstallPath);
if (buildFilePath) {
this.buildFileStatusItem = BuildFileStatusItemFactory.create(buildFilePath);
}
} else {
RuntimeStatusItemFactory.update(this.runtimeStatusItem, text, projectInfo.vmInstallPath);
if (buildFilePath) {
BuildFileStatusItemFactory.update(this.buildFileStatusItem, buildFilePath);
}
}

if (isLombokImported()) {
if (!this.lombokVersionItem) {
if (isLombokImported()) {
this.lombokVersionItem = LombokVersionItemFactory.create(getLombokVersion());
}
this.lombokVersionItem = LombokVersionItemFactory.create(getLombokVersion());
} else {
if (isLombokImported()) {
LombokVersionItemFactory.update(this.lombokVersionItem, getLombokVersion());
}
LombokVersionItemFactory.update(this.lombokVersionItem, getLombokVersion());
}
} else {
this.statusBarItem.text = text;
this.statusBarItem.tooltip = projectInfo.vmInstallPath ? `Language Level: ${this.statusBarItem.text} <${projectInfo.vmInstallPath}>` : "Configure Java Runtime";
this.statusBarItem.show();
}
}

Expand Down Expand Up @@ -302,4 +266,4 @@ interface IProjectInfo {
const SOURCE_LEVEL_KEY = "org.eclipse.jdt.core.compiler.source";
const VM_INSTALL_PATH = "org.eclipse.jdt.ls.core.vm.location";

export const runtimeStatusBarProvider: RuntimeStatusBarProvider = new RuntimeStatusBarProvider();
export const languageStatusBarProvider: LanguageStatusBarProvider = new LanguageStatusBarProvider();

0 comments on commit 7ef38c6

Please sign in to comment.