Skip to content

Commit

Permalink
Multi Cursor for paredit Expand selection and shrink selection
Browse files Browse the repository at this point in the history
  • Loading branch information
riotrah committed Apr 2, 2022
1 parent d80fce8 commit f67d1fe
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 118 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Expand Up @@ -203,7 +203,11 @@
],
"cSpell.languageSettings": [
{
"languageId": ["clojure", "json", "typescript"],
"languageId": [
"clojure",
"json",
"typescript"
],
"allowCompoundWords": false
}
],
Expand Down
38 changes: 28 additions & 10 deletions src/cursor-doc/model.ts
@@ -1,4 +1,4 @@
import { isUndefined } from 'lodash';
import { isUndefined, max, min } from 'lodash';
import { deepEqual as equal } from '../util/object';
import { Scanner, ScannerState, Token } from './clojure-lexer';
import { LispTokenCursor } from './token-cursor';
Expand Down Expand Up @@ -84,6 +84,7 @@ export type ModelEditOptions = {
undoStopBefore?: boolean;
formatDepth?: number;
skipFormat?: boolean;
selections?: ModelEditSelection[];
selection?: ModelEditSelection;
};

Expand All @@ -105,11 +106,14 @@ export interface EditableModel {

export interface EditableDocument {
selection: ModelEditSelection;
selections: ModelEditSelection[];
model: EditableModel;
selectionsStack: ModelEditSelection[][];
selectionStack: ModelEditSelection[];
getTokenCursor: (offset?: number, previous?: boolean) => LispTokenCursor;
insertString: (text: string) => void;
getSelectionText: () => string;
getSelectionText: (index?: number) => string;
getSelectionsText: () => string[];
delete: () => Thenable<boolean>;
backspace: () => Thenable<boolean>;
}
Expand Down Expand Up @@ -536,6 +540,8 @@ export class StringDocument implements EditableDocument {

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

selectionsStack: ModelEditSelection[][] = [];
selections: ModelEditSelection[] = [];
selectionStack: ModelEditSelection[] = [];

getTokenCursor(offset?: number, previous?: boolean): LispTokenCursor {
Expand All @@ -546,23 +552,35 @@ export class StringDocument implements EditableDocument {
return this.model.getTokenCursor(offset);
}

getSelectionsText: () => string[];
insertString(text: string) {
this.model.insertString(0, text);
}

getSelectionText: () => string;

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

return this.model.edit(edits, {
selections,
});
}
getSelectionText: () => string;

backspace() {
const p = this.selection.anchor;
return this.model.edit([new ModelEdit('deleteRange', [p - 1, 1])], {
selection: new ModelEditSelection(p - 1),
const edits = [];
const selections = [];
this.selections.forEach(({ anchor: p }) => {
edits.push(new ModelEdit('deleteRange', [p - 1, 1]));
selections.push(new ModelEditSelection(p - 1));
});

return this.model.edit(edits, {
selections,
});
}
}

0 comments on commit f67d1fe

Please sign in to comment.