Skip to content

Commit

Permalink
refactor: clean up config and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleong committed Jan 9, 2022
1 parent 9885e8e commit 9122eaa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 75 deletions.
23 changes: 11 additions & 12 deletions lua/cosmic-ui/code-action/init.lua
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down Expand Up @@ -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')
Expand All @@ -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,
Expand All @@ -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
Expand Down
26 changes: 19 additions & 7 deletions lua/cosmic-ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 14 additions & 14 deletions lua/cosmic-ui/rename/init.lua
Original file line number Diff line number Diff line change
@@ -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('<cword>')

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
Expand All @@ -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
Expand Down
59 changes: 17 additions & 42 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ Cosmic-UI is a simple wrapper around specific vim functionality. Built in order
})
```

<!-- This is because `Cosmic-UI` will initialize `lsp_signature.nvim`, which must be set up after LSP server in order to properly hook into the correct LSP handler. -->

## ⚙️ Configuration

You may override any of the settings below by passing a config object to `.setup`
Expand All @@ -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',
},
}
}
Expand Down

0 comments on commit 9122eaa

Please sign in to comment.