|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { RoslynLanguageServer } from '../roslynLanguageServer'; |
| 8 | +import { VSGetProjectContextsRequest, VSProjectContext, VSProjectContextList } from '../roslynProtocol'; |
| 9 | +import { TextDocumentIdentifier } from 'vscode-languageserver-protocol'; |
| 10 | +import { UriConverter } from '../uriConverter'; |
| 11 | +import { LanguageServerEvents } from '../languageServerEvents'; |
| 12 | +import { ServerState } from '../serverStateChange'; |
| 13 | + |
| 14 | +export interface ProjectContextChangeEvent { |
| 15 | + uri: vscode.Uri; |
| 16 | + context: VSProjectContext; |
| 17 | +} |
| 18 | + |
| 19 | +export class ProjectContextService { |
| 20 | + private readonly _contextChangeEmitter = new vscode.EventEmitter<ProjectContextChangeEvent>(); |
| 21 | + private _source = new vscode.CancellationTokenSource(); |
| 22 | + |
| 23 | + constructor(private _languageServer: RoslynLanguageServer, _languageServerEvents: LanguageServerEvents) { |
| 24 | + _languageServerEvents.onServerStateChange((e) => { |
| 25 | + // When the project initialization is complete, open files |
| 26 | + // could move from the miscellaneous workspace context into |
| 27 | + // an open project. |
| 28 | + if (e.state === ServerState.ProjectInitializationComplete) { |
| 29 | + this.refresh(); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + vscode.window.onDidChangeActiveTextEditor(async (_) => this.refresh()); |
| 34 | + } |
| 35 | + |
| 36 | + public get onActiveFileContextChanged(): vscode.Event<ProjectContextChangeEvent> { |
| 37 | + return this._contextChangeEmitter.event; |
| 38 | + } |
| 39 | + |
| 40 | + public async refresh() { |
| 41 | + const textEditor = vscode.window.activeTextEditor; |
| 42 | + if (textEditor?.document?.languageId !== 'csharp') { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const uri = textEditor.document.uri; |
| 47 | + |
| 48 | + // If we have an open request, cancel it. |
| 49 | + this._source.cancel(); |
| 50 | + this._source = new vscode.CancellationTokenSource(); |
| 51 | + |
| 52 | + const contextList = await this.getProjectContexts(uri, this._source.token); |
| 53 | + if (!contextList) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const context = contextList._vs_projectContexts[contextList._vs_defaultIndex]; |
| 58 | + this._contextChangeEmitter.fire({ uri, context }); |
| 59 | + } |
| 60 | + |
| 61 | + private async getProjectContexts( |
| 62 | + uri: vscode.Uri, |
| 63 | + token: vscode.CancellationToken |
| 64 | + ): Promise<VSProjectContextList | undefined> { |
| 65 | + const uriString = UriConverter.serialize(uri); |
| 66 | + const textDocument = TextDocumentIdentifier.create(uriString); |
| 67 | + |
| 68 | + try { |
| 69 | + return this._languageServer.sendRequest( |
| 70 | + VSGetProjectContextsRequest.type, |
| 71 | + { _vs_textDocument: textDocument }, |
| 72 | + token |
| 73 | + ); |
| 74 | + } catch (error) { |
| 75 | + if (error instanceof vscode.CancellationError) { |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + |
| 79 | + throw error; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments