Skip to content
This repository has been archived by the owner on Nov 25, 2021. It is now read-only.

fix: exclude end position in findElementWithOffset #366

Merged
merged 1 commit into from
Apr 12, 2021
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
10 changes: 5 additions & 5 deletions src/token_position.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ describe('token_positions', () => {
const content = `${tabChar}if rv := contextGet(r, routeKey); rv != nil {`

const ranges = [
{ offsetStart: 2, offsetEnd: 3, textContent: 'if' },
{ offsetStart: 2, offsetEnd: 4, textContent: 'if' },
{ offsetStart: 2, offsetEnd: 5, textContent: 'if ' },
// Intentional limitation: match whole text node at a given offset
// since that is much simpler to highlight.
{ offsetStart: 3, offsetEnd: 5, textContent: 'if rv' },
{ offsetStart: 2, offsetEnd: 5, textContent: 'if rv' },
{ offsetStart: 3, offsetEnd: 5, textContent: 'if ' },
{ offsetStart: 2, offsetEnd: 6, textContent: 'if rv' },
{ offsetStart: 11, offsetEnd: 33, textContent: 'contextGet(r, routeKey)' },
{ offsetStart: 11, offsetEnd: 34, textContent: 'contextGet(r, routeKey)' },
// If offsetEnd is less or equal to offsetStart, range should be treated as a position (offsetStart)
{ offsetStart: 11, offsetEnd: 4, textContent: 'contextGet' },
]
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('token_positions', () => {

const offsets = [
{ offsetStart: content.length + 1, offsetEnd: content.length + 2 },
{ offsetStart: 1, offsetEnd: content.length + 1 },
{ offsetStart: 1, offsetEnd: content.length + 2 },
]

const elem = dom.createElementFromString(content)
Expand Down
3 changes: 2 additions & 1 deletion src/token_position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ export function findElementWithOffset(
}
// offsetEnd should be greater than offsetStart, so only check for it after targetStartNode has been found
if (targetStartNode) {
if (offsetStep <= offsetEnd && offsetStep + text.length > offsetEnd) {
// End position of range is exclusive
if (offsetStep < offsetEnd && offsetStep + text.length >= offsetEnd) {
targetEndNode = node
break
}
Expand Down