|
10 | 10 | inherit (lib.meta) getExe; |
11 | 11 | inherit (lib.generators) mkLuaInline; |
12 | 12 | inherit (lib.types) bool enum package; |
13 | | - inherit (lib.nvim.types) mkGrammarOption deprecatedSingleOrListOf; |
| 13 | + inherit (lib.nvim.types) mkGrammarOption diagnostics deprecatedSingleOrListOf; |
14 | 14 | inherit (lib.nvim.dag) entryAfter; |
15 | 15 | inherit (lib.nvim.attrsets) mapListToAttrs; |
16 | 16 |
|
|
78 | 78 | package = pkgs.delve; |
79 | 79 | }; |
80 | 80 | }; |
| 81 | + |
| 82 | + defaultDiagnosticsProvider = ["golangci-lint"]; |
| 83 | + diagnosticsProviders = { |
| 84 | + golangci-lint = let |
| 85 | + pkg = pkgs.golangci-lint; |
| 86 | + in { |
| 87 | + package = pkg; |
| 88 | + config = { |
| 89 | + cmd = getExe pkg; |
| 90 | + args = [ |
| 91 | + "run" |
| 92 | + "--output.json.path=stdout" |
| 93 | + "--issues-exit-code=0" |
| 94 | + "--show-stats=false" |
| 95 | + "--fix=false" |
| 96 | + "--path-mode=abs" |
| 97 | + # Overwrite values that could be configured and result in unwanted writes |
| 98 | + "--output.text.path=" |
| 99 | + "--output.tab.path=" |
| 100 | + "--output.html.path=" |
| 101 | + "--output.checkstyle.path=" |
| 102 | + "--output.code-climate.path=" |
| 103 | + "--output.junit-xml.path=" |
| 104 | + "--output.teamcity.path=" |
| 105 | + "--output.sarif.path=" |
| 106 | + ]; |
| 107 | + parser = mkLuaInline '' |
| 108 | + function(output, bufnr) |
| 109 | + local SOURCE = "golangci-lint"; |
| 110 | +
|
| 111 | + local function display_tool_error(msg) |
| 112 | + return{ |
| 113 | + { |
| 114 | + bufnr = bufnr, |
| 115 | + lnum = 0, |
| 116 | + col = 0, |
| 117 | + message = string.format("[%s] %s", SOURCE, msg), |
| 118 | + severity = vim.diagnostic.severity.ERROR, |
| 119 | + source = SOURCE, |
| 120 | + }, |
| 121 | + } |
| 122 | + end |
| 123 | +
|
| 124 | + if output == "" then |
| 125 | + return display_tool_error("no output provided") |
| 126 | + end |
| 127 | +
|
| 128 | + local ok, decoded = pcall(vim.json.decode, output) |
| 129 | + if not ok then |
| 130 | + return display_tool_error("failed to parse JSON output") |
| 131 | + end |
| 132 | +
|
| 133 | + if not decoded or not decoded.Issues then |
| 134 | + return display_tool_error("unexpected output format") |
| 135 | + end |
| 136 | +
|
| 137 | + local severity_map = { |
| 138 | + error = vim.diagnostic.severity.ERROR, |
| 139 | + warning = vim.diagnostic.severity.WARN, |
| 140 | + info = vim.diagnostic.severity.INFO, |
| 141 | + hint = vim.diagnostic.severity.HINT, |
| 142 | + } |
| 143 | + local diagnostics = {} |
| 144 | + for _, issue in ipairs(decoded.Issues) do |
| 145 | + local sev = vim.diagnostic.severity.ERROR |
| 146 | + if issue.Severity and issue.Severity ~= "" then |
| 147 | + local normalized = issue.Severity:lower() |
| 148 | + sev = severity_map[normalized] or vim.diagnostic.severity.ERROR |
| 149 | + end |
| 150 | + table.insert(diagnostics, { |
| 151 | + bufnr = bufnr, |
| 152 | + lnum = issue.Pos.Line - 1, |
| 153 | + col = issue.Pos.Column - 1, |
| 154 | + message = issue.Text, |
| 155 | + code = issue.FromLinter, |
| 156 | + severity = sev, |
| 157 | + source = SOURCE, |
| 158 | + }) |
| 159 | + end |
| 160 | + return diagnostics |
| 161 | + end |
| 162 | + ''; |
| 163 | + }; |
| 164 | + }; |
| 165 | + }; |
81 | 166 | in { |
82 | 167 | options.vim.languages.go = { |
83 | 168 | enable = mkEnableOption "Go language support"; |
@@ -134,6 +219,14 @@ in { |
134 | 219 | default = debuggers.${cfg.dap.debugger}.package; |
135 | 220 | }; |
136 | 221 | }; |
| 222 | + extraDiagnostics = { |
| 223 | + enable = mkEnableOption "extra Go diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;}; |
| 224 | + types = diagnostics { |
| 225 | + langDesc = "Go"; |
| 226 | + inherit diagnosticsProviders; |
| 227 | + inherit defaultDiagnosticsProvider; |
| 228 | + }; |
| 229 | + }; |
137 | 230 | }; |
138 | 231 |
|
139 | 232 | config = mkIf cfg.enable (mkMerge [ |
@@ -179,5 +272,15 @@ in { |
179 | 272 | debugger.nvim-dap.enable = true; |
180 | 273 | }; |
181 | 274 | }) |
| 275 | + |
| 276 | + (mkIf cfg.extraDiagnostics.enable { |
| 277 | + vim.diagnostics.nvim-lint = { |
| 278 | + enable = true; |
| 279 | + linters_by_ft.go = cfg.extraDiagnostics.types; |
| 280 | + linters = |
| 281 | + mkMerge (map (name: {${name} = diagnosticsProviders.${name}.config;}) |
| 282 | + cfg.extraDiagnostics.types); |
| 283 | + }; |
| 284 | + }) |
182 | 285 | ]); |
183 | 286 | } |
0 commit comments