Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(diagnostic): ansi code colored disagnostics in floating and spli… #456

Merged
merged 7 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- LSP: Added colored ansi code diagnotic to floating and split window.

## [5.0.0] - 2024-07-26

### BREAKING CHANGES
Expand Down
139 changes: 84 additions & 55 deletions lua/rustaceanvim/commands/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ local _window_state = {

---@param bufnr integer
---@param winnr integer
---@param lines string[]
local function set_open_split_keymap(bufnr, winnr, lines)
---@param render_fn function
local function set_split_open_keymap(bufnr, winnr, render_fn)
local function open_split()
-- check if a buffer with the latest id is already open, if it is then
-- delete it and continue
Expand All @@ -28,11 +28,7 @@ local function set_open_split_keymap(bufnr, winnr, lines)
-- split the window to create a new buffer and set it to our window
local vsplit = config.tools.float_win_config.open_split == 'vertical'
ui.split(vsplit, _window_state.latest_scratch_buf_id)

-- set filetype to rust for syntax highlighting
vim.bo[_window_state.latest_scratch_buf_id].filetype = 'rust'
-- write the expansion content to the buffer
vim.api.nvim_buf_set_lines(_window_state.latest_scratch_buf_id, 0, 0, false, lines)
render_fn()
end
vim.keymap.set('n', '<CR>', function()
local line = vim.api.nvim_win_get_cursor(winnr)[1]
Expand Down Expand Up @@ -141,7 +137,12 @@ function M.explain_error()
)
_window_state.float_winnr = winnr
set_close_keymaps(bufnr)
set_open_split_keymap(bufnr, winnr, markdown_lines)
set_split_open_keymap(bufnr, winnr, function()
-- set filetype to rust for syntax highlighting
vim.bo[_window_state.latest_scratch_buf_id].filetype = 'rust'
-- write the expansion content to the buffer
vim.api.nvim_buf_set_lines(_window_state.latest_scratch_buf_id, 0, 0, false, markdown_lines)
end)

if config.tools.float_win_config.auto_focus then
vim.api.nvim_set_current_win(winnr)
Expand Down Expand Up @@ -211,7 +212,12 @@ function M.explain_error_current_line()
)
_window_state.float_winnr = winnr
set_close_keymaps(bufnr)
set_open_split_keymap(bufnr, winnr, markdown_lines)
set_split_open_keymap(bufnr, winnr, function()
-- set filetype to rust for syntax highlighting
vim.bo[_window_state.latest_scratch_buf_id].filetype = 'rust'
-- write the expansion content to the buffer
vim.api.nvim_buf_set_lines(_window_state.latest_scratch_buf_id, 0, 0, false, markdown_lines)
end)

if config.tools.float_win_config.auto_focus then
vim.api.nvim_set_current_win(winnr)
Expand All @@ -233,6 +239,73 @@ local function get_rendered_diagnostic(diagnostic)
end
end

---@param rendered_diagnostic string
local function render_ansi_code_diagnostic(rendered_diagnostic)
-- adopted from https://stackoverflow.com/questions/48948630/lua-ansi-escapes-pattern
local lines =
vim.split(rendered_diagnostic:gsub('[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]', ''), '\n', { trimempty = true })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Add this info as a comment

Suggested change
vim.split(rendered_diagnostic:gsub('[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]', ''), '\n', { trimempty = true })
-- adopted from https://stackoverflow.com/questions/48948630/lua-ansi-escapes-pattern
vim.split(rendered_diagnostic:gsub('[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]', ''), '\n', { trimempty = true })

local float_preview_lines = vim.deepcopy(lines)
table.insert(float_preview_lines, 1, '---')
table.insert(float_preview_lines, 1, '1. Open in split')
vim.schedule(function()
close_hover()
local bufnr, winnr = vim.lsp.util.open_floating_preview(
float_preview_lines,
'',
vim.tbl_extend('keep', config.tools.float_win_config, {
focus = false,
focusable = true,
focus_id = 'ra-render-diagnostic',
close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' },
})
)
vim.api.nvim_create_autocmd('WinEnter', {
callback = function()
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes(
[[<c-\><c-n>]] .. '<cmd>lua vim.api.nvim_win_set_cursor(' .. winnr .. ',{1,0})<CR>',
true,
false,
true
),
'n',
true
)
end,
buffer = bufnr,
})

