Skip to content

Commit 230c2b5

Browse files
committed
Deduplicate document language test
1 parent a014fe8 commit 230c2b5

File tree

4 files changed

+21
-36
lines changed

4 files changed

+21
-36
lines changed

Extension/src/LanguageServer/client.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,20 +1537,16 @@ export class DefaultClient implements Client {
15371537
}
15381538

15391539
public onDidChangeTextDocument(textDocumentChangeEvent: vscode.TextDocumentChangeEvent): void {
1540-
if (textDocumentChangeEvent.document.uri.scheme === "file") {
1541-
if (textDocumentChangeEvent.document.languageId === "c"
1542-
|| textDocumentChangeEvent.document.languageId === "cpp"
1543-
|| textDocumentChangeEvent.document.languageId === "cuda-cpp") {
1544-
// If any file has changed, we need to abort the current rename operation
1545-
if (DefaultClient.renamePending) {
1546-
this.cancelReferences();
1547-
}
1540+
if (textDocumentChangeEvent.document.uri.scheme === "file" && util.isCppToolsFile(textDocumentChangeEvent.document)) {
1541+
// If any file has changed, we need to abort the current rename operation
1542+
if (DefaultClient.renamePending) {
1543+
this.cancelReferences();
1544+
}
15481545

1549-
const oldVersion: number | undefined = openFileVersions.get(textDocumentChangeEvent.document.uri.toString());
1550-
const newVersion: number = textDocumentChangeEvent.document.version;
1551-
if (oldVersion === undefined || newVersion > oldVersion) {
1552-
openFileVersions.set(textDocumentChangeEvent.document.uri.toString(), newVersion);
1553-
}
1546+
const oldVersion: number | undefined = openFileVersions.get(textDocumentChangeEvent.document.uri.toString());
1547+
const newVersion: number = textDocumentChangeEvent.document.version;
1548+
if (oldVersion === undefined || newVersion > oldVersion) {
1549+
openFileVersions.set(textDocumentChangeEvent.document.uri.toString(), newVersion);
15541550
}
15551551
}
15561552
}
@@ -2533,10 +2529,7 @@ export class DefaultClient implements Client {
25332529

25342530
private updateActiveDocumentTextOptions(): void {
25352531
const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
2536-
if (editor?.document?.uri.scheme === "file"
2537-
&& (editor.document.languageId === "c"
2538-
|| editor.document.languageId === "cpp"
2539-
|| editor.document.languageId === "cuda-cpp")) {
2532+
if (editor?.document?.uri.scheme === "file" && util.isCppToolsFile(editor.document)) {
25402533
vscode.commands.executeCommand('setContext', 'BuildAndDebug.isSourceFile', util.isCppOrCFile(editor.document.uri));
25412534
vscode.commands.executeCommand('setContext', 'BuildAndDebug.isFolderOpen', util.isFolderOpen(editor.document.uri));
25422535
// If using vcFormat, check for a ".editorconfig" file, and apply those text options to the active document.
@@ -3050,15 +3043,7 @@ export class DefaultClient implements Client {
30503043

30513044
public async handleGenerateDoxygenComment(args: DoxygenCodeActionCommandArguments | vscode.Uri | undefined): Promise<void> {
30523045
const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
3053-
if (!editor) {
3054-
return;
3055-
}
3056-
3057-
if (editor.document.uri.scheme !== "file") {
3058-
return;
3059-
}
3060-
3061-
if (!(editor.document.languageId === "c" || editor.document.languageId === "cpp" || editor.document.languageId === "cuda-cpp")) {
3046+
if (editor?.document.uri.scheme !== "file" || !util.isCppToolsFile(editor.document)) {
30623047
return;
30633048
}
30643049

Extension/src/LanguageServer/extension.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export function onDidChangeActiveTextEditor(editor?: vscode.TextEditor): void {
297297
}
298298

299299
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
300-
if (!editor || !activeEditor || activeEditor.document.uri.scheme !== "file" || (activeEditor.document.languageId !== "c" && activeEditor.document.languageId !== "cpp" && activeEditor.document.languageId !== "cuda-cpp")) {
300+
if (!editor || activeEditor?.document.uri.scheme !== "file" || !util.isCppToolsFile(activeEditor.document)) {
301301
activeDocument = "";
302302
} else {
303303
activeDocument = editor.document.uri.toString();
@@ -354,7 +354,7 @@ export async function processDelayedDidOpen(document: vscode.TextDocument): Prom
354354
function onDidChangeVisibleTextEditors(editors: readonly vscode.TextEditor[]): void {
355355
// Process delayed didOpen for any visible editors we haven't seen before
356356
editors.forEach(async (editor) => {
357-
if ((editor.document.uri.scheme === "file") && (editor.document.languageId === "c" || editor.document.languageId === "cpp" || editor.document.languageId === "cuda-cpp")) {
357+
if (editor.document.uri.scheme === "file" && util.isCppToolsFile(editor.document)) {
358358
const client: Client = clients.getClientFor(editor.document.uri);
359359
await client.requestWhenReady(() => processDelayedDidOpen(editor.document));
360360
client.onDidChangeVisibleTextEditor(editor);
@@ -508,20 +508,15 @@ function onDisabledCommand(): void {
508508

509509
function onRestartIntelliSenseForFile(): void {
510510
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
511-
if (!activeEditor || !activeEditor.document || activeEditor.document.uri.scheme !== "file" ||
512-
(activeEditor.document.languageId !== "c" && activeEditor.document.languageId !== "cpp" && activeEditor.document.languageId !== "cuda-cpp")) {
511+
if (activeEditor?.document.uri.scheme !== "file" || !util.isCppToolsFile(activeEditor.document)) {
513512
return;
514513
}
515514
clients.ActiveClient.restartIntelliSenseForFile(activeEditor.document);
516515
}
517516

518517
async function onSwitchHeaderSource(): Promise<void> {
519518
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
520-
if (!activeEditor || !activeEditor.document) {
521-
return;
522-
}
523-
524-
if (activeEditor.document.languageId !== "c" && activeEditor.document.languageId !== "cpp" && activeEditor.document.languageId !== "cuda-cpp") {
519+
if (!activeEditor?.document || !util.isCppToolsFile(activeEditor?.document)) {
525520
return;
526521
}
527522

Extension/src/LanguageServer/ui.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getCustomConfigProviders, CustomConfigurationProviderCollection, isSame
1313
import * as telemetry from '../telemetry';
1414
import { IExperimentationService } from 'tas-client';
1515
import { CppSettings } from './settings';
16+
import { isCppToolsFile } from '../common';
1617

1718
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
1819
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
@@ -283,7 +284,7 @@ export class OldUI implements UI {
283284
if (!activeEditor) {
284285
this.ShowConfiguration = false;
285286
} else {
286-
const isCpp: boolean = (activeEditor.document.uri.scheme === "file" && (activeEditor.document.languageId === "c" || activeEditor.document.languageId === "cpp" || activeEditor.document.languageId === "cuda-cpp"));
287+
const isCpp: boolean = (activeEditor.document.uri.scheme === "file" && isCppToolsFile(activeEditor.document));
287288

288289
let isCppPropertiesJson: boolean = false;
289290
if (activeEditor.document.languageId === "json" || activeEditor.document.languageId === "jsonc") {

Extension/src/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ export function isCppOrCFile(uri: vscode.Uri | undefined): boolean {
180180
return isCppFile(uri) || isCFile(uri);
181181
}
182182

183+
export function isCppToolsFile(document: vscode.TextDocument): boolean {
184+
return [ "c", "cpp", "cuda-cpp" ].includes(document.languageId);
185+
}
186+
183187
export function isFolderOpen(uri: vscode.Uri): boolean {
184188
const folder: vscode.WorkspaceFolder | undefined = vscode.workspace.getWorkspaceFolder(uri);
185189
return folder ? true : false;

0 commit comments

Comments
 (0)