Skip to content

Commit

Permalink
Return the API before importing the projects
Browse files Browse the repository at this point in the history
This also has the side effect that extensions that depend on vscode-java
can start up sooner.

Fixes #2900

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored and rgrunber committed Jan 30, 2023
1 parent 2cdb0ce commit 6765a6e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
18 changes: 10 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,16 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
};

apiManager.initialize(requirements, serverMode);
resolve(apiManager.getApiInstance());
// the promise is resolved
// no need to pass `resolve` into any code past this point,
// since `resolve` is a no-op from now on

if (requireSyntaxServer) {
if (process.env['SYNTAXLS_CLIENT_PORT']) {
syntaxClient.initialize(requirements, clientOptions, resolve);
syntaxClient.initialize(requirements, clientOptions);
} else {
syntaxClient.initialize(requirements, clientOptions, resolve, prepareExecutable(requirements, syntaxServerWorkspacePath, getJavaConfig(requirements.java_home), context, true));
syntaxClient.initialize(requirements, clientOptions, prepareExecutable(requirements, syntaxServerWorkspacePath, getJavaConfig(requirements.java_home), context, true));
}
syntaxClient.start();
serverStatusBarProvider.showLightWeightStatus();
Expand Down Expand Up @@ -347,7 +351,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
}

if (choice === "Yes") {
await startStandardServer(context, requirements, clientOptions, workspacePath, resolve);
await startStandardServer(context, requirements, clientOptions, workspacePath);
}
});

Expand All @@ -372,18 +376,16 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
const importOnStartup = config.get(importOnStartupSection);
if (importOnStartup === "disabled" ||
env.uiKind === UIKind.Web && env.appName.includes("Visual Studio Code")) {
syntaxClient.resolveApi(resolve);
requireStandardServer = false;
} else if (importOnStartup === "interactive" && await workspaceContainsBuildFiles()) {
syntaxClient.resolveApi(resolve);
requireStandardServer = await promptUserForStandardServer(config);
} else {
requireStandardServer = true;
}
}

if (requireStandardServer) {
await startStandardServer(context, requirements, clientOptions, workspacePath, resolve);
await startStandardServer(context, requirements, clientOptions, workspacePath);
}

const onDidGrantWorkspaceTrust = (workspace as any).onDidGrantWorkspaceTrust;
Expand Down Expand Up @@ -411,7 +413,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
});
}