local chanid = vim.api.nvim_open_term(bufnr, {})
vim.api.nvim_chan_send(chanid, vim.trim('1. Open in split\r\n' .. '---\r\n' .. rendered_diagnostic))

_window_state.float_winnr = winnr
set_close_keymaps(bufnr)
set_split_open_keymap(bufnr, winnr, function()
local chan_id = vim.api.nvim_open_term(_window_state.latest_scratch_buf_id, {})
vim.api.nvim_chan_send(chan_id, vim.trim(rendered_diagnostic))
end)
if config.tools.float_win_config.auto_focus then
vim.api.nvim_set_current_win(winnr)
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes(
'<cmd>lua vim.api.nvim_set_current_win('
.. winnr
.. ')<CR>'
.. [[<c-\><c-n>]]
.. '<cmd>lua vim.api.nvim_win_set_cursor('
.. winnr
.. ',{1,0})<CR>',
true,
false,
true
),
'n',
true
)
Comment on lines +289 to +304
Copy link
Contributor Author

@xzbdmw xzbdmw Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to ensure cursor syncs properly, nvim_win_set_cursor is not a synchronized call, when cursor first enters the window, Terminal mode is activated, and the cursor is at last line.

end
end)
end

function M.render_diagnostic()
local diagnostics = vim.tbl_filter(function(diagnostic)
return get_rendered_diagnostic(diagnostic) ~= nil
Expand Down Expand Up @@ -288,29 +361,7 @@ function M.render_diagnostic()
-- Open folds under the cursor
vim.cmd('normal! zv')

local lines = vim.split(rendered_diagnostic, '\n')
local float_preview_lines = vim.deepcopy(lines)
table.insert(float_preview_lines, 1, '---')
table.insert(float_preview_lines, 1, '1. Open in split')
vim.schedule(function()
close_hover()
local bufnr, winnr = vim.lsp.util.open_floating_preview(
float_preview_lines,
'',
vim.tbl_extend('keep', config.tools.float_win_config, {
focus = false,
focusable = true,
focus_id = 'ra-render-diagnostic',
close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' },
})
)
_window_state.float_winnr = winnr
set_close_keymaps(bufnr)
set_open_split_keymap(bufnr, winnr, lines)
if config.tools.float_win_config.auto_focus then
vim.api.nvim_set_current_win(winnr)
end
end)
render_ansi_code_diagnostic(rendered_diagnostic)
end

function M.render_diagnostic_current_line()
Expand All @@ -337,29 +388,7 @@ function M.render_diagnostic_current_line()
end

local rendered_diagnostic = rendered_diagnostics[1]
local lines = vim.split(rendered_diagnostic, '\n')
local float_preview_lines = vim.deepcopy(lines)
table.insert(float_preview_lines, 1, '---')
table.insert(float_preview_lines, 1, '1. Open in split')
vim.schedule(function()
close_hover()
local bufnr, winnr = vim.lsp.util.open_floating_preview(
float_preview_lines,
'',
vim.tbl_extend('keep', config.tools.float_win_config, {
focus = false,
focusable = true,
focus_id = 'ra-render-diagnostic',
close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' },
})
)
_window_state.float_winnr = winnr
set_close_keymaps(bufnr)
set_open_split_keymap(bufnr, winnr, lines)
if config.tools.float_win_config.auto_focus then
vim.api.nvim_set_current_win(winnr)
end
end)
render_ansi_code_diagnostic(rendered_diagnostic)
end

return M
1 change: 1 addition & 0 deletions lua/rustaceanvim/config/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ local function make_rustaceanvim_capabilities()
-- send actions with hover request
capabilities.experimental = {
hoverActions = true,
colorDiagnosticOutput = true,
hoverRange = true,
serverStatusNotification = true,
snippetTextEdit = true,
Expand Down
Loading