Skip to content

Commit

Permalink
Format json response
Browse files Browse the repository at this point in the history
  • Loading branch information
3Rafal committed Jan 5, 2024
1 parent 77853db commit 975466a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 51 deletions.
7 changes: 3 additions & 4 deletions src/frontend/ocamlmerlin/query_json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,12 @@ let json_of_locate resp =

let json_of_signature_help resp =
let param { label_start; label_end } =
`Assoc [ "label_start", `Int label_start; "label_end", `Int label_end] in
`Assoc ["label", `List [`Int label_start; `Int label_end]] in
match resp with
| None -> `Assoc []
| Some { function_name; signature; parameters; active_param } ->
| Some { label; parameters; active_param } ->
`Assoc
[ "function_name", `String (Option.value ~default:"_" function_name);
"signature", `String signature;
[ "label", `String label;
"parameters", `List (List.map ~f:param parameters);
"active_param", `Int active_param
]
Expand Down
9 changes: 4 additions & 5 deletions src/frontend/query_commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -855,11 +855,10 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a =
Option.value ~default:"_" s.function_name
in
sprintf "%s : " fun_name in
Some { function_name = s.function_name;
signature = s.signature;
parameters = List.map ~f:(param (String.length prefix)) s.parameters;
active_param = Option.value ~default:0 s.active_param;
}
Some { label = prefix ^ s.signature;
parameters = List.map ~f:(param (String.length prefix)) s.parameters;
active_param = Option.value ~default:0 s.active_param;
}
| None -> None)

| Version ->
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/query_protocol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ type signature_help_param = {
}

type signature_help = {
function_name : string option;
signature : string;
label : string;
parameters : signature_help_param list;
active_param : int
}
Expand Down
99 changes: 59 additions & 40 deletions tests/test-dirs/signature-help.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ It can provide signature help after a function-type value.
{
"class": "return",
"value": {
"function_name": "map",
"signature": "f:('a -> 'b) -> 'a list -> 'b list",
"label": "map : f:('a -> 'b) -> 'a list -> 'b list",
"parameters": [
{
"label_start": 6,
"label_end": 18
"label": [
6,
18
]
},
{
"label_start": 22,
"label_end": 29
"label": [
22,
29
]
}
],
"active_param": 1
Expand All @@ -33,16 +36,19 @@ It can provide signature help for an operator.
{
"class": "return",
"value": {
"function_name": "(+)",
"signature": "int -> int -> int",
"label": "(+) : int -> int -> int",
"parameters": [
{
"label_start": 6,
"label_end": 9
"label": [
6,
9
]
},
{
"label_start": 13,
"label_end": 16
"label": [
13,
16
]
}
],
"active_param": 1
Expand All @@ -58,12 +64,13 @@ It can provide signature help for an anonymous function.
{
"class": "return",
"value": {
"function_name": "_",
"signature": "int -> int",
"label": "_ : int -> int",
"parameters": [
{
"label_start": 4,
"label_end": 7
"label": [
4,
7
]
}
],
"active_param": 0
Expand All @@ -80,16 +87,19 @@ It can make the non-labelled parameter active.
{
"class": "return",
"value": {
"function_name": "map",
"signature": "f:('a -> 'b) -> 'a list -> 'b list",
"label": "map : f:('a -> 'b) -> 'a list -> 'b list",
"parameters": [
{
"label_start": 6,
"label_end": 18
"label": [
6,
18
]
},
{
"label_start": 22,
"label_end": 29
"label": [
22,
29
]
}
],
"active_param": 1
Expand All @@ -105,16 +115,19 @@ It can make the labelled parameter active.
{
"class": "return",
"value": {
"function_name": "map",
"signature": "f:(int -> int) -> int list -> int list",
"label": "map : f:(int -> int) -> int list -> int list",
"parameters": [
{
"label_start": 6,
"label_end": 20
"label": [
6,
20
]
},
{
"label_start": 24,
"label_end": 32
"label": [
24,
32
]
}
],
"active_param": 0
Expand All @@ -131,16 +144,19 @@ It can make a labelled parameter active by prefix.
{
"class": "return",
"value": {
"function_name": "mem",
"signature": "'a -> set:'a list -> bool",
"label": "mem : 'a -> set:'a list -> bool",
"parameters": [
{
"label_start": 6,
"label_end": 8
"label": [
6,
8
]
},
{
"label_start": 12,
"label_end": 23
"label": [
12,
23
]
}
],
"active_param": 1
Expand All @@ -157,16 +173,19 @@ It can make an optional parameter active by prefix.
{
"class": "return",
"value": {
"function_name": "create",
"signature": "?random:bool -> int -> ('a, 'b) Hashtbl.t",
"label": "create : ?random:bool -> int -> ('a, 'b) Hashtbl.t",
"parameters": [
{
"label_start": 9,
"label_end": 21
"label": [
9,
21
]
},
{
"label_start": 25,
"label_end": 28
"label": [
25,
28
]
}
],
"active_param": 0
Expand Down

0 comments on commit 975466a

Please sign in to comment.