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

Remove some cursor meta-data awareness #1589

Merged
merged 4 commits into from
Mar 15, 2022
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
Changes to Calva.

## [Unreleased]
- Maintenance: [Dumb down the token cursor some dealing with meta data and readers](https://github.com/BetterThanTomorrow/calva/pull/1585)

## [2.0.253] - 2022-03-09
- Fix: [Structural editing hangs in specific cases of unbalanced forms](https://github.com/BetterThanTomorrow/calva/pull/1585)
- Fix: [over snippet markdown and adds example](https://github.com/BetterThanTomorrow/calva/pull/1582)
- Fix: [Hover snippet markdown and adds example](https://github.com/BetterThanTomorrow/calva/pull/1582)
- Maintenance: [Begin work on enabling strictNullChecks in the TypeScript config.](https://github.com/BetterThanTomorrow/calva/pull/1568)

## [2.0.252] - 2022-03-05
Expand Down
12 changes: 1 addition & 11 deletions src/cursor-doc/paredit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,7 @@ export function rangeToForwardDownList(
goPastWhitespace = false
): [number, number] {
const cursor = doc.getTokenCursor(offset);
do {
cursor.forwardThroughAnyReader();
cursor.forwardWhitespace();
if (
cursor.getToken().type === 'open' &&
!cursor.tokenBeginsMetadata()
) {
break;
}
} while (cursor.forwardSexp());
if (cursor.downList(true)) {
if (cursor.downListSkippingMeta()) {
if (goPastWhitespace) {
cursor.forwardWhitespace();
}
Expand Down
35 changes: 27 additions & 8 deletions src/cursor-doc/token-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,27 +578,46 @@ export class LispTokenCursor extends TokenCursor {
}

/**
* If possible, moves this cursor forwards past any whitespace, and then past the immediately following open-paren and returns true.
* If possible, moves this cursor forwards past any readers and whitespace,
* and then past the immediately following open-paren and returns true.
* If the source does not match this, returns false and does not move the cursor.
*/
downList(skipMetadata = false): boolean {
downList(): boolean {
const cursor = this.clone();
cursor.forwardThroughAnyReader();
cursor.forwardWhitespace();
if (cursor.getToken().type === 'open') {
if (skipMetadata) {
while (cursor.tokenBeginsMetadata()) {
cursor.forwardSexp();
cursor.forwardWhitespace();
}
}
cursor.next();
this.set(cursor);
return true;
}
return false;
}

/**
* If possible, moves this cursor forwards past any readers, whitespace, and metadata,
* and then past the immediately following open-paren and returns true.
* If the source does not match this, returns false and does not move the cursor.
*/
downListSkippingMeta(): boolean {
const cursor = this.clone();
do {
cursor.forwardThroughAnyReader();
cursor.forwardWhitespace();
if (
cursor.getToken().type === 'open' &&
!cursor.tokenBeginsMetadata()
) {
break;
}
} while (cursor.forwardSexp());
if (cursor.downList()) {
this.set(cursor);
return true;
}
return false;
}

/**
* If possible, moves this cursor forwards past any whitespace, and then past the immediately following close-paren and returns true.
* If the source does not match this, returns false and does not move the cursor.
Expand Down
2 changes: 1 addition & 1 deletion src/debugger/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function moveTokenCursorToBreakpoint(
const coor = [...debugResponse.coor]; // Copy the array so we do not modify the one stored in state

for (let i = 0; i < coor.length; i++) {
while (!tokenCursor.downList(true)) {
while (!tokenCursor.downListSkippingMeta()) {
tokenCursor.next();
}
const previousToken = tokenCursor.getPrevToken();
Expand Down
20 changes: 15 additions & 5 deletions src/extension-test/unit/cursor-doc/token-cursor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,28 @@ describe('Token Cursor', () => {
cursor.downList();
expect(cursor.offsetStart).toBe(b.selectionLeft);
});
it('Does not skip metadata by default', () => {
it('Does not skip metadata', () => {
const a = docFromTextNotation('(a| ^{:x 1} (b 1))');
const b = docFromTextNotation('(a ^{|:x 1} (b 1))');
const cursor: LispTokenCursor = a.getTokenCursor(a.selectionLeft);
cursor.downList();
expect(cursor.offsetStart).toBe(b.selectionLeft);
});
it('Skips metadata when skipMetadata is true', () => {
const a = docFromTextNotation('(a| ^{:x 1} (b 1))');
const b = docFromTextNotation('(a ^{:x 1} (|b 1))');
});

describe('downListSkippingMeta', () => {
it('Moves down, skipping metadata', () => {
const a = docFromTextNotation('(|a #b ^{:x 1} (c 1))');
const b = docFromTextNotation('(a #b ^{:x 1} (|c 1))');
const cursor: LispTokenCursor = a.getTokenCursor(a.selectionLeft);
cursor.downListSkippingMeta();
expect(cursor.offsetStart).toBe(b.selectionLeft);
});
it('Moves down when there is no metadata', () => {
const a = docFromTextNotation('(|a #b (c 1))');
const b = docFromTextNotation('(a #b (|c 1))');
const cursor: LispTokenCursor = a.getTokenCursor(a.selectionLeft);
cursor.downList(true);
cursor.downListSkippingMeta();
expect(cursor.offsetStart).toBe(b.selectionLeft);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
;; 3, 2, 2, 2
#dbg
(defn test-metadata-symbol
[x]
(let [y x]
^{:hello "world"}
(+ x ^{:inner "meta"} (+ 1 y|))))
6 changes: 6 additions & 0 deletions src/extension-test/unit/debugger/test-files/metadata-map.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;; 3, 2, 2, 2
(defn test-metadata-symbol
[x]
(let [y x]
^{:hello "world"}
(+ x ^{:inner "meta"} (+ 1 #break y|))))
8 changes: 8 additions & 0 deletions src/extension-test/unit/debugger/util-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ describe('Debugger Util', () => {
expectBreakpointToBeFound('metadata-symbol.clj');
});

it('metadata map', () => {
expectBreakpointToBeFound('metadata-map.clj');
});

it('metadata map last sexp', () => {
expectBreakpointToBeFound('metadata-map-last.clj');
});

it('ignored forms', () => {
expectBreakpointToBeFound('ignored-forms.clj');
});
Expand Down
18 changes: 18 additions & 0 deletions test-data/debugger_metadata.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns debugger-metadata)

#dbg
(defn test-metadata-symbol
[x]
(let [y x]
^{:hello "world"}
(+ x ^{:inner "meta"} (+ 1 y))))

(defn test-metadata-symbol2
[x]
(let [y x]
^{:hello "world"}
(+ x ^{:inner "meta"} (+ 1 #break y))))

(comment
(test-metadata-symbol 42)
(test-metadata-symbol2 42))