-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathTerminalService.ts
More file actions
128 lines (109 loc) · 4.74 KB
/
TerminalService.ts
File metadata and controls
128 lines (109 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2024-2025 The MathWorks, Inc.
import * as vscode from 'vscode'
import { MVM } from './MVM'
import { Notifier, ResolvablePromise, createResolvablePromise } from './Utilities'
import CommandWindow from './CommandWindow'
import Notification from '../Notifications'
/**
* Manages the MATLAB VS Code terminal ensuring that only a single one is open at a time
*/
export default class TerminalService {
private readonly _mvm: MVM;
private readonly _client: Notifier;
private readonly _commandWindow: CommandWindow;
private readonly _terminalOptions: vscode.ExtensionTerminalOptions;
private _currentMatlabTerminal?: vscode.Terminal;
private _terminalCreationPromise?: ResolvablePromise<void>;
constructor (client: Notifier, mvm: MVM) {
this._mvm = mvm;
this._client = client;
this._commandWindow = new CommandWindow(mvm, client);
this._terminalOptions = {
name: 'MATLAB',
pty: this._commandWindow,
isTransient: true
};
vscode.window.onDidOpenTerminal((terminal) => {
if (terminal.creationOptions.name === 'MATLAB') {
this._currentMatlabTerminal = terminal;
client.sendNotification(Notification.MatlabRequestInstance);
this._currentMatlabTerminal.show();
setTimeout(() => {
if (this._terminalCreationPromise != null) {
this._terminalCreationPromise.resolve();
}
}, 100);
}
});
vscode.window.onDidCloseTerminal((terminal) => {
if (terminal === this._currentMatlabTerminal) {
this._currentMatlabTerminal = undefined;
}
});
vscode.window.registerTerminalProfileProvider('matlab.terminal-profile', new MatlabTerminalProvider(this, this._terminalOptions));
vscode.window.onDidChangeActiveTerminal(terminal => {
if ((this._currentMatlabTerminal != null) && terminal === this._currentMatlabTerminal) {
void vscode.commands.executeCommand('setContext', 'matlab.isActiveTerminal', true);
} else {
void vscode.commands.executeCommand('setContext', 'matlab.isActiveTerminal', false);
}
})
// Required to ensure that Ctrl+C keybinding is handled by vscode and not the terminal itself
const terminalConfiguration = vscode.workspace.getConfiguration('terminal.integrated');
const commandsToNotSendToTerminal: string[] | undefined = terminalConfiguration.get<string[]>('commandsToSkipShell');
if ((commandsToNotSendToTerminal != null) && !commandsToNotSendToTerminal.includes('matlab.interrupt')) {
commandsToNotSendToTerminal.push('matlab.interrupt');
void terminalConfiguration.update('commandsToSkipShell', commandsToNotSendToTerminal, true);
}
}
/**
* Opens or brings the MATLAB termianl to the front.
* @returns resolves when the terminal is visible
*/
async openTerminalOrBringToFront (): Promise<void> {
this._client.sendNotification(Notification.MatlabRequestInstance);
if (this._currentMatlabTerminal != null) {
this._currentMatlabTerminal.show();
} else {
vscode.window.createTerminal(this._terminalOptions);
this._terminalCreationPromise = createResolvablePromise();
await this._terminalCreationPromise;
}
}
/**
* Close the current MATLAB terminal
*/
closeTerminal (): void {
if (this._currentMatlabTerminal != null) {
this._currentMatlabTerminal.dispose();
}
}
/**
* @returns The command window
*/
getCommandWindow (): CommandWindow {
return this._commandWindow;
}
/**
* Get cursor position information for testing purposes.
* Returns the logical line number (0-based) and column position (0-based) within that line.
*/
getCursorPosition (): { line: number, column: number } {
return this._commandWindow.getCursorPosition();
}
}
/**
* Provides a VS Code terminal window backed by the command window.
*/
class MatlabTerminalProvider {
private readonly _terminalService: TerminalService;
private readonly _terminalOptions: vscode.ExtensionTerminalOptions;
constructor (terminalService: TerminalService, terminalOptions: vscode.ExtensionTerminalOptions) {
this._terminalService = terminalService;
this._terminalOptions = terminalOptions;
}
provideTerminalProfile (token: vscode.CancellationToken): vscode.ProviderResult<vscode.TerminalProfile> {
this._terminalService.closeTerminal();
return new vscode.TerminalProfile(this._terminalOptions);
}
}