Skip to content

Commit

Permalink
Fix onivim#1152: Key binding regression w/ legacy 'when' syntax (oniv…
Browse files Browse the repository at this point in the history
…im#1153)

* Add test case

* Add parsing for legacy when expressions (nested arrays)
  • Loading branch information
bryphe authored Jan 2, 2020
1 parent 90db1db commit eaa5a13
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Input/Keybindings.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,35 @@ module Keybinding = {
condition: Expression.t,
};

let parseAndExpression = (json: Yojson.Safe.t) =>
switch (json) {
| `String(expr) => Expression.Variable(expr)
| `List(andExpressions) =>
List.fold_left(
(acc, curr) => {
let result =
switch (curr) {
| `String(expr) => Expression.Variable(expr)
| _ => Expression.False
};
Expression.And(result, acc);
},
Expression.True,
andExpressions,
)
| _ => Expression.False
};

let condition_of_yojson = (json: Yojson.Safe.t) => {
switch (json) {
| `List(orExpressions) =>
Ok(
List.fold_left(
(acc, curr) => {Expression.Or(parseAndExpression(curr), acc)},
Expression.False,
orExpressions,
),
)
| `String(v) =>
switch (When.parse(v)) {
| Error(err) => Error(err)
Expand Down
32 changes: 32 additions & 0 deletions test/Input/KeybindingsTests.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open TestFramework;

open Oni_Input.Keybindings;
module Expression = Oni_Input.Expression;

let keybindingsJSON =
{|
Expand All @@ -27,6 +28,14 @@ bindings: [{key: "<C-V>", command: "quickOpen.show", when: "editorTextFocus"}]
|}
|> Yojson.Safe.from_string;

let regressionTest1152 =
{|
{
bindings: [{key: "<F2>", command: "explorer.toggle", when: [["editorTextFocus"]]}]
}
|}
|> Yojson.Safe.from_string;

let isOk = v =>
switch (v) {
| Ok(_) => true
Expand All @@ -39,6 +48,12 @@ let bindingCount = v =>
| Error(_) => 0
};

let getFirstBinding = v =>
switch (v) {
| Ok(([firstBinding], _)) => firstBinding
| _ => failwith("No binding found")
};

let errorCount = v =>
switch (v) {
| Ok((_, errors)) => List.length(errors)
Expand Down Expand Up @@ -66,5 +81,22 @@ describe("Keybindings", ({describe, _}) => {
expect.int(bindingCount(result)).toBe(1);
expect.int(errorCount(result)).toBe(0);
});
test("regression test: #1152 (legacy expression)", ({expect}) => {
let result = of_yojson_with_errors(regressionTest1152);
expect.bool(isOk(result)).toBe(true);
expect.int(bindingCount(result)).toBe(1);
expect.int(errorCount(result)).toBe(0);

let binding = getFirstBinding(result);
expect.equal(
binding,
Keybinding.{
key: "<F2>",
command: "explorer.toggle",
condition:
Expression.Or(And(Variable("editorTextFocus"), True), False),
},
);
});
})
});

0 comments on commit eaa5a13

Please sign in to comment.