From 9122eaa4e91254895746defa539ebc08e99adcc0 Mon Sep 17 00:00:00 2001 From: Matt Leong Date: Sat, 8 Jan 2022 20:32:21 -0800 Subject: [PATCH] refactor: clean up config and readme --- lua/cosmic-ui/code-action/init.lua | 23 ++++++------ lua/cosmic-ui/init.lua | 26 +++++++++---- lua/cosmic-ui/rename/init.lua | 28 +++++++------- readme.md | 59 +++++++++--------------------- 4 files changed, 61 insertions(+), 75 deletions(-) diff --git a/lua/cosmic-ui/code-action/init.lua b/lua/cosmic-ui/code-action/init.lua index 863e791..7e3f8c3 100644 --- a/lua/cosmic-ui/code-action/init.lua +++ b/lua/cosmic-ui/code-action/init.lua @@ -1,7 +1,7 @@ -- big shout out to telescope -- https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/builtin/lsp.lua#L144 local Menu = require('nui.menu') -local NuiText = require('nui.text') +local Text = require('nui.text') local event = require('nui.utils.autocmd').event local utils = require('cosmic-ui.utils') local M = {} @@ -74,7 +74,7 @@ M.code_actions = function(opts) if response.result and not vim.tbl_isempty(response.result) then local client = vim.lsp.get_client_by_id(client_id) - table.insert(menu_items, Menu.separator(NuiText('(' .. client.name .. ')', 'Comment'))) + table.insert(menu_items, Menu.separator(Text('(' .. client.name .. ')', 'Comment'))) for _, result in pairs(response.result) do local command_title = result.title:gsub('\r\n', '\\r\\n'):gsub('\n', '\\n') @@ -101,25 +101,23 @@ M.code_actions = function(opts) return end - local popup_opts = utils.merge({ + local user_border = _G.CosmicUI_user_opts.code_actions.border + local popup_opts = { position = { row = 1, col = 0, }, relative = 'cursor', border = { - highlight = 'FloatBorder', - style = _G.CosmicUI_user_opts.border, + highlight = user_border.highlight, + style = user_border.style or _G.CosmicUI_user_opts.border_style, text = { - top = NuiText('Code Actions'), - top_align = 'center', + top = Text(user_border.title, user_border.title_hl), + top_align = user_border.title_align, }, padding = { 0, 1 }, }, - win_options = { - winhighlight = 'Normal:Normal', - }, - }, _G.CosmicUI_user_opts.code_actions.popup_opts or {}) + } local menu = Menu(popup_opts, { lines = menu_items, @@ -136,7 +134,8 @@ M.code_actions = function(opts) }, on_change = function(item, menu) local pos = utils.index_of(result_items, item) - menu.border:set_text('bottom', '(' .. tostring(pos) .. '/' .. #result_items .. ')', 'right') + local text = '(' .. tostring(pos) .. '/' .. #result_items .. ')' + menu.border:set_text('bottom', Text(text, user_border.bottom_hl), 'right') end, on_submit = function(item) local action = item.ctx.command diff --git a/lua/cosmic-ui/init.lua b/lua/cosmic-ui/init.lua index 5acb4d2..56ada47 100644 --- a/lua/cosmic-ui/init.lua +++ b/lua/cosmic-ui/init.lua @@ -3,24 +3,36 @@ local M = {} local default_border = 'single' local default_user_opts = { - border = default_border, + border_style = default_border, rename = { + border = { + highlight = 'FloatBorder', + style = nil, + title = 'Rename', + title_align = 'left', + title_hl = 'FloatBorder', + }, prompt = '> ', - popup_opts = {}, + prompt_hl = 'Comment', }, code_actions = { - popup_opts = {}, + min_width = nil, + border = { + bottom_hl = 'FloatBorder', + highlight = 'FloatBorder', + style = nil, + title = 'Code Actions', + title_align = 'center', + title_hl = 'FloatBorder', + }, }, } _G.CosmicUI_user_opts = {} M.setup = function(user_opts) - -- get default opts with borders set from user config - local default_opts = utils.set_border(user_opts.border or default_border, default_user_opts) - -- get parsed user opts - _G.CosmicUI_user_opts = utils.merge(default_opts, user_opts or {}) + _G.CosmicUI_user_opts = utils.merge(default_user_opts, user_opts or {}) user_opts = _G.CosmicUI_user_opts end diff --git a/lua/cosmic-ui/rename/init.lua b/lua/cosmic-ui/rename/init.lua index 6d975e4..d660ce6 100644 --- a/lua/cosmic-ui/rename/init.lua +++ b/lua/cosmic-ui/rename/init.lua @@ -1,37 +1,41 @@ local lsp = vim.lsp local utils = require('cosmic-ui.utils') local rename_handler = require('cosmic-ui.rename.handler') +local Text = require('nui.text') local function rename(popup_opts, opts) local Input = require('nui.input') local event = require('nui.utils.autocmd').event local curr_name = vim.fn.expand('') + local user_border = _G.CosmicUI_user_opts.rename.border + local width = 25 + if #curr_name > width then + width = #curr_name + end + popup_opts = utils.merge({ position = { row = 1, col = 0, }, size = { - width = 25, + width = width, height = 2, }, relative = 'cursor', border = { - highlight = 'FloatBorder', - style = _G.CosmicUI_user_opts.border, + highlight = user_border.highlight, + style = user_border.style or _G.CosmicUI_user_opts.border_style, text = { - top = ' Rename ', - top_align = 'left', + top = Text(user_border.title, user_border.title_hl), + top_align = user_border.title_align, }, }, - win_options = { - winhighlight = 'Normal:Normal', - }, - }, _G.CosmicUI_user_opts.rename.popup_opts or {}, popup_opts or {}) + }, popup_opts or {}) opts = utils.merge({ - prompt = _G.CosmicUI_user_opts.rename.prompt, + prompt = Text(_G.CosmicUI_user_opts.rename.prompt, _G.CosmicUI_user_opts.rename.prompt_hl), default_value = curr_name, on_submit = function(new_name) if not (new_name and #new_name > 0) or new_name == curr_name then @@ -48,10 +52,6 @@ local function rename(popup_opts, opts) -- mount/open the component input:mount() - -- las value is length of highlight - vim.api.nvim_buf_add_highlight(input.bufnr, -1, 'LspRenamePrompt', 0, 0, #opts.prompt) - vim.cmd('hi link LspRenamePrompt Comment') - utils.default_mappings(input) -- unmount component when cursor leaves buffer diff --git a/readme.md b/readme.md index c10a6e6..718f3ed 100644 --- a/readme.md +++ b/readme.md @@ -39,8 +39,6 @@ Cosmic-UI is a simple wrapper around specific vim functionality. Built in order }) ``` - - ## ⚙️ Configuration You may override any of the settings below by passing a config object to `.setup` @@ -49,53 +47,30 @@ You may override any of the settings below by passing a config object to `.setup { -- default border to use -- 'single', 'double', 'rounded', 'solid', 'shadow' - border = 'rounded', + border = 'single', -- rename popup settings rename = { - prompt = '> ', - -- same as nui popup options - popup_opts = { - position = { - row = 1, - col = 0, - }, - size = { - width = 25, - height = 2, - }, - relative = 'cursor', - border = { - highlight = 'FloatBorder', - style = _G.CosmicUI_user_opts.border, - text = { - top = ' Rename ', - top_align = 'left', - }, - }, - win_options = { - winhighlight = 'Normal:Normal', - }, + border = { + highlight = 'FloatBorder', + style = 'single', + title = ' Rename ', + title_align = 'left', + title_hl = 'FloatBorder', }, + prompt = '> ', + prompt_hl = 'Comment', }, code_actions = { - min_width = {}, - -- same as nui popup options - popup_opts = { - position = { - row = 1, - col = 0, - }, - relative = 'cursor', - border = { - highlight = 'FloatBorder', - text = { - top = 'Code Actions', - top_align = 'center', - }, - padding = { 0, 1 }, - }, + min_width = nil, + border = { + bottom_hl = 'FloatBorder', + highlight = 'FloatBorder', + style = 'single', + title = 'Code Actions', + title_align = 'center', + title_hl = 'FloatBorder', }, } }