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

Commit

Permalink
fix: exclude end position in findElementWithOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
tjkandala committed Apr 9, 2021
1 parent 15f133d commit bd3e953
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
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

0 comments on commit bd3e953

Please sign in to comment.