From 7fd653b4d8aa2c0b1777f49918b0321139f2636e Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Tue, 23 Apr 2024 16:08:55 +0300 Subject: [PATCH] plugins/treesitter: add an internal `defaultGrammars` options --- modules/plugins/treesitter/config.nix | 14 ++-- modules/plugins/treesitter/context.nix | 89 ----------------------- modules/plugins/treesitter/default.nix | 4 +- modules/plugins/treesitter/treesitter.nix | 50 ++++++++++--- 4 files changed, 51 insertions(+), 106 deletions(-) delete mode 100644 modules/plugins/treesitter/context.nix diff --git a/modules/plugins/treesitter/config.nix b/modules/plugins/treesitter/config.nix index 92802a8..e4d6c24 100644 --- a/modules/plugins/treesitter/config.nix +++ b/modules/plugins/treesitter/config.nix @@ -1,5 +1,6 @@ { config, + pkgs, lib, ... }: let @@ -11,7 +12,7 @@ cfg = config.vim.treesitter; usingNvimCmp = config.vim.autocomplete.enable && config.vim.autocomplete.type == "nvim-cmp"; - self = import ./treesitter.nix {inherit lib;}; + self = import ./treesitter.nix {inherit pkgs lib;}; mappingDefinitions = self.options.vim.treesitter.mappings; mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions; in { @@ -32,7 +33,6 @@ in { (mkSetBinding mappings.incrementalSelection.decrementByNode ":lua require('nvim-treesitter.incremental_selection').node_decremental()") ]; }; - # For some reason treesitter highlighting does not work on start if this is set before syntax on configRC.treesitter-fold = mkIf cfg.fold (entryBefore ["basic"] '' set foldmethod=expr @@ -42,14 +42,18 @@ in { luaConfigRC.treesitter = entryAnywhere '' require'nvim-treesitter.configs'.setup { + -- Disable imperative treesitter options that would attempt to fetch + -- grammars into the read-only Nix store. To add additionall grammars here + -- you must use the `config.vim.treesitter.grammars` option. + auto_install = false, + sync_install = false, + ensure_installed = {}, + highlight = { enable = true, disable = {}, }, - auto_install = false, - ensure_installed = {}, - incremental_selection = { enable = true, keymaps = { diff --git a/modules/plugins/treesitter/context.nix b/modules/plugins/treesitter/context.nix deleted file mode 100644 index 8bb65b5..0000000 --- a/modules/plugins/treesitter/context.nix +++ /dev/null @@ -1,89 +0,0 @@ -{ - config, - lib, - ... -}: let - inherit (lib.options) mkOption mkEnableOption; - inherit (lib.types) int bool str nullOr enum; - inherit (lib.modules) mkIf; - inherit (lib.trivial) boolToString; - inherit (lib.nvim.lua) nullString; - inherit (lib.nvim.dag) entryAnywhere; - - inherit (config.vim) treesitter; - cfg = treesitter.context; -in { - options.vim.treesitter.context = { - enable = mkEnableOption "context of current buffer contents [nvim-treesitter-context] "; - - maxLines = mkOption { - description = "How many lines the window should span. Values <=0 mean no limit."; - type = int; - default = 0; - }; - - minWindowHeight = mkOption { - description = "Minimum editor window height to enable context. Values <= 0 mean no limit."; - type = int; - default = 0; - }; - - lineNumbers = mkOption { - description = ""; - type = bool; - default = true; - }; - - multilineThreshold = mkOption { - description = "Maximum number of lines to collapse for a single context line."; - type = int; - default = 20; - }; - - trimScope = mkOption { - description = "Which context lines to discard if [](#opt-vim.treesitter.context.maxLines) is exceeded."; - type = enum ["inner" "outer"]; - default = "outer"; - }; - - mode = mkOption { - description = "Line used to calculate context."; - type = enum ["cursor" "topline"]; - default = "cursor"; - }; - - separator = mkOption { - description = '' - 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. - ''; - type = nullOr str; - default = null; - }; - - zindex = mkOption { - description = "The Z-index of the context window."; - type = int; - default = 20; - }; - }; - - config = mkIf (treesitter.enable && cfg.enable) { - vim.startPlugins = ["nvim-treesitter-context"]; - - vim.luaConfigRC.treesitter-context = entryAnywhere '' - require'treesitter-context'.setup { - enable = true, - max_lines = ${toString cfg.maxLines}, - min_window_height = ${toString cfg.minWindowHeight}, - line_numbers = ${boolToString cfg.lineNumbers}, - multiline_threshold = ${toString cfg.multilineThreshold}, - trim_scope = '${cfg.trimScope}', - mode = '${cfg.mode}', - separator = ${nullString cfg.separator}, - max_lines = ${toString cfg.zindex}, - } - ''; - }; -} diff --git a/modules/plugins/treesitter/default.nix b/modules/plugins/treesitter/default.nix index 5520cfe..a859f3a 100644 --- a/modules/plugins/treesitter/default.nix +++ b/modules/plugins/treesitter/default.nix @@ -1,7 +1,9 @@ { imports = [ + # treesitter extras + ./ts-context + ./treesitter.nix - ./context.nix ./config.nix ]; } diff --git a/modules/plugins/treesitter/treesitter.nix b/modules/plugins/treesitter/treesitter.nix index feae8d7..373f0ee 100644 --- a/modules/plugins/treesitter/treesitter.nix +++ b/modules/plugins/treesitter/treesitter.nix @@ -1,20 +1,16 @@ -{lib, ...}: let +{ + pkgs, + lib, + ... +}: let inherit (lib.options) mkOption mkEnableOption; inherit (lib.nvim.binds) mkMappingOption; inherit (lib.types) listOf package; + + inherit (pkgs.vimPlugins.nvim-treesitter) builtGrammars; in { options.vim.treesitter = { enable = mkEnableOption "treesitter, also enabled automatically through language options"; - fold = mkEnableOption "fold with treesitter"; - autotagHtml = mkEnableOption "autoclose and rename html tag"; - grammars = mkOption { - type = listOf package; - default = []; - description = '' - List of treesitter grammars to install. For supported languages - use the `vim.language..treesitter` option - ''; - }; mappings.incrementalSelection = { init = mkMappingOption "Init selection [treesitter]" "gnn"; @@ -22,5 +18,37 @@ in { incrementByScope = mkMappingOption "Increment selection by scope [treesitter]" "grc"; decrementByNode = mkMappingOption "Decrement selection by node [treesitter]" "grm"; }; + + fold = mkEnableOption "fold with treesitter"; + autotagHtml = mkEnableOption "autoclose and rename html tag"; + grammars = mkOption { + type = listOf package; + default = []; + description = '' + List of treesitter grammars to install. + + For languages already supported by neovim-flake, you may + use the {option}`vim.language..treesitter` options, which + will automatically add the required grammars to this. + ''; + }; + + defaultGrammars = mkOption { + internal = true; + readOnly = true; + type = listOf package; + default = with builtGrammars; [c lua vim vimdoc query]; + description = '' + A list of treesitter grammars that will be installed by default + if treesitter has been enabled. + + ::: {.warning} + Regardless of which language module options you enable, Neovim + depends on those grammars to be enabled while treesitter is enabled. + This list cannot be modified, but its contents will only be appended + if the list of grammars does not contain the required grammars. + ::: + ''; + }; }; }