Skip to content

Commit 1bfd9b4

Browse files
committed
Support hovering on unsaved code.
1 parent baa2194 commit 1bfd9b4

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

analysis/src/Commands.ml

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ let hover ~path ~line ~col ~currentFile ~debug =
4545
in
4646
match completions with
4747
| {kind = Label typString} :: _ ->
48-
Protocol.stringifyHover {contents = Hover.codeBlock typString}
49-
| _ -> Protocol.null)
48+
Protocol.stringifyHover (Hover.codeBlock typString)
49+
| _ -> (
50+
match CompletionBackEnd.completionsGetTypeEnv completions with
51+
| Some (typ, _env) ->
52+
Protocol.stringifyHover (Hover.codeBlock (Shared.typeToString typ))
53+
| None -> Protocol.null))
5054
| Some locItem -> (
5155
let isModule =
5256
match locItem.locType with
@@ -70,7 +74,7 @@ let hover ~path ~line ~col ~currentFile ~debug =
7074
let hoverText = Hover.newHover ~full locItem in
7175
match hoverText with
7276
| None -> Protocol.null
73-
| Some s -> Protocol.stringifyHover {contents = s}))
77+
| Some s -> Protocol.stringifyHover s))
7478
in
7579
print_endline result
7680

@@ -259,6 +263,30 @@ let test ~path =
259263
| Some text ->
260264
let lines = text |> String.split_on_char '\n' in
261265
let processLine i line =
266+
let createCurrentFile () =
267+
let currentFile, cout = Filename.open_temp_file "def" "txt" in
268+
let removeLineComment l =
269+
let len = String.length l in
270+
let rec loop i =
271+
if i + 2 <= len && l.[i] = '/' && l.[i + 1] = '/' then Some (i + 2)
272+
else if i + 2 < len && l.[i] = ' ' then loop (i + 1)
273+
else None
274+
in
275+
match loop 0 with
276+
| None -> l
277+
| Some indexAfterComment ->
278+
String.make indexAfterComment ' '
279+
^ String.sub l indexAfterComment (len - indexAfterComment)
280+
in
281+
lines
282+
|> List.iteri (fun j l ->
283+
let lineToOutput =
284+
if j == i - 1 then removeLineComment l else l
285+
in
286+
Printf.fprintf cout "%s\n" lineToOutput);
287+
close_out cout;
288+
currentFile
289+
in
262290
if Str.string_match (Str.regexp "^ *//[ ]*\\^") line 0 then
263291
let matched = Str.matched_string line in
264292
let len = line |> String.length in
@@ -284,7 +312,9 @@ let test ~path =
284312
print_endline
285313
("Hover " ^ path ^ " " ^ string_of_int line ^ ":"
286314
^ string_of_int col);
287-
hover ~path ~line ~col ~currentFile:path ~debug:true
315+
let currentFile = createCurrentFile () in
316+
hover ~path ~line ~col ~currentFile ~debug:true;
317+
Sys.remove currentFile
288318
| "ref" ->
289319
print_endline
290320
("References " ^ path ^ " " ^ string_of_int line ^ ":"
@@ -305,28 +335,7 @@ let test ~path =
305335
print_endline
306336
("Complete " ^ path ^ " " ^ string_of_int line ^ ":"
307337
^ string_of_int col);
308-
let currentFile, cout = Filename.open_temp_file "def" "txt" in
309-
let removeLineComment l =
310-
let len = String.length l in
311-
let rec loop i =
312-
if i + 2 <= len && l.[i] = '/' && l.[i + 1] = '/' then
313-
Some (i + 2)
314-
else if i + 2 < len && l.[i] = ' ' then loop (i + 1)
315-
else None
316-
in
317-
match loop 0 with
318-
| None -> l
319-
| Some indexAfterComment ->
320-
String.make indexAfterComment ' '
321-
^ String.sub l indexAfterComment (len - indexAfterComment)
322-
in
323-
lines
324-
|> List.iteri (fun j l ->
325-
let lineToOutput =
326-
if j == i - 1 then removeLineComment l else l
327-
in
328-
Printf.fprintf cout "%s\n" lineToOutput);
329-
close_out cout;
338+
let currentFile = createCurrentFile () in
330339
completion ~debug:true ~path ~pos:(line, col) ~currentFile;
331340
Sys.remove currentFile
332341
| "hig" ->

analysis/src/Protocol.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type completionItem = {
1010
documentation : markupContent option;
1111
}
1212

13-
type hover = {contents : string}
13+
type hover = string
1414
type location = {uri : string; range : range}
1515
type documentSymbolItem = {name : string; kind : int; location : location}
1616
type renameFile = {oldUri : string; newUri : string}
@@ -65,8 +65,7 @@ let stringifyCompletionItem c =
6565
| None -> null
6666
| Some doc -> stringifyMarkupContent doc)
6767

68-
let stringifyHover h =
69-
Printf.sprintf {|{"contents": "%s"}|} (Json.escape h.contents)
68+
let stringifyHover s = Printf.sprintf {|{"contents": "%s"}|} (Json.escape s)
7069

7170
let stringifyLocation (h : location) =
7271
Printf.sprintf {|{"uri": "%s", "range": %s}|} (Json.escape h.uri)

analysis/tests/src/Completion.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,6 @@ let _ =
346346

347347
let _ = <div name="" />
348348
// ^hov
349+
350+
// let _ = FAO.forAutoObject["age"]
351+
// ^hov

analysis/tests/src/expected/Completion.res.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,3 +1351,12 @@ JSX <div:[346:9->346:12] name[346:13->346:17]=...[346:18->346:20]> _children:346
13511351
Completable: Cjsx([div], name, [name])
13521352
{"contents": "```rescript\nstring\n```"}
13531353

1354+
Hover tests/src/Completion.res 349:17
1355+
Nothing at that position. Now trying to use completion.
1356+
posCursor:[349:17] posNoWhite:[349:16] Found expr:[349:11->349:35]
1357+
Pexp_send age[349:30->349:33] e:[349:11->349:28]
1358+
posCursor:[349:17] posNoWhite:[349:16] Found expr:[349:11->349:28]
1359+
Pexp_ident FAO.forAutoObject:[349:11->349:28]
1360+
Completable: Cpath Value[FAO, forAutoObject]
1361+
{"contents": "```rescript\n{\"age\": int, \"forAutoLabel\": FAR.forAutoRecord}\n```"}
1362+

0 commit comments

Comments
 (0)