diff --git a/src/definitions/vscodeJavaApi.ts b/src/definitions/vscodeJavaApi.ts new file mode 100644 index 0000000..a0cfee5 --- /dev/null +++ b/src/definitions/vscodeJavaApi.ts @@ -0,0 +1,40 @@ +/** + * Copyright 2024 Red Hat, Inc. and others. + + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import type { Event } from "vscode"; + +/** + * @see https://github.com/redhat-developer/vscode-java/blob/master/src/extension.api.ts#L116 + */ +export type JavaExtensionAPI = { + serverMode: ServerMode; + readonly javaRequirement: RequirementsData; + readonly onDidServerModeChange: Event; + readonly serverReady: () => Promise; +}; + +export enum ServerMode { + STANDARD = "Standard", + LIGHTWEIGHT = "LightWeight", + HYBRID = "Hybrid", +} + +/* eslint-disable @typescript-eslint/naming-convention */ +export interface RequirementsData { + tooling_jre: string; + tooling_jre_version: number; + java_home: string; + java_version: number; +} diff --git a/src/extension.ts b/src/extension.ts index ec83ec0..52818b1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -13,14 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { getRedHatService, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib'; import { RedHatService } from '@redhat-developer/vscode-redhat-telemetry'; -import { CodeAction as VSCodeAction, CodeActionKind, Command as VSCommand, commands, Diagnostic as VSDiagnostic, ExtensionContext, extensions, window, workspace, TextDocument, FileCreateEvent } from 'vscode'; +import { getRedHatService, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib'; +import { CodeActionKind, commands, ExtensionContext, extensions, FileCreateEvent, TextDocument, CodeAction as VSCodeAction, Command as VSCommand, Diagnostic as VSDiagnostic, window, workspace } from 'vscode'; import { CancellationToken, CodeAction, CodeActionResolveRequest, Command, DidChangeConfigurationNotification, DocumentSelector, LanguageClientOptions, RequestType } from 'vscode-languageclient'; import { LanguageClient } from 'vscode-languageclient/node'; import { APPLY_CODE_ACTION_WITH_TELEMETRY } from './definitions/commands'; import * as CommandKind from './definitions/lspCommandKind'; import * as MicroProfileLS from './definitions/microProfileLSRequestNames'; +import { JavaExtensionAPI } from './definitions/vscodeJavaApi'; import { prepareExecutable } from './languageServer/javaServerStarter'; import { collectMicroProfileJavaExtensions, handleExtensionChange, MicroProfileContribution } from './languageServer/plugin'; import { resolveRequirements } from './languageServer/requirements'; @@ -28,13 +29,10 @@ import { registerConfigurationUpdateCommand, registerOpenURICommand } from './ls import { JAVA_EXTENSION_ID, waitForStandardMode } from './util/javaServerMode'; import { sendCodeActionTelemetry } from './util/telemetry'; import { getFilePathsFromWorkspace } from './util/workspaceUtils'; -import { MicroProfilePropertiesChangeEvent, registerYamlSchemaSupport, YamlSchemaCache, getYamlSchemaCache } from './yaml/YamlSchema'; +import { getYamlSchemaCache, MicroProfilePropertiesChangeEvent, registerYamlSchemaSupport, YamlSchemaCache } from './yaml/YamlSchema'; let languageClient: LanguageClient; -// alias for vscode-java's ExtensionAPI -export type JavaExtensionAPI = any; - export async function activate(context: ExtensionContext): Promise { if (await isJavaProject()) { await doActivate(context); @@ -296,7 +294,7 @@ async function connectToLS(context: ExtensionContext, api: JavaExtensionAPI, doc * Returns a json object with key 'microprofile' and a json object value that * holds all microprofile settings. */ - function getVSCodeMicroProfileSettings(): { microprofile: any } { + function getVSCodeMicroProfileSettings(): { microprofile } { const defaultMicroProfileSettings = {}; const configMicroProfile = workspace.getConfiguration().get('microprofile'); const microprofileSettings = configMicroProfile ? configMicroProfile : defaultMicroProfileSettings; diff --git a/src/languageServer/javaServerStarter.ts b/src/languageServer/javaServerStarter.ts index 6445054..f83c138 100644 --- a/src/languageServer/javaServerStarter.ts +++ b/src/languageServer/javaServerStarter.ts @@ -3,7 +3,7 @@ import * as os from 'os'; import * as path from 'path'; import { workspace } from 'vscode'; import { Executable, ExecutableOptions } from 'vscode-languageclient/node'; -import { RequirementsData } from './requirements'; +import { RequirementsData } from '../definitions/vscodeJavaApi'; const DEBUG = startedInDebugMode(); const DEBUG_PORT = 1064; diff --git a/src/languageServer/plugin.ts b/src/languageServer/plugin.ts index 321c850..d85aaf6 100644 --- a/src/languageServer/plugin.ts +++ b/src/languageServer/plugin.ts @@ -1,8 +1,8 @@ -import * as vscode from 'vscode'; import * as path from 'path'; -import * as Commands from '../definitions/commands'; -import { DocumentFilter, DocumentSelector } from 'vscode-languageclient'; import { isDeepStrictEqual } from 'util'; +import * as vscode from 'vscode'; +import { DocumentFilter, DocumentSelector, TextDocumentFilter } from 'vscode-languageclient'; +import * as Commands from '../definitions/commands'; let existingExtensions: MicroProfileContribution[]; @@ -19,7 +19,7 @@ export interface MicroProfileContribution { * * @param extensions array of extensions to search contributions from */ -export function collectMicroProfileJavaExtensions(extensions: readonly vscode.Extension[]): MicroProfileContribution[] { +export function collectMicroProfileJavaExtensions(extensions: readonly vscode.Extension[]): MicroProfileContribution[] { const result: MicroProfileContribution[] = []; if (extensions && extensions.length) { for (const extension of extensions) { @@ -42,7 +42,7 @@ export function collectMicroProfileJavaExtensions(extensions: readonly vscode.Ex return result; } -export function handleExtensionChange(extensions: readonly vscode.Extension[]): void { +export function handleExtensionChange(extensions: readonly vscode.Extension[]): void { if (!existingExtensions) { return; } @@ -78,7 +78,7 @@ export function handleExtensionChange(extensions: readonly vscode.Extension } } -function setJarExtensionsIfExists(obj: MicroProfileContribution, section: any, extensionPath: string): void { +function setJarExtensionsIfExists(obj: MicroProfileContribution, section: { jarExtensions: string[]; }, extensionPath: string): void { if (Array.isArray(section.jarExtensions)) { for (const microprofileJavaExtensionPath of section.jarExtensions) { obj.jarExtensions.push(path.resolve(extensionPath, microprofileJavaExtensionPath)); @@ -86,12 +86,12 @@ function setJarExtensionsIfExists(obj: MicroProfileContribution, section: any, e } } -function setDocumentSelectorIfExists(obj: MicroProfileContribution, section: any): void { - if (!Array.isArray(section.documentSelector)) { +function setDocumentSelectorIfExists(obj: MicroProfileContribution, section: { documentSelector: (string | TextDocumentFilter)[]; }): void { + if (!section.documentSelector || !Array.isArray(section.documentSelector)) { return; } const documentSelector: DocumentSelector = []; - section.documentSelector.forEach((selector: any) => { + section.documentSelector.forEach((selector) => { if (typeof selector === 'string') { documentSelector.push(selector); } else if (selector) { diff --git a/src/languageServer/requirements.ts b/src/languageServer/requirements.ts index 2d19f71..30f989a 100644 --- a/src/languageServer/requirements.ts +++ b/src/languageServer/requirements.ts @@ -6,18 +6,11 @@ import * as path from 'path'; import { Uri, workspace } from 'vscode'; import * as expandHomeDir from 'expand-home-dir'; -import { findRuntimes, IJavaRuntime, getSources } from 'jdk-utils'; -import { JavaExtensionAPI } from '../extension'; +import { IJavaRuntime, findRuntimes, getSources } from 'jdk-utils'; +import { JavaExtensionAPI, RequirementsData } from '../definitions/vscodeJavaApi'; const isWindows = process.platform.indexOf('win') === 0; const JAVA_FILENAME = 'java' + (isWindows ? '.exe' : ''); -export interface RequirementsData { - tooling_jre: string; - tooling_jre_version: number; - java_home: string; - java_version: number; -} - /** * Resolves the requirements needed to run the extension. * Returns a promise that will resolve to a RequirementsData if diff --git a/src/lsp-commands.ts b/src/lsp-commands.ts index 84f1029..b0d5b0f 100644 --- a/src/lsp-commands.ts +++ b/src/lsp-commands.ts @@ -77,7 +77,7 @@ function addToPreferenceArray(key: string, value: T): void { interface ConfigurationItemEdit { section: string; - value: any; + value; editType: ConfigurationItemEditType; } diff --git a/src/test/suite/languageServer/documentSelectorPlugin.test.ts b/src/test/suite/languageServer/documentSelectorPlugin.test.ts index 3ecc1a8..6b61e7e 100644 --- a/src/test/suite/languageServer/documentSelectorPlugin.test.ts +++ b/src/test/suite/languageServer/documentSelectorPlugin.test.ts @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { expect } from "chai"; import * as vscode from "vscode"; +import { DocumentSelector, TextDocumentFilter } from "vscode-languageclient"; import * as plugin from "../../../languageServer/plugin"; -import { expect } from "chai"; import { MicroProfileContribution } from "../../../languageServer/plugin"; -import { TextDocumentFilter, DocumentSelector } from "vscode-languageclient"; /** * This file ensures that DocumentSelectors contributed by other VS Code extensions @@ -167,8 +167,8 @@ describe("Document selector collection from language server plugins", () => { * * @param pluginDocumentSelector array of objects to create a DocumentSelector from. */ - function collectDocumentSelectors(pluginDocumentSelector: any[]): DocumentSelector { - const fakePlugin: vscode.Extension = { + function collectDocumentSelectors(pluginDocumentSelector: unknown[]): DocumentSelector { + const fakePlugin: vscode.Extension = { id: "fake-no-plugin-extension", extensionUri: vscode.Uri.parse("https://example.org"), extensionPath: "", @@ -188,7 +188,6 @@ describe("Document selector collection from language server plugins", () => { const contribution: MicroProfileContribution[] = plugin.collectMicroProfileJavaExtensions([ fakePlugin ]); expect(contribution).to.have.length(1); - const selector: DocumentSelector = contribution[0].documentSelector; return contribution[0].documentSelector; } }); diff --git a/src/util/javaServerMode.ts b/src/util/javaServerMode.ts index 3aae64f..d18d3f3 100644 --- a/src/util/javaServerMode.ts +++ b/src/util/javaServerMode.ts @@ -1,14 +1,8 @@ -import { window, commands } from "vscode"; -import { JavaExtensionAPI } from "../extension"; +import { commands, window } from "vscode"; +import { JavaExtensionAPI, ServerMode } from "../definitions/vscodeJavaApi"; export const JAVA_EXTENSION_ID = "redhat.java"; -export enum ServerMode { - STANDARD = "Standard", - LIGHTWEIGHT = "LightWeight", - HYBRID = "Hybrid", -} - /** * Waits for the java language server to launch in standard mode * Before activating Tools for MicroProfile. diff --git a/src/yaml/YamlSchema.ts b/src/yaml/YamlSchema.ts index 413f156..5adcafd 100644 --- a/src/yaml/YamlSchema.ts +++ b/src/yaml/YamlSchema.ts @@ -13,19 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import * as vscode from 'vscode'; import * as path from 'path'; import * as semver from 'semver'; +import * as vscode from 'vscode'; import { - VSCODE_YAML_EXTENSION_ID, + MICROPROFILE_SCHEMA, + MICROPROFILE_SCHEMA_PREFIX, VSCODE_YAML_DISPLAY_NAME, - VSCODE_YAML_NOT_INSTALLED_MESSAGE, - VSCODE_YAML_LOW_VERSION_MESSAGE, - VSCODE_YAML_NO_REGISTRATION_MESSAGE, + VSCODE_YAML_EXTENSION_ID, VSCODE_YAML_INSTALL_SUCCESS, - MICROPROFILE_SCHEMA, - MICROPROFILE_SCHEMA_PREFIX + VSCODE_YAML_LOW_VERSION_MESSAGE, + VSCODE_YAML_NOT_INSTALLED_MESSAGE, + VSCODE_YAML_NO_REGISTRATION_MESSAGE } from "./YamlConstants"; import { Uri } from 'vscode'; @@ -112,7 +112,7 @@ const yamlSchemaCache = new YamlSchemaCache(); let listener: vscode.Disposable|undefined = undefined; export async function registerYamlSchemaSupport(){ - const yamlPlugin: any = await activateYamlExtension(); + const yamlPlugin = await activateYamlExtension(); if (!yamlPlugin || !yamlPlugin.registerContributor) { // activateYamlExtension has already alerted users about errors. return undefined; @@ -123,7 +123,7 @@ export async function registerYamlSchemaSupport(){ // find redhat.vscode-yaml extension and try to activate it to get the yaml contributor // this function should only be called once when vscode-microprofile activates -async function activateYamlExtension(): Promise<{ registerContributor: YamlSchemaContributor } | undefined> { +async function activateYamlExtension(): Promise<{ registerContributor?: YamlSchemaContributor } | undefined> { const ext = vscode.extensions.getExtension(VSCODE_YAML_EXTENSION_ID); const isApplicationYamlOpened: boolean = isEditorApplicationYaml(vscode.window.activeTextEditor);