From c0790c549468f78d6b3598f8f2851cd59fcb6c07 Mon Sep 17 00:00:00 2001 From: Soliprem <73885403+Soliprem@users.noreply.github.com> Date: Thu, 10 Oct 2024 20:14:52 +0200 Subject: [PATCH] languages/kotlin: init (#390) * merge * created otter file merge * update merge * update merge * committing flake.lock merge * merge * haskell: added LSP and treesitter * haskell: default to isMaximal * haskell: haskell support * kotlin: LSP and treesitter * haskell: LSP cmd definition * haskell: LSP cmd definition (currently broken) * kotlin: LSP and treesitter working * removing haskell from kotlin branch * merge * merge * kotlin: capitalisation * kotlin: implemented formatter * kotlin: cleanup * kotlin: formatter and linter both work * kotlin: cleanup * kotlin: massive speedup in loadtimes for lsp * otter: cleanup * kotlin: changelog entry * flake.lock: reverting accidental formatting * kotlin: removed redundant description * kotlin: fixed typo * kotlin: using symlinkjoin better * kotlin: moved wrapper to example * kotlin: cleaning up and fixing docs render --------- Co-authored-by: raf --- configuration.nix | 1 + docs/release-notes/rl-0.7.md | 2 + modules/plugins/languages/default.nix | 1 + modules/plugins/languages/kotlin.nix | 107 ++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 modules/plugins/languages/kotlin.nix diff --git a/configuration.nix b/configuration.nix index 797edba..2ef5e64 100644 --- a/configuration.nix +++ b/configuration.nix @@ -51,6 +51,7 @@ isMaximal: { css.enable = isMaximal; sql.enable = isMaximal; java.enable = isMaximal; + kotlin.enable = isMaximal; ts.enable = isMaximal; svelte.enable = isMaximal; go.enable = isMaximal; diff --git a/docs/release-notes/rl-0.7.md b/docs/release-notes/rl-0.7.md index 673fcff..d5133dd 100644 --- a/docs/release-notes/rl-0.7.md +++ b/docs/release-notes/rl-0.7.md @@ -259,6 +259,8 @@ everyone. - Add LSP and Treesitter support for R under `vim.languages.R`. - Add Otter support under `vim.lsp.otter` and an assert to prevent conflict with ccc +- Add LSP, diagnostics, formatter and Treesitter support for Kotlin under + `vim.languages.kotlin` [Bloxx12](https://github.com/Bloxx12) diff --git a/modules/plugins/languages/default.nix b/modules/plugins/languages/default.nix index 28c1fd8..b46d992 100644 --- a/modules/plugins/languages/default.nix +++ b/modules/plugins/languages/default.nix @@ -8,6 +8,7 @@ in { ./css.nix ./elixir.nix ./go.nix + ./kotlin.nix ./html.nix ./java.nix ./lua.nix diff --git a/modules/plugins/languages/kotlin.nix b/modules/plugins/languages/kotlin.nix new file mode 100644 index 0000000..5637b4f --- /dev/null +++ b/modules/plugins/languages/kotlin.nix @@ -0,0 +1,107 @@ +{ + config, + pkgs, + lib, + ... +}: let + inherit (lib.options) mkEnableOption mkOption literalExpression; + inherit (lib.modules) mkIf mkMerge; + inherit (lib.meta) getExe; + inherit (lib.nvim.languages) diagnosticsToLua; + inherit (lib.types) package; + inherit (lib.nvim.types) mkGrammarOption diagnostics; + inherit (lib.lists) isList; + inherit (lib.nvim.lua) expToLua; + + cfg = config.vim.languages.kotlin; + + defaultDiagnosticsProvider = ["ktlint"]; + diagnosticsProviders = { + ktlint = { + package = pkgs.ktlint; + nullConfig = pkg: '' + table.insert( + ls_sources, + null_ls.builtins.diagnostics.ktlint.with({ + command = "${getExe pkg}", + }) + ) + ''; + }; + }; +in { + options.vim.languages.kotlin = { + enable = mkEnableOption "Kotlin/HCL support"; + + treesitter = { + enable = mkEnableOption "Kotlin treesitter" // {default = config.vim.languages.enableTreesitter;}; + package = mkGrammarOption pkgs "kotlin"; + }; + + lsp = { + enable = mkEnableOption "Kotlin LSP support" // {default = config.vim.languages.enableLSP;}; + + package = mkOption { + description = "kotlin_language_server package with Kotlin runtime"; + type = package; + example = literalExpression '' + pkgs.symlinkJoin { + name = "kotlin-language-server-wrapped"; + paths = [pkgs.kotlin-language-server]; + nativeBuildInputs = [pkgs.makeWrapper]; + postBuild = ''' + wrapProgram $out/bin/kotlin-language-server \ + --prefix PATH : ''${pkgs.kotlin}/bin + '''; + }; + ''; + default = pkgs.kotlin-language-server; + }; + }; + + extraDiagnostics = { + enable = mkEnableOption "extra Kotlin diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;}; + + types = diagnostics { + langDesc = "Kotlin"; + inherit diagnosticsProviders; + inherit defaultDiagnosticsProvider; + }; + }; + }; + config = mkIf cfg.enable (mkMerge [ + (mkIf cfg.treesitter.enable { + vim.treesitter.enable = true; + vim.treesitter.grammars = [cfg.treesitter.package]; + }) + + (mkIf cfg.extraDiagnostics.enable { + vim.lsp.null-ls.enable = true; + vim.lsp.null-ls.sources = diagnosticsToLua { + lang = "kotlin"; + config = cfg.extraDiagnostics.types; + inherit diagnosticsProviders; + }; + }) + + (mkIf cfg.lsp.enable { + vim.lsp.lspconfig.enable = true; + vim.lsp.lspconfig.sources.kotlin_language_server = '' + lspconfig.kotlin_language_server.setup { + capabilities = capabilities, + root_dir = lspconfig.util.root_pattern("main.kt", ".git"), + on_attach=default_on_attach, + init_options = { + -- speeds up the startup time for the LSP + storagePath = vim.fn.stdpath('state') .. '/kotlin', + }, + cmd = ${ + if isList cfg.lsp.package + then expToLua cfg.lsp.package + else ''{"${cfg.lsp.package}/bin/kotlin-language-server"}'' + }, + } + ''; + }) + ]); +}