Skip to content

Commit

Permalink
feat: Myank includes diagnostics, add to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gsuuon committed Nov 29, 2024
1 parent ff0f16e commit 202a66e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ Start a new [chat](#chat-prompts)
Run a chat buffer
- `:Mchat` — Request the assistant response in a chat buffer. You can save an `mchat` buffer as `my_conversation.mchat`, reload it later and run `:Mchat` with your next message to continue where you left off. You'll need to have the same ChatPrompt configured in setup.

#### Utility
Yank a file or range with filename and diagnostics
- `:Myank` — in normal mode, yanks the entire file and diagnostics to the default register. In visual, yanks the given lines and diagnostics within the selected region. Can provide an argument to yank to a different register. Use `:Myank` then `p` to quickly add some context into a chat.

##### Telescope extension
If you use [telescope](https://github.com/nvim-telescope/telescope.nvim), mchat buffers can be browsed with `:Telescope model mchat`.

Expand Down
87 changes: 58 additions & 29 deletions lua/model/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,75 @@ local input = require('model.core.input')

local M = {}

local function yank_with_line_numbers_and_filename(register, whole_file)
local function yank_with_line_numbers_and_filename(register, range)
register = register or '"'

-- Capture the selected lines
local lines, filename, buf_name
local file_text = ''
do
buf_name = vim.fn.expand('%')
if buf_name ~= '' then
filename = util.path.relative_norm(buf_name)
else
filename = '[No Name]'
local lines, filename, buf_name
do
buf_name = vim.fn.expand('%')
if buf_name ~= '' then
filename = util.path.relative_norm(buf_name)
else
filename = '[No Name]'
end

if range == nil then
lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
else
lines =
vim.api.nvim_buf_get_lines(0, range.start - 1, range.stop, false)
filename = filename .. '#L' .. range.start .. '-L' .. range.stop
end
end

-- Get the visual selection range
local start_line = vim.fn.line("'<")
local end_line = vim.fn.line("'>")
---@cast start_line number
---@cast end_line number
local file_info = 'File: `' .. filename .. '`\n```'
local filetype = vim.fn.expand('%:e')

if whole_file then
lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
if filetype and filetype ~= '' then
file_info = file_info .. filetype .. '\n'
else
lines = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false)
filename = filename .. '#L' .. start_line .. '-L' .. end_line
file_info = file_info .. '\n'
end

file_text = file_info .. table.concat(lines, '\n') .. '\n```\n'
end

-- Add the filename and the markdown code fence language syntax
local file_info = 'File: `' .. filename .. '`\n```'
local filetype = vim.fn.expand('%:e')
local diagnostic_text = ''
do
local diagnostics
do
if range == nil then
diagnostics = vim.diagnostic.get(0)
else
diagnostics = vim.tbl_filter(function(d)
local lnum = d.lnum
return lnum >= range.start and lnum <= range.stop
end, vim.diagnostic.get(0))
end
end

if #diagnostics > 0 then
local lines = { '\nDiagnostics:\n```' }

for _, d in ipairs(diagnostics) do
local severity = vim.diagnostic.severity[d.severity]
table.insert(
lines,
string.format('[%s] L%d: %s', severity, d.lnum + 1, d.message)
)
end

if filetype and filetype ~= '' then
file_info = file_info .. filetype .. '\n'
else
file_info = file_info .. '\n'
table.insert(lines, '```')
diagnostic_text = table.concat(lines, '\n')
end
end

local result = file_info .. table.concat(lines, '\n') .. '\n```\n'
local result = file_text .. diagnostic_text

-- Set the content in the chosen register
vim.fn.setreg(register, result)

-- Return the result as a string
return result
end

Expand Down Expand Up @@ -356,8 +382,11 @@ local function setup_commands()
end
end, {})

vim.api.nvim_create_user_command('Myank', function(cmd_params)
yank_with_line_numbers_and_filename(cmd_params.args, cmd_params.range == 0)
vim.api.nvim_create_user_command('Myank', function(opts)
yank_with_line_numbers_and_filename(
opts.args,
opts.range == 2 and { start = opts.line1, stop = opts.line2 } or nil
)
end, {
range = true,
nargs = '?',
Expand Down

0 comments on commit 202a66e

Please sign in to comment.