Skip to content

Commit

Permalink
Deduplicate document language test
Browse files Browse the repository at this point in the history
  • Loading branch information
csnover committed Feb 21, 2023
1 parent a014fe8 commit 230c2b5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
37 changes: 11 additions & 26 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,20 +1537,16 @@ export class DefaultClient implements Client {
}

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

const oldVersion: number | undefined = openFileVersions.get(textDocumentChangeEvent.document.uri.toString());
const newVersion: number = textDocumentChangeEvent.document.version;
if (oldVersion === undefined || newVersion > oldVersion) {
openFileVersions.set(textDocumentChangeEvent.document.uri.toString(), newVersion);
}
const oldVersion: number | undefined = openFileVersions.get(textDocumentChangeEvent.document.uri.toString());
const newVersion: number = textDocumentChangeEvent.document.version;
if (oldVersion === undefined || newVersion > oldVersion) {
openFileVersions.set(textDocumentChangeEvent.document.uri.toString(), newVersion);
}
}
}
Expand Down Expand Up @@ -2533,10 +2529,7 @@ export class DefaultClient implements Client {

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

public async handleGenerateDoxygenComment(args: DoxygenCodeActionCommandArguments | vscode.Uri | undefined): Promise<void> {
const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
if (!editor) {
return;
}

if (editor.document.uri.scheme !== "file") {
return;
}

if (!(editor.document.languageId === "c" || editor.document.languageId === "cpp" || editor.document.languageId === "cuda-cpp")) {
if (editor?.document.uri.scheme !== "file" || !util.isCppToolsFile(editor.document)) {
return;
}

Expand Down
13 changes: 4 additions & 9 deletions Extension/src/LanguageServer/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export function onDidChangeActiveTextEditor(editor?: vscode.TextEditor): void {
}

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

function onRestartIntelliSenseForFile(): void {
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
if (!activeEditor || !activeEditor.document || activeEditor.document.uri.scheme !== "file" ||
(activeEditor.document.languageId !== "c" && activeEditor.document.languageId !== "cpp" && activeEditor.document.languageId !== "cuda-cpp")) {
if (activeEditor?.document.uri.scheme !== "file" || !util.isCppToolsFile(activeEditor.document)) {
return;
}
clients.ActiveClient.restartIntelliSenseForFile(activeEditor.document);
}

async function onSwitchHeaderSource(): Promise<void> {
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
if (!activeEditor || !activeEditor.document) {
return;
}

if (activeEditor.document.languageId !== "c" && activeEditor.document.languageId !== "cpp" && activeEditor.document.languageId !== "cuda-cpp") {
if (!activeEditor?.document || !util.isCppToolsFile(activeEditor?.document)) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion Extension/src/LanguageServer/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getCustomConfigProviders, CustomConfigurationProviderCollection, isSame
import * as telemetry from '../telemetry';
import { IExperimentationService } from 'tas-client';
import { CppSettings } from './settings';
import { isCppToolsFile } from '../common';

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

let isCppPropertiesJson: boolean = false;
if (activeEditor.document.languageId === "json" || activeEditor.document.languageId === "jsonc") {
Expand Down
4 changes: 4 additions & 0 deletions Extension/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ export function isCppOrCFile(uri: vscode.Uri | undefined): boolean {
return isCppFile(uri) || isCFile(uri);
}

export function isCppToolsFile(document: vscode.TextDocument): boolean {
return [ "c", "cpp", "cuda-cpp" ].includes(document.languageId);
}

export function isFolderOpen(uri: vscode.Uri): boolean {
const folder: vscode.WorkspaceFolder | undefined = vscode.workspace.getWorkspaceFolder(uri);
return folder ? true : false;
Expand Down

0 comments on commit 230c2b5

Please sign in to comment.