2020-12-31 18:15:19 +00:00
|
|
|
{
|
2023-02-20 11:42:13 +01:00
|
|
|
lib,
|
2023-11-06 15:04:08 +01:00
|
|
|
helpers,
|
2023-02-20 11:42:13 +01:00
|
|
|
config,
|
2023-11-06 15:04:08 +01:00
|
|
|
pkgs,
|
2023-02-20 11:42:13 +01:00
|
|
|
...
|
|
|
|
}:
|
|
|
|
with lib; let
|
|
|
|
cfg = config.plugins.treesitter;
|
|
|
|
in {
|
2020-12-31 18:15:19 +00:00
|
|
|
options = {
|
2022-09-18 11:19:23 +01:00
|
|
|
plugins.treesitter = {
|
2023-01-22 03:32:08 +00:00
|
|
|
enable = mkEnableOption "tree-sitter syntax highlighting";
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2023-01-19 10:45:15 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.vimPlugins.nvim-treesitter;
|
|
|
|
description = "Plugin to use for nvim-treesitter. If using nixGrammars, it should include a `withPlugins` function";
|
|
|
|
};
|
|
|
|
|
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 {
|
2023-02-20 11:42:13 +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";
|
|
|
|
};
|
|
|
|
|
2024-01-03 17:25:20 +01:00
|
|
|
gccPackage = mkOption {
|
|
|
|
type = with types; nullOr package;
|
|
|
|
default =
|
|
|
|
if cfg.nixGrammars
|
|
|
|
then null
|
|
|
|
else pkgs.gcc;
|
|
|
|
example = null;
|
|
|
|
description = ''
|
|
|
|
Which package (if any) to be added as the GCC compiler.
|
|
|
|
This is required to build grammars if you are not using `nixGrammars`.
|
|
|
|
To disable the installation of GCC, set this option to `null`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-10-31 18:32:59 +08:00
|
|
|
parserInstallDir = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
2023-01-12 20:17:43 +01:00
|
|
|
default =
|
|
|
|
if cfg.nixGrammars
|
|
|
|
then null
|
2023-02-20 11:42:13 +01:00
|
|
|
else "$XDG_DATA_HOME/nvim/treesitter";
|
2023-01-12 20:17:43 +01:00
|
|
|
description = ''
|
|
|
|
Location of the parsers to be installed by the plugin (only needed when nixGrammars is disabled).
|
|
|
|
This default might not work on your own install, please make sure that $XDG_DATA_HOME is set if you want to use the default. Otherwise, change it to something that will work for you!
|
|
|
|
'';
|
2022-10-31 18:32:59 +08:00
|
|
|
};
|
|
|
|
|
2022-09-02 01:19:26 +02:00
|
|
|
ignoreInstall = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2023-02-20 11:42:13 +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;
|
2023-02-20 11:42:13 +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;
|
2023-02-20 11:42:13 +01:00
|
|
|
default = {};
|
2021-02-01 16:21:42 +00:00
|
|
|
description = "Custom capture group highlighting";
|
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
incrementalSelection = let
|
|
|
|
keymap = default:
|
|
|
|
mkOption {
|
2022-09-12 13:04:24 +01:00
|
|
|
type = types.str;
|
|
|
|
inherit default;
|
|
|
|
};
|
2023-02-20 11:42:13 +01:00
|
|
|
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
|
|
|
};
|
2023-02-20 11:42:13 +01:00
|
|
|
};
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2023-01-22 03:32:08 +00:00
|
|
|
indent = mkEnableOption "tree-sitter based indentation";
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2023-01-22 03:32:08 +00:00
|
|
|
folding = mkEnableOption "tree-sitter based folding";
|
2022-12-01 14:05:35 +00:00
|
|
|
|
2023-06-03 22:29:14 +02:00
|
|
|
languageRegister = mkOption {
|
|
|
|
type = with types; attrsOf (either str (listOf str));
|
|
|
|
description = ''
|
|
|
|
This is a wrapping of the `vim.treesitter.language.register` function.
|
|
|
|
Register specific parsers to one or several filetypes.
|
|
|
|
The keys are the parser names and the values are either one or several filetypes.
|
|
|
|
'';
|
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
cpp = "onelab";
|
|
|
|
python = ["myFiletype" "anotherFiletype"];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-12-01 14:07:08 +00:00
|
|
|
grammarPackages = mkOption {
|
|
|
|
type = with types; listOf package;
|
2023-01-21 18:02:16 +01:00
|
|
|
default = cfg.package.passthru.allGrammars;
|
2022-12-01 14:07:08 +00:00
|
|
|
description = "Grammar packages to install";
|
|
|
|
};
|
2023-01-05 15:23:23 +01:00
|
|
|
|
|
|
|
moduleConfig = mkOption {
|
|
|
|
type = types.attrsOf types.anything;
|
2023-02-20 11:42:13 +01:00
|
|
|
default = {};
|
2023-01-05 15:23:23 +01:00
|
|
|
description = "This is the configuration for extra modules. It should not be used directly";
|
|
|
|
};
|
2023-04-21 20:29:33 +02:00
|
|
|
|
|
|
|
nixvimInjections =
|
|
|
|
mkEnableOption
|
|
|
|
"nixvim specific injections, like lua highlighting in extraConfigLua";
|
2022-12-01 14:05:35 +00:00
|
|
|
};
|
2020-12-31 18:15:19 +00:00
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
config = let
|
|
|
|
tsOptions =
|
|
|
|
{
|
2022-09-12 13:04:24 +01:00
|
|
|
highlight = {
|
2023-05-22 15:45:47 +05:30
|
|
|
inherit (cfg) enable;
|
2023-02-20 11:42:13 +01:00
|
|
|
disable =
|
|
|
|
if (cfg.disabledLanguages != [])
|
|
|
|
then cfg.disabledLanguages
|
|
|
|
else null;
|
|
|
|
|
|
|
|
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 =
|
2023-02-20 11:42:13 +01:00
|
|
|
if cfg.incrementalSelection.enable
|
|
|
|
then {
|
2022-09-12 13:04:24 +01:00
|
|
|
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;
|
|
|
|
};
|
2023-02-20 11:42:13 +01:00
|
|
|
}
|
|
|
|
else null;
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2022-09-12 13:04:24 +01:00
|
|
|
indent =
|
2023-02-20 11:42:13 +01:00
|
|
|
if cfg.indent
|
|
|
|
then {
|
2022-09-12 13:04:24 +01:00
|
|
|
enable = true;
|
2023-02-20 11:42:13 +01:00
|
|
|
}
|
|
|
|
else null;
|
2021-12-11 15:09:11 +00:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
ensure_installed =
|
|
|
|
if cfg.nixGrammars
|
|
|
|
then []
|
|
|
|
else cfg.ensureInstalled;
|
2022-09-12 13:04:24 +01:00
|
|
|
ignore_install = cfg.ignoreInstall;
|
2022-10-31 18:32:59 +08:00
|
|
|
parser_install_dir = cfg.parserInstallDir;
|
2023-02-20 11:42:13 +01:00
|
|
|
}
|
|
|
|
// cfg.moduleConfig;
|
|
|
|
in
|
2022-09-12 13:04:24 +01:00
|
|
|
mkIf cfg.enable {
|
2023-02-20 11:42:13 +01:00
|
|
|
extraConfigLua =
|
|
|
|
(optionalString (cfg.parserInstallDir != null) ''
|
|
|
|
vim.opt.runtimepath:append("${cfg.parserInstallDir}")
|
|
|
|
'')
|
|
|
|
+ ''
|
|
|
|
require('nvim-treesitter.configs').setup(${helpers.toLuaObject tsOptions})
|
2023-06-03 22:29:14 +02:00
|
|
|
''
|
2023-12-21 12:17:00 +01:00
|
|
|
+ (optionalString (cfg.languageRegister != {}) ''
|
2023-06-03 22:29:14 +02:00
|
|
|
__parserFiltypeMappings = ${helpers.toLuaObject cfg.languageRegister}
|
|
|
|
|
|
|
|
for parser_name, ft in pairs(__parserFiltypeMappings) do
|
|
|
|
require('vim.treesitter.language').register(parser_name, ft)
|
|
|
|
end
|
|
|
|
'');
|
2022-09-18 11:19:23 +01:00
|
|
|
|
2023-04-21 20:29:33 +02:00
|
|
|
extraFiles = mkIf cfg.nixvimInjections {
|
|
|
|
"queries/nix/injections.scm" = ''
|
|
|
|
;; extends
|
|
|
|
|
|
|
|
(binding
|
|
|
|
attrpath: (attrpath (identifier) @_path)
|
|
|
|
expression: [
|
|
|
|
(string_expression (string_fragment) @lua)
|
|
|
|
(indented_string_expression (string_fragment) @lua)
|
|
|
|
]
|
|
|
|
(#match? @_path "^extraConfigLua(Pre|Post)?$"))
|
2023-05-23 16:38:25 +01:00
|
|
|
|
|
|
|
(binding
|
|
|
|
attrpath: (attrpath (identifier) @_path)
|
|
|
|
expression: [
|
|
|
|
(string_expression (string_fragment) @vim)
|
|
|
|
(indented_string_expression (string_fragment) @vim)
|
|
|
|
]
|
|
|
|
(#match? @_path "^extraConfigVim(Pre|Post)?$"))
|
2023-04-21 20:29:33 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-06-01 10:31:23 +02:00
|
|
|
extraPlugins =
|
2023-02-20 11:42:13 +01:00
|
|
|
if cfg.nixGrammars
|
|
|
|
then [(cfg.package.withPlugins (_: cfg.grammarPackages))]
|
|
|
|
else [cfg.package];
|
2024-01-03 17:25:20 +01:00
|
|
|
extraPackages = with pkgs;
|
|
|
|
[
|
|
|
|
tree-sitter
|
|
|
|
nodejs
|
|
|
|
]
|
|
|
|
++ optional (cfg.gccPackage != null) cfg.gccPackage;
|
2022-09-18 11:19:23 +01:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|