From 7ef38c61b2351dee4cda0811b0c0f26f965a59f8 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Fri, 8 Dec 2023 10:53:49 +0800 Subject: [PATCH] Remove the compatibility check for createLanguageStatusItem() api Signed-off-by: Sheng Chen --- src/extension.ts | 8 +-- src/languageStatusItemFactory.ts | 50 ++++++----------- src/lombokSupport.ts | 24 ++++---- src/runtimeStatusBarProvider.ts | 94 ++++++++++---------------------- 4 files changed, 60 insertions(+), 116 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index e8a94e011..5246d3b37 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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'; @@ -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'; @@ -399,7 +399,7 @@ export async function activate(context: ExtensionContext): Promise 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); @@ -410,7 +410,7 @@ export async function activate(context: ExtensionContext): Promise if (event === ServerMode.standard) { syntaxClient.stop(); fileEventHandler.setServerStatus(true); - runtimeStatusBarProvider.initialize(context); + languageStatusBarProvider.initialize(context); } commands.executeCommand('setContext', 'java:serverMode', event); }); diff --git a/src/languageStatusItemFactory.ts b/src/languageStatusItemFactory.ts index 0ddf89398..d647bf6d2 100644 --- a/src/languageStatusItemFactory.ts +++ b/src/languageStatusItemFactory.ts @@ -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", @@ -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", @@ -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 { @@ -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 { diff --git a/src/lombokSupport.ts b/src/lombokSupport.ts index bfdd77d4e..bd23144ef 100644 --- a/src/lombokSupport.ts +++ b/src/lombokSupport.ts @@ -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"; @@ -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); } @@ -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 { diff --git a/src/runtimeStatusBarProvider.ts b/src/runtimeStatusBarProvider.ts index ae86d9112..08fa38962 100644 --- a/src/runtimeStatusBarProvider.ts +++ b/src/runtimeStatusBarProvider.ts @@ -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; private fileProjectMapping: Map; 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(); this.fileProjectMapping = new Map(); this.disposables = []; - this.isAdvancedStatusBarItem = semver.gte(version, "1.57.0"); } public async initialize(context: ExtensionContext): Promise { @@ -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); @@ -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); })); @@ -111,7 +92,6 @@ class RuntimeStatusBarProvider implements Disposable { } public dispose(): void { - this.statusBarItem?.dispose(); this.runtimeStatusItem?.dispose(); this.buildFileStatusItem?.dispose(); this.lombokVersionItem?.dispose(); @@ -176,63 +156,47 @@ class RuntimeStatusBarProvider implements Disposable { } private async updateItem(context: ExtensionContext, textEditor: TextEditor): Promise { - 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(); } } @@ -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();