Skip to content

Commit 4f77f77

Browse files
committed
Make ModelEdit generic with edit fn as type param + utils, fix dead code
1 parent 2242e08 commit 4f77f77

File tree

2 files changed

+44
-82
lines changed

2 files changed

+44
-82
lines changed

src/cursor-doc/model.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ export class TextLine {
2828
}
2929

3030
export type ModelEditFunction = 'insertString' | 'changeRange' | 'deleteRange';
31-
32-
export class ModelEdit {
33-
constructor(public editFn: ModelEditFunction, public args: any[]) {}
31+
export type ModelEditFunctionArgs<T extends ModelEditFunction> = T extends 'insertString'
32+
? Parameters<LineInputModel['insertString']>
33+
: T extends 'changeRange'
34+
? Parameters<LineInputModel['changeRange']>
35+
: Parameters<LineInputModel['deleteRange']>;
36+
37+
export class ModelEdit<T extends ModelEditFunction = ModelEditFunction> {
38+
constructor(public editFn: T, public args: ModelEditFunctionArgs<T>) {}
3439
}
3540

3641
/**
@@ -191,7 +196,6 @@ export type ModelEditOptions = {
191196
undoStopBefore?: boolean;
192197
formatDepth?: number;
193198
skipFormat?: boolean;
194-
// selection?: ModelEditSelection;
195199
selections?: ModelEditSelection[];
196200
};
197201

@@ -208,7 +212,10 @@ export interface EditableModel {
208212
* For some EditableModel's these are performed as one atomic set of edits.
209213
* @param edits
210214
*/
211-
edit: (edits: ModelEdit[], options: ModelEditOptions) => Thenable<ModelEditResult>;
215+
edit: (
216+
edits: ModelEdit<ModelEditFunction>[],
217+
options: ModelEditOptions
218+
) => Thenable<ModelEditResult>;
212219

213220
getText: (start: number, end: number, mustBeWithin?: boolean) => string;
214221
getLineText: (line: number) => string;
@@ -523,13 +530,7 @@ export class LineInputModel implements EditableModel {
523530
* @param oldSelection the old selection
524531
* @param newSelection the new selection
525532
*/
526-
private changeRange(
527-
start: number,
528-
end: number,
529-
text: string,
530-
oldSelection?: [number, number],
531-
newSelection?: [number, number]
532-
) {
533+
private changeRange(start: number, end: number, text: string) {
533534
const t1 = new Date();
534535

535536
const startPos = Math.min(start, end);
@@ -595,13 +596,8 @@ export class LineInputModel implements EditableModel {
595596
* @param text the text to insert
596597
* @param oldCursor the [row,col] of the cursor at the start of the operation
597598
*/
598-
insertString(
599-
offset: number,
600-
text: string,
601-
oldSelection?: [number, number],
602-
newSelection?: [number, number]
603-
): number {
604-
this.changeRange(offset, offset, text, oldSelection, newSelection);
599+
insertString(offset: number, text: string): number {
600+
this.changeRange(offset, offset, text);
605601
return text.length;
606602
}
607603

@@ -620,7 +616,7 @@ export class LineInputModel implements EditableModel {
620616
oldSelection?: [number, number],
621617
newSelection?: [number, number]
622618
) {
623-
this.changeRange(offset, offset + count, '', oldSelection, newSelection);
619+
this.changeRange(offset, offset + count, '');
624620
}
625621

626622
/** Return the offset of the last character in this model. */

src/cursor-doc/paredit.ts

Lines changed: 28 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { isEqual, isNumber, last, pick, property, clone, isBoolean } from 'lodash';
1+
import { isEqual, last, pick, property, clone, isBoolean, orderBy } from 'lodash';
22
import { validPair } from './clojure-lexer';
3-
import { EditableDocument, ModelEdit, ModelEditSelection, ModelEditResult } from './model';
3+
import {
4+
EditableDocument,
5+
ModelEdit,
6+
ModelEditSelection,
7+
ModelEditResult,
8+
ModelEditFunctionArgs,
9+
} from './model';
410
import { LispTokenCursor } from './token-cursor';
511
import { replaceAt } from '../util/array';
6-
import { ShowDocumentRequest } from 'vscode-languageclient';
712

813
// NB: doc.model.edit returns a Thenable, so that the vscode Editor can compose commands.
914
// But don't put such chains in this module because that won't work in the repl-console.
@@ -595,8 +600,8 @@ export function wrapSexpr(
595600
new ModelEdit('insertString', [
596601
range[0],
597602
open,
598-
[end, end],
599-
[start + open.length, start + open.length],
603+
// [end, end],
604+
// [start + open.length, start + open.length],
600605
]),
601606
],
602607
{
@@ -703,8 +708,8 @@ export function joinSexp(doc: EditableDocument): Thenable<ModelEditResult> {
703708
prevEnd - 1,
704709
nextStart + 1,
705710
prevToken.type === 'close' ? ' ' : '',
706-
[start, start],
707-
[prevEnd, prevEnd],
711+
// [start, start],
712+
// [prevEnd, prevEnd],
708713
])
709714
);
710715
selections[index] = new ModelEditSelection(prevEnd);
@@ -810,7 +815,7 @@ export function _forwardSlurpSexpSingle(
810815
const wsEndOffset = wsOutSideCursor.offsetStart;
811816
const newCloseOffset = cursor.offsetStart;
812817
const replacedText = doc.model.getText(wsStartOffset, wsEndOffset);
813-
const changeArgs =
818+
const changeArgs: ModelEditFunctionArgs<'changeRange'> =
814819
replacedText.indexOf('\n') >= 0
815820
? [currentCloseOffset, currentCloseOffset + close.length, '']
816821
: [wsStartOffset, wsEndOffset, ' '];
@@ -1454,13 +1459,7 @@ export function transpose(
14541459
}
14551460
edits.push(
14561461
new ModelEdit('changeRange', [rightStart, rightEnd, leftText]),
1457-
new ModelEdit('changeRange', [
1458-
leftStart,
1459-
leftEnd,
1460-
rightText,
1461-
[left, left],
1462-
[newCursorPos, newCursorPos],
1463-
])
1462+
new ModelEdit('changeRange', [leftStart, leftEnd, rightText])
14641463
);
14651464
selections[index] = new ModelEditSelection(newCursorPos);
14661465
}
@@ -1681,10 +1680,7 @@ export function dragSexprBackwardUp(
16811680
wsInfo.rightWsRange[1] - currentRange[0],
16821681
]);
16831682
}
1684-
edits.push(
1685-
deleteEdit,
1686-
new ModelEdit('insertString', [listStart, dragText, [p, p], [newCursorPos, newCursorPos]])
1687-
);
1683+
edits.push(deleteEdit, new ModelEdit('insertString', [listStart, dragText]));
16881684
selections[index] = new ModelEditSelection(newCursorPos);
16891685
}
16901686
});
@@ -1722,12 +1718,7 @@ export function dragSexprForwardDown(
17221718
const insertText =
17231719
doc.model.getText(...currentRange) + (wsInfo.rightWsHasNewline ? '\n' : ' ');
17241720
edits.push(
1725-
new ModelEdit('insertString', [
1726-
insertStart,
1727-
insertText,
1728-
[p, p],
1729-
[newCursorPos, newCursorPos],
1730-
]),
1721+
new ModelEdit('insertString', [insertStart, insertText]),
17311722
new ModelEdit('deleteRange', [currentRange[0], deleteLength])
17321723
);
17331724
selections[index] = new ModelEditSelection(newCursorPos);
@@ -1770,7 +1761,7 @@ export function dragSexprForwardUp(
17701761
}
17711762
const newCursorPos = listEnd + newPosOffset + 1 - deleteLength;
17721763
edits.push(
1773-
new ModelEdit('insertString', [listEnd, dragText, [p, p], [newCursorPos, newCursorPos]]),
1764+
new ModelEdit('insertString', [listEnd, dragText]),
17741765
new ModelEdit('deleteRange', [deleteStart, deleteLength])
17751766
);
17761767
selections[index] = new ModelEditSelection(newCursorPos);
@@ -1813,12 +1804,7 @@ export function dragSexprBackwardDown(
18131804
insertText = (siblingWsInfo.leftWsHasNewline ? '\n' : ' ') + insertText;
18141805
edits.push(
18151806
new ModelEdit('deleteRange', [wsInfo.leftWsRange[0], deleteLength]),
1816-
new ModelEdit('insertString', [
1817-
insertStart,
1818-
insertText,
1819-
[p, p],
1820-
[newCursorPos, newCursorPos],
1821-
])
1807+
new ModelEdit('insertString', [insertStart, insertText])
18221808
);
18231809
selections[index] = new ModelEditSelection(newCursorPos);
18241810
break;
@@ -1872,21 +1858,11 @@ export function addRichComment(
18721858
checkIfRichCommentExistsCursor.forwardWhitespace(false);
18731859
// insert nothing, just place cursor
18741860
const newCursorPos = checkIfRichCommentExistsCursor.offsetStart;
1875-
void doc.model.edit(
1876-
[
1877-
new ModelEdit('insertString', [
1878-
newCursorPos,
1879-
'',
1880-
[newCursorPos, newCursorPos],
1881-
[newCursorPos, newCursorPos],
1882-
]),
1883-
],
1884-
{
1885-
selections: [new ModelEditSelection(newCursorPos)],
1886-
skipFormat: true,
1887-
undoStopBefore: false,
1888-
}
1889-
);
1861+
void doc.model.edit([new ModelEdit('insertString', [newCursorPos, ''])], {
1862+
selections: [new ModelEditSelection(newCursorPos)],
1863+
skipFormat: true,
1864+
undoStopBefore: false,
1865+
});
18901866
return;
18911867
}
18921868
}
@@ -1900,19 +1876,9 @@ export function addRichComment(
19001876
const append = '\n'.repeat(numAppendNls);
19011877
const insertText = `${prepend}${richComment}${append}`;
19021878
const newCursorPos = insertStart + 11 + numPrependNls * doc.model.lineEndingLength;
1903-
void doc.model.edit(
1904-
[
1905-
new ModelEdit('insertString', [
1906-
insertStart,
1907-
insertText,
1908-
[insertStart, insertStart],
1909-
[newCursorPos, newCursorPos],
1910-
]),
1911-
],
1912-
{
1913-
selections: [new ModelEditSelection(newCursorPos)],
1914-
skipFormat: false,
1915-
undoStopBefore: true,
1916-
}
1917-
);
1879+
void doc.model.edit([new ModelEdit('insertString', [insertStart, insertText])], {
1880+
selections: [new ModelEditSelection(newCursorPos)],
1881+
skipFormat: false,
1882+
undoStopBefore: true,
1883+
});
19181884
}

0 commit comments

Comments
 (0)