nvf/modules/plugins/languages/kotlin.nix

107 lines
2.9 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 13:41:41 +02:00
inherit (lib.meta) getExe;
inherit (lib.nvim.languages) diagnosticsToLua;
inherit (lib.types) package;
2024-10-07 13:41:41 +02:00
inherit (lib.nvim.types) mkGrammarOption diagnostics;
2024-09-27 18:14:03 +02:00
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
];
};
2024-10-07 12:38:54 +02:00
2024-10-07 13:41:41 +02:00
defaultDiagnosticsProvider = ["ktlint"];
diagnosticsProviders = {
ktlint = {
package = pkgs.ktlint;
nullConfig = pkg: ''
table.insert(
ls_sources,
null_ls.builtins.diagnostics.ktlint.with({
command = "${getExe pkg}",
})
)
'';
};
};
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-08 10:49:38 +02:00
enable = mkEnableOption "Kotlin LSP support" // {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 13:41:41 +02:00
extraDiagnostics = {
enable = mkEnableOption "extra Kotlin diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;};
types = diagnostics {
langDesc = "Kotlin";
inherit diagnosticsProviders;
inherit defaultDiagnosticsProvider;
};
};
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];
})
2024-10-07 13:41:41 +02:00
(mkIf cfg.extraDiagnostics.enable {
vim.lsp.null-ls.enable = true;
vim.lsp.null-ls.sources = diagnosticsToLua {
2024-10-09 20:30:05 +02:00
lang = "kotlin";
2024-10-07 13:41:41 +02:00
config = cfg.extraDiagnostics.types;
inherit diagnosticsProviders;
};
})
2024-09-27 18:14:03 +02:00
(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-30 12:43:04 +02:00
},
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"}''
},
}
'';
})
]);
}