Skip to content

Commit

Permalink
WIP Add drag swexp back up
Browse files Browse the repository at this point in the history
When this PR is done it fixes #500
  • Loading branch information
PEZ committed Dec 15, 2019
1 parent f348385 commit b899044
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,11 @@
"command": "paredit.pushSexprRight",
"title": "Push Sexp Right"
},
{
"category": "Calva Paredit",
"command": "paredit.pushSexprBackwardUp",
"title": "Push Sexp Backward Up"
},
{
"category": "Calva Paredit",
"command": "paredit.convolute",
Expand Down
18 changes: 18 additions & 0 deletions src/cursor-doc/paredit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,22 @@ export function pushSexprRight(doc: EditableDocument, start = doc.selectionLeft,
if (forwardRange[0] !== currentRange[0]) { // there is a sexp to the right
transpose(doc, start, currentRange[1], { fromRight: newPosOffset });
}
}

export function pushSexprBackwardUp(doc: EditableDocument, start = doc.selectionLeft, end = doc.selectionRight) {
if (start == end) {
const p = start,
cursor = doc.getTokenCursor(p),
currentRange = cursor.rangeForCurrentForm(p),
currentText = doc.model.getText(...currentRange),
currentListRange = cursor.rangeForList();
if (currentListRange !== undefined) {
const newPosOffset = p - currentRange[0],
newCursorPos = currentListRange[0] + newPosOffset;
doc.model.edit([
new ModelEdit('deleteRange', [currentRange[0], currentRange[1] - currentRange[0]]),
new ModelEdit('insertString', [currentListRange[0], currentText, [p, p], [newCursorPos, newCursorPos]])
], { selection: new ModelEditSelection(newCursorPos) });
}
}
}
6 changes: 3 additions & 3 deletions src/cursor-doc/token-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class LispTokenCursor extends TokenCursor {
* Finds the range of the current list. If you are particular about which type of list, supply the `openingBracket`
* @param openingBracket
*/
rangeForList(openingBracket?: string): [[number, number], [number, number]] {
rangeForList(openingBracket?: string): [number, number] {
const cursor = this.clone();
if (openingBracket === undefined) {
if (!(cursor.backwardList() && cursor.backwardUpList())) {
Expand All @@ -354,11 +354,11 @@ export class LispTokenCursor extends TokenCursor {
return undefined;
}
}
const start = cursor.rowCol as [number, number];
const start = cursor.offsetStart;
if (!cursor.forwardSexp()) {
return undefined;
}
const end = cursor.rowCol as [number, number];
const end = cursor.offsetEndß;
return [start, end]
}

Expand Down
5 changes: 5 additions & 0 deletions src/paredit/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ const pareditCommands: PareditCommand[] = [
handler: paredit.pushSexprRight,
replWindowCommand: "push-sexp-right"
},
{
command: 'paredit.pushSexprBackwardUp',
handler: paredit.pushSexprBackwardUp,
replWindowCommand: "push-sexp-backward-up"
},
{
command: 'paredit.convolute',
handler: paredit.convolute,
Expand Down
6 changes: 6 additions & 0 deletions src/webview/repl-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ export class ReplConsole {
// this.readline.repaint();
// });
},
"push-sexp-backward-up": () => {
this.readline.withUndo(() => {
paredit.pushSexprBackwardUp(this.readline);
this.readline.repaint();
});
},
"convolute-sexp": () => {
console.warn("Convolute is disabled in the REPL window, because: https://github.com/BetterThanTomorrow/calva/issues/490");
// this.readline.withUndo(() => {
Expand Down

0 comments on commit b899044

Please sign in to comment.