Skip to content

Commit

Permalink
refactor ui of code action
Browse files Browse the repository at this point in the history
  • Loading branch information
glepnir committed May 20, 2024
1 parent f07c10e commit 64bc34e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
29 changes: 12 additions & 17 deletions lua/lspsaga/codeaction/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local api, lsp = vim.api, vim.lsp
local config = require('lspsaga').config
local win = require('lspsaga.window')
local preview = require('lspsaga.codeaction.preview')
local ns = api.nvim_create_namespace('saga_action')
local util = require('lspsaga.util')

local act = {}
Expand Down Expand Up @@ -40,7 +41,7 @@ function act:action_callback(tuples, enriched_ctx)
return
end
if client_with_actions[2].title then
action_title = '[' .. index .. '] ' .. clean_msg(client_with_actions[2].title)
action_title = '**' .. index .. '** ' .. clean_msg(client_with_actions[2].title)
end
if config.code_action.show_server_name == true then
if type(client_with_actions[1]) == 'string' then
Expand All @@ -64,8 +65,10 @@ function act:action_callback(tuples, enriched_ctx)

if config.ui.title then
float_opt.title = {
{ config.ui.code_action .. ' Code Actions', 'Title' },
{ ' ' .. #content .. ' ', 'SagaCount' },
{ config.ui.button[1], 'SagaButton' },
{ config.ui.code_action .. 'Actions: ', 'SagaActionTitle' },
{ tostring(#content), 'SagaActionTitle' },
{ config.ui.button[2], 'SagaButton' },
}
end

Expand All @@ -78,36 +81,28 @@ function act:action_callback(tuples, enriched_ctx)
:new_float(float_opt, true)
:setlines(content)
:bufopt({
['filetype'] = 'saga_codeaction',
['buftype'] = 'nofile',
['bufhidden'] = 'wipe',
['modifiable'] = false,
['filetype'] = 'markdown',
})
:winopt({
['conceallevel'] = 2,
['concealcursor'] = 'niv',
['cursorline'] = config.code_action.cursorline,
['cursorlineopt'] = 'both',
})
:winhl('SagaNormal', 'SagaBorder')
:wininfo()

-- initial position in code action window
api.nvim_win_set_cursor(self.action_winid, { 1, 1 })

api.nvim_win_set_hl_ns(self.action_winid, ns)
api.nvim_create_autocmd('CursorMoved', {
buffer = self.action_bufnr,
callback = function()
self:set_cursor(tuples)
end,
})

vim.opt.winhl:append('CursorLine:CodeActionCursorLine')
for i = 1, #content, 1 do
local row = i - 1
local col = content[i]:find('%]')
api.nvim_buf_add_highlight(self.action_bufnr, -1, 'CodeActionText', row, 0, -1)
api.nvim_buf_add_highlight(self.action_bufnr, 0, 'CodeActionNumber', row, 0, col)
api.nvim_buf_add_highlight(self.action_bufnr, -1, 'CodeActionText', i - 1, 0, -1)
end

self:apply_action_keys(tuples, enriched_ctx)
Expand Down Expand Up @@ -217,23 +212,23 @@ end
local function get_num()
local num
local cur_text = api.nvim_get_current_line()
num = cur_text:match('%[(%d+)%]%s+%S')
num = cur_text:match('%*%*(%d+)%*%*')
if num then
num = tonumber(num)
end
return num
end

function act:set_cursor(action_tuples)
api.nvim_buf_clear_namespace(self.action_bufnr, ns, 0, -1)
local col = 1
local current_line = api.nvim_win_get_cursor(self.action_winid)[1]

if current_line == #action_tuples + 1 then
api.nvim_win_set_cursor(self.action_winid, { 1, col })
else
api.nvim_win_set_cursor(self.action_winid, { current_line, col })
end

api.nvim_buf_add_highlight(self.action_bufnr, ns, 'SagaSelect', current_line - 1, 0, -1)
local num = get_num()
if not num or not action_tuples[num] then
return
Expand Down
2 changes: 1 addition & 1 deletion lua/lspsaga/codeaction/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ local api, lsp = vim.api, vim.lsp
local config = require('lspsaga').config
local win = require('lspsaga.window')
local util = require('lspsaga.util')
local act = require('lspsaga.codeaction')

local function get_action_diff(main_buf, tuple)
if not tuple or not tuple[2] then
return
end
local id, action = unpack(tuple)
local client = lsp.get_client_by_id(id)
local act = require('lspsaga.codeaction')
if not action.edit and client and act:support_resolve(client) then
action = act:get_resolve_action(client, action, main_buf)
if not action then
Expand Down
7 changes: 7 additions & 0 deletions lua/lspsaga/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ local function init_highlight()
fg = 'Black',
})
end

local hint_conf = api.nvim_get_hl(0, { name = 'DiagnosticHint' })
if hint_conf.link then
hint_conf = api.nvim_get_hl(0, { name = hint_conf.link })
end
api.nvim_set_hl(0, 'SagaButton', { fg = hint_conf.fg })
api.nvim_set_hl(0, 'SagaActionTitle', { fg = 'Black', bg = hint_conf.fg })
end

return {
Expand Down

0 comments on commit 64bc34e

Please sign in to comment.