forked from mathworks/MATLAB-extension-for-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandWindow.ts
More file actions
785 lines (680 loc) · 28.8 KB
/
CommandWindow.ts
File metadata and controls
785 lines (680 loc) · 28.8 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Copyright 2024 The MathWorks, Inc.
import * as vscode from 'vscode'
import { MVM, MatlabState } from './MVM'
import { TextEvent, PromptState } from './MVMInterface'
/**
* Direction of cursor movement
*/
enum CursorDirection {
LEFT,
RIGHT
}
/**
* Direction of history movement
*/
enum HistoryDirection {
BACKWARDS,
FORWARDS
}
/**
* Indicator of whether the selection anchor should be kept in place or moved
*/
enum AnchorPolicy {
MOVE,
KEEP
}
const ESC = '\x1b';
/**
* Various terminal escape sequences
*/
const ACTION_KEYS = {
LEFT: ESC + '[D',
RIGHT: ESC + '[C',
UP: ESC + '[A',
DOWN: ESC + '[B',
SHIFT_LEFT: ESC + '[1;2D',
SHIFT_RIGHT: ESC + '[1;2C',
HOME: ESC + '[H',
END: ESC + '[F',
SHIFT_HOME: ESC + '[1;2H',
SHIFT_END: ESC + '[1;2F',
NEWLINE: '\r\n',
BACKSPACE: '\x7f',
BACKSPACE_ALTERNATIVE: '\b',
SELECT_ALL: '\x01',
DELETE: ESC + '[3~',
ESCAPE: ESC,
INVERT_COLORS: ESC + '[7m',
RESTORE_COLORS: ESC + '[27m',
RED_FOREGROUND: ESC + '[31m',
ALL_DEFAULT_COLORS: ESC + '[0m',
COPY: '\x03',
PASTE: '\x16',
MOVE_TO_POSITION_IN_LINE: (n: number) => ESC + '[' + n.toString() + 'G',
CLEAR_AND_MOVE_TO_BEGINNING: ESC + '[0G' + ESC + '[0J',
CLEAR_COMPLETELY: ESC + '[2J' + ESC + '[1;1H',
QUERY_CURSOR: ESC + '[6n',
SET_CURSOR_STYLE_TO_BAR: ESC + '[5 q'
};
const PROMPTS = {
IDLE_PROMPT: '>> ',
DEBUG_PROMPT: 'K>> ',
FAKE_INPUT_PROMPT: '? ',
BUSY_PROMPT: ''
};
/**
* Represents command window. Is a pseudoterminal to be used as the input/output processor in a VS Code terminal.
*/
export default class CommandWindow implements vscode.Pseudoterminal {
private readonly _mvm: MVM;
private readonly _writeEmitter: vscode.EventEmitter<string>;
private _initialized: boolean = false;
private _currentPrompt = PROMPTS.IDLE_PROMPT;
private _currentState: PromptState = PromptState.INITIALIZING;
private _currentPromptLine: string = this._currentPrompt;
private _cursorIndex: number = 0;
private _anchorIndex?: number = undefined;
private _lastOutputLine: string = '';
private readonly _commandHistory: string[] = [];
private _historyIndex: number = 0;
private _lastKnownCurrentLine: string = '';
private _filteredCommandHistory: string[] = [];
private _filteredHistoryIndex: number = 0;
private _commandHistoryFilter: string = '';
private _terminalDimensions: vscode.TerminalDimensions;
private _lastSentTerminalDimensions: vscode.TerminalDimensions | null = null;
private _justTypedLastInColumn: boolean = false;
constructor (mvm: MVM) {
this._mvm = mvm;
this._mvm.on(MVM.Events.output, this.addOutput.bind(this));
this._mvm.on(MVM.Events.clc, this.clear.bind(this));
this._mvm.on(MVM.Events.promptChange, this._handlePromptChange.bind(this));
this._initialized = false;
this._writeEmitter = new vscode.EventEmitter<string>();
this.onDidWrite = this._writeEmitter.event;
this._terminalDimensions = { rows: 30, columns: 100 };
this._mvm.on(MVM.Events.stateChanged, this._handleMatlabStateChange.bind(this));
this._updateHasSelectionContext();
}
/**
* Called when a terminal with this pseudoterminal is opened.
*
* Depending on MATLAB state we will either clear the terminal or write the current line again.
* @param initialDimensions
*/
open (initialDimensions?: vscode.TerminalDimensions): void {
if (initialDimensions != null) {
this._terminalDimensions = initialDimensions;
}
this._writeEmitter.fire(ACTION_KEYS.SET_CURSOR_STYLE_TO_BAR);
const currentMatlabState = this._mvm.getMatlabState();
if (currentMatlabState === MatlabState.READY) {
this._initialized = true;
this._writeCurrentPromptLine();
} else if (currentMatlabState === MatlabState.DISCONNECTED) {
this._clearState();
this._initialized = false;
this._currentState = PromptState.INITIALIZING;
} else if (currentMatlabState === MatlabState.BUSY) {
this._clearState();
this._initialized = true;
}
}
close (): void {
// Unimplemented
}
/**
* Resets the terminal state
*/
private _clearState (): void {
this._writeEmitter.fire(ACTION_KEYS.CLEAR_COMPLETELY)
this._setToEmptyPrompt();
this._lastSentTerminalDimensions = null;
}
private _handleMatlabStateChange (oldState: MatlabState, newState: MatlabState): void {
if (oldState === newState) {
return;
}
if (newState === MatlabState.READY) {
this._clearState();
this._initialized = true;
this._writeCurrentPromptLine();
} else if (newState === MatlabState.DISCONNECTED) {
this._clearState();
this._initialized = false;
} else if (newState === MatlabState.BUSY) {
this._clearState();
this._initialized = true;
}
}
/**
* Clear current line and selection
*/
private _setToEmptyPrompt (): void {
this._currentPromptLine = this._currentPrompt;
this._lastKnownCurrentLine = '';
this._cursorIndex = 0;
this._anchorIndex = undefined;
this._updateHasSelectionContext();
this._updateWhetherJustTypedInLastColumn();
}
/**
* Insert a command to run and submit it.
* @param command
*/
insertCommandForEval (command: string): void {
if (this._currentPromptLine !== this._currentPrompt) {
this._setToEmptyPrompt();
this._writeCurrentPromptLine();
}
this.handleInput(command + ACTION_KEYS.NEWLINE);
}
/**
* Handles input data from the user. Adds it to a queue to be processed asynchronously when we are next idle.
* @param data
* @returns
*/
handleInput (data: string): void {
if (!this._initialized || this._currentState === PromptState.INITIALIZING) {
return;
}
if (this._currentState === PromptState.PAUSE) {
this._mvm.unpause();
return;
}
this.handleText(data, false);
}
/**
* Processes the incoming text, handling the terminal escape sequences as needed.
* @param data
* @param isOutput
* @returns
*/
handleText (data: string, isOutput: boolean): void {
if (this._isSpecialKey(data)) {
// For now, disallow output from containing control characters.
if (isOutput) {
return;
}
if (this._handleActionKeys(data)) {
this._writeCurrentPromptLine();
}
return;
}
if (data.length === 1 && data.charCodeAt(0) < ' '.charCodeAt(0) && data !== '\r' && data !== '\n') {
return;
}
const lines = this._preprocessInputLines(data);
if (isOutput) {
for (let i = 0; i < lines.length; i++) {
this._handleOutputLine(lines[i], i !== lines.length - 1);
}
} else {
// Case 1: Normal typing
if (lines.length === 1) {
this._handleLine(lines[0]);
// Case 2: Normal typing followed by an enter.
} else if (lines.length === 2 && lines[1].length === 0) {
this._handleLine(lines[0]);
this._handleEnter();
// Case 3: Multi-line input (ie, from pasting, etc)
} else {
for (let i = 0; i < lines.length; i++) {
this._handleLine(lines[i] + ((i === lines.length - 1) ? '' : ACTION_KEYS.NEWLINE));
}
this._handleEnter();
}
}
}
private _handleOutputLine (line: string, implicitNewlineAtEnd: boolean): void {
const numberOfLinesBehind = Math.floor(this._getAbsoluteIndexOnLine(this._cursorIndex) / this._terminalDimensions.columns);
if (numberOfLinesBehind !== 0) {
this._writeEmitter.fire(ACTION_KEYS.UP.repeat(numberOfLinesBehind));
}
if (this._lastOutputLine.length !== 0) {
this._writeEmitter.fire(ACTION_KEYS.UP);
}
this._writeEmitter.fire(ACTION_KEYS.CLEAR_AND_MOVE_TO_BEGINNING);
this._lastOutputLine += line;
this._writeEmitter.fire(this._lastOutputLine);
if (implicitNewlineAtEnd) {
this._handleOutputNewline();
}
if (this._lastOutputLine.length !== 0) {
this._writeEmitter.fire(ACTION_KEYS.NEWLINE)
}
this._writeCurrentPromptLine(false);
}
private _handleOutputNewline (): void {
this._writeEmitter.fire(ACTION_KEYS.NEWLINE);
this._lastOutputLine = '';
}
private _isSpecialKey (data: string): boolean {
return data.startsWith(ESC) || Object.values(ACTION_KEYS).includes(data)
}
private _handleActionKeys (keyCode: string): boolean {
switch (keyCode) {
case ACTION_KEYS.LEFT:
return this._handleLeftRight(CursorDirection.LEFT, AnchorPolicy.MOVE);
case ACTION_KEYS.RIGHT:
return this._handleLeftRight(CursorDirection.RIGHT, AnchorPolicy.MOVE);
case ACTION_KEYS.SHIFT_LEFT:
return this._handleLeftRight(CursorDirection.LEFT, AnchorPolicy.KEEP);
case ACTION_KEYS.SHIFT_RIGHT:
return this._handleLeftRight(CursorDirection.RIGHT, AnchorPolicy.KEEP);
case ACTION_KEYS.END:
return this._handleEnd(AnchorPolicy.MOVE);
case ACTION_KEYS.SHIFT_END:
return this._handleEnd(AnchorPolicy.KEEP);
case ACTION_KEYS.HOME:
return this._handleHome(AnchorPolicy.MOVE);
case ACTION_KEYS.SHIFT_HOME:
return this._handleHome(AnchorPolicy.KEEP);
case ACTION_KEYS.DELETE:
return this._handleDelete();
case ACTION_KEYS.UP:
return this._handleNavigateHistory(HistoryDirection.BACKWARDS);
case ACTION_KEYS.DOWN:
return this._handleNavigateHistory(HistoryDirection.FORWARDS);
case ACTION_KEYS.ESCAPE:
return this._handleEscape();
case ACTION_KEYS.BACKSPACE:
case ACTION_KEYS.BACKSPACE_ALTERNATIVE:
return this._handleBackspace();
case ACTION_KEYS.SELECT_ALL:
return this._handleSelectAll();
case ACTION_KEYS.COPY:
return this._handleCopy();
case ACTION_KEYS.PASTE:
return this._handlePaste();
default:
return false;
}
}
private _preprocessInputLines (data: string): string[] {
data = data.replace(/\r\n?/g, '\n');
const lines = data.split('\n');
return lines;
}
private _handleNavigateHistory (direction: HistoryDirection): boolean {
const isCurrentlyAtEndOfHistory = this._historyIndex === this._commandHistory.length;
if (isCurrentlyAtEndOfHistory && this._stripCurrentPrompt(this._currentPromptLine) !== '') {
// Only update the filtered history if the current line changes
if (this._filteredCommandHistory.length === 0) {
this._commandHistoryFilter = this._stripCurrentPrompt(this._currentPromptLine);
this._filteredCommandHistory = this._commandHistory.filter(cmd =>
cmd.toLowerCase().startsWith(this._commandHistoryFilter.toLowerCase()));
this._filteredHistoryIndex = this._filteredCommandHistory.length;
}
// Filter history based on the current prompt text
return this._navigateHistory(
direction,
this._filteredHistoryIndex,
this._filteredCommandHistory,
(newIndex) => { this._filteredHistoryIndex = newIndex }
);
}
return this._navigateHistory(
direction,
this._historyIndex,
this._commandHistory,
(newIndex) => { this._historyIndex = newIndex }
);
}
private _navigateHistory (
direction: HistoryDirection,
currentIndex: number,
history: string[],
updateIndex: (newIndex: number) => void
): boolean {
const isAtEnd = currentIndex === history.length;
const isAtBeginning = currentIndex === 0;
if ((direction === HistoryDirection.BACKWARDS && isAtBeginning) ||
(direction === HistoryDirection.FORWARDS && isAtEnd)) {
return false;
}
if (isAtEnd) {
this._lastKnownCurrentLine = this._stripCurrentPrompt(this._currentPromptLine);
}
currentIndex += direction === HistoryDirection.BACKWARDS ? -1 : 1;
updateIndex(currentIndex);
const line = (currentIndex < history.length)
? history[currentIndex]
: this._lastKnownCurrentLine;
return this._replaceCurrentLineWithNewLine(this._currentPrompt + line);
}
private _markCurrentLineChanged (): void {
this._historyIndex = this._commandHistory.length;
this._filteredCommandHistory = [];
this._lastKnownCurrentLine = '';
}
private _possiblyUpdateAnchorForCursorChange (policy: AnchorPolicy): boolean {
let isLineDirty = false;
if (policy === AnchorPolicy.MOVE && this._anchorIndex !== undefined) {
this._anchorIndex = undefined;
isLineDirty = true;
} else if (policy === AnchorPolicy.KEEP) {
if (this._anchorIndex === undefined) {
this._anchorIndex = this._cursorIndex;
}
isLineDirty = true;
}
this._updateHasSelectionContext();
return isLineDirty;
}
private _handleEnd (anchorPolicy: AnchorPolicy): boolean {
const currentCursorLine = Math.ceil(this._getAbsoluteIndexOnLine(this._cursorIndex) / this._terminalDimensions.columns);
const isLineDirty = this._possiblyUpdateAnchorForCursorChange(anchorPolicy);
this._cursorIndex = this._getMaxIndexOnLine();
this._moveCursorToCurrent(currentCursorLine);
return isLineDirty;
}
private _handleHome (anchorPolicy: AnchorPolicy): boolean {
const currentCursorLine = Math.ceil(this._getAbsoluteIndexOnLine(this._cursorIndex) / this._terminalDimensions.columns);
const isLineDirty = this._possiblyUpdateAnchorForCursorChange(anchorPolicy);
this._cursorIndex = 0;
this._moveCursorToCurrent(currentCursorLine);
return isLineDirty;
}
private _handleLeftRight (direction: CursorDirection, anchorPolicy: AnchorPolicy): boolean {
let isLineDirty = false;
if (direction === CursorDirection.LEFT && this._cursorIndex !== 0) {
if (this._justTypedLastInColumn) {
// Don't actually move the cursor, but do move the index we think the cursor is at.
this._justTypedLastInColumn = false;
} else {
if (this._getAbsoluteIndexOnLine(this._cursorIndex) % this._terminalDimensions.columns === 0) {
this._writeEmitter.fire(ACTION_KEYS.UP + ACTION_KEYS.MOVE_TO_POSITION_IN_LINE(this._terminalDimensions.columns));
} else {
this._writeEmitter.fire(ACTION_KEYS.LEFT);
}
}
isLineDirty = this._possiblyUpdateAnchorForCursorChange(anchorPolicy);
this._cursorIndex--;
}
if (direction === CursorDirection.RIGHT && this._cursorIndex !== this._getMaxIndexOnLine()) {
if (this._justTypedLastInColumn) {
// Not possible
} else {
if (this._getAbsoluteIndexOnLine(this._cursorIndex) % this._terminalDimensions.columns === (this._terminalDimensions.columns - 1)) {
this._writeEmitter.fire(ACTION_KEYS.DOWN + ACTION_KEYS.MOVE_TO_POSITION_IN_LINE(0));
} else {
this._writeEmitter.fire(ACTION_KEYS.RIGHT);
}
}
isLineDirty = this._possiblyUpdateAnchorForCursorChange(anchorPolicy);
this._cursorIndex++;
}
return isLineDirty;
}
private _getMaxIndexOnLine (): number {
return this._currentPromptLine.length - this._currentPrompt.length;
}
private _getAbsoluteIndexOnLine (index: number): number {
return index + this._currentPrompt.length;
}
private _handleBackspace (): boolean {
if (this._anchorIndex !== undefined) {
return this._removeSelection();
}
if (this._cursorIndex === 0) {
return false;
}
const before = this._currentPromptLine.substring(0, this._getAbsoluteIndexOnLine(this._cursorIndex) - 1);
const after = this._currentPromptLine.substring(this._getAbsoluteIndexOnLine(this._cursorIndex));
this._currentPromptLine = before + after;
this._cursorIndex--;
this._markCurrentLineChanged();
return true;
}
private _handleSelectAll (): boolean {
this._cursorIndex = this._getMaxIndexOnLine();
this._anchorIndex = 0;
this._updateHasSelectionContext();
return true;
}
private _handleDelete (): boolean {
if (this._anchorIndex !== undefined) {
return this._removeSelection();
}
if (this._cursorIndex === this._getMaxIndexOnLine()) {
return false;
}
const before = this._currentPromptLine.substring(0, this._getAbsoluteIndexOnLine(this._cursorIndex));
const after = this._currentPromptLine.substring(this._getAbsoluteIndexOnLine(this._cursorIndex) + 1);
this._currentPromptLine = before + after;
this._markCurrentLineChanged();
return true;
}
private _writeCurrentPromptLine (eraseExisting: boolean = true): void {
if (eraseExisting) {
this._eraseExistingPromptLine();
}
if (this._anchorIndex === undefined) {
this._writeEmitter.fire(this._currentPromptLine)
} else {
const selectionStart = this._currentPrompt.length + Math.min(this._cursorIndex, this._anchorIndex);
const selectionEnd = this._currentPrompt.length + Math.max(this._cursorIndex, this._anchorIndex);
const preSelection = this._currentPromptLine.slice(0, selectionStart);
const selection = this._currentPromptLine.slice(selectionStart, selectionEnd);
const postSelection = this._currentPromptLine.slice(selectionEnd);
this._writeEmitter.fire(preSelection);
this._writeEmitter.fire(ACTION_KEYS.INVERT_COLORS);
this._writeEmitter.fire(selection);
this._writeEmitter.fire(ACTION_KEYS.RESTORE_COLORS);
this._writeEmitter.fire(postSelection);
}
const currentCursorLine = Math.ceil(this._currentPromptLine.length / this._terminalDimensions.columns);
this._moveCursorToCurrent(currentCursorLine);
}
private _eraseExistingPromptLine (): void {
const numberOfLinesBehind = Math.floor(this._getAbsoluteIndexOnLine(this._cursorIndex) / this._terminalDimensions.columns);
if (numberOfLinesBehind !== 0) {
this._writeEmitter.fire(ACTION_KEYS.UP.repeat(numberOfLinesBehind))
}
this._writeEmitter.fire(ACTION_KEYS.CLEAR_AND_MOVE_TO_BEGINNING)
}
private _replaceCurrentLineWithNewLine (updatedLine: string): boolean {
this._eraseExistingPromptLine();
this._currentPromptLine = updatedLine;
this._cursorIndex = this._getMaxIndexOnLine();
this._anchorIndex = undefined;
this._updateWhetherJustTypedInLastColumn();
this._writeCurrentPromptLine(false);
return false;
}
private _removeSelection (): boolean {
if (this._anchorIndex === undefined || this._cursorIndex === this._anchorIndex) {
this._anchorIndex = undefined;
this._updateHasSelectionContext();
return false;
}
const selectionStart = this._getAbsoluteIndexOnLine(Math.min(this._cursorIndex, this._anchorIndex));
const selectionEnd = this._getAbsoluteIndexOnLine(Math.max(this._cursorIndex, this._anchorIndex));
const preSelection = this._currentPromptLine.slice(0, selectionStart);
const postSelection = this._currentPromptLine.slice(selectionEnd);
this._currentPromptLine = preSelection + postSelection;
this._cursorIndex = selectionStart - this._currentPrompt.length;
this._anchorIndex = undefined;
this._updateHasSelectionContext();
return true;
}
private _handleLine (line: string): void {
if (this._removeSelection()) {
this._writeCurrentPromptLine();
}
if (this._cursorIndex === this._getMaxIndexOnLine()) {
this._currentPromptLine += line;
this._cursorIndex += line.length;
this._writeEmitter.fire(line);
} else {
const before = this._currentPromptLine.substring(0, this._getAbsoluteIndexOnLine(this._cursorIndex));
const after = this._currentPromptLine.substring(this._getAbsoluteIndexOnLine(this._cursorIndex));
this._currentPromptLine = before + line + after;
this._cursorIndex += line.length;
this._writeCurrentPromptLine();
}
this._markCurrentLineChanged();
this._justTypedLastInColumn = this._getAbsoluteIndexOnLine(this._cursorIndex) % this._terminalDimensions.columns === 0;
}
private _handleEnter (): void {
const stringToEvaluate = this._stripCurrentPrompt(this._currentPromptLine).trim();
this._addToHistory(stringToEvaluate);
this._handleEnd(AnchorPolicy.MOVE);
this._writeEmitter.fire(ACTION_KEYS.NEWLINE);
this._lastOutputLine = '';
this._currentPromptLine = this._currentPrompt;
this._justTypedLastInColumn = this._getAbsoluteIndexOnLine(this._cursorIndex) % this._terminalDimensions.columns === 0;
this._cursorIndex = 0;
this._anchorIndex = undefined;
this._updateHasSelectionContext();
this._lastKnownCurrentLine = this._stripCurrentPrompt(this._currentPromptLine);
this._writeCurrentPromptLine();
void this._evaluateCommand(stringToEvaluate);
}
private _addToHistory (command: string): void {
const isEmpty = command === '';
const isLastInHistory = this._commandHistory.length !== 0 && command === this._commandHistory[this._commandHistory.length - 1];
if (!isEmpty && !isLastInHistory) {
this._commandHistory.push(command);
}
this._historyIndex = this._commandHistory.length;
}
private _moveCursorToCurrent (lineOfInputCursorIsCurrentlyOn?: number): void {
const lineNumberCursorShouldBeOn = Math.max(1, Math.ceil(this._getAbsoluteIndexOnLine(this._cursorIndex) / this._terminalDimensions.columns));
if (lineOfInputCursorIsCurrentlyOn === undefined) {
lineOfInputCursorIsCurrentlyOn = lineNumberCursorShouldBeOn;
}
lineOfInputCursorIsCurrentlyOn = Math.max(1, lineOfInputCursorIsCurrentlyOn);
if (lineNumberCursorShouldBeOn > lineOfInputCursorIsCurrentlyOn) {
this._writeEmitter.fire(ACTION_KEYS.DOWN.repeat(lineNumberCursorShouldBeOn - lineOfInputCursorIsCurrentlyOn));
} else if (lineNumberCursorShouldBeOn < lineOfInputCursorIsCurrentlyOn) {
this._writeEmitter.fire(ACTION_KEYS.UP.repeat(lineOfInputCursorIsCurrentlyOn - lineNumberCursorShouldBeOn));
}
this._writeEmitter.fire(ACTION_KEYS.MOVE_TO_POSITION_IN_LINE((this._getAbsoluteIndexOnLine(this._cursorIndex) % this._terminalDimensions.columns) + 1));
}
setDimensions (dimensions: vscode.TerminalDimensions): void {
this._terminalDimensions = dimensions;
}
private _sendTerminalDimensionsIfNeeded (): void {
if ((this._lastSentTerminalDimensions == null) || this._lastSentTerminalDimensions.columns !== this._terminalDimensions.columns || this._lastSentTerminalDimensions.rows !== this._terminalDimensions.rows) {
void this._mvm.eval(`try; if usejava('jvm'); com.mathworks.mde.cmdwin.CmdWinMLIF.setCWSize(${this._terminalDimensions.rows}, ${this._terminalDimensions.columns}); end; end;`);
this._lastSentTerminalDimensions = this._terminalDimensions;
}
}
private async _evaluateCommand (command: string): Promise<void> {
this._sendTerminalDimensionsIfNeeded();
return await (this._mvm.eval(command) as Promise<void>);
}
/**
*
* @param output Add an output TextEvent to the command window. Stderr is displayed in red.
*/
addOutput (output: TextEvent): void {
if (this._initialized) {
if (output.stream === 0) {
this.handleText(output.text, true);
} else {
this._writeEmitter.fire(ACTION_KEYS.RED_FOREGROUND);
this.handleText(output.text, true);
this._writeEmitter.fire(ACTION_KEYS.ALL_DEFAULT_COLORS);
}
}
}
/**
* Clears the command window, and also wipes out the terminal's scroll history as well.
*/
clear (): void {
this._writeEmitter.fire(ACTION_KEYS.CLEAR_COMPLETELY)
void vscode.commands.executeCommand('workbench.action.terminal.clear');
this._setToEmptyPrompt();
}
private _updateHasSelectionContext (): void {
void vscode.commands.executeCommand('setContext', 'matlab.terminalHasSelection', this._anchorIndex !== undefined);
}
private _handleCopy (): boolean {
if (this._anchorIndex === undefined) {
return false;
}
const selectionStart = this._currentPrompt.length + Math.min(this._cursorIndex, this._anchorIndex);
const selectionEnd = this._currentPrompt.length + Math.max(this._cursorIndex, this._anchorIndex);
const selection = this._currentPromptLine.slice(selectionStart, selectionEnd);
void vscode.env.clipboard.writeText(selection);
return false;
}
private _handlePaste (): boolean {
vscode.env.clipboard.readText().then((text: string) => {
this.handleInput(text);
}, () => {
// Ignored
});
return false;
}
private _handleEscape (): boolean {
this._setToEmptyPrompt();
return true;
}
private _handlePromptChange (state: PromptState, isIdle: boolean): void {
this._currentState = state;
if (state === PromptState.READY) {
this._changePrompt(PROMPTS.IDLE_PROMPT);
} else if (state === PromptState.DEBUG) {
this._changePrompt(PROMPTS.DEBUG_PROMPT);
} else if (state === PromptState.PAUSE) {
this._changePrompt(PROMPTS.BUSY_PROMPT);
} else if (state === PromptState.INPUT) {
this._changePrompt(PROMPTS.FAKE_INPUT_PROMPT);
} else {
this._changePrompt(PROMPTS.BUSY_PROMPT);
}
}
private _changePrompt (prompt: string): void {
if (this._currentPrompt !== PROMPTS.BUSY_PROMPT) {
this._currentPromptLine = this._stripCurrentPrompt(this._currentPromptLine);
}
this._currentPrompt = prompt;
this._currentPromptLine = this._currentPrompt + this._currentPromptLine;
this._updateWhetherJustTypedInLastColumn();
this._writeCurrentPromptLine();
}
private _stripCurrentPrompt (line: string): string {
return this._currentPromptLine.slice(this._currentPrompt.length);
}
private _updateWhetherJustTypedInLastColumn (): void {
this._justTypedLastInColumn = this._getAbsoluteIndexOnLine(this._cursorIndex) % this._terminalDimensions.columns === 0;
}
onDidWrite: vscode.Event<string>;
onDidOverrideDimensions?: vscode.Event<vscode.TerminalDimensions | undefined> | undefined;
onDidClose?: vscode.Event<number> | undefined;
onDidChangeName?: vscode.Event<string> | undefined;
/**
*
* @param data Helper used to log input is a readible manner
*/
private _logInput (data: string): void {
let shouldPrint = false;
let s = '[';
const prefix = ''
for (let i = 0; i < data.length; i++) {
let ch = data[i];
if (data.charCodeAt(i) === 0x1b) {
ch = 'ESC'
shouldPrint = true;
} else {
if (ch.match(/[a-z0-9,./;'[\]\\`~!@#$%^&*()_+\-=|:'{}<>?]/i) === null) {
let hex = data.charCodeAt(i).toString(16);
if (hex.length === 1) {
hex = '0' + hex;
}
ch = '\\x' + hex;
shouldPrint = true;
}
}
s += prefix + ch;
}
s += ']'
if (shouldPrint) {
console.log(s);
}
}
}