Skip to content

Commit 5a72d3b

Browse files
authored
feat: share lsp diagnostic errors with goose (#53)
1 parent 762cccb commit 5a72d3b

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ The following editor context is automatically captured and included in your conv
162162
| Current file | Path to the focused file before entering goose |
163163
| Selected text | Text and lines currently selected in visual mode |
164164
| Mentioned files | File info added through [mentions](#file-mentions) |
165+
| Diagnostics | Error diagnostics from the current file (if any) |
165166

166167
<a id="file-mentions"></a>
167168
### Adding more files to context through file mentions

lua/goose/context.lua

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ M.context = {
1212

1313
-- attachments
1414
mentioned_files = nil,
15-
selections = nil
15+
selections = nil,
16+
linter_errors = nil
1617
}
1718

1819
function M.unload_attachments()
1920
M.context.mentioned_files = nil
2021
M.context.selections = nil
22+
M.context.linter_errors = nil
2123
end
2224

2325
function M.load()
@@ -27,6 +29,7 @@ function M.load()
2729

2830
M.context.current_file = current_file
2931
M.context.cursor_data = cursor_data
32+
M.context.linter_errors = M.check_linter_errors()
3033
end
3134

3235
local current_selection = M.get_current_selection()
@@ -40,6 +43,23 @@ function M.load()
4043
end
4144
end
4245

46+
function M.check_linter_errors()
47+
local diagnostics = vim.diagnostic.get(0, { severity = vim.diagnostic.severity.ERROR })
48+
if #diagnostics == 0 then
49+
return nil
50+
end
51+
52+
local message = "Found " .. #diagnostics .. " error" .. (#diagnostics > 1 and "s" or "") .. ":"
53+
54+
for i, diagnostic in ipairs(diagnostics) do
55+
local line_number = diagnostic.lnum + 1 -- Convert to 1-based line numbers
56+
local short_message = diagnostic.message:gsub("%s+", " "):gsub("^%s", ""):gsub("%s$", "")
57+
message = message .. "\n Line " .. line_number .. ": " .. short_message
58+
end
59+
60+
return message
61+
end
62+
4363
function M.new_selection(file, content, lines)
4464
return {
4565
file = file,

template/prompt.tpl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<? if current_file or mentioned_files or selections then ?>
1+
<? if current_file or mentioned_files or selections or linter_errors then ?>
22
<additional-data>
33
Below is context that may help answer the user query. Ignore if not relevant
44
<? if current_file then ?>
55
<current-file>
66
Path: <%= current_file.path %>
77
</current-file>
88
<? end ?>
9-
<? if selections or mentioned_files then ?>
9+
<? if selections or mentioned_files or linter_errors then ?>
1010
<attached-files>
1111
<? if selections then ?>
1212
<? for x, selection in ipairs(selections) do ?>
@@ -30,6 +30,11 @@
3030
</mentioned-file>
3131
<? end ?>
3232
<? end ?>
33+
<? if linter_errors then ?>
34+
<linter-errors>
35+
<%= linter_errors %>
36+
</linter-errors>
37+
<? end ?>
3338
</attached-files>
3439
<? end ?>
3540
</additional-data>
@@ -38,4 +43,4 @@
3843
</user-query>
3944
<? else ?>
4045
<%= prompt %>
41-
<? end ?>
46+
<? end ?>

0 commit comments

Comments
 (0)