Skip to content

Commit 0e6b597

Browse files
authored
Merge pull request #1705 from goblint/arg-complete-array
Add bash completion for array element addition and removal
2 parents c6fd7b6 + 7d10632 commit 0e6b597

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/config/options.ml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@ let rec element_paths (element: element): string list =
2121
| String _
2222
| Boolean
2323
| Integer _
24-
| Number _
25-
| Monomorphic_array _ ->
24+
| Number _ ->
2625
[""]
26+
| Monomorphic_array _ ->
27+
[""; "[+]"; "[-]"; "[*]"]
2728
| Object object_specs ->
2829
List.concat_map (fun (name, field_element, _, _) ->
29-
List.map (fun path -> name ^ "." ^ path) (element_paths field_element)
30+
List.map (fun path -> "." ^ name ^ path) (element_paths field_element)
3031
) object_specs.properties
3132
| _ ->
3233
Logs.Format.error "%a" Json_schema.pp (create element);
3334
failwith "element_paths"
3435

3536
let schema_paths (schema: schema): string list =
3637
element_paths (root schema)
37-
|> List.map BatString.rchop (* remove trailing '.' *)
38+
|> List.map BatString.lchop (* remove first '.' *)
3839

3940
let paths = schema_paths schema
4041

@@ -48,9 +49,17 @@ let rec element_completions (element: element): (string * string list) list =
4849
in
4950
match element.kind with
5051
| Integer _
51-
| Number _
52-
| Monomorphic_array _ ->
52+
| Number _ ->
5353
default_completion ()
54+
| Monomorphic_array (array_element, array_specs) ->
55+
let array_element_completions =
56+
element_completions array_element
57+
|> List.concat_map (fun (path, cs) ->
58+
assert (path = ""); (* Arrays of objects/arrays not supported. Currently we only have arrays of strings.*)
59+
[("[+]", cs); ("[-]", cs); ("[*]", cs)]
60+
)
61+
in
62+
default_completion () @ array_element_completions
5463
| Boolean ->
5564
[("", ["false"; "true"])]
5665
| String string_specs ->
@@ -68,15 +77,15 @@ let rec element_completions (element: element): (string * string list) list =
6877
end
6978
| Object object_specs ->
7079
List.concat_map (fun (name, field_element, _, _) ->
71-
List.map (fun (path, cs) -> (name ^ "." ^ path, cs)) (element_completions field_element)
80+
List.map (fun (path, cs) -> ("." ^ name ^ path, cs)) (element_completions field_element)
7281
) object_specs.properties
7382
| _ ->
7483
Logs.Format.error "%a" Json_schema.pp (create element);
7584
failwith "element_completions"
7685

7786
let schema_completions (schema: schema): (string * string list) list =
7887
element_completions (root schema)
79-
|> List.map (BatTuple.Tuple2.map1 BatString.rchop) (* remove trailing '.' *)
88+
|> List.map (BatTuple.Tuple2.map1 BatString.lchop) (* remove first '.' *)
8089

8190
let completions = schema_completions schema
8291

0 commit comments

Comments
 (0)