From d1f4d1514d6295907759b386d8d7c8d85a864564 Mon Sep 17 00:00:00 2001 From: Daniel Gorin Date: Wed, 24 Jun 2020 18:30:16 +0100 Subject: [PATCH] kakoune: escape showWhitespace separators We were passing the separators for the `show-whitespaces` highlighter verbatim. This was problematic in case one wanted to use, spaces, quotes or `%` as separators since the resulting kakoune configuration would be invalid. According to kakoune's docs, the separator has to be one character long, so we can use a simple rule for escaping them. It is possible that people has been working this around by passing, e.g. `"' '"` as separator in order to get a space (i.e., escaped explicitly by the user), so we just let longer strings be used verbatim. PR #1357 --- modules/programs/kakoune.nix | 22 +++++++++++----- tests/modules/programs/kakoune/default.nix | 6 ++++- .../whitespace-highlighter-corner-cases.nix | 25 +++++++++++++++++++ .../kakoune/whitespace-highlighter.nix | 2 +- 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 tests/modules/programs/kakoune/whitespace-highlighter-corner-cases.nix diff --git a/modules/programs/kakoune.nix b/modules/programs/kakoune.nix index d2fd8eb10..3c2349151 100644 --- a/modules/programs/kakoune.nix +++ b/modules/programs/kakoune.nix @@ -506,12 +506,22 @@ let ]; showWhitespaceOptions = with cfg.config.showWhitespace; - concatStrings [ - (optionalString (tab != null) " -tab ${tab}") - (optionalString (tabStop != null) " -tabpad ${tabStop}") - (optionalString (space != null) " -spc ${space}") - (optionalString (nonBreakingSpace != null) " -nbsp ${nonBreakingSpace}") - (optionalString (lineFeed != null) " -lf ${lineFeed}") + let + quoteSep = sep: + if sep == "'" then + ''"'"'' + else if lib.strings.stringLength sep == 1 then + "'${sep}'" + else + sep; # backwards compat, in case sep == "' '", etc. + + in concatStrings [ + (optionalString (tab != null) " -tab ${quoteSep tab}") + (optionalString (tabStop != null) " -tabpad ${quoteSep tabStop}") + (optionalString (space != null) " -spc ${quoteSep space}") + (optionalString (nonBreakingSpace != null) + " -nbsp ${quoteSep nonBreakingSpace}") + (optionalString (lineFeed != null) " -lf ${quoteSep lineFeed}") ]; uiOptions = with cfg.config.ui; diff --git a/tests/modules/programs/kakoune/default.nix b/tests/modules/programs/kakoune/default.nix index 9d88abf49..9f906a946 100644 --- a/tests/modules/programs/kakoune/default.nix +++ b/tests/modules/programs/kakoune/default.nix @@ -1 +1,5 @@ -{ kakoune-whitespace-highlighter = ./whitespace-highlighter.nix; } +{ + kakoune-whitespace-highlighter = ./whitespace-highlighter.nix; + kakoune-whitespace-highlighter-corner-cases = + ./whitespace-highlighter-corner-cases.nix; +} diff --git a/tests/modules/programs/kakoune/whitespace-highlighter-corner-cases.nix b/tests/modules/programs/kakoune/whitespace-highlighter-corner-cases.nix new file mode 100644 index 000000000..142aaac8f --- /dev/null +++ b/tests/modules/programs/kakoune/whitespace-highlighter-corner-cases.nix @@ -0,0 +1,25 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + programs.kakoune = { + enable = true; + config.showWhitespace = { + enable = true; + lineFeed = ''"''; + space = " "; + nonBreakingSpace = "' '"; # backwards compat + tab = "'"; + # tabStop = + }; + }; + + nmt.script = '' + assertFileExists home-files/.config/kak/kakrc + assertFileContains home-files/.config/kak/kakrc \ + "add-highlighter global/ show-whitespaces -tab \"'\" -spc ' ' -nbsp ' ' -lf '\"'" + ''; + }; +} diff --git a/tests/modules/programs/kakoune/whitespace-highlighter.nix b/tests/modules/programs/kakoune/whitespace-highlighter.nix index edeb6b52d..514c26a11 100644 --- a/tests/modules/programs/kakoune/whitespace-highlighter.nix +++ b/tests/modules/programs/kakoune/whitespace-highlighter.nix @@ -19,7 +19,7 @@ with lib; nmt.script = '' assertFileExists home-files/.config/kak/kakrc assertFileContains home-files/.config/kak/kakrc \ - "add-highlighter global/ show-whitespaces -tab 4 -tabpad 5 -spc 2 -nbsp 3 -lf 1" + "add-highlighter global/ show-whitespaces -tab '4' -tabpad '5' -spc '2' -nbsp '3' -lf '1'" ''; }; }