Skip to content

Commit

Permalink
BetterThanTomorrow#2691 - reduce the awaiting in deleteForward
Browse files Browse the repository at this point in the history
  • Loading branch information
pbwolf committed Jan 4, 2025
1 parent 901737a commit fa5e7c3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 48 deletions.
8 changes: 0 additions & 8 deletions src/cursor-doc/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ export interface EditableDocument {
getTokenCursor: (offset?: number, previous?: boolean) => LispTokenCursor;
insertString: (text: string) => void;
getSelectionText: () => string;
delete: () => Thenable<boolean>;
}

/** The underlying model for the REPL readline. */
Expand Down Expand Up @@ -798,11 +797,4 @@ export class StringDocument implements EditableDocument {
}

getSelectionText: () => string;

delete() {
const p = this.selections[0].anchor;
return this.model.edit([new ModelEdit('deleteRange', [p, 1])], {
selections: [new ModelEditSelection(p)],
});
}
}
22 changes: 14 additions & 8 deletions src/cursor-doc/paredit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,35 +1163,41 @@ export function backspace(
}
}

export async function deleteForward(
export function deleteForward(
doc: EditableDocument,
builder?: TextEditorEdit,
start: number = doc.selections[0].anchor,
end: number = doc.selections[0].active
) {
if (start != end) {
await doc.delete();
const [left, right] = [Math.min(start, end), Math.max(start, end)];
return doc.model.editNow([new ModelEdit('deleteRange', [start, end - start])], {
builder: builder,
});
} else {
const cursor = doc.getTokenCursor(start);
const prevToken = cursor.getPrevToken();
const nextToken = cursor.getToken();
const p = start;
if (doc.model.getText(p, p + 2, true) == '\\"') {
return doc.model.edit([new ModelEdit('deleteRange', [p, 2])], {
selections: [new ModelEditSelection(p)],
return doc.model.editNow([new ModelEdit('deleteRange', [p, 2])], {
builder: builder,
});
} else if (prevToken.type === 'open' && nextToken.type === 'close') {
return doc.model.edit(
return doc.model.editNow(
[new ModelEdit('deleteRange', [p - prevToken.raw.length, prevToken.raw.length + 1])],
{
selections: [new ModelEditSelection(p - prevToken.raw.length)],
builder: builder,
}
);
} else {
if (['open', 'close'].includes(nextToken.type) && cursor.docIsBalanced()) {
doc.selections = [new ModelEditSelection(p + 1)];
return new Promise<boolean>((resolve) => resolve(true));
return;
} else {
return doc.delete();
return doc.model.editNow([new ModelEdit('deleteRange', [start, 1])], {
builder: builder,
});
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/doc-mirror/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,6 @@ export class MirroredDocument implements EditableDocument {
selection = editor.selections[0];
return this.document.getText(selection);
}

public delete(): Thenable<boolean> {
return vscode.commands.executeCommand('deleteRight');
}
}

let registered = false;
Expand Down
52 changes: 26 additions & 26 deletions src/extension-test/unit/cursor-doc/paredit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2043,83 +2043,83 @@ describe('paredit', () => {
});

describe('Kill character forwards (delete)', () => {
it('Leaves closing paren of empty list alone', async () => {
it('Leaves closing paren of empty list alone', () => {
const a = docFromTextNotation('{::foo |()• ::bar :foo}');
const b = docFromTextNotation('{::foo (|)• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes closing paren if unbalance', async () => {
it('Deletes closing paren if unbalance', () => {
const a = docFromTextNotation('{::foo |)• ::bar :foo}');
const b = docFromTextNotation('{::foo |• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Leaves opening paren of non-empty list alone', async () => {
it('Leaves opening paren of non-empty list alone', () => {
const a = docFromTextNotation('{::foo |(a)• ::bar :foo}');
const b = docFromTextNotation('{::foo (|a)• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Leaves opening quote of non-empty string alone', async () => {
it('Leaves opening quote of non-empty string alone', () => {
const a = docFromTextNotation('{::foo |"a"• ::bar :foo}');
const b = docFromTextNotation('{::foo "|a"• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Leaves closing quote of non-empty string alone', async () => {
it('Leaves closing quote of non-empty string alone', () => {
const a = docFromTextNotation('{::foo "a|"• ::bar :foo}');
const b = docFromTextNotation('{::foo "a"|• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes contents in strings', async () => {
it('Deletes contents in strings', () => {
const a = docFromTextNotation('{::foo "|a"• ::bar :foo}');
const b = docFromTextNotation('{::foo "|"• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes contents in strings 2', async () => {
it('Deletes contents in strings 2', () => {
const a = docFromTextNotation('{::foo "|aa"• ::bar :foo}');
const b = docFromTextNotation('{::foo "|a"• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes quoted quote', async () => {
it('Deletes quoted quote', () => {
const a = docFromTextNotation('{::foo |\\"• ::bar :foo}');
const b = docFromTextNotation('{::foo |• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes quoted quote in string', async () => {
it('Deletes quoted quote in string', () => {
const a = docFromTextNotation('{::foo "|\\""• ::bar :foo}');
const b = docFromTextNotation('{::foo "|"• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes contents in list', async () => {
it('Deletes contents in list', () => {
const a = docFromTextNotation('{::foo (|a)• ::bar :foo}');
const b = docFromTextNotation('{::foo (|)• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes empty list function', async () => {
it('Deletes empty list function', () => {
const a = docFromTextNotation('{::foo (|)• ::bar :foo}');
const b = docFromTextNotation('{::foo |• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes empty set', async () => {
it('Deletes empty set', () => {
const a = docFromTextNotation('#{|}');
const b = docFromTextNotation('|');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Deletes empty literal function with trailing newline', async () => {
it('Deletes empty literal function with trailing newline', () => {
// https://github.com/BetterThanTomorrow/calva/issues/1079
const a = docFromTextNotation('{::foo #(|)• ::bar :foo}');
const b = docFromTextNotation('{::foo |• ::bar :foo}');
await paredit.deleteForward(a);
paredit.deleteForward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/paredit/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ const pareditCommands = [
},
{
command: 'paredit.deleteForward',
handler: async (doc: EditableDocument) => {
await paredit.deleteForward(doc);
handlerNow: (doc: EditableDocument, builder: vscode.TextEditorEdit) => {
paredit.deleteForward(doc, builder);
},
},
{
Expand Down

0 comments on commit fa5e7c3

Please sign in to comment.