Skip to content

Commit 3561be2

Browse files
authored
fix change project context from command palette (#8930)
2 parents 460f8ad + 631f628 commit 3561be2

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

l10n/bundle.l10n.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
"Text editor must be focused to fix all issues": "Text editor must be focused to fix all issues",
142142
"Fix all issues": "Fix all issues",
143143
"Select fix all action": "Select fix all action",
144+
"No file selected to change project context.": "No file selected to change project context.",
144145
"C# LSP Trace Logs": "C# LSP Trace Logs",
145146
"Open solution": "Open solution",
146147
"Restart server": "Restart server",
@@ -166,7 +167,6 @@
166167
"C# Project Context Status": "C# Project Context Status",
167168
"Active Context": "Active Context",
168169
"Not all language features will be available.": "Not all language features will be available.",
169-
"No file selected to change project context.": "No file selected to change project context.",
170170
"Failed to change context for {0}: {1}": "Failed to change context for {0}: {1}",
171171
"Select project context": "Select project context",
172172
"Initializing dotnet-trace.../dotnet-trace is a command name and should not be localized": {

src/lsptoolshost/commands.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ function registerExtensionCommands(
5252
reporter: TelemetryReporter
5353
) {
5454
context.subscriptions.push(
55-
vscode.commands.registerCommand(changeProjectContextCommandName, async (document, options) => {
56-
reporter.sendTelemetryEvent(TelemetryEventNames.ProjectContextChangeCommand);
57-
await changeProjectContext(languageServer, document, options);
58-
})
55+
vscode.commands.registerCommand(
56+
changeProjectContextCommandName,
57+
async (document: vscode.TextDocument | undefined, options) => {
58+
reporter.sendTelemetryEvent(TelemetryEventNames.ProjectContextChangeCommand);
59+
await changeProjectContext(languageServer, document, options);
60+
}
61+
)
5962
);
6063
context.subscriptions.push(
6164
vscode.commands.registerCommand(changeProjectContextFileExplorer, async (uri) => {

src/lsptoolshost/projectContext/projectContextCommands.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55
import * as vscode from 'vscode';
6+
import { isRelevantDocument } from './projectContextService';
67
import { RoslynLanguageServer } from '../server/roslynLanguageServer';
78
import { VSProjectContext } from '../server/roslynProtocol';
89
import { CancellationToken } from 'vscode-languageclient/node';
@@ -31,9 +32,15 @@ export async function openAndChangeProjectContext(
3132

3233
export async function changeProjectContext(
3334
languageServer: RoslynLanguageServer,
34-
document: vscode.TextDocument,
35+
document: vscode.TextDocument | undefined,
3536
options: ChangeProjectContextOptions | undefined
3637
): Promise<VSProjectContext | undefined> {
38+
document = document ?? vscode.window.activeTextEditor?.document;
39+
if (!isRelevantDocument(document)) {
40+
vscode.window.showErrorMessage(vscode.l10n.t('No file selected to change project context.'));
41+
return;
42+
}
43+
3744
const contextList = await languageServer._projectContextService.queryServerProjectContexts(
3845
document.uri,
3946
CancellationToken.None

src/lsptoolshost/projectContext/projectContextService.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,19 @@ type UriString = string;
3333
const VerificationDelay = 5 * 1000;
3434

3535
let _verifyTimeout: NodeJS.Timeout | undefined;
36-
let _documentUriToVerify: vscode.Uri | undefined;
36+
37+
const _documentSelector = combineDocumentSelectors(
38+
languageServerOptions.documentSelector,
39+
RazorLanguage.documentSelector
40+
);
41+
42+
export function isRelevantDocument(document: vscode.TextDocument | undefined): document is vscode.TextDocument {
43+
if (document === undefined) {
44+
return false;
45+
}
46+
47+
return vscode.languages.match(_documentSelector, document) > 0;
48+
}
3749

3850
export class ProjectContextService {
3951
// This map tracks which project context is active for a given context key. New entries are
@@ -59,11 +71,6 @@ export class ProjectContextService {
5971
_vs_is_miscellaneous: false,
6072
};
6173

62-
private readonly _documentSelector = combineDocumentSelectors(
63-
languageServerOptions.documentSelector,
64-
RazorLanguage.documentSelector
65-
);
66-
6774
constructor(
6875
private _languageServer: RoslynLanguageServer,
6976
_languageClient: RoslynLanguageClient,
@@ -105,7 +112,7 @@ export class ProjectContextService {
105112
contextList: VSProjectContextList,
106113
context: VSProjectContext
107114
): Promise<void> {
108-
if (!this.isRelevantDocument(document)) {
115+
if (!isRelevantDocument(document)) {
109116
return;
110117
}
111118

@@ -127,7 +134,7 @@ export class ProjectContextService {
127134

128135
public async refresh() {
129136
const textEditor = vscode.window.activeTextEditor;
130-
if (!this.isRelevantDocument(textEditor?.document)) {
137+
if (!isRelevantDocument(textEditor?.document)) {
131138
return;
132139
}
133140

@@ -145,10 +152,6 @@ export class ProjectContextService {
145152
_verifyTimeout = undefined;
146153
}
147154

148-
if (_documentUriToVerify) {
149-
_documentUriToVerify = undefined;
150-
}
151-
152155
if (!this._languageServer.isRunning()) {
153156
this._contextChangeEmitter.fire({
154157
document,
@@ -210,14 +213,6 @@ export class ProjectContextService {
210213
}
211214
}
212215

213-
private isRelevantDocument(document: vscode.TextDocument | undefined): boolean {
214-
if (document === undefined) {
215-
return false;
216-
}
217-
218-
return vscode.languages.match(this._documentSelector, document) > 0;
219-
}
220-
221216
private updateCaches(uriString: UriString, contextList: VSProjectContextList) {
222217
const oldContextKey = this._uriToContextKeyMap.get(uriString);
223218
const newContextKey = contextList._vs_key;

0 commit comments

Comments
 (0)