Skip to content

Commit

Permalink
Tell the language server to open projects if no solution exists
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmalinowski committed Aug 7, 2023
1 parent cb5f4d3 commit 0e43179
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/lsptoolshost/openProjectsParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { DocumentUri } from 'vscode-languageclient/node';

export class OpenProjectParams {
constructor(public readonly projects: DocumentUri[]) {}
}
57 changes: 42 additions & 15 deletions src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import EventEmitter = require('events');
import Disposable from '../disposable';
import * as RoslynProtocol from './roslynProtocol';
import { OpenSolutionParams } from './openSolutionParams';
import { OpenProjectParams } from './openProjectsParams';
import { CSharpDevKitExports } from '../csharpDevKitExports';
import { ISolutionSnapshotProvider, SolutionSnapshotId } from './services/ISolutionSnapshotProvider';
import { Options } from '../shared/options';
Expand Down Expand Up @@ -114,6 +115,9 @@ export class RoslynLanguageServer {
*/
private _solutionFile: vscode.Uri | undefined;

/** The project files previously opened; we hold onto this for the same reason as _solutionFile. */
private _projectFiles: vscode.Uri[] = new Array<vscode.Uri>();

constructor(
private platformInfo: PlatformInformation,
private hostExecutableResolver: IHostExecutableResolver,
Expand Down Expand Up @@ -181,10 +185,10 @@ export class RoslynLanguageServer {
this._languageClient.onDidChangeState(async (state) => {
if (state.newState === State.Running) {
await this._languageClient!.setTrace(languageClientTraceLevel);
if (this._solutionFile) {
await this.sendOpenSolutionNotification();
if (this._solutionFile || this._projectFiles.length > 0) {
await this.sendOpenSolutionAndProjectsNotifications();
} else {
await this.openDefaultSolution();
await this.openDefaultSolutionOrProjects();
}
await this.sendOrSubscribeForServiceBrokerConnection();
this._eventBus.emit(RoslynLanguageServer.serverStateChangeEvent, ServerStateChange.Started);
Expand Down Expand Up @@ -320,21 +324,33 @@ export class RoslynLanguageServer {

public async openSolution(solutionFile: vscode.Uri): Promise<void> {
this._solutionFile = solutionFile;
await this.sendOpenSolutionNotification();
this._projectFiles = new Array<vscode.Uri>();
await this.sendOpenSolutionAndProjectsNotifications();
}

private async sendOpenSolutionNotification(): Promise<void> {
if (
this._solutionFile !== undefined &&
this._languageClient !== undefined &&
this._languageClient.isRunning()
) {
const protocolUri = this._languageClient.clientOptions.uriConverters!.code2Protocol(this._solutionFile);
await this._languageClient.sendNotification('solution/open', new OpenSolutionParams(protocolUri));
public async openProjects(projectFiles: vscode.Uri[]): Promise<void> {
this._solutionFile = undefined;
this._projectFiles = projectFiles;
await this.sendOpenSolutionAndProjectsNotifications();
}

private async sendOpenSolutionAndProjectsNotifications(): Promise<void> {
if (this._languageClient !== undefined && this._languageClient.isRunning()) {
if (this._solutionFile !== undefined) {
const protocolUri = this._languageClient.clientOptions.uriConverters!.code2Protocol(this._solutionFile);
await this._languageClient.sendNotification('solution/open', new OpenSolutionParams(protocolUri));
}

if (this._projectFiles.length > 0) {
const projectProtocolUris = this._projectFiles.map((uri) =>
this._languageClient!.clientOptions.uriConverters!.code2Protocol(uri)
);
await this._languageClient.sendNotification('project/open', new OpenProjectParams(projectProtocolUris));
}
}
}

private async openDefaultSolution(): Promise<void> {
private async openDefaultSolutionOrProjects(): Promise<void> {
const options = this.optionProvider.GetLatestOptions();

// If Dev Kit isn't installed, then we are responsible for picking the solution to open, assuming the user hasn't explicitly
Expand All @@ -349,8 +365,19 @@ export class RoslynLanguageServer {
} else {
// Auto open if there is just one solution target; if there's more the one we'll just let the user pick with the picker.
const solutionUris = await vscode.workspace.findFiles('**/*.sln', '**/node_modules/**', 2);
if (solutionUris && solutionUris.length === 1) {
this.openSolution(solutionUris[0]);
if (solutionUris) {
if (solutionUris.length === 1) {
this.openSolution(solutionUris[0]);
} else if (solutionUris.length === 0) {
// We have no solutions, so we'll enumerate what project files we have and just use those.
const projectUris = await vscode.workspace.findFiles(
'**/*.csproj',
'**/node_modules/**',
options.omnisharpOptions.maxProjectResults
);

this.openProjects(projectUris);
}
}
}
}
Expand Down

0 comments on commit 0e43179

Please sign in to comment.