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

Commit 67269e4

Browse files
committed
Test completion cleanup
This might be safer and easier on the eyes? We might also be able to ditch JsonShort and Json serialization afterward? Dunno
1 parent 99f0a28 commit 67269e4

File tree

5 files changed

+70
-46
lines changed

5 files changed

+70
-46
lines changed

src/EditorSupportCommands.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ let dump files =
113113

114114
let autocomplete ~currentFile ~full ~package ~pos ~state =
115115
let maybeText = Files.readFile currentFile in
116-
let completions =
117-
NewCompletions.computeCompletions ~full ~maybeText ~package ~pos ~state
118-
in
119-
Json.stringify completions
116+
NewCompletions.computeCompletions ~full ~maybeText ~package ~pos ~state
117+
|> List.map Protocol.stringifyCompletionItem
118+
|> Protocol.array
120119

121120
let complete ~pathWithPos ~currentFile =
122121
let state = TopTypes.empty () in

src/JsonShort.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ let o o = Object o
44
let s s = String s
55
let i i = Number (float_of_int i)
66
let l l = Array l
7-
let null = Null

src/NewCompletions.ml

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,6 @@ let getItems ~full ~package ~rawOpens ~getModule ~allModules ~pos ~parts =
424424
@ List.concat
425425
(opens |> List.map (fun env -> attributeCompletions ~env ~suffix))))
426426

427-
module J = JsonShort
428-
429427
let mkItem ~name ~kind ~detail ~deprecated ~docstring ~uri ~pos_lnum =
430428
let valueMessage =
431429
(match deprecated with None -> "" | Some s -> "Deprecated: " ^ s ^ "\n\n")
@@ -437,17 +435,18 @@ let mkItem ~name ~kind ~detail ~deprecated ~docstring ~uri ~pos_lnum =
437435
let tags =
438436
match deprecated = None with
439437
| true -> []
440-
| false -> [J.i 1 (* deprecated *)]
438+
| false -> [1 (* deprecated *)]
441439
in
442-
J.o
443-
[
444-
("label", J.s name);
445-
("kind", J.i kind);
446-
("tags", J.l tags);
447-
("detail", detail |> J.s);
448-
( "documentation",
449-
J.o [("kind", J.s "markdown"); ("value", J.s valueMessage)] );
450-
]
440+
Protocol.{
441+
label = name;
442+
kind = kind;
443+
tags = tags;
444+
detail = detail;
445+
documentation = {
446+
kind = "markdown";
447+
value = valueMessage;
448+
};
449+
}
451450

452451
let processCompletable ~findItems ~full ~package ~pos ~rawOpens
453452
(completable : PartialParser.completable) =
@@ -668,34 +667,31 @@ let processCompletable ~findItems ~full ~package ~pos ~rawOpens
668667
|> List.map mkLabel
669668

670669
let computeCompletions ~full ~maybeText ~package ~pos ~state =
671-
let items =
672-
match maybeText with
670+
match maybeText with
671+
| None -> []
672+
| Some text -> (
673+
match PartialParser.positionToOffset text pos with
673674
| None -> []
674-
| Some text -> (
675-
match PartialParser.positionToOffset text pos with
675+
| Some offset -> (
676+
match PartialParser.findCompletable text offset with
676677
| None -> []
677-
| Some offset -> (
678-
match PartialParser.findCompletable text offset with
679-
| None -> []
680-
| Some completable ->
681-
let rawOpens = PartialParser.findOpens text offset in
682-
let allModules =
683-
package.TopTypes.localModules @ package.dependencyModules
684-
in
685-
let findItems ~exact parts =
686-
let items =
687-
getItems ~full ~package ~rawOpens
688-
~getModule:(State.fileForModule state ~package)
689-
~allModules ~pos ~parts
690-
in
691-
match parts |> List.rev with
692-
| last :: _ when exact ->
693-
items
694-
|> List.filter (fun (_uri, {SharedTypes.name = {txt}}) ->
695-
txt = last)
696-
| _ -> items
678+
| Some completable ->
679+
let rawOpens = PartialParser.findOpens text offset in
680+
let allModules =
681+
package.TopTypes.localModules @ package.dependencyModules
682+
in
683+
let findItems ~exact parts =
684+
let items =
685+
getItems ~full ~package ~rawOpens
686+
~getModule:(State.fileForModule state ~package)
687+
~allModules ~pos ~parts
697688
in
698-
completable
699-
|> processCompletable ~findItems ~full ~package ~pos ~rawOpens))
700-
in
701-
if items = [] then J.null else items |> J.l
689+
match parts |> List.rev with
690+
| last :: _ when exact ->
691+
items
692+
|> List.filter (fun (_uri, {SharedTypes.name = {txt}}) ->
693+
txt = last)
694+
| _ -> items
695+
in
696+
completable
697+
|> processCompletable ~findItems ~full ~package ~pos ~rawOpens))

src/Protocol.ml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,34 @@ let posOfLexing {Lexing.pos_lnum; pos_cnum; pos_bol} =
55

66
let rangeOfLoc {Location.loc_start; loc_end} =
77
J.o [("start", posOfLexing loc_start); ("end", posOfLexing loc_end)]
8+
9+
let array l = "[" ^ (String.concat ", " l) ^ "]"
10+
11+
type markupContent = {
12+
kind: string;
13+
value: string;
14+
}
15+
type completionItem = {
16+
label: string;
17+
kind: int;
18+
tags: int list;
19+
detail: string;
20+
documentation: markupContent;
21+
}
22+
let stringifyMarkupContent (m: markupContent) =
23+
Printf.sprintf {|{"kind": "%s", "value": "%s"}|}
24+
m.kind (String.escaped m.value)
25+
26+
let stringifyCompletionItem c =
27+
Printf.sprintf {|{
28+
"label": "%s",
29+
"kind": %i,
30+
"tags": %s,
31+
"detail": "%s",
32+
"documentation": %s
33+
}|}
34+
(String.escaped c.label)
35+
c.kind
36+
(c.tags |> List.map string_of_int |> array)
37+
(String.escaped c.detail)
38+
(stringifyMarkupContent c.documentation)

src/RescriptEditorSupport.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
module J = JsonShort
21
module StringSet = Set.Make (String)
32

43
let parseArgs args =

0 commit comments

Comments
 (0)