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

Commit 11c935e

Browse files
committed
fix(positions): don't return last character if target is code element
Previously, if the code element itself was the target, the last character of the line was returned. Return only the line number instead.
1 parent 30091a9 commit 11c935e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/token_position.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ describe('token_positions', () => {
209209
}
210210
}
211211
})
212+
213+
it('returns only the line number when the code element itself is hovered', () => {
214+
for (const { codeView, ...domOptions } of testcases) {
215+
const target = domOptions.getCodeElementFromLineNumber(codeView, 1)!
216+
const result = locateTarget(target, domOptions)
217+
assert.deepStrictEqual(result, { line: 1 })
218+
}
219+
})
220+
221+
it('returns undefined when the code view itself is hovered', () => {
222+
for (const { codeView, ...domOptions } of testcases) {
223+
const target = codeView
224+
const result = locateTarget(target, domOptions)
225+
assert.strictEqual(result, undefined)
226+
}
227+
})
212228
})
213229

214230
describe('getCodeElementsInRange()', () => {

src/token_position.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ export function locateTarget(
253253
}
254254

255255
const line = getLineNumberFromCodeElement(codeElement)
256+
257+
// If the hovered target was the code element itself or a parent,
258+
// make sure to not return the last character
259+
if (target === codeElement || target.contains(codeElement)) {
260+
return { line }
261+
}
262+
256263
const part = getDiffCodePart && getDiffCodePart(codeElement)
257264

258265
let character = 1

0 commit comments

Comments
 (0)