-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCommandWindow.ts
More file actions
958 lines (834 loc) · 38.2 KB
/
CommandWindow.ts
File metadata and controls
958 lines (834 loc) · 38.2 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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
// Copyright 2024-2025 The MathWorks, Inc.
import * as vscode from 'vscode'
import { MVM, MatlabState } from './MVM'
import { TextEvent, PromptState } from './MVMInterface'
import { createResolvablePromise, Notifier, ResolvablePromise } from './Utilities';
import Notification from '../Notifications';
import { CompletionList } from 'vscode-languageclient';
/**
* Direction of cursor movement
*/
enum CursorDirection {
LEFT,
RIGHT
}
/**
* Direction of history movement
*/
enum Direction {
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,
TAB: '\t',
SHIFT_TAB: ESC + '[Z',
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 + '[3J' + ESC + '[1;1H',
QUERY_CURSOR: ESC + '[6n',
SET_CURSOR_STYLE_TO_BAR: ESC + '[5 q'
};
// eslint-disable-next-line no-control-regex
const LEFT_REGEX = /^(\x1b\[D)+$/;
// eslint-disable-next-line no-control-regex
const RIGHT_REGEX = /^(\x1b\[C)+$/;
const PROMPTS = {
IDLE_PROMPT: '>> ',
DEBUG_PROMPT: 'K>> ',
FAKE_INPUT_PROMPT: '? ',
BUSY_PROMPT: ''
};
// A modification of the word boundary regex being used by VS Code when replacing completions.
// The first part splits on numbers. The second/third parts split on quoted strings, ie. plot("Color"|
// the fourth part splits on unquoted words (same as VS Code's original regex),
// And the fifth part splits on unfinished quotes. ie. plot("C|
// eslint-disable-next-line no-useless-escape
const WORD_REGEX = /(-?\d*\.\d\w*)|(\"[^\"]*\"?)|(\'[^\']*\'?)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)|(\"|\')/
type MatlabData = any; // eslint-disable-line @typescript-eslint/no-explicit-any
/**
* 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: string = 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 _rawCommandHistory: string[] = [];
private _historyIndex: number = 0;
private _lastKnownCurrentLine: string = '';
private _filteredCommandHistory: string[] = [];
private _terminalDimensions: vscode.TerminalDimensions;
private _lastSentTerminalDimensions: vscode.TerminalDimensions | null = null;
private _justTypedLastInColumn: boolean = false;
private readonly _notifier: Notifier;
private _latestTabCompletionData?: CompletionList;
private _currentCompletionIndex: number = -1;
private _pendingTabCompletionRequestNumber: number = -1;
private _pendingTabCompletionPromise?: ResolvablePromise<CompletionList>;
constructor (mvm: MVM, notifier: Notifier) {
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._notifier = notifier;
this._notifier.onNotification(Notification.TerminalCompletionResponse, this._handleCompletionDataResponse.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 {
this._invalidateCompletionData();
// 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 (input: string): boolean {
switch (input) {
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(Direction.BACKWARDS);
case ACTION_KEYS.DOWN:
return this._handleNavigateHistory(Direction.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();
case ACTION_KEYS.TAB:
return this._handleTab(Direction.FORWARDS);
case ACTION_KEYS.SHIFT_TAB:
return this._handleTab(Direction.BACKWARDS);
default: {
let result = false;
// Handle repeated left/right arrow keys. This is what is received when using Alt+Mouse to move the cursor.
if (input.match(RIGHT_REGEX) != null) {
const count = input.length / 3;
for (let i = 0; i < count; i++) {
result ||= this._handleLeftRight(CursorDirection.RIGHT, AnchorPolicy.MOVE);
}
} else if (input.match(LEFT_REGEX) != null) {
const count = input.length / 3;
for (let i = 0; i < count; i++) {
result ||= this._handleLeftRight(CursorDirection.LEFT, AnchorPolicy.MOVE);
}
}
return result;
}
}
}
private _preprocessInputLines (data: string): string[] {
data = data.replace(/\r\n?/g, '\n');
const lines = data.split('\n');
return lines;
}
private _handleNavigateHistory (direction: Direction): boolean {
const isAtEnd = this._historyIndex === this._filteredCommandHistory.length;
const isAtBeginning = this._historyIndex === 0;
if ((direction === Direction.BACKWARDS && isAtBeginning) ||
(direction === Direction.FORWARDS && isAtEnd)) {
return false;
}
if (isAtEnd) {
this._lastKnownCurrentLine = this._stripCurrentPrompt(this._currentPromptLine);
}
this._historyIndex += direction === Direction.BACKWARDS ? -1 : 1;
const line = this._getHistoryItem(this._historyIndex);
return this._replaceCurrentLineWithNewLine(this._currentPrompt + line);
}
private _markCurrentLineChanged (): void {
const commandHistoryFilter = this._stripCurrentPrompt(this._currentPromptLine);
if (commandHistoryFilter !== '') {
this._filteredCommandHistory = this._rawCommandHistory.filter(cmd =>
cmd.toLowerCase().startsWith(commandHistoryFilter.toLowerCase()));
} else {
this._filteredCommandHistory = this._rawCommandHistory
}
this._historyIndex = this._filteredCommandHistory.length;
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 {
// Check if the character before cursor is a newline (explicit line break)
const charBeforeCursor = this._currentPromptLine.charAt(this._getAbsoluteIndexOnLine(this._cursorIndex) - 1);
if (charBeforeCursor === '\n') {
// Moving left across an explicit newline - need to go up and find position on previous line
const textBeforeNewline = this._currentPromptLine.substring(0, this._getAbsoluteIndexOnLine(this._cursorIndex) - 1);
const previousNewlineIndex = textBeforeNewline.lastIndexOf('\n');
const positionOnPreviousLine = previousNewlineIndex === -1
? textBeforeNewline.length
: textBeforeNewline.length - previousNewlineIndex - 1;
this._writeEmitter.fire(ACTION_KEYS.UP + ACTION_KEYS.MOVE_TO_POSITION_IN_LINE((positionOnPreviousLine % this._terminalDimensions.columns) + 1));
} 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 {
// Check if the character at cursor is a newline (explicit line break)
const charAtCursor = this._currentPromptLine.charAt(this._getAbsoluteIndexOnLine(this._cursorIndex));
if (charAtCursor === '\n') {
// Moving right across an explicit newline - go down to start of next line
this._writeEmitter.fire(ACTION_KEYS.DOWN + ACTION_KEYS.MOVE_TO_POSITION_IN_LINE(1));
} 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++;
}
this._invalidateCompletionData();
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();
this._invalidateCompletionData();
return true;
}
private _handleSelectAll (): boolean {
this._cursorIndex = this._getMaxIndexOnLine();
this._anchorIndex = 0;
this._updateHasSelectionContext();
this._invalidateCompletionData();
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();
this._invalidateCompletionData();
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 textUpToCursor = this._currentPromptLine.substring(0, this._getAbsoluteIndexOnLine(this._cursorIndex));
const numberOfExplicitNewlines = (textUpToCursor.match(/\r?\n/g) ?? []).length;
const numberOfLinesBehind = Math.floor(this._getAbsoluteIndexOnLine(this._cursorIndex) / this._terminalDimensions.columns) + numberOfExplicitNewlines;
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, cursorIndex?: number): boolean {
this._eraseExistingPromptLine();
this._currentPromptLine = updatedLine;
this._cursorIndex = 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();
this._invalidateCompletionData();
void this._evaluateCommand(stringToEvaluate);
}
private _addToHistory (command: string): void {
const isEmpty = command === '';
const isLastInHistory =
this._rawCommandHistory.length !== 0 &&
command === this._rawCommandHistory[this._rawCommandHistory.length - 1];
if (!isEmpty && !isLastInHistory) {
this._rawCommandHistory.push(command);
}
this._historyIndex = this._rawCommandHistory.length;
this._filteredCommandHistory = this._rawCommandHistory
}
private _getHistoryItem (n: number): string {
return (this._historyIndex < this._filteredCommandHistory.length)
? this._filteredCommandHistory[n]
: this._lastKnownCurrentLine;
}
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));
}
// Calculate column position accounting for explicit newlines
const textUpToCursor = this._currentPromptLine.substring(0, this._getAbsoluteIndexOnLine(this._cursorIndex));
const lastNewlineIndex = textUpToCursor.lastIndexOf('\n');
const positionOnCurrentLine = lastNewlineIndex === -1
? this._getAbsoluteIndexOnLine(this._cursorIndex)
: textUpToCursor.length - lastNewlineIndex - 1;
this._writeEmitter.fire(ACTION_KEYS.MOVE_TO_POSITION_IN_LINE((positionOnCurrentLine % 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)
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 {
this._invalidateCompletionData();
vscode.env.clipboard.readText().then((text: string) => {
this.handleInput(text);
}, () => {
// Ignored
});
return false;
}
private _handleEscape (): boolean {
this._setToEmptyPrompt();
this._invalidateCompletionData();
return true;
}
private _requestCompletionData (code: string, offset: number): Promise<CompletionList> {
this._invalidateCompletionData();
this._pendingTabCompletionRequestNumber = this._pendingTabCompletionRequestNumber + 1;
this._notifier.sendNotification(Notification.TerminalCompletionRequest, {
requestId: this._pendingTabCompletionRequestNumber,
code,
offset
});
this._pendingTabCompletionPromise = createResolvablePromise<CompletionList>();
return this._pendingTabCompletionPromise;
}
private _handleCompletionDataResponse (data: MatlabData): void {
if (data.requestId === this._pendingTabCompletionRequestNumber && (this._pendingTabCompletionPromise != null)) {
this._pendingTabCompletionPromise.resolve(data.result);
}
}
private _invalidateCompletionData (): void {
this._latestTabCompletionData = undefined;
this._pendingTabCompletionPromise?.reject();
this._pendingTabCompletionPromise = undefined;
}
private _doCompletion (): boolean {
if (this._latestTabCompletionData === undefined || this._latestTabCompletionData?.items.length === 0) {
return false;
}
const currentCompletion = this._latestTabCompletionData.items[this._currentCompletionIndex].label;
const currentLine = this._stripCurrentPrompt(this._currentPromptLine);
// Split the current line into words and non-words
const words = currentLine.split(WORD_REGEX).filter(match => match !== undefined && match !== '');
const wordLengths = words.map(match => match.length);
const validWords = words.map(match => WORD_REGEX.test(match));
validWords.unshift(false);
// Find the first word/non-word the cursor is within
const cumulativeLengths = [];
let cumulativeLength = 0;
cumulativeLengths.push(0);
wordLengths.forEach((value) => {
cumulativeLength += value;
cumulativeLengths.push(cumulativeLength);
});
let i;
for (i = 0; i < cumulativeLengths.length; i++) {
if (this._cursorIndex <= cumulativeLengths[i]) {
break;
}
}
if (i === cumulativeLengths.length) {
return false;
}
// If the cursor is within or at the end of a valid word, then we want to replace that word.
if (validWords[i]) {
// Then get the code before the replacement and after the replacement
const codeBefore = currentLine.substring(0, cumulativeLengths[i - 1]);
const codeAfter = currentLine.substring(cumulativeLengths[i]);
// And construct the new line with the replacement made
const newLine = codeBefore + currentCompletion + codeAfter;
this._replaceCurrentLineWithNewLine(this._currentPrompt + newLine, codeBefore.length + currentCompletion.length);
} else {
// Otherwise we want to just insert the new completion directly at the cursor.
const codeBefore = currentLine.substring(0, this._cursorIndex);
const codeAfter = currentLine.substring(this._cursorIndex);
// And construct the new line with the replacement made
const newLine = codeBefore + currentCompletion + codeAfter;
this._replaceCurrentLineWithNewLine(this._currentPrompt + newLine, codeBefore.length + currentCompletion.length);
}
return true;
}
private _handleTab (direction: Direction): boolean {
// If we have data and that not been invalidated, just increment the match index and do the replacement
if (this._latestTabCompletionData !== undefined) {
this._currentCompletionIndex = (this._currentCompletionIndex + this._latestTabCompletionData.items.length + (direction === Direction.FORWARDS ? 1 : -1)) % this._latestTabCompletionData.items.length;
return this._doCompletion();
} else {
// Otherwise, request new completion data and do a completion when the data has come in.
const code = this._stripCurrentPrompt(this._currentPromptLine);
const offset = this._cursorIndex;
if (code.trim() === '') {
return false;
}
// If the request isn't invalidated before the data has arrived, then do the completion. Otherwise it will be rejected and ignored.
this._requestCompletionData(code, offset).then((completions: CompletionList) => {
this._latestTabCompletionData = completions;
this._currentCompletionIndex = 0;
this._doCompletion();
}, () => { /* intentionally empty */ });
}
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;
}
/**
* Get cursor position information for testing purposes.
* Returns the logical line number (0-based) and column position (0-based) within that line.
* For multi-line commands with explicit newlines, the line is determined by counting newlines.
*/
getCursorPosition (): { line: number, column: number } {
const textUpToCursor = this._currentPromptLine.substring(0, this._getAbsoluteIndexOnLine(this._cursorIndex));
const lastNewlineIndex = textUpToCursor.lastIndexOf('\n');
// Count newlines to determine line number
const line = (textUpToCursor.match(/\n/g) ?? []).length;
// Calculate column position within the current line
const column = lastNewlineIndex === -1
? this._cursorIndex // No newlines, so cursor is on first line
: textUpToCursor.length - lastNewlineIndex - 1 - this._currentPrompt.length;
return { line, column };
}
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);
}
}
}