11import pWaitFor from "p-wait-for"
22import * as vscode from "vscode"
3+
4+ /*
5+ The new shellIntegration API gives us access to terminal command execution output handling.
6+ However, we don't update our VSCode type definitions or engine requirements to maintain compatibility
7+ with older VSCode versions. Users on older versions will automatically fall back to using sendText
8+ for terminal command execution.
9+ Interestingly, some environments like Cursor enable these APIs even without the latest VSCode engine.
10+ This approach allows us to leverage advanced features when available while ensuring broad compatibility.
11+ */
12+ declare module "vscode" {
13+ // https://github.com/microsoft/vscode/blob/f0417069c62e20f3667506f4b7e53ca0004b4e3e/src/vscode-dts/vscode.d.ts#L7442
14+ interface Terminal {
15+ shellIntegration ?: {
16+ cwd ?: vscode . Uri
17+ executeCommand ?: ( command : string ) => {
18+ read : ( ) => AsyncIterable < string >
19+ }
20+ }
21+ }
22+ // https://github.com/microsoft/vscode/blob/f0417069c62e20f3667506f4b7e53ca0004b4e3e/src/vscode-dts/vscode.d.ts#L10794
23+ interface Window {
24+ onDidStartTerminalShellExecution ?: (
25+ listener : ( e : any ) => any ,
26+ thisArgs ?: any ,
27+ disposables ?: vscode . Disposable [ ] ,
28+ ) => vscode . Disposable
29+ }
30+ }
331import { arePathsEqual } from "../../utils/path"
432import { mergePromise , TerminalProcess , TerminalProcessResultPromise } from "./TerminalProcess"
533import { TerminalInfo , TerminalRegistry } from "./TerminalRegistry"
@@ -61,34 +89,6 @@ Resources:
6189- https://github.com/microsoft/vscode-extension-samples/blob/main/shell-integration-sample/src/extension.ts
6290*/
6391
64- /*
65- The new shellIntegration API gives us access to terminal command execution output handling.
66- However, we don't update our VSCode type definitions or engine requirements to maintain compatibility
67- with older VSCode versions. Users on older versions will automatically fall back to using sendText
68- for terminal command execution.
69- Interestingly, some environments like Cursor enable these APIs even without the latest VSCode engine.
70- This approach allows us to leverage advanced features when available while ensuring broad compatibility.
71- */
72- declare module "vscode" {
73- // https://github.com/microsoft/vscode/blob/f0417069c62e20f3667506f4b7e53ca0004b4e3e/src/vscode-dts/vscode.d.ts#L7442
74- interface Terminal {
75- shellIntegration ?: {
76- cwd ?: vscode . Uri
77- executeCommand ?: ( command : string ) => {
78- read : ( ) => AsyncIterable < string >
79- }
80- }
81- }
82- // https://github.com/microsoft/vscode/blob/f0417069c62e20f3667506f4b7e53ca0004b4e3e/src/vscode-dts/vscode.d.ts#L10794
83- interface Window {
84- onDidStartTerminalShellExecution ?: (
85- listener : ( e : any ) => any ,
86- thisArgs ?: any ,
87- disposables ?: vscode . Disposable [ ] ,
88- ) => vscode . Disposable
89- }
90- }
91-
9292export class TerminalManager {
9393 private terminalIds : Set < number > = new Set ( )
9494 private processes : Map < number , TerminalProcess > = new Map ( )
@@ -109,7 +109,12 @@ export class TerminalManager {
109109 }
110110 }
111111
112- runCommand ( terminalInfo : TerminalInfo , command : string ) : TerminalProcessResultPromise {
112+ runCommand (
113+ terminalInfo : TerminalInfo ,
114+ command : string ,
115+ contextLimit ?: number ,
116+ usedContext ?: number ,
117+ ) : TerminalProcessResultPromise {
113118 terminalInfo . busy = true
114119 terminalInfo . lastCommand = command
115120 const process = new TerminalProcess ( )
@@ -141,14 +146,14 @@ export class TerminalManager {
141146 // if shell integration is already active, run the command immediately
142147 if ( terminalInfo . terminal . shellIntegration ) {
143148 process . waitForShellIntegration = false
144- process . run ( terminalInfo . terminal , command )
149+ process . run ( terminalInfo . terminal , command , contextLimit , usedContext )
145150 } else {
146151 // docs recommend waiting 3s for shell integration to activate
147152 pWaitFor ( ( ) => terminalInfo . terminal . shellIntegration !== undefined , { timeout : 4000 } ) . finally ( ( ) => {
148153 const existingProcess = this . processes . get ( terminalInfo . id )
149154 if ( existingProcess && existingProcess . waitForShellIntegration ) {
150155 existingProcess . waitForShellIntegration = false
151- existingProcess . run ( terminalInfo . terminal , command )
156+ existingProcess . run ( terminalInfo . terminal , command , contextLimit , usedContext )
152157 }
153158 } )
154159 }
0 commit comments