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;
|
2024-07-01 23:00:34 -05:00
|
|
|
helpers.neovim-plugin.mkNeovimPlugin config {
|
|
|
|
name = "treesitter";
|
|
|
|
originalName = "nvim-treesitter";
|
|
|
|
luaName = "nvim-treesitter.configs";
|
|
|
|
defaultPackage = pkgs.vimPlugins.nvim-treesitter;
|
2023-01-19 10:45:15 +00:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
maintainers = [ lib.maintainers.khaneliman ];
|
2022-09-18 11:19:23 +01:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
# TODO introduced 2024-07-06: remove after 24.11
|
|
|
|
optionsRenamedToSettings = [
|
|
|
|
"ensureInstalled"
|
|
|
|
"ignoreInstall"
|
|
|
|
"parserInstallDir"
|
|
|
|
[
|
|
|
|
"incrementalSelection"
|
|
|
|
"enable"
|
|
|
|
]
|
|
|
|
[
|
|
|
|
"incrementalSelection"
|
|
|
|
"keymaps"
|
|
|
|
"initSelection"
|
|
|
|
"nodeDecremental"
|
|
|
|
]
|
|
|
|
[
|
|
|
|
"incrementalSelection"
|
|
|
|
"keymaps"
|
|
|
|
"initSelection"
|
|
|
|
"nodeIncremental"
|
|
|
|
]
|
|
|
|
[
|
|
|
|
"incrementalSelection"
|
|
|
|
"keymaps"
|
|
|
|
"initSelection"
|
|
|
|
"scopeIncremental"
|
|
|
|
]
|
|
|
|
];
|
2022-09-02 01:19:26 +02:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
imports =
|
|
|
|
let
|
|
|
|
basePluginPath = [
|
|
|
|
"plugins"
|
|
|
|
"treesitter"
|
|
|
|
];
|
|
|
|
settingsPath = basePluginPath ++ [ "settings" ];
|
|
|
|
in
|
|
|
|
[
|
|
|
|
(lib.mkRenamedOptionModule (basePluginPath ++ [ "moduleConfig" ]) settingsPath)
|
|
|
|
(lib.mkRenamedOptionModule (basePluginPath ++ [ "customCaptures" ]) (
|
|
|
|
settingsPath
|
|
|
|
++ [
|
|
|
|
"highlight"
|
|
|
|
"custom_captures"
|
|
|
|
]
|
|
|
|
))
|
|
|
|
(lib.mkRenamedOptionModule (basePluginPath ++ [ "disabledLanguages" ]) (
|
|
|
|
settingsPath
|
|
|
|
++ [
|
|
|
|
"highlight"
|
|
|
|
"disable"
|
|
|
|
]
|
|
|
|
))
|
|
|
|
(lib.mkRenamedOptionModule (basePluginPath ++ [ "indent" ]) (
|
|
|
|
settingsPath
|
|
|
|
++ [
|
|
|
|
"indent"
|
|
|
|
"enable"
|
|
|
|
]
|
|
|
|
))
|
|
|
|
];
|
2024-01-03 17:25:20 +01:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
settingsOptions = {
|
|
|
|
auto_install = helpers.defaultNullOpts.mkBool false ''
|
|
|
|
Whether to automatically install missing parsers when entering a buffer.
|
|
|
|
'';
|
2022-10-31 18:32:59 +08:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
highlight = {
|
|
|
|
additional_vim_regex_highlighting =
|
|
|
|
helpers.defaultNullOpts.mkNullableWithRaw
|
|
|
|
(with helpers.nixvimTypes; either bool (listOf (maybeRaw str)))
|
|
|
|
false
|
|
|
|
''
|
|
|
|
Setting this to true will run `syntax` and tree-sitter at the same time. \
|
|
|
|
Set this to `true` if you depend on 'syntax' being enabled (e.g. for indentation). \
|
|
|
|
See `:h syntax`.
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
Using this option may slow down your editor, and you may see some duplicate highlights. \
|
|
|
|
Instead of true, it can also be a list of languages.
|
|
|
|
'';
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
enable = helpers.defaultNullOpts.mkBool false ''
|
|
|
|
Whether to enable treesitter highlighting.
|
|
|
|
'';
|
|
|
|
|
|
|
|
disable =
|
|
|
|
helpers.defaultNullOpts.mkStrLuaFnOr (with helpers.nixvimTypes; listOf (maybeRaw str)) null
|
|
|
|
''
|
|
|
|
Can either be a list of the names of parsers you wish to disable or
|
|
|
|
a lua function that returns a boolean indicating the parser should be disabled.
|
|
|
|
'';
|
|
|
|
|
|
|
|
custom_captures = helpers.defaultNullOpts.mkAttrsOf types.str { } ''
|
|
|
|
Custom capture group highlighting.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
incremental_selection = {
|
|
|
|
enable = helpers.defaultNullOpts.mkBool false ''
|
|
|
|
Incremental selection based on the named nodes from the grammar.
|
|
|
|
'';
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
keymaps =
|
2023-02-20 11:42:13 +01:00
|
|
|
let
|
2024-07-01 23:00:34 -05:00
|
|
|
mkKeymap = default: helpers.defaultNullOpts.mkStr default "Key shortcut";
|
2024-05-05 19:39:35 +02:00
|
|
|
in
|
|
|
|
{
|
2024-07-01 23:00:34 -05:00
|
|
|
init_selection = mkKeymap "gnn";
|
|
|
|
node_incremental = mkKeymap "grn";
|
|
|
|
scope_incremental = mkKeymap "grc";
|
|
|
|
node_decremental = mkKeymap "grm";
|
2021-02-01 16:40:07 +00:00
|
|
|
};
|
2024-07-01 23:00:34 -05:00
|
|
|
};
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
indent = {
|
|
|
|
enable = helpers.defaultNullOpts.mkBool false ''
|
|
|
|
Whether to enable treesitter indentation.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-07-13 10:56:24 +01:00
|
|
|
ensure_installed = helpers.defaultNullOpts.mkNullable' {
|
|
|
|
type =
|
|
|
|
with helpers.nixvimTypes;
|
|
|
|
oneOf [
|
|
|
|
(enum [ "all" ])
|
|
|
|
(listOf (maybeRaw str))
|
|
|
|
rawLua
|
|
|
|
];
|
|
|
|
pluginDefault = [ ];
|
|
|
|
description = ''
|
|
|
|
Either `"all"` or a list of languages to ensure installing.
|
|
|
|
'';
|
|
|
|
};
|
2024-07-01 23:00:34 -05:00
|
|
|
|
|
|
|
ignore_install = helpers.defaultNullOpts.mkListOf types.str [ ] ''
|
2024-07-13 10:56:24 +01:00
|
|
|
List of parsers to ignore installing. Used when `ensure_installed` is set to `"all"`.
|
2024-07-01 23:00:34 -05:00
|
|
|
'';
|
|
|
|
|
2024-07-11 17:27:00 -05:00
|
|
|
parser_install_dir = helpers.mkNullOrOption' {
|
|
|
|
type = with helpers.nixvimTypes; maybeRaw str;
|
|
|
|
# Backport the default from nvim-treesitter 1.0
|
|
|
|
# The current default doesn't work on nix, as it is readonly
|
|
|
|
default.__raw = "vim.fs.joinpath(vim.fn.stdpath('data'), 'site')";
|
|
|
|
pluginDefault = lib.literalMD "the plugin's package directory";
|
|
|
|
description = ''
|
|
|
|
Location of the parsers to be installed by the plugin (only needed when `nixGrammars` is disabled).
|
2024-07-01 23:00:34 -05:00
|
|
|
|
2024-07-11 17:27:00 -05:00
|
|
|
By default, parsers are installed to the "site" dir.
|
|
|
|
If set to `null` the _plugin default_ is used, which will not work on nix.
|
|
|
|
'';
|
|
|
|
};
|
2023-06-03 22:29:14 +02:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
sync_install = helpers.defaultNullOpts.mkBool false ''
|
|
|
|
Install parsers synchronously (only applied to `ensure_installed`).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settingsExample = {
|
2024-07-08 14:17:44 -05:00
|
|
|
auto_install = false;
|
2024-07-13 10:56:24 +01:00
|
|
|
ensure_installed = "all";
|
2024-07-01 23:00:34 -05:00
|
|
|
ignore_install = [ "rust" ];
|
2024-07-11 17:27:00 -05:00
|
|
|
parser_install_dir.__raw = "vim.fs.joinpath(vim.fn.stdpath('data'), 'treesitter')";
|
2024-07-08 14:17:44 -05:00
|
|
|
sync_install = false;
|
2024-07-01 23:00:34 -05:00
|
|
|
|
|
|
|
highlight = {
|
|
|
|
enable = true;
|
2024-07-08 14:17:44 -05:00
|
|
|
|
|
|
|
additional_vim_regex_highlighting = true;
|
2024-07-01 23:00:34 -05:00
|
|
|
disable = [ "rust" ];
|
|
|
|
custom_captures = { };
|
|
|
|
};
|
|
|
|
|
|
|
|
incremental_selection = {
|
|
|
|
enable = true;
|
|
|
|
|
|
|
|
keymaps = {
|
|
|
|
init_selection = "gnn";
|
2024-07-08 14:17:44 -05:00
|
|
|
node_decremental = "grm";
|
2024-07-01 23:00:34 -05:00
|
|
|
node_incremental = "grn";
|
|
|
|
scope_incremental = "grc";
|
2022-12-01 14:07:08 +00:00
|
|
|
};
|
2024-07-01 23:00:34 -05:00
|
|
|
};
|
2024-07-08 14:17:44 -05:00
|
|
|
|
|
|
|
indent = {
|
|
|
|
enable = true;
|
|
|
|
};
|
2024-07-01 23:00:34 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = {
|
|
|
|
folding = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
description = "Whether to enable treesitter folding.";
|
|
|
|
};
|
|
|
|
|
|
|
|
gccPackage = helpers.mkPackageOption {
|
|
|
|
name = "gcc";
|
|
|
|
default = pkgs.gcc;
|
|
|
|
defaultText = literalExpression "pkgs.gcc";
|
|
|
|
example = literalExpression "pkgs.gcc14";
|
|
|
|
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`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
grammarPackages = mkOption {
|
|
|
|
type = with types; listOf package;
|
|
|
|
default = config.plugins.treesitter.package.passthru.allGrammars;
|
|
|
|
example = literalExpression "pkgs.vimPlugins.nvim-treesitter.passthru.allGrammars";
|
|
|
|
defaultText = literalExpression "config.plugins.treesitter.package.passthru.allGrammars";
|
|
|
|
description = "Grammar packages to install";
|
|
|
|
};
|
2023-01-05 15:23:23 +01:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
# TODO: Implement rawLua support to be passed into extraConfigLua.
|
|
|
|
languageRegister = mkOption {
|
|
|
|
type = with types; attrsOf (coercedTo str toList (listOf str));
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
cpp = "onelab";
|
|
|
|
python = [
|
|
|
|
"foo"
|
|
|
|
"bar"
|
|
|
|
];
|
2023-01-05 15:23:23 +01:00
|
|
|
};
|
2024-07-01 23:00:34 -05:00
|
|
|
description = ''
|
|
|
|
This is a wrapping of the `vim.treesitter.language.register` function.
|
2023-04-21 20:29:33 +02:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
Register specific parsers to one or several filetypes.
|
|
|
|
|
|
|
|
The keys are the parser names and the values are either one or several filetypes.
|
|
|
|
'';
|
2022-12-01 14:05:35 +00:00
|
|
|
};
|
2020-12-31 18:15:19 +00:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
nixGrammars = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
description = "Whether to install grammars defined in `grammarPackages`.";
|
|
|
|
};
|
2024-05-05 19:39:35 +02:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
nixvimInjections = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
description = "Whether to enable Nixvim injections, e.g. highlighting `extraConfigLua` as lua.";
|
|
|
|
};
|
2021-02-01 16:21:42 +00:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
nodejsPackage = helpers.mkPackageOption {
|
|
|
|
name = "nodejs";
|
|
|
|
default = pkgs.nodejs;
|
|
|
|
defaultText = literalExpression "pkgs.nodejs";
|
|
|
|
example = literalExpression "pkgs.nodejs_22";
|
|
|
|
description = ''
|
|
|
|
Which package (if any) to be added as the nodejs package.
|
|
|
|
|
|
|
|
This is required to build grammars if you are not using `nixGrammars`.
|
|
|
|
To disable the installation of NodeJS, set this option to `null`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
treesitterPackage = helpers.mkPackageOption {
|
|
|
|
name = "tree-sitter";
|
|
|
|
default = pkgs.tree-sitter;
|
|
|
|
defaultText = literalExpression "pkgs.tree-sitter";
|
|
|
|
description = ''
|
|
|
|
Which package (if any) to be added as the tree-sitter binary.
|
|
|
|
|
|
|
|
This is required to build grammars if you are not using `nixGrammars`.
|
|
|
|
To disable the installation of tree-sitter, set this option to `null`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# NOTE: We call setup manually below.
|
|
|
|
callSetup = false;
|
|
|
|
# NOTE: We install cfg.package manually so we can install grammars using it.
|
|
|
|
installPackage = false;
|
|
|
|
|
|
|
|
extraConfig = cfg: {
|
|
|
|
extraConfigLua =
|
|
|
|
# NOTE: Upstream state that the parser MUST be at the beginning of runtimepath.
|
|
|
|
# Otherwise the parsers from Neovim takes precedent, which may be incompatible with some queries.
|
|
|
|
(optionalString (cfg.settings.parser_install_dir != null) ''
|
2024-07-11 17:27:00 -05:00
|
|
|
vim.opt.runtimepath:prepend(${helpers.toLuaObject cfg.settings.parser_install_dir})
|
2024-07-01 23:00:34 -05:00
|
|
|
'')
|
|
|
|
+ ''
|
|
|
|
require('nvim-treesitter.configs').setup(${helpers.toLuaObject cfg.settings})
|
|
|
|
''
|
|
|
|
+ (optionalString (cfg.languageRegister != { }) ''
|
|
|
|
do
|
|
|
|
local __parserFiletypeMappings = ${helpers.toLuaObject cfg.languageRegister}
|
2023-06-03 22:29:14 +02:00
|
|
|
|
2024-03-07 19:44:13 +01:00
|
|
|
for parser_name, ft in pairs(__parserFiletypeMappings) do
|
2023-06-03 22:29:14 +02:00
|
|
|
require('vim.treesitter.language').register(parser_name, ft)
|
|
|
|
end
|
2024-07-01 23:00:34 -05:00
|
|
|
end
|
|
|
|
'');
|
2023-04-21 20:29:33 +02:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
extraFiles = mkIf cfg.nixvimInjections { "queries/nix/injections.scm".source = ./injections.scm; };
|
2022-09-18 11:19:23 +01:00
|
|
|
|
2024-07-01 23:00:34 -05:00
|
|
|
extraPlugins = mkIf (cfg.package != null) [
|
|
|
|
(mkIf cfg.nixGrammars (cfg.package.withPlugins (_: cfg.grammarPackages)))
|
|
|
|
(mkIf (!cfg.nixGrammars) cfg.package)
|
|
|
|
];
|
|
|
|
|
|
|
|
extraPackages = [
|
|
|
|
cfg.gccPackage
|
|
|
|
cfg.nodejsPackage
|
|
|
|
cfg.treesitterPackage
|
|
|
|
];
|
|
|
|
|
2024-07-12 12:13:27 -05:00
|
|
|
opts = mkIf cfg.folding {
|
|
|
|
foldmethod = mkDefault "expr";
|
|
|
|
foldexpr = mkDefault "nvim_treesitter#foldexpr()";
|
|
|
|
};
|
2024-07-01 23:00:34 -05:00
|
|
|
};
|
2020-12-31 18:15:19 +00:00
|
|
|
}
|