Skip to content

Commit 9052a26

Browse files
authored
fix playground hover (#1155)
* set hoverTime to 100ms * fix hover hint selection
1 parent e96990b commit 9052a26

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

src/components/CodeMirror.res

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,35 @@ 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+
// let \"<" = (pos1, pos2) => compareLineCol(pos1, pos2) === Ordering.less
69+
let \"<=" = (pos1, pos2) => compareLineCol(pos1, pos2) !== Ordering.greater
70+
// let \">" = (pos1, pos2) => compareLineCol(pos1, pos2) === Ordering.greater
71+
let \">=" = (pos1, pos2) => compareLineCol(pos1, pos2) !== Ordering.less
72+
// let \"==" = (pos1, pos2) => compareLineCol(pos1, pos2) === Ordering.equal
5273
}
5374

5475
type t = {
55-
start: position,
56-
end: position,
76+
start: Position.t,
77+
end: Position.t,
5778
hint: string,
5879
}
5980
}
@@ -178,9 +199,14 @@ module CM6 = {
178199
@module("@codemirror/view")
179200
external dropCursor: unit => extension = "dropCursor"
180201

202+
module HoverTooltipOptions = {
203+
type t = {hoverTime?: int, hideOnChange?: bool}
204+
}
181205
@module("@codemirror/view")
182-
external hoverTooltip: ((editorView, int, Side.t) => null<Tooltip.t>) => extension =
183-
"hoverTooltip"
206+
external hoverTooltip: (
207+
(editorView, int, Side.t) => null<Tooltip.t>,
208+
~options: HoverTooltipOptions.t=?,
209+
) => extension = "hoverTooltip"
184210

185211
module UpdateListener = {
186212
type update
@@ -604,12 +630,25 @@ let createLinterExtension = (errors: array<Error.t>): CM6.extension => {
604630
}
605631

606632
let createHoverHintExtension = (hoverHints: array<HoverHint.t>) => {
607-
CM6.EditorView.hoverTooltip((view, pos, _side) => {
633+
CM6.EditorView.hoverTooltip(~options={hoverTime: 100}, (view, pos, _side) => {
608634
let doc = view->CM6.EditorView.state->CM6.EditorState.doc
609635
let {number: line, from} = doc->CM6.Text.lineAt(pos)
610636
let col = pos - from
611-
let found = hoverHints->Array.find(({start, end}) => {
612-
line >= start.line && line <= end.line && col >= start.col && col <= end.col
637+
let mousePos = {HoverHint.Position.line, col}
638+
639+
let found = Array.reduce(hoverHints, None, (prev, currentHint) => {
640+
open! HoverHint.Position
641+
let currentHintIncludesMousePos = currentHint.start <= mousePos && currentHint.end >= mousePos
642+
switch prev {
643+
| None if currentHintIncludesMousePos => Some(currentHint)
644+
| Some(prevHint)
645+
if currentHintIncludesMousePos &&
646+
(currentHint.start >= prevHint.start &&
647+
currentHint.end <= prevHint.end) =>
648+
Some(currentHint)
649+
| None
650+
| Some(_) => prev
651+
}
613652
})
614653
switch found {
615654
| 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)