@@ -46,14 +46,35 @@ module Error = {
4646}
4747
4848module 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
606632let 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 }) =>
0 commit comments