async function startStandardServer(context: ExtensionContext, requirements: requirements.RequirementsData, clientOptions: LanguageClientOptions, workspacePath: string, resolve: (value?: ExtensionAPI | PromiseLike<ExtensionAPI>) => void) {
async function startStandardServer(context: ExtensionContext, requirements: requirements.RequirementsData, clientOptions: LanguageClientOptions, workspacePath: string) {
if (standardClient.getClientStatus() !== ClientStatus.uninitialized) {
return;
}
Expand All @@ -426,7 +428,7 @@ async function startStandardServer(context: ExtensionContext, requirements: requ
apiManager.getApiInstance().serverMode = ServerMode.hybrid;
apiManager.fireDidServerModeChange(ServerMode.hybrid);
}
await standardClient.initialize(context, requirements, clientOptions, workspacePath, jdtEventEmitter, resolve);
await standardClient.initialize(context, requirements, clientOptions, workspacePath, jdtEventEmitter);
standardClient.start();
serverStatusBarProvider.showStandardStatus();
}
Expand Down
6 changes: 2 additions & 4 deletions src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { apiManager } from "./apiManager";
import * as buildPath from './buildpath';
import { javaRefactorKinds, RefactorDocumentProvider } from "./codeActionProvider";
import { Commands } from "./commands";
import { ClientStatus, ExtensionAPI } from "./extension.api";
import { ClientStatus } from "./extension.api";
import * as fileEventHandler from './fileEventHandler';
import { gradleCodeActionMetadata, GradleCodeActionProvider } from "./gradle/gradleCodeActionProvider";
import { JavaInlayHintsProvider } from "./inlayHintsProvider";
Expand Down Expand Up @@ -56,7 +56,7 @@ export class StandardLanguageClient {
private languageClient: LanguageClient;
private status: ClientStatus = ClientStatus.uninitialized;

public async initialize(context: ExtensionContext, requirements: RequirementsData, clientOptions: LanguageClientOptions, workspacePath: string, jdtEventEmitter: EventEmitter<Uri>, resolve: (value: ExtensionAPI) => void): Promise<void> {
public async initialize(context: ExtensionContext, requirements: RequirementsData, clientOptions: LanguageClientOptions, workspacePath: string, jdtEventEmitter: EventEmitter<Uri>): Promise<void> {
if (this.status !== ClientStatus.uninitialized) {
return;
}
Expand Down Expand Up @@ -139,13 +139,11 @@ export class StandardLanguageClient {
serverStatus.updateServerStatus(ServerStatusKind.ready);
commands.executeCommand('setContext', 'javaLSReady', true);
apiManager.updateStatus(ClientStatus.started);
resolve(apiManager.getApiInstance());
break;
case 'Error':
this.status = ClientStatus.error;
serverStatus.updateServerStatus(ServerStatusKind.error);
apiManager.updateStatus(ClientStatus.error);
resolve(apiManager.getApiInstance());
break;
case 'ProjectStatus':
if (report.message === "WARNING") {
Expand Down
18 changes: 4 additions & 14 deletions src/syntaxLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { DidChangeConfigurationNotification, LanguageClientOptions } from "vscod
import { LanguageClient, ServerOptions, StreamInfo } from "vscode-languageclient/node";
import { apiManager } from "./apiManager";
import { ClientErrorHandler } from "./clientErrorHandler";
import { ClientStatus, ExtensionAPI } from "./extension.api";
import { ClientStatus } from "./extension.api";
import { logger } from "./log";
import { OutputInfoCollector } from "./outputInfoCollector";
import { StatusNotification } from "./protocol";
import { RequirementsData } from "./requirements";
import { ServerMode } from "./settings";
import { snippetCompletionProvider } from "./snippetCompletionProvider";
import { getJavaConfig } from "./utils";
Expand All @@ -19,7 +20,7 @@ export class SyntaxLanguageClient {
private languageClient: LanguageClient;
private status: ClientStatus = ClientStatus.uninitialized;

public initialize(requirements, clientOptions: LanguageClientOptions, resolve: (value: ExtensionAPI) => void, serverOptions?: ServerOptions) {
public initialize(requirements: RequirementsData, clientOptions: LanguageClientOptions, serverOptions?: ServerOptions) {
const newClientOptions: LanguageClientOptions = Object.assign({}, clientOptions, {
middleware: {
workspace: {
Expand Down Expand Up @@ -75,7 +76,7 @@ export class SyntaxLanguageClient {
break;
}
if (apiManager.getApiInstance().serverMode === ServerMode.lightWeight) {
this.resolveApiOnReady(resolve);
apiManager.fireDidServerModeChange(ServerMode.lightWeight);
}
});
});
Expand Down Expand Up @@ -111,15 +112,4 @@ export class SyntaxLanguageClient {
return this.languageClient;
}

public resolveApi(resolve: (value: ExtensionAPI) => void): void {
apiManager.getApiInstance().serverMode = ServerMode.lightWeight;
apiManager.fireDidServerModeChange(ServerMode.lightWeight);
this.resolveApiOnReady(resolve);
}

private resolveApiOnReady(resolve: (value: ExtensionAPI) => void): void {
if ([ClientStatus.started, ClientStatus.error].includes(this.status)) {
resolve(apiManager.getApiInstance());
}
}
}
10 changes: 5 additions & 5 deletions test/lightweight-mode-suite/publicApi.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

import * as assert from 'assert';
import * as fse from 'fs-extra';
import * as path from 'path';
import { ExtensionAPI, extensionApiVersion, ClasspathResult } from '../../src/extension.api';
import { Uri, DocumentSymbol, extensions, commands } from 'vscode';
import { commands, DocumentSymbol, extensions, Uri } from 'vscode';
import { Commands } from '../../src/commands';
import { ClasspathResult, ExtensionAPI, extensionApiVersion } from '../../src/extension.api';
import { ServerMode } from '../../src/settings';
import * as fse from 'fs-extra';
import { getJavaConfiguration } from '../../src/utils';
import { Commands } from '../../src/commands';
import { constants } from '../common';

const pomPath: string = path.join(constants.projectFsPath, 'pom.xml');
Expand All @@ -31,7 +31,7 @@ suite('Public APIs - LightWeight', () => {

test('status should be correct', async function () {
const api: ExtensionAPI = extensions.getExtension('redhat.java').exports;
assert.equal(api.status, 'Started');
assert.equal(api.status, 'Starting');
});

test('registerHoverCommand should work', async function () {
Expand Down
6 changes: 4 additions & 2 deletions test/standard-mode-suite/gotoSuperImplementation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import * as assert from 'assert';
import * as path from 'path';
import { Uri, extensions, commands, TextDocument, workspace, window, Selection, Position } from 'vscode';
import { commands, extensions, Position, Selection, TextDocument, Uri, window, workspace } from 'vscode';
import { Commands } from '../../src/commands';
import { ExtensionAPI } from '../../src/extension.api';

const projectFsPath: string = path.join(__dirname, '..', '..', '..', 'test', 'resources', 'projects', 'maven', 'salut');
const fileFsPath: string = path.join(projectFsPath, 'src', 'main', 'java', 'java', 'Foo3.java');

suite('Goto Super Implementation', () => {

suiteSetup(async function() {
await extensions.getExtension('redhat.java').activate();
const api: ExtensionAPI = await extensions.getExtension('redhat.java').activate();
await api.serverReady();
});

test('go to super implementation should work', async function () {
Expand Down
11 changes: 6 additions & 5 deletions test/standard-mode-suite/publicApi.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

import * as assert from 'assert';
import * as fse from 'fs-extra';
import * as path from 'path';
import { ExtensionAPI, extensionApiVersion, ClasspathResult } from '../../src/extension.api';
import { Uri, DocumentSymbol, extensions, commands } from 'vscode';
import { env } from 'process';
import { commands, DocumentSymbol, extensions, Uri } from 'vscode';
import { Commands } from '../../src/commands';
import { ClasspathResult, ExtensionAPI, extensionApiVersion } from '../../src/extension.api';
import { ServerMode } from '../../src/settings';
import * as fse from 'fs-extra';
import { getJavaConfiguration } from '../../src/utils';
import { Commands } from '../../src/commands';
import { constants } from '../common';
import { env } from 'process';

const pomPath: string = path.join(constants.projectFsPath, 'pom.xml');
const gradleTestFolder: string = path.join(constants.projectFsPath, 'testGradle');
Expand All @@ -34,6 +34,7 @@ suite('Public APIs - Standard', () => {

test('status should be correct', async function () {
const api: ExtensionAPI = extensions.getExtension('redhat.java').exports;
await api.serverReady();
assert.equal(api.status, 'Started');
});

Expand Down

0 comments on commit 6765a6e

Please sign in to comment.