forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Launch Native REPL using terminal link #24734
Merged
anthonykim1
merged 22 commits into
microsoft:main
from
anthonykim1:shellintegrationLink
Jan 23, 2025
+151
−2
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d1f0b8c
initial set to link pythonrc text to native repl launch.
anthonykim1 8854bb6
add question
anthonykim1 78a25a4
do not use vscode.command.executeCommand directly
anthonykim1 590b6cb
avoid using windows vscode method directly, better separate customLin…
anthonykim1 cb63ae7
compile
anthonykim1 f9093d8
Take feedback
anthonykim1 93d8e77
change wording
anthonykim1 2724718
add python test
anthonykim1 6e216e2
add typescript test
anthonykim1 51c6620
stop messing with other tests
anthonykim1 c053215
remove pytest
anthonykim1 ae09a31
attempting to fix wrongly caches monkeypatch
anthonykim1 7b00e95
fix
anthonykim1 94cef91
i
anthonykim1 b39dcd1
double quote
anthonykim1 f9e2f85
ruff my imports
anthonykim1 541849b
reformat for the very last time
anthonykim1 ee8909f
address feedback, cleanup
anthonykim1 c648fda
add more test
anthonykim1 0a07887
remove unused
anthonykim1 c98e18d
more tests
anthonykim1 fb0fa8d
negative test case against provideTerminalLinks
anthonykim1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
import { | ||
CancellationToken, | ||
Disposable, | ||
ProviderResult, | ||
TerminalLink, | ||
TerminalLinkContext, | ||
TerminalLinkProvider, | ||
} from 'vscode'; | ||
import { executeCommand } from '../common/vscodeApis/commandApis'; | ||
import { registerTerminalLinkProvider } from '../common/vscodeApis/windowApis'; | ||
import { Repl } from '../common/utils/localize'; | ||
|
||
interface CustomTerminalLink extends TerminalLink { | ||
command: string; | ||
} | ||
|
||
export class CustomTerminalLinkProvider implements TerminalLinkProvider<CustomTerminalLink> { | ||
provideTerminalLinks( | ||
context: TerminalLinkContext, | ||
_token: CancellationToken, | ||
): ProviderResult<CustomTerminalLink[]> { | ||
const links: CustomTerminalLink[] = []; | ||
const expectedNativeLink = 'VS Code Native REPL'; | ||
|
||
if (context.line.includes(expectedNativeLink)) { | ||
links.push({ | ||
startIndex: context.line.indexOf(expectedNativeLink), | ||
length: expectedNativeLink.length, | ||
tooltip: Repl.launchNativeRepl, | ||
command: 'python.startNativeREPL', | ||
}); | ||
} | ||
return links; | ||
} | ||
|
||
async handleTerminalLink(link: CustomTerminalLink): Promise<void> { | ||
await executeCommand(link.command); | ||
} | ||
} | ||
|
||
export function registerCustomTerminalLinkProvider(disposables: Disposable[]): void { | ||
disposables.push(registerTerminalLinkProvider(new CustomTerminalLinkProvider())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,23 @@ | |
|
||
import * as sinon from 'sinon'; | ||
import * as TypeMoq from 'typemoq'; | ||
import { GlobalEnvironmentVariableCollection, Uri, WorkspaceConfiguration } from 'vscode'; | ||
import { | ||
GlobalEnvironmentVariableCollection, | ||
Uri, | ||
WorkspaceConfiguration, | ||
Disposable, | ||
CancellationToken, | ||
TerminalLinkContext, | ||
Terminal, | ||
EventEmitter, | ||
} from 'vscode'; | ||
import { assert } from 'chai'; | ||
import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis'; | ||
import { registerPythonStartup } from '../../../client/terminals/pythonStartup'; | ||
import { IExtensionContext } from '../../../client/common/types'; | ||
import * as pythonStartupLinkProvider from '../../../client/terminals/pythonStartupLinkProvider'; | ||
import { CustomTerminalLinkProvider } from '../../../client/terminals/pythonStartupLinkProvider'; | ||
import { Repl } from '../../../client/common/utils/localize'; | ||
|
||
suite('Terminal - Shell Integration with PYTHONSTARTUP', () => { | ||
let getConfigurationStub: sinon.SinonStub; | ||
|
@@ -20,7 +33,6 @@ suite('Terminal - Shell Integration with PYTHONSTARTUP', () => { | |
setup(() => { | ||
context = TypeMoq.Mock.ofType<IExtensionContext>(); | ||
globalEnvironmentVariableCollection = TypeMoq.Mock.ofType<GlobalEnvironmentVariableCollection>(); | ||
|
||
// Question: Why do we have to set up environmentVariableCollection and globalEnvironmentVariableCollection in this flip-flop way? | ||
// Reference: /vscode-python/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts | ||
context.setup((c) => c.environmentVariableCollection).returns(() => globalEnvironmentVariableCollection.object); | ||
|
@@ -122,4 +134,64 @@ suite('Terminal - Shell Integration with PYTHONSTARTUP', () => { | |
|
||
globalEnvironmentVariableCollection.verify((c) => c.delete('PYTHONSTARTUP'), TypeMoq.Times.once()); | ||
}); | ||
|
||
test('Ensure registering terminal link calls registerTerminalLinkProvider', async () => { | ||
anthonykim1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const registerTerminalLinkProviderStub = sinon.stub( | ||
pythonStartupLinkProvider, | ||
'registerCustomTerminalLinkProvider', | ||
); | ||
const disposableArray: Disposable[] = []; | ||
pythonStartupLinkProvider.registerCustomTerminalLinkProvider(disposableArray); | ||
|
||
sinon.assert.calledOnce(registerTerminalLinkProviderStub); | ||
sinon.assert.calledWith(registerTerminalLinkProviderStub, disposableArray); | ||
|
||
registerTerminalLinkProviderStub.restore(); | ||
}); | ||
|
||
test('Verify provideTerminalLinks returns links when context.line contains expectedNativeLink', () => { | ||
const provider = new CustomTerminalLinkProvider(); | ||
const context: TerminalLinkContext = { | ||
line: 'Some random string with VS Code Native REPL in it', | ||
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'); | ||
Comment on lines
+165
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should check You also need a negative test case. |
||
assert.isNotEmpty(links, 'Expected links to be not empty'); | ||
|
||
if (Array.isArray(links)) { | ||
assert.equal(links[0].command, 'python.startNativeREPL', 'Expected command to be python.startNativeREPL'); | ||
assert.equal( | ||
links[0].startIndex, | ||
context.line.indexOf('VS Code Native REPL'), | ||
'Expected startIndex to be 0', | ||
); | ||
assert.equal(links[0].length, 'VS Code Native REPL'.length, 'Expected length to be 16'); | ||
assert.equal(links[0].tooltip, Repl.launchNativeRepl, 'Expected tooltip to be Launch VS Code Native REPL'); | ||
} | ||
}); | ||
|
||
test('Verify provideTerminalLinks returns no links when context.line does not contain expectedNativeLink', () => { | ||
const provider = new CustomTerminalLinkProvider(); | ||
const context: TerminalLinkContext = { | ||
line: 'Some random string without the expected link', | ||
terminal: {} as Terminal, | ||
}; | ||
const token: CancellationToken = { | ||
isCancellationRequested: false, | ||
onCancellationRequested: new EventEmitter<unknown>().event, | ||
}; | ||
|
||
const links = provider.provideTerminalLinks(context, token); | ||
|
||
assert.isArray(links, 'Expected links to be an array'); | ||
assert.isEmpty(links, 'Expected links to be empty'); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it weird if only part of this string is fixed to create a link with l10n? Will that translate only half the command?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean
tooltip: l10n.t('Launch VS Code Native REPL'),
part?I think the the tooltip would only show
Launch VS Code Native REPL