Skip to content

Commit e82aba7

Browse files
committed
Extract individual handlers from CodeEditorWidget.trigger
1 parent d886660 commit e82aba7

3 files changed

Lines changed: 120 additions & 76 deletions

File tree

src/vs/editor/browser/view/viewController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export interface ICommandDelegate {
4040
paste(text: string, pasteOnNewLine: boolean, multicursorText: string[] | null, mode: string | null): void;
4141
type(text: string): void;
4242
replacePreviousChar(text: string, replaceCharCnt: number): void;
43-
compositionStart(): void;
44-
compositionEnd(): void;
43+
startComposition(): void;
44+
endComposition(): void;
4545
cut(): void;
4646
}
4747

@@ -82,11 +82,11 @@ export class ViewController {
8282
}
8383

8484
public compositionStart(): void {
85-
this.commandDelegate.compositionStart();
85+
this.commandDelegate.startComposition();
8686
}
8787

8888
public compositionEnd(): void {
89-
this.commandDelegate.compositionEnd();
89+
this.commandDelegate.endComposition();
9090
}
9191

9292
public cut(): void {

src/vs/editor/browser/widget/codeEditorWidget.ts

Lines changed: 89 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -976,40 +976,31 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
976976
public trigger(source: string | null | undefined, handlerId: string, payload: any): void {
977977
payload = payload || {};
978978

979-
// Special case for typing
980-
if (handlerId === editorCommon.Handler.Type) {
981-
if (!this._modelData || typeof payload.text !== 'string' || payload.text.length === 0) {
982-
// nothing to do
979+
switch (handlerId) {
980+
case editorCommon.Handler.CompositionStart:
981+
this._startComposition();
982+
return;
983+
case editorCommon.Handler.CompositionEnd:
984+
this._endComposition(source);
985+
return;
986+
case editorCommon.Handler.Type: {
987+
const args = <Partial<editorCommon.TypePayload>>payload;
988+
this._type(source, args.text || '');
983989
return;
984990
}
985-
if (source === 'keyboard') {
986-
this._onWillType.fire(payload.text);
987-
}
988-
this._modelData.viewModel.cursor.type(payload.text, source);
989-
if (source === 'keyboard') {
990-
this._onDidType.fire(payload.text);
991-
}
992-
return;
993-
}
994-
995-
// Special case for pasting
996-
if (handlerId === editorCommon.Handler.Paste) {
997-
if (!this._modelData || typeof payload.text !== 'string' || payload.text.length === 0) {
998-
// nothing to do
991+
case editorCommon.Handler.ReplacePreviousChar: {
992+
const args = <Partial<editorCommon.ReplacePreviousCharPayload>>payload;
993+
this._replacePreviousChar(source, args.text || '', args.replaceCharCnt || 0);
999994
return;
1000995
}
1001-
const startPosition = this._modelData.viewModel.getSelection().getStartPosition();
1002-
this._modelData.viewModel.cursor.paste(<string>payload.text, <boolean>payload.pasteOnNewLine, <string[]>payload.multicursorText, source);
1003-
const endPosition = this._modelData.viewModel.getSelection().getStartPosition();
1004-
if (source === 'keyboard') {
1005-
this._onDidPaste.fire(
1006-
{
1007-
range: new Range(startPosition.lineNumber, startPosition.column, endPosition.lineNumber, endPosition.column),
1008-
mode: payload.mode
1009-
}
1010-
);
996+
case editorCommon.Handler.Paste: {
997+
const args = <Partial<editorCommon.PastePayload>>payload;
998+
this._paste(source, args.text || '', args.pasteOnNewLine || false, args.multicursorText || null, args.mode || null);
999+
return;
10111000
}
1012-
return;
1001+
case editorCommon.Handler.Cut:
1002+
this._cut(source);
1003+
return;
10131004
}
10141005

10151006
const action = this.getAction(handlerId);
@@ -1025,28 +1016,64 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
10251016
if (this._triggerEditorCommand(source, handlerId, payload)) {
10261017
return;
10271018
}
1019+
}
10281020

1029-
switch (handlerId) {
1030-
case editorCommon.Handler.CompositionStart:
1031-
this._modelData.viewModel.cursor.startComposition();
1032-
break;
1033-
case editorCommon.Handler.CompositionEnd:
1034-
this._modelData.viewModel.cursor.endComposition(source);
1035-
break;
1036-
case editorCommon.Handler.ReplacePreviousChar:
1037-
this._modelData.viewModel.cursor.replacePreviousChar(<string>payload.text, <number>payload.replaceCharCnt, source);
1038-
break;
1039-
case editorCommon.Handler.Cut:
1040-
this._modelData.viewModel.cursor.cut(source);
1041-
break;
1021+
private _startComposition(): void {
1022+
if (!this._modelData) {
1023+
return;
1024+
}
1025+
this._modelData.viewModel.cursor.startComposition();
1026+
this._onDidCompositionStart.fire();
1027+
}
1028+
1029+
private _endComposition(source: string | null | undefined): void {
1030+
if (!this._modelData) {
1031+
return;
1032+
}
1033+
this._modelData.viewModel.cursor.endComposition(source);
1034+
this._onDidCompositionEnd.fire();
1035+
}
1036+
1037+
private _type(source: string | null | undefined, text: string): void {
1038+
if (!this._modelData || text.length === 0) {
1039+
return;
1040+
}
1041+
if (source === 'keyboard') {
1042+
this._onWillType.fire(text);
1043+
}
1044+
this._modelData.viewModel.cursor.type(text, source);
1045+
if (source === 'keyboard') {
1046+
this._onDidType.fire(text);
1047+
}
1048+
}
1049+
1050+
private _replacePreviousChar(source: string | null | undefined, text: string, replaceCharCnt: number): void {
1051+
if (!this._modelData) {
1052+
return;
10421053
}
1054+
this._modelData.viewModel.cursor.replacePreviousChar(text, replaceCharCnt, source);
1055+
}
10431056

1044-
if (handlerId === editorCommon.Handler.CompositionStart) {
1045-
this._onDidCompositionStart.fire();
1057+
private _paste(source: string | null | undefined, text: string, pasteOnNewLine: boolean, multicursorText: string[] | null, mode: string | null): void {
1058+
if (!this._modelData || text.length === 0) {
1059+
return;
10461060
}
1047-
if (handlerId === editorCommon.Handler.CompositionEnd) {
1048-
this._onDidCompositionEnd.fire();
1061+
const startPosition = this._modelData.viewModel.getSelection().getStartPosition();
1062+
this._modelData.viewModel.cursor.paste(text, pasteOnNewLine, multicursorText, source);
1063+
const endPosition = this._modelData.viewModel.getSelection().getStartPosition();
1064+
if (source === 'keyboard') {
1065+
this._onDidPaste.fire({
1066+
range: new Range(startPosition.lineNumber, startPosition.column, endPosition.lineNumber, endPosition.column),
1067+
mode: mode
1068+
});
1069+
}
1070+
}
1071+
1072+
private _cut(source: string | null | undefined): void {
1073+
if (!this._modelData) {
1074+
return;
10491075
}
1076+
this._modelData.viewModel.cursor.cut(source);
10501077
}
10511078

10521079
private _triggerEditorCommand(source: string | null | undefined, handlerId: string, payload: any): boolean {
@@ -1520,22 +1547,22 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
15201547
editorCommand.runCoreEditorCommand(viewModel.getCursors(), args);
15211548
},
15221549
paste: (text: string, pasteOnNewLine: boolean, multicursorText: string[] | null, mode: string | null) => {
1523-
this.trigger('keyboard', editorCommon.Handler.Paste, { text, pasteOnNewLine, multicursorText, mode });
1550+
this._paste('keyboard', text, pasteOnNewLine, multicursorText, mode);
15241551
},
15251552
type: (text: string) => {
1526-
this.trigger('keyboard', editorCommon.Handler.Type, { text });
1553+
this._type('keyboard', text);
15271554
},
15281555
replacePreviousChar: (text: string, replaceCharCnt: number) => {
1529-
this.trigger('keyboard', editorCommon.Handler.ReplacePreviousChar, { text, replaceCharCnt });
1556+
this._replacePreviousChar('keyboard', text, replaceCharCnt);
15301557
},
1531-
compositionStart: () => {
1532-
this.trigger('keyboard', editorCommon.Handler.CompositionStart, undefined);
1558+
startComposition: () => {
1559+
this._startComposition();
15331560
},
1534-
compositionEnd: () => {
1535-
this.trigger('keyboard', editorCommon.Handler.CompositionEnd, undefined);
1561+
endComposition: () => {
1562+
this._endComposition('keyboard');
15361563
},
15371564
cut: () => {
1538-
this.trigger('keyboard', editorCommon.Handler.Cut, undefined);
1565+
this._cut('keyboard');
15391566
}
15401567
};
15411568
} else {
@@ -1544,28 +1571,21 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
15441571
editorCommand.runCoreEditorCommand(viewModel.getCursors(), args);
15451572
},
15461573
paste: (text: string, pasteOnNewLine: boolean, multicursorText: string[] | null, mode: string | null) => {
1547-
this._commandService.executeCommand(editorCommon.Handler.Paste, {
1548-
text: text,
1549-
pasteOnNewLine: pasteOnNewLine,
1550-
multicursorText: multicursorText,
1551-
mode
1552-
});
1574+
const payload: editorCommon.PastePayload = { text, pasteOnNewLine, multicursorText, mode };
1575+
this._commandService.executeCommand(editorCommon.Handler.Paste, payload);
15531576
},
15541577
type: (text: string) => {
1555-
this._commandService.executeCommand(editorCommon.Handler.Type, {
1556-
text: text
1557-
});
1578+
const payload: editorCommon.TypePayload = { text };
1579+
this._commandService.executeCommand(editorCommon.Handler.Type, payload);
15581580
},
15591581
replacePreviousChar: (text: string, replaceCharCnt: number) => {
1560-
this._commandService.executeCommand(editorCommon.Handler.ReplacePreviousChar, {
1561-
text: text,
1562-
replaceCharCnt: replaceCharCnt
1563-
});
1582+
const payload: editorCommon.ReplacePreviousCharPayload = { text, replaceCharCnt };
1583+
this._commandService.executeCommand(editorCommon.Handler.ReplacePreviousChar, payload);
15641584
},
1565-
compositionStart: () => {
1585+
startComposition: () => {
15661586
this._commandService.executeCommand(editorCommon.Handler.CompositionStart, {});
15671587
},
1568-
compositionEnd: () => {
1588+
endComposition: () => {
15691589
this._commandService.executeCommand(editorCommon.Handler.CompositionEnd, {});
15701590
},
15711591
cut: () => {

src/vs/editor/common/editorCommon.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,11 +689,35 @@ export const EditorType = {
689689
* @internal
690690
*/
691691
export const enum Handler {
692-
ExecuteCommands = 'executeCommands',
693-
Type = 'type',
694-
ReplacePreviousChar = 'replacePreviousChar',
695692
CompositionStart = 'compositionStart',
696693
CompositionEnd = 'compositionEnd',
694+
Type = 'type',
695+
ReplacePreviousChar = 'replacePreviousChar',
697696
Paste = 'paste',
698697
Cut = 'cut',
699698
}
699+
700+
/**
701+
* @internal
702+
*/
703+
export interface TypePayload {
704+
text: string;
705+
}
706+
707+
/**
708+
* @internal
709+
*/
710+
export interface ReplacePreviousCharPayload {
711+
text: string;
712+
replaceCharCnt: number;
713+
}
714+
715+
/**
716+
* @internal
717+
*/
718+
export interface PastePayload {
719+
text: string;
720+
pasteOnNewLine: boolean;
721+
multicursorText: string[] | null;
722+
mode: string | null;
723+
}

0 commit comments

Comments
 (0)