Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drag backwards around ignore markers #2170

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cursor-doc/token-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export class LispTokenCursor extends TokenCursor {
let hasReader = false;
while (true) {
cursor.backwardWhitespace();
if (cursor.getPrevToken().type === 'reader') {
if (['reader', 'ignore'].includes(cursor.getPrevToken().type)) {
cursor.previous();
this.set(cursor);
hasReader = true;
Expand All @@ -425,7 +425,7 @@ export class LispTokenCursor extends TokenCursor {
let hasReader = false;
while (true) {
cursor.forwardWhitespace();
if (cursor.getToken().type === 'reader') {
if (['reader', 'ignore'].includes(cursor.getToken().type)) {
cursor.next();
this.set(cursor);
hasReader = true;
Expand Down Expand Up @@ -653,6 +653,7 @@ export class LispTokenCursor extends TokenCursor {
cursor.getToken().type !== 'reader' &&
!cursor.tokenBeginsMetadata() &&
cursor.getPrevToken().type !== 'reader' &&
cursor.getPrevToken().type !== 'ignore' &&
!cursor.prevTokenBeginsMetadata()
) {
if (cursor.backwardSexp() && !cursor.tokenBeginsMetadata()) {
Expand Down
56 changes: 56 additions & 0 deletions src/extension-test/unit/cursor-doc/paredit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,62 @@ describe('paredit', () => {
await paredit.dragSexprBackward(a, ['b']);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});

describe('Ignore markers', () => {
it('Drags past symbol', async () => {
const a = docFromTextNotation('#_a (:b c)|');
const b = docFromTextNotation('#_(:b c)| a');
await paredit.dragSexprBackward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Drags past map', async () => {
const a = docFromTextNotation('#_{:a b} (:c d)|');
const b = docFromTextNotation('#_(:c d)| {:a b}');
await paredit.dragSexprBackward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Drags past ignore after newline', async () => {
const a = docFromTextNotation('(:a b)•#_{:c d}|');
const b = docFromTextNotation('(:a b)•{:c d}|#_');
await paredit.dragSexprBackward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
it('Drags past ignore after space', async () => {
const a = docFromTextNotation('(:a b) #_{:c d}|');
const b = docFromTextNotation('(:a b) {:c d}|#_');
await paredit.dragSexprBackward(a);
expect(textAndSelection(a)).toEqual(textAndSelection(b));
});
});
});

describe('currentSexpsRange', () => {
const docSelectionCursorPosition = (positionNotation: string, rangeNotation: string) => {
const positionDoc = docFromTextNotation(positionNotation);
const rangeDoc = docFromTextNotation(rangeNotation);
const [_text1, positionSelection] = textAndSelection(positionDoc);
const position = positionSelection[0];
const [_text2, selection] = textAndSelection(rangeDoc);
const cursor = positionDoc.getTokenCursor(position);
return { doc: positionDoc, selection: selection, cursor, position };
};

it('Finds range with cursor between ignore marker and form, separated by whitespace before', () => {
const { doc, selection, cursor, position } = docSelectionCursorPosition(
'#_ |(:a b)',
'#_ |(:a b)|'
);
const range = paredit.currentSexpsRange(doc, cursor, position, false);
expect(range).toStrictEqual(selection);
});
it('Finds range with cursor between ignore marker and form, no whitespace', () => {
const { doc, selection, cursor, position } = docSelectionCursorPosition(
'#_|(:a b)',
'#_|(:a b)|'
);
const range = paredit.currentSexpsRange(doc, cursor, position, false);
expect(range).toStrictEqual(selection);
});
});

describe('backwardUp - one line', () => {
Expand Down
12 changes: 12 additions & 0 deletions src/extension-test/unit/cursor-doc/token-cursor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,18 @@ describe('Token Cursor', () => {
const cursor: LispTokenCursor = a.getTokenCursor(a.selections[0].anchor);
expect(cursor.rangeForCurrentForm(a.selections[0].anchor)).toBeUndefined();
});
it('Selects atomic form to the right, when squeezed by an ignore marker', () => {
const a = docFromTextNotation('#_|a');
const b = docFromTextNotation('#_|a|');
const cursor: LispTokenCursor = a.getTokenCursor(a.selections[0].anchor);
expect(cursor.rangeForCurrentForm(a.selections[0].anchor)).toEqual(textAndSelection(b)[1]);
});
it('Selects list form to the right, when squeezed by an ignore marker', () => {
const a = docFromTextNotation('#_|(a)');
const b = docFromTextNotation('#_|(a)|');
const cursor: LispTokenCursor = a.getTokenCursor(a.selections[0].anchor);
expect(cursor.rangeForCurrentForm(a.selections[0].anchor)).toEqual(textAndSelection(b)[1]);
});
});

describe('Top Level Form', () => {
Expand Down