Skip to content

Commit

Permalink
fixes #1607 selection api cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
riotrah committed Mar 26, 2022
1 parent 51e8689 commit da0bf02
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 300 deletions.
24 changes: 6 additions & 18 deletions src/cursor-doc/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Scanner, Token, ScannerState } from './clojure-lexer';
import { LispTokenCursor } from './token-cursor';
import { deepEqual as equal } from '../util/object';
import { isUndefined } from 'lodash';
import { deepEqual as equal } from '../util/object';
import { Scanner, ScannerState, Token } from './clojure-lexer';
import { LispTokenCursor } from './token-cursor';

let scanner: Scanner;

Expand Down Expand Up @@ -104,8 +104,6 @@ export interface EditableModel {
}

export interface EditableDocument {
readonly selectionLeft: number;
readonly selectionRight: number;
selection: ModelEditSelection;
model: EditableModel;
selectionStack: ModelEditSelection[];
Expand Down Expand Up @@ -534,17 +532,7 @@ export class StringDocument implements EditableDocument {
}
}

selectionLeft: number;
selectionRight: number;

get selection() {
return new ModelEditSelection(this.selectionLeft, this.selectionRight);
}

set selection(sel: ModelEditSelection) {
this.selectionLeft = sel.anchor;
this.selectionRight = sel.active;
}
selection: ModelEditSelection;

model: LineInputModel = new LineInputModel(1, this);

