From b8c3385599e90f1252eb3318dd00dd763dcc56f6 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Fri, 25 Aug 2023 15:09:44 +0200 Subject: [PATCH] plugins/treesitter-context: reflect upstream options changes --- .../treesitter/treesitter-context.nix | 114 ++++++++++++------ .../treesitter/treesitter-context.nix | 27 +++++ 2 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 tests/test-sources/plugins/languages/treesitter/treesitter-context.nix diff --git a/plugins/languages/treesitter/treesitter-context.nix b/plugins/languages/treesitter/treesitter-context.nix index e9e452a2..c8e73fef 100644 --- a/plugins/languages/treesitter/treesitter-context.nix +++ b/plugins/languages/treesitter/treesitter-context.nix @@ -5,48 +5,92 @@ ... }: with lib; let + cfg = config.plugins.treesitter-context; helpers = import ../../helpers.nix {inherit lib;}; in { - options.plugins.treesitter-context = { - enable = mkEnableOption "nvim-treesitter-context"; + # Those warnings were introduced on 08/25/2023. TODO: remove them in October 2023. + imports = let + basePluginPath = ["plugins" "treesitter-context"]; + in [ + ( + mkRenamedOptionModule + (basePluginPath ++ ["maxWindowHeight"]) + (basePluginPath ++ ["minWindowHeight"]) + ) + ( + mkRemovedOptionModule (basePluginPath ++ ["patterns"]) "" + ) + ( + mkRemovedOptionModule (basePluginPath ++ ["extractPatterns"]) "" + ) + ]; + options.plugins.treesitter-context = + helpers.extraOptionsOptions + // { + enable = mkEnableOption "nvim-treesitter-context"; - package = helpers.mkPackageOption "treesitter-context" pkgs.vimPlugins.nvim-treesitter-context; + package = helpers.mkPackageOption "nvim-treesitter-context" pkgs.vimPlugins.nvim-treesitter-context; - maxLines = mkOption { - type = types.nullOr types.ints.positive; - default = null; - description = "How many lines the window should span. Null means no limit"; - }; + maxLines = helpers.defaultNullOpts.mkUnsignedInt 0 '' + How many lines the window should span. 0 means no limit. + ''; - trimScope = mkOption { - type = types.enum ["outer" "inner"]; - default = "outer"; - description = "Which context lines to discard if `max_lines` is exceeded"; - }; + minWindowHeight = helpers.defaultNullOpts.mkUnsignedInt 0 '' + Minimum editor window height to enable context. 0 means no limit. + ''; - maxWindowHeight = mkOption { - type = types.nullOr types.ints.positive; - default = null; - description = "Minimum editor window height to enable context"; - }; + lineNumbers = helpers.defaultNullOpts.mkBool true '' + Whether to show line numbers. + ''; - patterns = mkOption { - type = types.attrsOf (types.listOf types.str); - default = {}; - description = '' - Patterns to use for context delimitation. The 'default' key matches all filetypes + multilineThreshold = helpers.defaultNullOpts.mkUnsignedInt 20 '' + Maximum number of lines to collapse for a single context line. + ''; + + trimScope = helpers.defaultNullOpts.mkEnumFirstDefault ["outer" "inner"] '' + Which context lines to discard if `maxLines` is exceeded. + ''; + + mode = helpers.defaultNullOpts.mkEnumFirstDefault ["cursor" "topline"] '' + Line used to calculate context. + ''; + + separator = helpers.mkNullOrOption types.str '' + Separator between context and content. + Should be a single character string, like "-". + When separator is set, the context will only show up when there are at least 2 lines above + cursorline. + ''; + + zindex = helpers.defaultNullOpts.mkUnsignedInt 20 '' + The Z-index of the context window. + ''; + + onAttach = helpers.mkNullOrOption types.str '' + The implementation of a lua function which takes an integer `buf` as parameter and returns a + boolean. + Return `false` to disable attaching. ''; }; - exactPatterns = mkOption { - type = types.attrsOf types.bool; - default = {}; - description = "Treat the coresponding entry in patterns as an exact match"; - }; - }; - config = let - cfg = config.plugins.treesitter-context; + setupOptions = with cfg; + { + max_lines = maxLines; + min_window_height = minWindowHeight; + line_numbers = lineNumbers; + multiline_threshold = multilineThreshold; + trim_scope = trimScope; + inherit + mode + separator + zindex + ; + on_attach = + helpers.ifNonNull' onAttach + (helpers.mkRaw onAttach); + } + // cfg.extraOptions; in mkIf cfg.enable { warnings = mkIf (!config.plugins.treesitter.enable) [ @@ -55,10 +99,8 @@ in { extraPlugins = [cfg.package]; - plugins.treesitter.moduleConfig.context = { - max_lines = cfg.maxLines; - trim_scope = cfg.trimScope; - min_window_height = cfg.maxWindowHeight; - }; + extraConfigLua = '' + require('treesitter-context').setup(${helpers.toLuaObject setupOptions}) + ''; }; } diff --git a/tests/test-sources/plugins/languages/treesitter/treesitter-context.nix b/tests/test-sources/plugins/languages/treesitter/treesitter-context.nix new file mode 100644 index 00000000..c96bb7b5 --- /dev/null +++ b/tests/test-sources/plugins/languages/treesitter/treesitter-context.nix @@ -0,0 +1,27 @@ +{pkgs}: { + empty = { + plugins = { + treesitter.enable = true; + treesitter-context.enable = true; + }; + }; + + default = { + plugins = { + treesitter.enable = true; + treesitter-context = { + enable = true; + + maxLines = 0; + minWindowHeight = 0; + lineNumbers = true; + multilineThreshold = 20; + trimScope = "outer"; + mode = "cursor"; + separator = null; + zindex = 20; + onAttach = null; + }; + }; + }; +}