diff --git a/src/Input/Keybindings.re b/src/Input/Keybindings.re index 3c6ad6ac9d..ceda8e1995 100644 --- a/src/Input/Keybindings.re +++ b/src/Input/Keybindings.re @@ -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) diff --git a/test/Input/KeybindingsTests.re b/test/Input/KeybindingsTests.re index 3c4b8ea0cc..14e1f85b7c 100644 --- a/test/Input/KeybindingsTests.re +++ b/test/Input/KeybindingsTests.re @@ -1,6 +1,7 @@ open TestFramework; open Oni_Input.Keybindings; +module Expression = Oni_Input.Expression; let keybindingsJSON = {| @@ -27,6 +28,14 @@ bindings: [{key: "", command: "quickOpen.show", when: "editorTextFocus"}] |} |> Yojson.Safe.from_string; +let regressionTest1152 = + {| +{ +bindings: [{key: "", command: "explorer.toggle", when: [["editorTextFocus"]]}] +} +|} + |> Yojson.Safe.from_string; + let isOk = v => switch (v) { | Ok(_) => true @@ -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) @@ -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: "", + command: "explorer.toggle", + condition: + Expression.Or(And(Variable("editorTextFocus"), True), False), + }, + ); + }); }) });