Expand All @@ -565,14 +553,14 @@ export class StringDocument implements EditableDocument {
getSelectionText: () => string;

delete() {
const p = this.selectionLeft;
const p = this.selection.anchor;
return this.model.edit([new ModelEdit('deleteRange', [p, 1])], {
selection: new ModelEditSelection(p),
});
}

backspace() {
const p = this.selectionLeft;
const p = this.selection.anchor;
return this.model.edit([new ModelEdit('deleteRange', [p - 1, 1])], {
selection: new ModelEditSelection(p - 1),
});
Expand Down
91 changes: 45 additions & 46 deletions src/cursor-doc/paredit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { includes } from 'lodash';
import { validPair } from './clojure-lexer';
import { ModelEdit, EditableDocument, ModelEditSelection } from './model';
import { EditableDocument, ModelEdit, ModelEditSelection } from './model';
import { LispTokenCursor } from './token-cursor';

// NB: doc.model.edit returns a Thenable, so that the vscode Editor can compose commands.
Expand All @@ -16,8 +15,8 @@ import { LispTokenCursor } from './token-cursor';
export function killRange(
doc: EditableDocument,
range: [number, number],
start = doc.selectionLeft,
end = doc.selectionRight
start = doc.selection.anchor,
end = doc.selection.active
) {
const [left, right] = [Math.min(...range), Math.max(...range)];
void doc.model.edit([new ModelEdit('deleteRange', [left, right - left, [start, end]])], {
Expand Down Expand Up @@ -86,7 +85,7 @@ export function selectBackwardDownSexp(doc: EditableDocument) {
}

export function selectForwardUpSexp(doc: EditableDocument) {
selectRangeForward(doc, rangeToForwardUpList(doc, doc.selectionRight));
selectRangeForward(doc, rangeToForwardUpList(doc, doc.selection.active));
}

export function selectBackwardUpSexp(doc: EditableDocument) {
Expand All @@ -98,7 +97,7 @@ export function selectBackwardUpSexp(doc: EditableDocument) {
}

export function selectCloseList(doc: EditableDocument) {
selectRangeForward(doc, rangeToForwardList(doc, doc.selectionRight));
selectRangeForward(doc, rangeToForwardList(doc, doc.selection.active));
}

export function selectOpenList(doc: EditableDocument) {
Expand Down Expand Up @@ -159,7 +158,7 @@ export function backwardSexpRange(

export function forwardListRange(
doc: EditableDocument,
start: number = doc.selectionRight
start: number = doc.selection.active
): [number, number] {
const cursor = doc.getTokenCursor(start);
cursor.forwardList();
Expand All @@ -168,7 +167,7 @@ export function forwardListRange(

export function backwardListRange(
doc: EditableDocument,
start: number = doc.selectionRight
start: number = doc.selection.active
): [number, number] {
const cursor = doc.getTokenCursor(start);
cursor.backwardList();
Expand Down Expand Up @@ -358,8 +357,8 @@ export function wrapSexpr(
doc: EditableDocument,
open: string,
close: string,
start: number = doc.selectionLeft,
end: number = doc.selectionRight,
start: number = doc.selection.anchor,
end: number = doc.selection.active,
options = { skipFormat: false }
): Thenable<boolean> {
const cursor = doc.getTokenCursor(end);
Expand Down Expand Up @@ -407,8 +406,8 @@ export function rewrapSexpr(
doc: EditableDocument,
open: string,
close: string,
start: number = doc.selectionLeft,
end: number = doc.selectionRight
start: number = doc.selection.anchor,
end: number = doc.selection.active
): Thenable<boolean> {
const cursor = doc.getTokenCursor(end);
if (cursor.backwardList()) {
Expand All @@ -428,7 +427,7 @@ export function rewrapSexpr(
}
}

export function splitSexp(doc: EditableDocument, start: number = doc.selectionRight) {
export function splitSexp(doc: EditableDocument, start: number = doc.selection.active) {
const cursor = doc.getTokenCursor(start);
if (!cursor.withinString() && !(cursor.isWhiteSpace() || cursor.previousIsWhiteSpace())) {
cursor.forwardWhitespace();
Expand All @@ -452,7 +451,7 @@ export function splitSexp(doc: EditableDocument, start: number = doc.selectionRi
*/
export function joinSexp(
doc: EditableDocument,
start: number = doc.selectionRight
start: number = doc.selection.active
): Thenable<boolean> {
const cursor = doc.getTokenCursor(start);
cursor.backwardWhitespace();
Expand Down Expand Up @@ -481,7 +480,7 @@ export function joinSexp(

export function spliceSexp(
doc: EditableDocument,
start: number = doc.selectionRight,
start: number = doc.selection.active,
undoStopBefore = true
): Thenable<boolean> {
const cursor = doc.getTokenCursor(start);
Expand Down Expand Up @@ -542,7 +541,7 @@ export function killForwardList(

export function forwardSlurpSexp(
doc: EditableDocument,
start: number = doc.selectionRight,
start: number = doc.selection.active,
extraOpts = { formatDepth: 1 }
) {
const cursor = doc.getTokenCursor(start);
Expand Down Expand Up @@ -587,7 +586,7 @@ export function forwardSlurpSexp(

export function backwardSlurpSexp(
doc: EditableDocument,
start: number = doc.selectionRight,
start: number = doc.selection.active,
extraOpts = {}
) {
const cursor = doc.getTokenCursor(start);
Expand Down Expand Up @@ -621,7 +620,7 @@ export function backwardSlurpSexp(
}
}

export function forwardBarfSexp(doc: EditableDocument, start: number = doc.selectionRight) {
export function forwardBarfSexp(doc: EditableDocument, start: number = doc.selection.active) {
const cursor = doc.getTokenCursor(start);
cursor.forwardList();
if (cursor.getToken().type == 'close') {
Expand All @@ -644,7 +643,7 @@ export function forwardBarfSexp(doc: EditableDocument, start: number = doc.selec
}
}

export function backwardBarfSexp(doc: EditableDocument, start: number = doc.selectionRight) {
export function backwardBarfSexp(doc: EditableDocument, start: number = doc.selection.active) {
const cursor = doc.getTokenCursor(start);
cursor.backwardList();
const tk = cursor.getPrevToken();
Expand Down Expand Up @@ -674,9 +673,9 @@ export function open(
doc: EditableDocument,
open: string,
close: string,
start: number = doc.selectionRight
start: number = doc.selection.active
) {
const [cs, ce] = [doc.selectionLeft, doc.selectionRight];
const [cs, ce] = [doc.selection.anchor, doc.selection.active];
doc.insertString(open + doc.getSelectionText() + close);
if (cs != ce) {
doc.selection = new ModelEditSelection(cs + open.length, ce + open.length);
Expand All @@ -694,7 +693,7 @@ function docIsBalanced(doc: EditableDocument, start: number = doc.selection.acti
return cursor.atEnd();
}

export function close(doc: EditableDocument, close: string, start: number = doc.selectionRight) {
export function close(doc: EditableDocument, close: string, start: number = doc.selection.active) {
const cursor = doc.getTokenCursor(start);
const inString = cursor.withinString();
cursor.forwardWhitespace(false);
Expand Down Expand Up @@ -754,8 +753,8 @@ export function backspace(

export function deleteForward(
doc: EditableDocument,
start: number = doc.selectionLeft,
end: number = doc.selectionRight
start: number = doc.selection.anchor,
end: number = doc.selection.active
) {
if (start != end) {
void doc.delete();
Expand Down Expand Up @@ -788,8 +787,8 @@ export function deleteForward(

export function stringQuote(
doc: EditableDocument,
start: number = doc.selectionLeft,
end: number = doc.selectionRight
start: number = doc.selection.anchor,
end: number = doc.selection.active
) {
if (start != end) {
doc.insertString('"');
Expand Down Expand Up @@ -826,8 +825,8 @@ export function stringQuote(

export function growSelection(
doc: EditableDocument,
start: number = doc.selectionLeft,
end: number = doc.selectionRight
start: number = doc.selection.anchor,
end: number = doc.selection.active
) {
const startC = doc.getTokenCursor(start),
endC = doc.getTokenCursor(end),
Expand Down Expand Up @@ -876,7 +875,7 @@ export function growSelectionStack(doc: EditableDocument, range: [number, number
const [start, end] = range;
if (doc.selectionStack.length > 0) {
const prev = doc.selectionStack[doc.selectionStack.length - 1];
if (!(doc.selectionLeft == prev.anchor && doc.selectionRight == prev.active)) {
if (!(doc.selection.anchor == prev.anchor && doc.selection.active == prev.active)) {
setSelectionStack(doc);
} else if (prev.anchor === range[0] && prev.active === range[1]) {
return;
Expand All @@ -893,8 +892,8 @@ export function shrinkSelection(doc: EditableDocument) {
const latest = doc.selectionStack.pop();
if (
doc.selectionStack.length &&
latest.anchor == doc.selectionLeft &&
latest.active == doc.selectionRight
latest.anchor == doc.selection.anchor &&
latest.active == doc.selection.active
) {
doc.selection = doc.selectionStack[doc.selectionStack.length - 1];
}
Expand All @@ -907,8 +906,8 @@ export function setSelectionStack(doc: EditableDocument, selection = doc.selecti

export function raiseSexp(
doc: EditableDocument,
start = doc.selectionLeft,
end = doc.selectionRight
start = doc.selection.anchor,
end = doc.selection.active
) {
const cursor = doc.getTokenCursor(end);
const [formStart, formEnd] = cursor.rangeForCurrentForm(start);
Expand Down Expand Up @@ -937,8 +936,8 @@ export function raiseSexp(

export function convolute(
doc: EditableDocument,
start = doc.selectionLeft,
end = doc.selectionRight
start = doc.selection.anchor,
end = doc.selection.active
) {
if (start == end) {
const cursorStart = doc.getTokenCursor(end);
Expand Down Expand Up @@ -977,8 +976,8 @@ export function convolute(

export function transpose(
doc: EditableDocument,
left = doc.selectionLeft,
right = doc.selectionRight,
left = doc.selection.anchor,
right = doc.selection.active,
newPosOffset: { fromLeft?: number; fromRight?: number } = {}
) {
const cursor = doc.getTokenCursor(right);
Expand Down Expand Up @@ -1088,8 +1087,8 @@ function currentSexpsRange(
export function dragSexprBackward(
doc: EditableDocument,
pairForms = bindingForms,
left = doc.selectionLeft,
right = doc.selectionRight
left = doc.selection.anchor,
right = doc.selection.active
) {
const cursor = doc.getTokenCursor(right);
const usePairs = isInPairsList(cursor, pairForms);
Expand All @@ -1115,8 +1114,8 @@ export function dragSexprBackward(
export function dragSexprForward(
doc: EditableDocument,
pairForms = bindingForms,
left = doc.selectionLeft,
right = doc.selectionRight
left = doc.selection.anchor,
right = doc.selection.active
) {
const cursor = doc.getTokenCursor(right);
const usePairs = isInPairsList(cursor, pairForms);
Expand Down Expand Up @@ -1161,7 +1160,7 @@ export type WhitespaceInfo = {
*/
export function collectWhitespaceInfo(
doc: EditableDocument,
p = doc.selectionRight
p = doc.selection.active
): WhitespaceInfo {
const cursor = doc.getTokenCursor(p);
const currentRange = cursor.rangeForCurrentForm(p);
Expand Down Expand Up @@ -1189,7 +1188,7 @@ export function collectWhitespaceInfo(
};
}

export function dragSexprBackwardUp(doc: EditableDocument, p = doc.selectionRight) {
export function dragSexprBackwardUp(doc: EditableDocument, p = doc.selection.active) {
const wsInfo = collectWhitespaceInfo(doc, p);
const cursor = doc.getTokenCursor(p);
const currentRange = cursor.rangeForCurrentForm(p);
Expand Down Expand Up @@ -1230,7 +1229,7 @@ export function dragSexprBackwardUp(doc: EditableDocument, p = doc.selectionRigh
}
}

export function dragSexprForwardDown(doc: EditableDocument, p = doc.selectionRight) {
export function dragSexprForwardDown(doc: EditableDocument, p = doc.selection.active) {
const wsInfo = collectWhitespaceInfo(doc, p);
const currentRange = doc.getTokenCursor(p).rangeForCurrentForm(p);
const newPosOffset = p - currentRange[0];
Expand Down Expand Up @@ -1266,7 +1265,7 @@ export function dragSexprForwardDown(doc: EditableDocument, p = doc.selectionRig
}
}

export function dragSexprForwardUp(doc: EditableDocument, p = doc.selectionRight) {
export function dragSexprForwardUp(doc: EditableDocument, p = doc.selection.active) {
const wsInfo = collectWhitespaceInfo(doc, p);
const cursor = doc.getTokenCursor(p);
const currentRange = cursor.rangeForCurrentForm(p);
Expand Down Expand Up @@ -1297,7 +1296,7 @@ export function dragSexprForwardUp(doc: EditableDocument, p = doc.selectionRight
}
}

export function dragSexprBackwardDown(doc: EditableDocument, p = doc.selectionRight) {
export function dragSexprBackwardDown(doc: EditableDocument, p = doc.selection.active) {
const wsInfo = collectWhitespaceInfo(doc, p);
const currentRange = doc.getTokenCursor(p).rangeForCurrentForm(p);
const newPosOffset = p - currentRange[0];
Expand Down
Loading

0 comments on commit da0bf02

Please sign in to comment.