Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Extension/src/Debugger/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import { SshTargetsProvider, getActiveSshTarget, initializeSshTargets, selectSsh
import { TargetLeafNode, setActiveSshTarget } from '../SSH/TargetsView/targetNodes';
import { sshCommandToConfig } from '../SSH/sshCommandToConfig';
import { getSshConfiguration, getSshConfigurationFiles, parseFailures, writeSshConfiguration } from '../SSH/sshHosts';
import { documentSelector, pathAccessible } from '../common';
import { documentSelector, isCpp, isCppOrCFile, pathAccessible } from '../common';
import { instrument } from '../instrumentation';
import { getSshChannel } from '../logger';
import { SessionState } from '../sessionState';
import { AttachItemsProvider, AttachPicker, RemoteAttachPicker } from './attachToProcess';
import { ConfigurationAssetProviderFactory, ConfigurationSnippetProvider, DebugConfigurationProvider, IConfigurationAssetProvider } from './configurationProvider';
import { DebuggerType } from './configurations';
Expand Down Expand Up @@ -129,6 +130,28 @@ export async function initialize(context: vscode.ExtensionContext): Promise<void
}
}
}));

// Track active editor changes so "Run and Debug" button session state is kept up to date
// even if the language server (IntelliSense) is disabled.
updateBuildAndDebugSessionState(vscode.window.activeTextEditor);
disposables.push(vscode.window.onDidChangeActiveTextEditor(editor => updateBuildAndDebugSessionState(editor)));
disposables.push(vscode.workspace.onDidOpenTextDocument(document => {
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
if (activeEditor?.document.uri.toString() === document.uri.toString()) {
updateBuildAndDebugSessionState(activeEditor);
}
}));
disposables.push(vscode.workspace.onDidChangeWorkspaceFolders(() => updateBuildAndDebugSessionState(vscode.window.activeTextEditor)));
}

export function updateBuildAndDebugSessionState(editor?: vscode.TextEditor): void {
if (editor && isCpp(editor.document)) {
void SessionState.buildAndDebugIsFolderOpen.set(vscode.workspace.getWorkspaceFolder(editor.document.uri) !== undefined);
void SessionState.buildAndDebugIsSourceFile.set(isCppOrCFile(editor.document.uri, editor.document.languageId));
} else {
void SessionState.buildAndDebugIsFolderOpen.set(vscode.workspace.workspaceFolders !== undefined);
void SessionState.buildAndDebugIsSourceFile.set(false);
}
}

export function dispose(): void {
Expand Down

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨Copilot (agent146): [Minor] Please add focused regression coverage for the event-driven behavior rather than testing only the exported helper. These four tests call updateBuildAndDebugSessionState directly, so they would still pass if any of the new onDidChangeActiveTextEditor, onDidOpenTextDocument, or onDidChangeWorkspaceFolders registrations were absent. They also never use a C++ URI outside the open workspace, so they would have passed the prior global-folder implementation as well. At minimum, add an external C++ source case that asserts isSourceFile === true and isFolderOpen === false, plus one listener-driven transition (ideally an active-document language change while IntelliSense is disabled) to cover the behavior this PR fixes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prashant Kumar Rai (@8prashant) FYI, my agents can add the regression coverage for you in a follow up PR if you do not want to....however, I don't think my GitHub agents have those instructions so I might have to directly inform them.

Also, I've been really busy -- I'll try to review your PRs as soon as I can.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as assert from 'assert';
import { suite, test } from 'mocha';
import * as vscode from 'vscode';
import { updateBuildAndDebugSessionState } from '../../../../src/Debugger/extension';
import { SessionState } from '../../../../src/sessionState';

suite("BuildAndDebug SessionState Tests", () => {
test("updateBuildAndDebugSessionState sets false when editor is undefined", () => {
updateBuildAndDebugSessionState(undefined);
assert.strictEqual(SessionState.buildAndDebugIsSourceFile.get(), false);
assert.strictEqual(SessionState.buildAndDebugIsFolderOpen.get(), true);
});

test("updateBuildAndDebugSessionState sets false for non-C/C++ editor", () => {
const mockEditor: any = {
document: {
uri: vscode.Uri.file("test.txt"),
languageId: "plaintext"
}
};
updateBuildAndDebugSessionState(mockEditor);
assert.strictEqual(SessionState.buildAndDebugIsSourceFile.get(), false);
assert.strictEqual(SessionState.buildAndDebugIsFolderOpen.get(), true);
});

test("updateBuildAndDebugSessionState sets true for C/C++ source file", () => {
const workspaceFolder = vscode.workspace.workspaceFolders?.[0] ?? assert.fail("No workspace folder available");
const mockEditor: any = {
document: {
uri: vscode.Uri.joinPath(workspaceFolder.uri, "main.cpp"),
languageId: "cpp"
}
};
updateBuildAndDebugSessionState(mockEditor);
assert.strictEqual(SessionState.buildAndDebugIsSourceFile.get(), true);
assert.strictEqual(SessionState.buildAndDebugIsFolderOpen.get(), true);
});

test("updateBuildAndDebugSessionState sets false for C/C++ header file", () => {
const workspaceFolder = vscode.workspace.workspaceFolders?.[0] ?? assert.fail("No workspace folder available");
const mockEditor: any = {
document: {
uri: vscode.Uri.joinPath(workspaceFolder.uri, "header.h"),
languageId: "cpp"
}
};
updateBuildAndDebugSessionState(mockEditor);
assert.strictEqual(SessionState.buildAndDebugIsSourceFile.get(), false);
assert.strictEqual(SessionState.buildAndDebugIsFolderOpen.get(), true);
});
});
Loading