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

Commit a327422

Browse files
committed
fix: off-by-one hover range result
Fix sourcegraph/sourcegraph#1417. The hover result was not being properly translated from 0-indexed to 1-indexed.
1 parent 7ed22c7 commit a327422

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/hoverifier.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('Hoverifier', () => {
3636

3737
const delayTime = 100
3838
const hoverRange = { start: { line: 1, character: 2 }, end: { line: 3, character: 4 } }
39+
const hoverRange1Indexed = { start: { line: 2, character: 3 }, end: { line: 4, character: 5 } }
3940

4041
scheduler.run(({ cold, expectObservable }) => {
4142
const hoverifier = createHoverifier({
@@ -82,7 +83,7 @@ describe('Hoverifier', () => {
8283
[key: string]: Range | undefined
8384
} = {
8485
a: undefined, // highlightedRange is undefined when the hover is loading
85-
b: hoverRange,
86+
b: hoverRange1Indexed,
8687
}
8788

8889
// Hover over https://sourcegraph.sgdev.org/github.com/gorilla/mux@cb4698366aa625048f3b815af6a0dea8aef9280a/-/blob/mux.go#L24:6

src/hoverifier.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ export function createHoverifier<C extends object>({
615615
return of({ hoverOrError, position: undefined as Position | undefined, ...rest })
616616
}
617617

618-
// LSP is 0-indexed, the code here is currently 1-indexed
618+
// The requested position is is 0-indexed; the code here is currently 1-indexed
619619
const { line, character } = pos
620620
pos = { line: line + 1, character: character + 1, ...pos }
621621

@@ -642,7 +642,17 @@ export function createHoverifier<C extends object>({
642642
let highlightedRange: Range | undefined
643643
if (hoverOrError && !isErrorLike(hoverOrError) && hoverOrError !== LOADING) {
644644
if (hoverOrError.range) {
645-
highlightedRange = hoverOrError.range
645+
// The result is 0-indexed; the code view is treated as 1-indexed.
646+
highlightedRange = {
647+
start: {
648+
line: hoverOrError.range.start.line + 1,
649+
character: hoverOrError.range.start.character + 1,
650+
},
651+
end: {
652+
line: hoverOrError.range.end.line + 1,
653+
character: hoverOrError.range.end.character + 1,
654+
},
655+
}
646656
} else if (position) {
647657
highlightedRange = { start: position, end: position }
648658
}

0 commit comments

Comments
 (0)