From b92d9e7e2638be558a466c6dfcc2d3e08b82e894 Mon Sep 17 00:00:00 2001 From: Venkatesan Ravi Date: Sun, 30 Mar 2025 20:07:18 -0700 Subject: [PATCH] languages/lua: add luacheck and stylua support (#763) * Add lint(luacheck), format(stylua) support for lua * fix comments for PR#763 * Update rl-0.8.md --------- Co-authored-by: raf --- docs/release-notes/rl-0.8.md | 4 ++ modules/plugins/languages/lua.nix | 67 ++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/docs/release-notes/rl-0.8.md b/docs/release-notes/rl-0.8.md index dc4a6fe8..8e3d93e0 100644 --- a/docs/release-notes/rl-0.8.md +++ b/docs/release-notes/rl-0.8.md @@ -284,6 +284,10 @@ - Add F# support under `vim.languages.fsharp`. +[venkyr77](https://github.com/venkyr77): + +- Add lint (luacheck) and formatting (stylua) support for Lua. + [tebuevd](https://github.com/tebuevd): - Fix `pickers` configuration for `telescope` by nesting it under `setupOpts` diff --git a/modules/plugins/languages/lua.nix b/modules/plugins/languages/lua.nix index 4ad49784..d5be4905 100644 --- a/modules/plugins/languages/lua.nix +++ b/modules/plugins/languages/lua.nix @@ -4,16 +4,30 @@ lib, ... }: let + inherit (builtins) attrNames; inherit (lib.options) mkEnableOption mkOption; inherit (lib.modules) mkIf mkMerge; inherit (lib.meta) getExe; inherit (lib.lists) isList; - inherit (lib.types) either listOf package str; - inherit (lib.nvim.types) mkGrammarOption; + inherit (lib.types) bool either enum listOf package str; + inherit (lib.nvim.types) diagnostics mkGrammarOption; inherit (lib.nvim.lua) expToLua; inherit (lib.nvim.dag) entryBefore; cfg = config.vim.languages.lua; + defaultFormat = "stylua"; + formats = { + stylua = { + package = pkgs.stylua; + }; + }; + + defaultDiagnosticsProvider = ["luacheck"]; + diagnosticsProviders = { + luacheck = { + package = pkgs.luajitPackages.luacheck; + }; + }; in { imports = [ (lib.mkRemovedOptionModule ["vim" "languages" "lua" "lsp" "neodev"] '' @@ -39,6 +53,34 @@ in { lazydev.enable = mkEnableOption "lazydev.nvim integration, useful for neovim plugin developers"; }; + + format = { + enable = mkOption { + type = bool; + default = config.vim.languages.enableFormat; + description = "Enable Lua formatting"; + }; + type = mkOption { + type = enum (attrNames formats); + default = defaultFormat; + description = "Lua formatter to use"; + }; + + package = mkOption { + type = package; + default = formats.${cfg.format.type}.package; + description = "Lua formatter package"; + }; + }; + + extraDiagnostics = { + enable = mkEnableOption "extra Lua diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;}; + types = diagnostics { + langDesc = "Lua"; + inherit diagnosticsProviders; + inherit defaultDiagnosticsProvider; + }; + }; }; config = mkMerge [ @@ -74,6 +116,27 @@ in { }) ''; }) + + (mkIf cfg.format.enable { + vim.formatter.conform-nvim = { + enable = true; + setupOpts.formatters_by_ft.lua = [cfg.format.type]; + setupOpts.formatters.${cfg.format.type} = { + command = getExe cfg.format.package; + }; + }; + }) + + (mkIf cfg.extraDiagnostics.enable { + vim.diagnostics.nvim-lint = { + enable = true; + linters_by_ft.lua = cfg.extraDiagnostics.types; + linters = mkMerge (map (name: { + ${name}.cmd = getExe diagnosticsProviders.${name}.package; + }) + cfg.extraDiagnostics.types); + }; + }) ])) ]; }