Skip to content

Account for editor.multiCursorModifier setting in Native REPL terminal link #25156

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
27 changes: 20 additions & 7 deletions src/client/terminals/pythonStartupLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,38 @@ import {
} from 'vscode';
import { executeCommand } from '../common/vscodeApis/commandApis';
import { registerTerminalLinkProvider } from '../common/vscodeApis/windowApis';
import { getConfiguration } from '../common/vscodeApis/workspaceApis';
import { Repl } from '../common/utils/localize';

interface CustomTerminalLink extends TerminalLink {
command: string;
}

/**
* Gets the appropriate modifier key text for the Native REPL link based on the
* editor.multiCursorModifier setting and platform.
*/
function getModifierKeyText(): string {
const editorConfig = getConfiguration('editor');
const multiCursorModifier = editorConfig.get<string>('multiCursorModifier', 'alt');

if (multiCursorModifier === 'ctrlCmd') {
// When multiCursorModifier is ctrlCmd, links use Alt/Option
return process.platform === 'darwin' ? 'Option' : 'Alt';
} else {
// Default behavior: multiCursorModifier is alt, links use Ctrl/Cmd
return process.platform === 'darwin' ? 'Cmd' : 'Ctrl';
}
}

export class CustomTerminalLinkProvider implements TerminalLinkProvider<CustomTerminalLink> {
provideTerminalLinks(
context: TerminalLinkContext,
_token: CancellationToken,
): ProviderResult<CustomTerminalLink[]> {
const links: CustomTerminalLink[] = [];
let expectedNativeLink;

if (process.platform === 'darwin') {
expectedNativeLink = 'Cmd click to launch VS Code Native REPL';
} else {
expectedNativeLink = 'Ctrl click to launch VS Code Native REPL';
}
const modifierKey = getModifierKeyText();
const expectedNativeLink = `${modifierKey} click to launch VS Code Native REPL`;

if (context.line.includes(expectedNativeLink)) {
links.push({
Expand Down
175 changes: 175 additions & 0 deletions src/test/terminals/shellIntegration/pythonStartup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ suite('Terminal - Shell Integration with PYTHONSTARTUP', () => {

pythonConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
editorConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();

// Set up default behavior for editor config
editorConfig.setup((p) => p.get('multiCursorModifier', 'alt')).returns(() => 'alt');

getConfigurationStub.callsFake((section: string) => {
if (section === 'python') {
return pythonConfig.object;
Expand All @@ -63,6 +67,9 @@ suite('Terminal - Shell Integration with PYTHONSTARTUP', () => {

teardown(() => {
sinon.restore();
// Reset editorConfig mock to default behavior for next test
editorConfig.reset();
editorConfig.setup((p) => p.get('multiCursorModifier', 'alt')).returns(() => 'alt');
});

test('Verify createDirectory is called when shell integration is enabled', async () => {
Expand Down Expand Up @@ -200,6 +207,73 @@ suite('Terminal - Shell Integration with PYTHONSTARTUP', () => {
);
}
});

test('Mac - Verify provideTerminalLinks returns links with Option modifier when multiCursorModifier is ctrlCmd', () => {
// Mock the editor configuration to return ctrlCmd
editorConfig.reset();
editorConfig.setup((p) => p.get('multiCursorModifier', 'alt')).returns(() => 'ctrlCmd');

const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
line: 'Some random string with Option click to launch VS Code Native REPL',
terminal: {} as Terminal,
};
const token: CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: new EventEmitter<unknown>().event,
};

const links = provider.provideTerminalLinks(context, token);

assert.isNotNull(links, 'Expected links to be not undefined');
assert.isArray(links, 'Expected links to be an array');
assert.isNotEmpty(links, 'Expected links to be not empty');

if (Array.isArray(links)) {
assert.equal(
links[0].startIndex,
context.line.indexOf('Option click to launch VS Code Native REPL'),
'start index should match',
);
assert.equal(
links[0].length,
'Option click to launch VS Code Native REPL'.length,
'Match expected length',
);
}
});

test('Mac - Verify provideTerminalLinks returns links with Cmd modifier when multiCursorModifier is default (alt)', () => {
// This test verifies the default behavior when multiCursorModifier is 'alt'
const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
line: 'Some random string with Cmd click to launch VS Code Native REPL',
terminal: {} as Terminal,
};
const token: CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: new EventEmitter<unknown>().event,
};

const links = provider.provideTerminalLinks(context, token);

assert.isNotNull(links, 'Expected links to be not undefined');
assert.isArray(links, 'Expected links to be an array');
assert.isNotEmpty(links, 'Expected links to be not empty');

if (Array.isArray(links)) {
assert.equal(
links[0].startIndex,
context.line.indexOf('Cmd click to launch VS Code Native REPL'),
'start index should match',
);
assert.equal(
links[0].length,
'Cmd click to launch VS Code Native REPL'.length,
'Match expected length',
);
}
});
}
if (process.platform !== 'darwin') {
test('Windows/Linux - Verify provideTerminalLinks returns links when context.line contains expectedNativeLink', () => {
Expand Down Expand Up @@ -242,8 +316,109 @@ suite('Terminal - Shell Integration with PYTHONSTARTUP', () => {
);
}
});

test('Windows/Linux - Verify provideTerminalLinks returns links with Alt modifier when multiCursorModifier is ctrlCmd', () => {
// Mock the editor configuration to return ctrlCmd
editorConfig.reset();
editorConfig.setup((p) => p.get('multiCursorModifier', 'alt')).returns(() => 'ctrlCmd');

const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
line: 'Some random string with Alt click to launch VS Code Native REPL',
terminal: {} as Terminal,
};
const token: CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: new EventEmitter<unknown>().event,
};

const links = provider.provideTerminalLinks(context, token);

assert.isNotNull(links, 'Expected links to be not undefined');
assert.isArray(links, 'Expected links to be an array');
assert.isNotEmpty(links, 'Expected links to be not empty');

if (Array.isArray(links)) {
assert.equal(
links[0].startIndex,
context.line.indexOf('Alt click to launch VS Code Native REPL'),
'start index should match',
);
assert.equal(
links[0].length,
'Alt click to launch VS Code Native REPL'.length,
'Match expected length',
);
}
});

test('Windows/Linux - Verify provideTerminalLinks returns links with Ctrl modifier when multiCursorModifier is default (alt)', () => {
// This test verifies the default behavior when multiCursorModifier is 'alt'
const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
line: 'Some random string with Ctrl click to launch VS Code Native REPL',
terminal: {} as Terminal,
};
const token: CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: new EventEmitter<unknown>().event,
};

const links = provider.provideTerminalLinks(context, token);

assert.isNotNull(links, 'Expected links to be not undefined');
assert.isArray(links, 'Expected links to be an array');
assert.isNotEmpty(links, 'Expected links to be not empty');

if (Array.isArray(links)) {
assert.equal(
links[0].startIndex,
context.line.indexOf('Ctrl click to launch VS Code Native REPL'),
'start index should match',
);
assert.equal(
links[0].length,
'Ctrl click to launch VS Code Native REPL'.length,
'Match expected length',
);
}
});
}

test('Verify provideTerminalLinks adapts to configuration changes', () => {
const provider = new CustomTerminalLinkProvider();

// Test with default setting (alt)
const context1: TerminalLinkContext = {
line: 'Some random string with Ctrl click to launch VS Code Native REPL',
terminal: {} as Terminal,
};
const token: CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: new EventEmitter<unknown>().event,
};

let links = provider.provideTerminalLinks(context1, token);
assert.isNotEmpty(links, 'Expected links with Ctrl modifier when setting is alt');

// Change configuration to ctrlCmd
editorConfig.reset();
editorConfig.setup((p) => p.get('multiCursorModifier', 'alt')).returns(() => 'ctrlCmd');

// Test with changed setting (ctrlCmd) - should now look for Alt
const context2: TerminalLinkContext = {
line: 'Some random string with Alt click to launch VS Code Native REPL',
terminal: {} as Terminal,
};

links = provider.provideTerminalLinks(context2, token);
assert.isNotEmpty(links, 'Expected links with Alt modifier when setting is ctrlCmd');

// Verify the old Ctrl link no longer works
links = provider.provideTerminalLinks(context1, token);
assert.isEmpty(links, 'Expected no links with Ctrl modifier when setting is ctrlCmd');
});

test('Verify provideTerminalLinks returns no links when context.line does not contain expectedNativeLink', () => {
const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
Expand Down