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

Commit 342afb8

Browse files
authored
fix: exclude end position in findElementWithOffset (#366)
1 parent 15f133d commit 342afb8

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/token_position.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ describe('token_positions', () => {
180180
const content = `${tabChar}if rv := contextGet(r, routeKey); rv != nil {`
181181

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

245245
const offsets = [
246246
{ offsetStart: content.length + 1, offsetEnd: content.length + 2 },
247-
{ offsetStart: 1, offsetEnd: content.length + 1 },
247+
{ offsetStart: 1, offsetEnd: content.length + 2 },
248248
]
249249

250250
const elem = dom.createElementFromString(content)

src/token_position.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ export function findElementWithOffset(
289289
}
290290
// offsetEnd should be greater than offsetStart, so only check for it after targetStartNode has been found
291291
if (targetStartNode) {
292-
if (offsetStep <= offsetEnd && offsetStep + text.length > offsetEnd) {
292+
// End position of range is exclusive
293+
if (offsetStep < offsetEnd && offsetStep + text.length >= offsetEnd) {
293294
targetEndNode = node
294295
break
295296
}

0 commit comments

Comments
 (0)