Skip to content

Commit 2cd4dcb

Browse files
committed
fix hover hint selection
1 parent 11612f3 commit 2cd4dcb

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

src/components/CodeMirror.res

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,36 @@ module Error = {
4646
}
4747

4848
module HoverHint = {
49-
type position = {
50-
line: int,
51-
col: int,
49+
module Position = {
50+
type t = {
51+
line: int,
52+
col: int,
53+
}
54+
let compareLineCol = (pos1, pos2) => {
55+
if pos1.line < pos2.line {
56+
Ordering.less
57+
} else if pos1.line > pos2.line {
58+
Ordering.greater
59+
} // same line, compare column
60+
else if pos1.col < pos2.col {
61+
Ordering.less
62+
} else if pos1.col > pos2.col {
63+
Ordering.greater
64+
} else {
65+
Ordering.equal
66+
}
67+
}
68+
@@warning("-32")
69+
let \"<" = (pos1, pos2) => compareLineCol(pos1, pos2) === Ordering.less
70+
let \"<=" = (pos1, pos2) => compareLineCol(pos1, pos2) !== Ordering.greater
71+
let \">" = (pos1, pos2) => compareLineCol(pos1, pos2) === Ordering.greater
72+
let \">=" = (pos1, pos2) => compareLineCol(pos1, pos2) !== Ordering.less
73+
let \"==" = (pos1, pos2) => compareLineCol(pos1, pos2) === Ordering.equal
5274
}
5375

5476
type t = {
55-
start: position,
56-
end: position,
77+
start: Position.t,
78+
end: Position.t,
5779
hint: string,
5880
}
5981
}
@@ -613,8 +635,21 @@ let createHoverHintExtension = (hoverHints: array<HoverHint.t>) => {
613635
let doc = view->CM6.EditorView.state->CM6.EditorState.doc
614636
let {number: line, from} = doc->CM6.Text.lineAt(pos)
615637
let col = pos - from
616-
let found = hoverHints->Array.find(({start, end}) => {
617-
line >= start.line && line <= end.line && col >= start.col && col <= end.col
638+
let mousePos = {HoverHint.Position.line, col}
639+
640+
let found = Array.reduce(hoverHints, None, (prev, currentHint) => {
641+
open! HoverHint.Position
642+
let currentHintIncludesMousePos = currentHint.start <= mousePos && currentHint.end >= mousePos
643+
switch prev {
644+
| None if currentHintIncludesMousePos => Some(currentHint)
645+
| Some(prevHint)
646+
if currentHintIncludesMousePos &&
647+
(currentHint.start >= prevHint.start &&
648+
currentHint.end <= prevHint.end) =>
649+
Some(currentHint)
650+
| None
651+
| Some(_) => prev
652+
}
618653
})
619654
switch found {
620655
| Some({hint, start, end}) =>

src/components/CodeMirror.resi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ module Error: {
1818
}
1919

2020
module HoverHint: {
21-
type position = {
22-
line: int,
23-
col: int,
21+
module Position: {
22+
type t = {
23+
line: int,
24+
col: int,
25+
}
2426
}
2527

2628
type t = {
27-
start: position,
28-
end: position,
29+
start: Position.t,
30+
end: Position.t,
2931
hint: string,
3032
}
3133
}

0 commit comments

Comments
 (0)