nvf/modules/plugins/languages/kotlin.nix

119 lines
3.3 KiB
Nix
Raw Normal View History

2024-09-27 18:14:03 +02:00
{
config,
pkgs,
lib,
...
}: let
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge;
2024-10-07 12:38:54 +02:00
inherit (lib.types) package enum;
inherit (lib.attrsets) attrNames;
2024-09-27 18:14:03 +02:00
inherit (lib.nvim.types) mkGrammarOption;
inherit (lib.lists) isList;
inherit (lib.nvim.lua) expToLua;
cfg = config.vim.languages.kotlin;
2024-09-30 12:43:04 +02:00
# Creating a version of the LSP with access to the kotlin binary.
# This is necessary for the LSP to load the standard library
kotlinLspWithRuntime = pkgs.symlinkJoin {
name = "kotlin-language-server-with-runtime";
paths = [
pkgs.kotlin-language-server
pkgs.kotlin
];
buildInputs = [pkgs.makeWrapper];
postBuild = ''
wrapProgram $out/bin/kotlin-language-server \
--prefix PATH : ${pkgs.lib.makeBinPath [pkgs.kotlin]}
'';
};
2024-10-07 12:38:54 +02:00
defaultFormat = "ktlint";
formats = {
ktlint = {
package = pkgs.kitlint;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.typstfmt.with({
command = "${cfg.format.package}/bin/ktlint",
})
)
'';
};
# https://github.com/Enter-tainer/typstyle
ktfmt = {
package = pkgs.ktfmt;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.typstfmt.with({
command = "${cfg.format.package}/bin/ktfmt",
})
)
'';
};
};
2024-09-27 18:14:03 +02:00
in {
options.vim.languages.kotlin = {
2024-10-07 12:27:26 +02:00
enable = mkEnableOption "Kotlin/HCL support";
2024-09-27 18:14:03 +02:00
treesitter = {
2024-10-07 12:27:26 +02:00
enable = mkEnableOption "Kotlin treesitter" // {default = config.vim.languages.enableTreesitter;};
2024-09-27 18:14:03 +02:00
package = mkGrammarOption pkgs "kotlin";
};
lsp = {
2024-10-07 12:27:26 +02:00
enable = mkEnableOption "Kotlin LSP support (kotlin_language_server)" // {default = config.vim.languages.enableLSP;};
2024-09-27 18:14:03 +02:00
package = mkOption {
2024-09-30 12:43:04 +02:00
description = "kotlin_language_server package with Kotlin runtime";
2024-09-27 18:14:03 +02:00
type = package;
2024-09-30 12:43:04 +02:00
default = kotlinLspWithRuntime;
2024-09-27 18:14:03 +02:00
};
};
2024-10-07 12:38:54 +02:00
format = {
enable = mkEnableOption "Typst document formatting" // {default = config.vim.languages.enableFormat;};
type = mkOption {
description = "Kotlin formatter to use";
type = enum (attrNames formats);
default = defaultFormat;
};
package = mkOption {
description = "Kotlin formatter package";
type = package;
default = formats.${cfg.format.type}.package;
};
};
2024-09-27 18:14:03 +02:00
};
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
})
(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
2024-09-30 12:43:04 +02:00
vim.lsp.lspconfig.sources.kotlin_language_server = ''
lspconfig.kotlin_language_server.setup {
2024-09-27 18:14:03 +02:00
capabilities = capabilities,
2024-09-30 12:43:04 +02:00
root_dir = lspconfig.util.root_pattern("main.kt", ".git"),
2024-09-27 18:14:03 +02:00
on_attach=default_on_attach,
2024-09-30 12:43:04 +02:00
init_options = {
-- speeds up the startup time for the LSP
storagePath = "vim.fn.stdpath('state') .. '/kotlin'",
},
2024-09-27 18:14:03 +02:00
cmd = ${
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/kotlin-language-server"}''
},
}
'';
})
]);
}