Integrated Terminal
Relevant source files
The following files were used as context for generating this wiki page:
- extensions/terminal-suggest/src/terminalSuggestMain.ts
- src/vscode-dts/vscode.proposed.terminalShellIntegration.d.ts
- test/smoke/src/areas/terminal/terminal.test.ts
- extensions/terminal-suggest/package.json
- extensions/npm/package.json
- extensions/terminal-suggest/cgmanifest.json
- extensions/github/package.json
The Integrated Terminal in Visual Studio Code is an extensible component that allows users to run shell commands and interact with command-line tools directly within the editor. It is designed with a rich API that enables features like shell integration, command suggestions, and contextual quick fixes, enhancing developer productivity.
Extensions can activate based on terminal events, contribute new commands, and provide dynamic content such as autocompletions and problem matchers. Key features like Terminal Suggest and NPM script integration are built upon this extensible foundation, leveraging proposed APIs to offer a more intelligent and interactive command-line experience.
Shell Integration API
A core aspect of the Integrated Terminal's advanced functionality is the Shell Integration API. This proposed API allows VS Code to understand the structure of the terminal's output, including where commands are executed and their exit codes.
Sources: src/vscode-dts/vscode.proposed.terminalShellIntegration.d.ts
TerminalShellIntegration Interface
The TerminalShellIntegration interface provides access to shell integration-related events and data. It is the primary entry point for extensions to interact with the terminal at a semantic level.
This diagram shows the structure of the TerminalShellIntegration interface and its related event types.
Key properties and events include:
onDidStartTerminalShellExecution: An event that fires when a command starts executing.onDidEndTerminalShellExecution: An event that fires when a command finishes executing, providing details like the exit code.executeCommand(commandLine): A method to programmatically execute a command line in the terminal as if the user had typed it.
Sources: src/vscode-dts/vscode.proposed.terminalShellIntegration.d.ts:21-30
Activation
Extensions can be activated when shell integration is available for a terminal by using the onTerminalShellIntegration activation event. This is crucial for features that need to understand the shell's state.
"activationEvents": [
"onTerminalShellIntegration:*"
]
Sources: extensions/terminal-suggest/package.json:29-31
Terminal Suggest Feature
The terminal-suggest extension provides IDE-style autocomplete for the terminal. It leverages shell integration to offer relevant command suggestions.
Sources: extensions/terminal-suggest/package.json:4, extensions/terminal-suggest/cgmanifest.json:11
Architecture and Dependencies
The feature relies on several key components and external data sources:
- Activation: Activates
onTerminalShellIntegrationto ensure it runs when the shell is ready. - API Proposals: Uses
terminalCompletionProviderandterminalShellEnvto provide suggestions and access shell environment variables. - Dependencies: Pulls autocomplete specifications from external sources like
withfig/autocomplete, and built-in command information fromzshandfish-shellrepositories.
This flow diagram illustrates how the Terminal Suggest feature is activated and provides completions.
Commands
The extension contributes a command for managing its internal state.
| Command | Category | Title | Description |
|---|---|---|---|
terminal.integrated.suggest.clearCachedGlobals | Terminal | Clear Cached Terminal Suggestions | Clears the cache of global command suggestions used by the terminal suggest feature. |
Sources: extensions/terminal-suggest/package.json:24-28
Terminal Quick Fixes
The Integrated Terminal supports "Quick Fixes" for failed commands, which are contextual actions suggested to the user. The npm extension provides a clear example of this functionality.
Sources: extensions/npm/package.json:15
terminalQuickFixProvider
This feature is enabled through the terminalQuickFixProvider API proposal. Extensions can contribute quick fixes by defining them in their package.json.
The following sequence diagram shows the process of triggering and providing an NPM-related quick fix in the terminal.
Sources: extensions/npm/package.json:206-217
Contribution Point
Quick fixes are defined under the terminalQuickFixes contribution point. The properties for a quick fix are detailed below.
| Property | Type | Description |
|---|---|---|
id | string | A unique identifier for the quick fix provider. |
commandLineMatcher | string | A regular expression to match against the failed command line. |
commandExitResult | string | The expected exit result of the command ('success' or 'error'). |
outputMatcher | object | An object with regexes to match against the terminal's output to find the suggestion. |
Sources: extensions/npm/package.json:206-217
Here is the npm extension's contribution for suggesting corrections to misspelled npm commands:
"terminalQuickFixes": [
{
"id": "ms-vscode.npm-command",
"commandLineMatcher": "npm",
"commandExitResult": "error",
"outputMatcher": {
"anchor": "bottom",
"length": 8,
"lineMatcher": "Did you mean (?:this|one of these)\\?((?:\\n.+?npm .+ #.+)+)",
"offset": 2
}
}
]
Sources: extensions/npm/package.json:206-217
Testing
The Integrated Terminal's functionality is validated through smoke tests. These tests ensure that core features work as expected in a clean environment. The test suite for the terminal covers:
- Verifying that the terminal panel can be opened.
- Creating terminals with specific shell paths and arguments.
- Checking for the presence of shell integration scripts to confirm that the
TerminalShellIntegrationis active. - Running commands and asserting their successful execution.