2020-12-31 18:15:19 +00:00
|
|
|
{ pkgs, lib, config, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
2022-09-18 11:19:23 +01:00
|
|
|
cfg = config.plugins.treesitter;
|
2021-02-01 16:21:42 +00:00
|
|
|
helpers = import ../helpers.nix { inherit lib; };
|
2020-12-31 18:15:19 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
2022-09-18 11:19:23 +01:00
|
|
|
plugins.treesitter = {
|
2021-02-01 16:21:42 +00:00
|
|
|
enable = mkEnableOption "Enable tree-sitter syntax highlighting";
|
|
|
|
|
2022-01-15 02:37:09 +00:00
|
|
|
nixGrammars = mkOption {
|
|
|
|
type = types.bool;
|
2022-09-18 11:19:23 +01:00
|
|
|
default = true;
|
|
|
|
description = "Install grammars with Nix";
|
2022-01-15 02:37:09 +00:00
|
|
|
};
|
2022-09-18 11:19:23 +01:00
|
|
|
|
2021-02-01 16:21:42 +00:00
|
|
|
ensureInstalled = mkOption {
|
2022-09-12 13:04:24 +01:00
|
|
|
type = with types; oneOf [ (enum [ "all" ]) (listOf str) ];
|
2022-09-02 01:19:26 +02:00
|
|
|
default = "all";
|
|
|
|
description = "Either \"all\" or a list of languages";
|
|
|
|
};
|
|
|
|
|
2022-10-31 18:32:59 +08:00
|
|
|
parserInstallDir = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Location of the parsers to be installed by the plugin (only needed when nixGrammars is disabled)";
|
|
|
|
};
|
|
|
|
|
2022-09-02 01:19:26 +02:00
|
|
|
ignoreInstall = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2022-09-12 13:04:24 +01:00
|
|
|
default = [ ];
|
2022-09-02 01:19:26 +02:00
|
|
|
description = "List of parsers to ignore installing (for \"all\")";
|
2021-02-01 16:21:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
disabledLanguages = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2022-09-12 13:04:24 +01:00
|
|
|
default = [ ];
|
2021-02-01 16:21:42 +00:00
|
|
|
description = "A list of languages to disable";
|
|
|
|
};
|
|
|
|
|
|
|
|
customCaptures = mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
2022-09-12 13:04:24 +01:00
|
|
|
default = { };
|
2021-02-01 16:21:42 +00:00
|
|
|
description = "Custom capture group highlighting";
|
|
|
|
};
|
|
|
|
|
2022-09-12 13:04:24 +01:00
|
|
|
incrementalSelection =
|
|
|
|
let
|
|
|
|
keymap = default: mkOption {
|
|
|
|
type = types.str;
|
|
|
|
inherit default;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
enable = mkEnableOption "Incremental selection based on the named nodes from the grammar";
|
|
|
|
keymaps = {
|
|
|
|
initSelection = keymap "gnn";
|
|
|
|
nodeIncremental = keymap "grn";
|
|
|
|
scopeIncremental = keymap "grc";
|
|
|
|
nodeDecremental = keymap "grm";
|
|
|
|
};
|
2021-02-01 16:40:07 +00:00
|
|
|
};
|
2021-02-01 16:21:42 +00:00
|
|
|
|
|
|
|
indent = mkEnableOption "Enable tree-sitter based indentation";
|
|
|
|
|
|
|
|
folding = mkEnableOption "Enable tree-sitter based folding";
|
2020-12-31 18:15:19 +00:00
|
|
|
};
|
2022-12-01 14:05:35 +00:00
|
|
|
|
|
|
|
grammarPackages = mkOption {
|
|
|
|
type = with types; listOf package;
|
|
|
|
default = pkgs.tree-sitter.allGrammars;
|
|
|
|
description = "Grammar packages to install";
|
|
|
|
};
|
2020-12-31 18:15:19 +00:00
|
|
|
};
|
|
|
|
|
2022-09-12 13:04:24 +01:00
|
|
|
config =
|
|
|
|
let
|
|
|
|
tsOptions = {
|
|
|
|
highlight = {
|
|
|
|
enable = cfg.enable;
|
|
|
|
disable = if (cfg.disabledLanguages != [ ]) then cfg.disabledLanguages else null;
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2022-09-12 13:04:24 +01:00
|
|
|
custom_captures = if (cfg.customCaptures != { }) then cfg.customCaptures else null;
|
2021-02-10 21:01:07 +00:00
|
|
|
};
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2022-09-12 13:04:24 +01:00
|
|
|
incremental_selection =
|
|
|
|
if cfg.incrementalSelection.enable then {
|
|
|
|
enable = true;
|
|
|
|
keymaps = {
|
|
|
|
init_selection = cfg.incrementalSelection.keymaps.initSelection;
|
|
|
|
node_incremental = cfg.incrementalSelection.keymaps.nodeIncremental;
|
|
|
|
scope_incremental = cfg.incrementalSelection.keymaps.scopeIncremental;
|
|
|
|
node_decremental = cfg.incrementalSelection.keymaps.nodeDecremental;
|
|
|
|
};
|
|
|
|
} else null;
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2022-09-12 13:04:24 +01:00
|
|
|
indent =
|
|
|
|
if cfg.indent then {
|
|
|
|
enable = true;
|
|
|
|
} else null;
|
2021-12-11 15:09:11 +00:00
|
|
|
|
2022-09-12 13:04:24 +01:00
|
|
|
ensure_installed = if cfg.nixGrammars then [ ] else cfg.ensureInstalled;
|
|
|
|
ignore_install = cfg.ignoreInstall;
|
2022-10-31 18:32:59 +08:00
|
|
|
parser_install_dir = cfg.parserInstallDir;
|
2022-09-12 13:04:24 +01:00
|
|
|
};
|
|
|
|
in
|
|
|
|
mkIf cfg.enable {
|
2022-09-18 11:19:23 +01:00
|
|
|
extraConfigLua = ''
|
|
|
|
require('nvim-treesitter.configs').setup(${helpers.toLuaObject tsOptions})
|
2022-10-31 18:32:59 +08:00
|
|
|
'' + optionalString (cfg.parserInstallDir != null) ''
|
|
|
|
vim.opt.runtimepath:append("${cfg.parserInstallDir}")
|
2022-09-18 11:19:23 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
extraPlugins = with pkgs; if cfg.nixGrammars then
|
2022-12-01 14:05:35 +00:00
|
|
|
[ (vimPlugins.nvim-treesitter.withPlugins (_: cfg.grammarPackages)) ]
|
2022-09-18 11:19:23 +01:00
|
|
|
else [ vimPlugins.nvim-treesitter ];
|
|
|
|
extraPackages = [ pkgs.tree-sitter pkgs.nodejs ];
|
|
|
|
|
|
|
|
options = mkIf cfg.folding {
|
|
|
|
foldmethod = "expr";
|
|
|
|
foldexpr = "nvim_treesitter#foldexpr()";
|
2021-02-01 16:21:42 +00:00
|
|
|
};
|
2020-12-31 18:15:19 +00:00
|
|
|
};
|
|
|
|
}
|
2022-09-02 01:19:26 +02:00
|
|
|
|