Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workspace status item to the Language Status bar. #7254

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"Failed to run test: {0}": "Failed to run test: {0}",
"Failed to start debugger: {0}": "Failed to start debugger: {0}",
"Test run already in progress": "Test run already in progress",
"Workspace projects": "Workspace projects",
"Your workspace has multiple Visual Studio Solution files; please select one to get full IntelliSense.": "Your workspace has multiple Visual Studio Solution files; please select one to get full IntelliSense.",
"Choose": "Choose",
"Choose and set default": "Choose and set default",
Expand All @@ -158,6 +159,8 @@
"C# configuration has changed. Would you like to reload the window to apply your changes?": "C# configuration has changed. Would you like to reload the window to apply your changes?",
"Nested Code Action": "Nested Code Action",
"Fix All: ": "Fix All: ",
"C# Workspace Status": "C# Workspace Status",
"Open solution": "Open solution",
"Pick a fix all scope": "Pick a fix all scope",
"Fix All Code Action": "Fix All Code Action",
"pipeArgs must be a string or a string array type": "pipeArgs must be a string or a string array type",
Expand Down
6 changes: 3 additions & 3 deletions src/lsptoolshost/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IWorkspaceDebugInformationProvider } from '../shared/IWorkspaceDebugInf
import { RoslynLanguageServer } from './roslynLanguageServer';
import { RoslynWorkspaceDebugInformationProvider } from './roslynWorkspaceDebugConfigurationProvider';
import { PlatformInformation } from '../shared/platform';
import { ServerStateChange } from './serverStateChange';
import { ServerState } from './serverStateChange';
import { DotnetConfigurationResolver } from '../shared/dotnetConfigurationProvider';
import { getCSharpDevKit } from '../utils/getCSharpDevKit';
import { RoslynLanguageServerEvents } from './languageServerEvents';
Expand All @@ -25,8 +25,8 @@ export function registerDebugger(
const workspaceInformationProvider: IWorkspaceDebugInformationProvider =
new RoslynWorkspaceDebugInformationProvider(languageServer, csharpOutputChannel);

const disposable = languageServerEvents.onServerStateChange(async (state) => {
if (state === ServerStateChange.ProjectInitializationComplete) {
const disposable = languageServerEvents.onServerStateChange(async (e) => {
if (e.state === ServerState.ProjectInitializationComplete) {
const csharpDevkitExtension = getCSharpDevKit();
if (!csharpDevkitExtension) {
// Update or add tasks.json and launch.json
Expand Down
8 changes: 4 additions & 4 deletions src/lsptoolshost/languageServerEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { ServerStateChange } from './serverStateChange';
import { ServerStateChangeEvent } from './serverStateChange';
import { IDisposable } from '../disposable';

/**
* Defines events that are fired by the language server.
* These events can be consumed to wait for the server to reach a certain state.
*/
export interface LanguageServerEvents {
readonly onServerStateChange: vscode.Event<ServerStateChange>;
readonly onServerStateChange: vscode.Event<ServerStateChangeEvent>;
}

/**
Expand All @@ -21,9 +21,9 @@ export interface LanguageServerEvents {
* register for events without having to know about the specific current state of the language server.
*/
export class RoslynLanguageServerEvents implements LanguageServerEvents, IDisposable {
public readonly onServerStateChangeEmitter = new vscode.EventEmitter<ServerStateChange>();
public readonly onServerStateChangeEmitter = new vscode.EventEmitter<ServerStateChangeEvent>();

public get onServerStateChange(): vscode.Event<ServerStateChange> {
public get onServerStateChange(): vscode.Event<ServerStateChangeEvent> {
return this.onServerStateChangeEmitter.event;
}

Expand Down
42 changes: 42 additions & 0 deletions src/lsptoolshost/languageStatusBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { RoslynLanguageServer } from './roslynLanguageServer';
import { RoslynLanguageServerEvents } from './languageServerEvents';
import { languageServerOptions } from '../shared/options';
import { ServerState } from './serverStateChange';
import { getCSharpDevKit } from '../utils/getCSharpDevKit';

export function registerLanguageStatusItems(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant for this PR, but something to check when we do TFMs - can two different extensions register language status items for the same language? Devkit does have a language status item (launch.json stuff).

context: vscode.ExtensionContext,
languageServer: RoslynLanguageServer,
languageServerEvents: RoslynLanguageServerEvents
) {
// DevKit will provide an equivalent workspace status item.
if (!getCSharpDevKit()) {
WorkspaceStatus.createStatusItem(context, languageServerEvents);
}
}

class WorkspaceStatus {
static createStatusItem(context: vscode.ExtensionContext, languageServerEvents: RoslynLanguageServerEvents) {
const item = vscode.languages.createLanguageStatusItem(
'csharp.workspaceStatus',
languageServerOptions.documentSelector
);
item.name = vscode.l10n.t('C# Workspace Status');
item.command = {
command: 'dotnet.openSolution',
title: vscode.l10n.t('Open solution'),
};
context.subscriptions.push(item);

languageServerEvents.onServerStateChange((e) => {
item.text = e.workspaceLabel;
item.busy = e.state === ServerState.ProjectInitializationStarted;
});
}
}
39 changes: 31 additions & 8 deletions src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import ShowInformationMessage from '../shared/observers/utils/showInformationMes
import * as RoslynProtocol from './roslynProtocol';
import { CSharpDevKitExports } from '../csharpDevKitExports';
import { SolutionSnapshotId } from './services/ISolutionSnapshotProvider';
import { ServerStateChange } from './serverStateChange';
import { ServerState } from './serverStateChange';
import TelemetryReporter from '@vscode/extension-telemetry';
import CSharpIntelliCodeExports from '../csharpIntelliCodeExports';
import { csharpDevkitExtensionId, csharpDevkitIntelliCodeExtensionId, getCSharpDevKit } from '../utils/getCSharpDevKit';
Expand All @@ -64,6 +64,7 @@ import { registerRestoreCommands } from './restore';
import { BuildDiagnosticsService } from './buildDiagnosticsService';
import { getComponentPaths } from './builtInComponents';
import { OnAutoInsertFeature } from './onAutoInsertFeature';
import { registerLanguageStatusItems } from './languageStatusBar';

let _channel: vscode.OutputChannel;
let _traceChannel: vscode.OutputChannel;
Expand Down Expand Up @@ -115,7 +116,7 @@ export class RoslynLanguageServer {
) {
this.registerSetTrace();
this.registerSendOpenSolution();
this.registerOnProjectInitializationComplete();
this.registerProjectInitialization();
this.registerReportProjectConfiguration();
this.registerExtensionsChanged();
this.registerTelemetryChanged();
Expand Down Expand Up @@ -157,14 +158,20 @@ export class RoslynLanguageServer {
await this.openDefaultSolutionOrProjects();
}
await this.sendOrSubscribeForServiceBrokerConnection();
this._languageServerEvents.onServerStateChangeEmitter.fire(ServerStateChange.Started);
this._languageServerEvents.onServerStateChangeEmitter.fire({
state: ServerState.Started,
workspaceLabel: this.workspaceDisplayName(),
});
}
});
}

private registerOnProjectInitializationComplete() {
private registerProjectInitialization() {
this._languageClient.onNotification(RoslynProtocol.ProjectInitializationCompleteNotification.type, () => {
this._languageServerEvents.onServerStateChangeEmitter.fire(ServerStateChange.ProjectInitializationComplete);
this._languageServerEvents.onServerStateChangeEmitter.fire({
state: ServerState.ProjectInitializationComplete,
workspaceLabel: this.workspaceDisplayName(),
});
});
}

Expand Down Expand Up @@ -262,6 +269,16 @@ export class RoslynLanguageServer {
await this._languageClient.restart();
}

public workspaceDisplayName(): string {
if (this._solutionFile !== undefined) {
JoeRobich marked this conversation as resolved.
Show resolved Hide resolved
return path.basename(this._solutionFile.fsPath);
} else if (this._projectFiles?.length > 0) {
return vscode.l10n.t('Workspace projects');
}

return '';
}

/**
* Returns whether or not the underlying LSP server is running or not.
*/
Expand Down Expand Up @@ -379,16 +396,20 @@ export class RoslynLanguageServer {
await this._languageClient.sendNotification(RoslynProtocol.OpenSolutionNotification.type, {
solution: protocolUri,
});
}

if (this._projectFiles.length > 0) {
} else if (this._projectFiles.length > 0) {
const projectProtocolUris = this._projectFiles.map((uri) =>
this._languageClient.clientOptions.uriConverters!.code2Protocol(uri)
);
await this._languageClient.sendNotification(RoslynProtocol.OpenProjectNotification.type, {
projects: projectProtocolUris,
});
} else {
return;
}
this._languageServerEvents.onServerStateChangeEmitter.fire({
state: ServerState.ProjectInitializationStarted,
workspaceLabel: this.workspaceDisplayName(),
});
}
}

Expand Down Expand Up @@ -979,6 +1000,8 @@ export async function activateRoslynLanguageServer(
languageServerEvents
);

registerLanguageStatusItems(context, languageServer, languageServerEvents);

// Register any commands that need to be handled by the extension.
registerCommands(context, languageServer, hostExecutableResolver, _channel);
registerNestedCodeActionCommands(context, languageServer, _channel);
Expand Down
10 changes: 8 additions & 2 deletions src/lsptoolshost/serverStateChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export enum ServerStateChange {
export enum ServerState {
Started = 0,
ProjectInitializationComplete = 1,
ProjectInitializationStarted = 1,
ProjectInitializationComplete = 2,
}

export interface ServerStateChangeEvent {
state: ServerState;
workspaceLabel: string;
}
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { RazorOmnisharpDownloader } from './razor/razorOmnisharpDownloader';
import { RoslynLanguageServerExport } from './lsptoolshost/roslynLanguageServerExportChannel';
import { registerOmnisharpOptionChanges } from './omnisharp/omnisharpOptionChanges';
import { RoslynLanguageServerEvents } from './lsptoolshost/languageServerEvents';
import { ServerStateChange } from './lsptoolshost/serverStateChange';
import { ServerState } from './lsptoolshost/serverStateChange';
import { SolutionSnapshotProvider } from './lsptoolshost/services/solutionSnapshotProvider';
import { commonOptions, languageServerOptions, omnisharpOptions, razorOptions } from './shared/options';
import { BuildResultDiagnostics } from './lsptoolshost/services/buildResultReporterService';
Expand Down Expand Up @@ -149,8 +149,8 @@ export async function activate(

// Setup a listener for project initialization complete before we start the server.
projectInitializationCompletePromise = new Promise((resolve, _) => {
roslynLanguageServerEvents.onServerStateChange(async (state) => {
if (state === ServerStateChange.ProjectInitializationComplete) {
roslynLanguageServerEvents.onServerStateChange(async (e) => {
if (e.state === ServerState.ProjectInitializationComplete) {
resolve();
}
});
Expand Down
6 changes: 3 additions & 3 deletions test/integrationTests/integrationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as vscode from 'vscode';
import * as path from 'path';
import { CSharpExtensionExports } from '../../src/csharpExtensionExports';
import { existsSync } from 'fs';
import { ServerStateChange } from '../../src/lsptoolshost/serverStateChange';
import { ServerState } from '../../src/lsptoolshost/serverStateChange';
import testAssetWorkspace from './testAssets/testAssetWorkspace';

export async function activateCSharpExtension(): Promise<void> {
Expand Down Expand Up @@ -70,8 +70,8 @@ export async function restartLanguageServer(): Promise<void> {
const csharpExtension = vscode.extensions.getExtension<CSharpExtensionExports>('ms-dotnettools.csharp');
// Register to wait for initialization events and restart the server.
const waitForInitialProjectLoad = new Promise<void>((resolve, _) => {
csharpExtension!.exports.experimental.languageServerEvents.onServerStateChange(async (state) => {
if (state === ServerStateChange.ProjectInitializationComplete) {
csharpExtension!.exports.experimental.languageServerEvents.onServerStateChange(async (e) => {
if (e.state === ServerState.ProjectInitializationComplete) {
resolve();
}
});
Expand Down
Loading