-
Notifications
You must be signed in to change notification settings - Fork 25
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
feat: begin/end ranges for primitive nodes #50
base: master
Are you sure you want to change the base?
Conversation
Hey, thanks for the pull request! I researched a little, and it would be great if we could comply with Unist—all To convert offset to public toPoint(offset: number): Point {
return this.#location.toPoint(offset);
}
public toPosition(begin: number, end: number): Position {
return {
start: this.toPoint(begin),
end: this.toPoint(end),
};
} That should also resolve Position information adds quite a lot of details and makes AST much bigger, so it would be nice if we would only add positional information if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise, looks good. we'll also need to update test snapshots (run npm test -- --update
from the root)
Co-authored-by: Alexey Shmalko <[email protected]>
When I try to run the test from the root path, I get this error |
Try running |
Codecov Report
@@ Coverage Diff @@
## master #50 +/- ##
==========================================
+ Coverage 95.93% 95.94% +0.01%
==========================================
Files 15 15
Lines 1622 1651 +29
Branches 522 532 +10
==========================================
+ Hits 1556 1584 +28
- Misses 65 66 +1
Partials 1 1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for a lot of comments. The position handling seems to be quite convoluted
It would be also great to support the positions for all nodes—I will try to look into this over the weekend.
packages/uniorg-parse/src/parser.ts
Outdated
@@ -1466,7 +1532,8 @@ class Parser { | |||
const contentsBegin = this.r.offset() + m.index + m[1].length + m[3].length; | |||
const contentsEnd = contentsBegin + m[4].length; | |||
this.r.resetOffset(contentsEnd + 1); | |||
return u('code', { value }, []); | |||
const [begin, end] = this.getCurrentRange(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❗🐛 getCurrentRange
assumes that cursor is the beginning of the element. It's at the end after this.r.resetOffset(contentsEnd + 1)
packages/uniorg-parse/src/parser.ts
Outdated
/* | ||
* Return begin and end positions from current cursor position + val length | ||
*/ | ||
private getCurrentRange(val: string): [number, number] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: this function seems to be rather unfortunate—most of the usages above misuse it because cursor dependency is not obvious. It seems that it's better to remove this function and inline all the usages (that would probably make the bugs more obvious)
Co-authored-by: Alexey Shmalko <[email protected]>
Co-authored-by: Alexey Shmalko <[email protected]>
Co-authored-by: Alexey Shmalko <[email protected]>
Co-authored-by: Alexey Shmalko <[email protected]>
Co-authored-by: Alexey Shmalko <[email protected]>
Co-authored-by: Alexey Shmalko <[email protected]>
Co-authored-by: Alexey Shmalko <[email protected]>
Co-authored-by: Alexey Shmalko <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, sorry for the long review 🙃 the things have been a bit crazy over here
packages/uniorg-parse/src/parser.ts
Outdated
this.parseEmptyLines(); | ||
const _end = this.r.offset(); | ||
|
||
return u('src-block', { affiliated, language, value }); | ||
return u('src-block', { | ||
affiliated, | ||
language, | ||
value, | ||
...this.getPosition(contentsBegin, contentsEnd), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still valid:
❗🐛 same here and all blocks below: begin/end should be wider
For all blocks, the begin/end should be wider than contenstBegin
/contentsEnd
and include #+begin_xxx
and #+end_xxx
lines.
this.parseEmptyLines(); | |
const _end = this.r.offset(); | |
return u('src-block', { affiliated, language, value }); | |
return u('src-block', { | |
affiliated, | |
language, | |
value, | |
...this.getPosition(contentsBegin, contentsEnd), | |
this.parseEmptyLines(); | |
const end = this.r.offset(); | |
return u('src-block', { | |
affiliated, | |
language, | |
value, | |
...this.getPosition(begin, end), |
packages/uniorg-parse/src/parser.ts
Outdated
this.r.advance(this.r.forceMatch(/^[ \t]*CLOCK:[ \t]*/)); | ||
const value = this.parseTimestamp(); | ||
|
||
const end = this.r.offset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❗🐛 the end
does not account for advances on the next two lines. Need to move this line just before this.parseEmptyLines()
packages/uniorg-parse/src/parser.ts
Outdated
this.r.resetOffset(contentsEnd + 1); | ||
return u('code', { value }, []); | ||
const begin = this.r.offset(); | ||
const end = begin + value.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❗🐛 the begin
here is set too late and points after contentsEnd
(because of resetOffset
on the previous line)
The correct begin
should be set before contentsBegin
to this.r.offset() + m.index
(can use this.r.advance(m.index)
right before it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that begin should be placed before backoff function here
private parseCode(): Code | null {
// backoff one char to check border
const begin = this.r.offset();
this.r.backoff(1);
const m = this.r.lookingAt(this.re.verbatimRe());
if (!m) return null;
const value = m[4];
const contentsBegin = this.r.offset() + m.index + m[1].length + m[3].length;
const contentsEnd = contentsBegin + m[4].length;
const end = contentsEnd + 1;
this.r.resetOffset(end);
return u('code', { value, ...this.getPosition(begin, end) }, []);
}
because this code also takes into account ~ symbol before code
packages/uniorg-parse/src/parser.ts
Outdated
@@ -1808,6 +1892,7 @@ class Parser { | |||
rawValue, | |||
start, | |||
end, | |||
...this.getPosition(begin, begin + rawValue.length), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: works correctly but best to use this.r.offset()
for consistency:
const end = this.r.offset();
...this.getPosition(begin, end),
No problem, it really is a crazy time for all of us Also i noticed that timestamp parsing was incorrect,
The real start should be 0. I added additional argument to |
- Fix begin/end positions for content with children - Fix timestamp range - Some minor refactor
I had no luck setting begin/end for the Timestamp object (since this type already has an end property, I could use a universal property name - contentsBegin/contentsEnd, if you don't mind).