chore: stylua

This commit is contained in:
Simrat Grewal
2022-08-10 15:29:39 -07:00
parent d1065bc492
commit ed6c058eee
16 changed files with 231 additions and 81 deletions

View File

@@ -52,7 +52,10 @@ M._refresh = utils.debounce(__refresh, 100)
local function goto_location(change_focus) local function goto_location(change_focus)
local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1] local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1]
local node = M.state.flattened_outline_items[current_line] local node = M.state.flattened_outline_items[current_line]
vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character }) vim.api.nvim_win_set_cursor(
M.state.code_win,
{ node.line + 1, node.character }
)
if change_focus then if change_focus then
vim.fn.win_gotoid(M.state.code_win) vim.fn.win_gotoid(M.state.code_win)
end end
@@ -64,11 +67,14 @@ end
function M._highlight_current_item(winnr) function M._highlight_current_item(winnr)
local has_provider = providers.has_provider() local has_provider = providers.has_provider()
local is_current_buffer_the_outline = M.view.bufnr == vim.api.nvim_get_current_buf() local is_current_buffer_the_outline = M.view.bufnr
== vim.api.nvim_get_current_buf()
local doesnt_have_outline_buf = not M.view.bufnr local doesnt_have_outline_buf = not M.view.bufnr
local should_exit = not has_provider or doesnt_have_outline_buf or is_current_buffer_the_outline local should_exit = not has_provider
or doesnt_have_outline_buf
or is_current_buffer_the_outline
-- Make a special case if we have a window number -- Make a special case if we have a window number
-- Because we might use this to manually focus so we dont want to quit this -- Because we might use this to manually focus so we dont want to quit this
@@ -87,7 +93,10 @@ function M._highlight_current_item(winnr)
local nodes = {} local nodes = {}
for index, value in ipairs(M.state.flattened_outline_items) do for index, value in ipairs(M.state.flattened_outline_items) do
if value.line == hovered_line or (hovered_line > value.range_start and hovered_line < value.range_end) then if
value.line == hovered_line
or (hovered_line > value.range_start and hovered_line < value.range_end)
then
value.line_in_outline = index value.line_in_outline = index
table.insert(nodes, value) table.insert(nodes, value)
end end
@@ -96,7 +105,11 @@ function M._highlight_current_item(winnr)
-- clear old highlight -- clear old highlight
ui.clear_hover_highlight(M.view.bufnr) ui.clear_hover_highlight(M.view.bufnr)
for _, value in ipairs(nodes) do for _, value in ipairs(nodes) do
ui.add_hover_highlight(M.view.bufnr, value.line_in_outline - 1, value.depth * 2) ui.add_hover_highlight(
M.view.bufnr,
value.line_in_outline - 1,
value.depth * 2
)
vim.api.nvim_win_set_cursor(M.view.winnr, { value.line_in_outline, 1 }) vim.api.nvim_win_set_cursor(M.view.winnr, { value.line_in_outline, 1 })
end end
end end
@@ -114,17 +127,28 @@ local function setup_keymaps(bufnr)
goto_location(false) goto_location(false)
end) end)
-- hover symbol -- hover symbol
map(config.options.keymaps.hover_symbol, require('symbols-outline.hover').show_hover) map(
config.options.keymaps.hover_symbol,
require('symbols-outline.hover').show_hover
)
-- rename symbol -- rename symbol
map(config.options.keymaps.rename_symbol, require('symbols-outline.rename').rename) map(
config.options.keymaps.rename_symbol,
require('symbols-outline.rename').rename
)
-- code actions -- code actions
map(config.options.keymaps.code_actions, require('symbols-outline.code_action').show_code_actions) map(
config.options.keymaps.code_actions,
require('symbols-outline.code_action').show_code_actions
)
-- show help -- show help
map(config.options.keymaps.show_help, require('symbols-outline.config').show_help) map(
config.options.keymaps.show_help,
require('symbols-outline.config').show_help
)
-- close outline -- close outline
map(config.options.keymaps.close, map(config.options.keymaps.close, function()
function () M.view:close()
M.view:close()
end) end)
end end

View File

@@ -25,7 +25,12 @@ function M.show_code_actions()
local params = get_action_params(node, main.state.code_win) local params = get_action_params(node, main.state.code_win)
buf_request(params.bufnr, 'textDocument/codeAction', params, vim.lsp.handlers['textDocument/codeAction']) buf_request(
params.bufnr,
'textDocument/codeAction',
params,
vim.lsp.handlers['textDocument/codeAction']
)
end end
return M return M

View File

@@ -22,19 +22,26 @@ function M.show_hover()
local hover_params = get_hover_params(node, so.state.code_win) local hover_params = get_hover_params(node, so.state.code_win)
buf_request(hover_params.bufnr, 'textDocument/hover', hover_params, function(_, result, _, config) buf_request(
if not (result and result.contents) then hover_params.bufnr,
-- return { 'No information available' } 'textDocument/hover',
return hover_params,
function(_, result, _, config)
if not (result and result.contents) then
-- return { 'No information available' }
return
end
local markdown_lines = util.convert_input_to_markdown_lines(
result.contents
)
markdown_lines = util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then
-- return { 'No information available' }
return
end
return util.open_floating_preview(markdown_lines, 'markdown', config)
end end
local markdown_lines = util.convert_input_to_markdown_lines(result.contents) )
markdown_lines = util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then
-- return { 'No information available' }
return
end
return util.open_floating_preview(markdown_lines, 'markdown', config)
end)
end end
return M return M

View File

@@ -131,7 +131,9 @@ function M.parse(response)
end end
local result = client_response['result'] local result = client_response['result']
if result == nil or type(result) ~= 'table' then goto continue end if result == nil or type(result) ~= 'table' then
goto continue
end
for _, value in pairs(result) do for _, value in pairs(result) do
table.insert(all_results, value) table.insert(all_results, value)

View File

@@ -66,7 +66,10 @@ local function update_preview(code_buf)
if state.preview_buf ~= nil then if state.preview_buf ~= nil then
vim.api.nvim_buf_set_lines(state.preview_buf, 0, -1, 0, lines) vim.api.nvim_buf_set_lines(state.preview_buf, 0, -1, 0, lines)
vim.api.nvim_win_set_cursor(state.preview_win, { node.line + 1, node.character }) vim.api.nvim_win_set_cursor(
state.preview_win,
{ node.line + 1, node.character }
)
end end
end end
@@ -119,14 +122,20 @@ local function update_hover()
end end
local markdown_lines = {} local markdown_lines = {}
if result ~= nil then if result ~= nil then
markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents) markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(
result.contents
)
end end
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines) markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then if vim.tbl_isempty(markdown_lines) then
markdown_lines = { '###No info available!' } markdown_lines = { '###No info available!' }
end end
markdown_lines = vim.lsp.util.stylize_markdown(state.hover_buf, markdown_lines, {}) markdown_lines = vim.lsp.util.stylize_markdown(
state.hover_buf,
markdown_lines,
{}
)
if state.hover_buf ~= nil then if state.hover_buf ~= nil then
vim.api.nvim_buf_set_lines(state.hover_buf, 0, -1, 0, markdown_lines) vim.api.nvim_buf_set_lines(state.hover_buf, 0, -1, 0, markdown_lines)
@@ -219,10 +228,14 @@ end
function M.close() function M.close()
if has_code_win() then if has_code_win() then
if state.preview_win ~= nil and vim.api.nvim_win_is_valid(state.preview_win) then if
state.preview_win ~= nil and vim.api.nvim_win_is_valid(state.preview_win)
then
vim.api.nvim_win_close(state.preview_win, true) vim.api.nvim_win_close(state.preview_win, true)
end end
if state.hover_win ~= nil and vim.api.nvim_win_is_valid(state.hover_win) then if
state.hover_win ~= nil and vim.api.nvim_win_is_valid(state.hover_win)
then
vim.api.nvim_win_close(state.hover_win, true) vim.api.nvim_win_close(state.hover_win, true)
end end
end end

View File

@@ -15,7 +15,15 @@ function M.should_use_provider(_)
end end
function M.hover_info(_, _, on_info) function M.hover_info(_, _, on_info)
on_info(nil, { contents = { kind = 'markdown', contents = { 'No extra information availaible!' } } }) on_info(
nil,
{
contents = {
kind = 'markdown',
contents = { 'No extra information availaible!' },
},
}
)
end end
---@param on_symbols function ---@param on_symbols function

View File

@@ -1,24 +1,33 @@
local M = {} local M = {}
local parsers = require("nvim-treesitter.parsers") local parsers = require 'nvim-treesitter.parsers'
local SYMBOL_COMPONENT = 27 local SYMBOL_COMPONENT = 27
local SYMBOL_FRAGMENT = 28 local SYMBOL_FRAGMENT = 28
function M.should_use_provider(bufnr) function M.should_use_provider(bufnr)
local ft = vim.api.nvim_buf_get_option(bufnr, 'ft') local ft = vim.api.nvim_buf_get_option(bufnr, 'ft')
return string.match(ft, 'typescriptreact') or string.match(ft, 'javascriptreact') return string.match(ft, 'typescriptreact')
or string.match(ft, 'javascriptreact')
end end
function M.hover_info(_, _, on_info) function M.hover_info(_, _, on_info)
on_info(nil, { contents = { kind = 'nvim-lsp-jsx', contents = { 'No extra information availaible!' } } }) on_info(
nil,
{
contents = {
kind = 'nvim-lsp-jsx',
contents = { 'No extra information availaible!' },
},
}
)
end end
local function get_open_tag(node) local function get_open_tag(node)
if node:type() == "jsx_element" then if node:type() == 'jsx_element' then
for _, outer in ipairs(node:field("open_tag")) do for _, outer in ipairs(node:field 'open_tag') do
if outer:type() == "jsx_opening_element" then if outer:type() == 'jsx_opening_element' then
return outer return outer
end end
end end
@@ -30,14 +39,21 @@ end
local function jsx_node_detail(node, buf) local function jsx_node_detail(node, buf)
node = get_open_tag(node) or node node = get_open_tag(node) or node
local param_nodes = node:field("attribute") local param_nodes = node:field 'attribute'
if #param_nodes == 0 then return nil end if #param_nodes == 0 then
return nil
end
local res = '{ ' .. table.concat(vim.tbl_map(function (el) local res = '{ '
local a, b, c, d = el:range() .. table.concat(
local text = vim.api.nvim_buf_get_text(buf, a, b, c, d, {}) vim.tbl_map(function(el)
return text[1] local a, b, c, d = el:range()
end, param_nodes), ' ') .. ' }' local text = vim.api.nvim_buf_get_text(buf, a, b, c, d, {})
return text[1]
end, param_nodes),
' '
)
.. ' }'
return res return res
end end
@@ -47,7 +63,7 @@ local function jsx_node_tagname(node, buf)
local identifier = nil local identifier = nil
for _, val in ipairs(tagnode:field('name')) do for _, val in ipairs(tagnode:field 'name') do
if val:type() == 'identifier' then if val:type() == 'identifier' then
identifier = val identifier = val
end end
@@ -62,28 +78,37 @@ local function jsx_node_tagname(node, buf)
end end
local function convert_ts(child, children, bufnr) local function convert_ts(child, children, bufnr)
local is_frag = (child:type() == 'jsx_fragment') local is_frag = (child:type() == 'jsx_fragment')
local a, b, c, d = child:range() local a, b, c, d = child:range()
local range = { start = { line = a, character = b }, ['end'] = { line = c, character = d } } local range = {
start = { line = a, character = b },
['end'] = { line = c, character = d },
}
local converted = { local converted = {
name = (not is_frag and (jsx_node_tagname(child, bufnr) or '<unknown>')) or 'fragment', name = (not is_frag and (jsx_node_tagname(child, bufnr) or '<unknown>'))
children = (#children > 0 and children) or nil, or 'fragment',
kind = (is_frag and SYMBOL_FRAGMENT) or SYMBOL_COMPONENT, children = (#children > 0 and children) or nil,
detail = jsx_node_detail(child, bufnr), kind = (is_frag and SYMBOL_FRAGMENT) or SYMBOL_COMPONENT,
range = range, detail = jsx_node_detail(child, bufnr),
selectionRange = range range = range,
} selectionRange = range,
}
return converted
return converted
end end
local function parse_ts(root, children, bufnr) local function parse_ts(root, children, bufnr)
children = children or {} children = children or {}
for child in root:iter_children() do for child in root:iter_children() do
if vim.tbl_contains({ 'jsx_element', 'jsx_self_closing_element' }, child:type()) then if
vim.tbl_contains(
{ 'jsx_element', 'jsx_self_closing_element' },
child:type()
)
then
local new_children = {} local new_children = {}
parse_ts(child, new_children, bufnr) parse_ts(child, new_children, bufnr)
@@ -105,7 +130,7 @@ function M.request_symbols(on_symbols)
local symbols = parse_ts(root, nil, bufnr) local symbols = parse_ts(root, nil, bufnr)
-- local symbols = convert_ts(ctree) -- local symbols = convert_ts(ctree)
on_symbols({ [1000000] = { result = symbols }}) on_symbols { [1000000] = { result = symbols } }
end end
return M return M

View File

@@ -8,7 +8,15 @@ function M.should_use_provider(bufnr)
end end
function M.hover_info(_, _, on_info) function M.hover_info(_, _, on_info)
on_info(nil, { contents = { kind = 'markdown', contents = { 'No extra information availaible!' } } }) on_info(
nil,
{
contents = {
kind = 'markdown',
contents = { 'No extra information availaible!' },
},
}
)
end end
---@param on_symbols function ---@param on_symbols function

View File

@@ -23,7 +23,15 @@ function M.hover_info(bufnr, params, on_info)
end end
if not used_client then if not used_client then
on_info(nil, { contents = { kind = 'markdown', content = { 'No extra information availaible!' } } }) on_info(
nil,
{
contents = {
kind = 'markdown',
content = { 'No extra information availaible!' },
},
}
)
end end
used_client.request('textDocument/hover', params, on_info, bufnr) used_client.request('textDocument/hover', params, on_info, bufnr)
@@ -51,7 +59,12 @@ end
---@param on_symbols function ---@param on_symbols function
function M.request_symbols(on_symbols) function M.request_symbols(on_symbols)
vim.lsp.buf_request_all(0, 'textDocument/documentSymbol', getParams(), on_symbols) vim.lsp.buf_request_all(
0,
'textDocument/documentSymbol',
getParams(),
on_symbols
)
end end
return M return M

View File

@@ -27,12 +27,17 @@ function M.rename()
params.newName = new_name params.newName = new_name
buf_request(params.bufnr, 'textDocument/rename', params, function(_, result, ctx) buf_request(
if result ~= nil then params.bufnr,
local client = vim.lsp.get_client_by_id(ctx.client_id) 'textDocument/rename',
vim.lsp.util.apply_workspace_edit(result, client.offset_encoding) params,
function(_, result, ctx)
if result ~= nil then
local client = vim.lsp.get_client_by_id(ctx.client_id)
vim.lsp.util.apply_workspace_edit(result, client.offset_encoding)
end
end end
end) )
end end
return M return M

View File

@@ -17,7 +17,14 @@ function M.clear_hover_highlight(bufnr)
end end
function M.add_hover_highlight(bufnr, line, col_start) function M.add_hover_highlight(bufnr, line, col_start)
vim.api.nvim_buf_add_highlight(bufnr, M.hovered_hl_ns, 'FocusedSymbol', line, col_start, -1) vim.api.nvim_buf_add_highlight(
bufnr,
M.hovered_hl_ns,
'FocusedSymbol',
line,
col_start,
-1
)
end end
local function highlight_text(name, text, hl_group) local function highlight_text(name, text, hl_group)
@@ -36,18 +43,32 @@ function M.setup_highlights()
-- notably making them italic, which messes up the outline connector. Fix -- notably making them italic, which messes up the outline connector. Fix
-- this by copying the foreground color from the comment hl into a new -- this by copying the foreground color from the comment hl into a new
-- highlight. -- highlight.
local comment_fg_gui = vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID 'Comment'), 'fg', 'gui') local comment_fg_gui = vim.fn.synIDattr(
vim.fn.synIDtrans(vim.fn.hlID 'Comment'),
'fg',
'gui'
)
if vim.fn.hlexists 'SymbolsOutlineConnector' == 0 then if vim.fn.hlexists 'SymbolsOutlineConnector' == 0 then
vim.cmd(string.format('hi SymbolsOutlineConnector guifg=%s', comment_fg_gui)) vim.cmd(
string.format('hi SymbolsOutlineConnector guifg=%s', comment_fg_gui)
)
end end
local symbols = config.options.symbols local symbols = config.options.symbols
-- markers -- markers
highlight_text('marker_middle', M.markers.middle, 'SymbolsOutlineConnector') highlight_text('marker_middle', M.markers.middle, 'SymbolsOutlineConnector')
highlight_text('marker_vertical', M.markers.vertical, 'SymbolsOutlineConnector') highlight_text(
highlight_text('markers_horizontal', M.markers.horizontal, 'SymbolsOutlineConnector') 'marker_vertical',
M.markers.vertical,
'SymbolsOutlineConnector'
)
highlight_text(
'markers_horizontal',
M.markers.horizontal,
'SymbolsOutlineConnector'
)
highlight_text('markers_bottom', M.markers.bottom, 'SymbolsOutlineConnector') highlight_text('markers_bottom', M.markers.bottom, 'SymbolsOutlineConnector')
end end

View File

@@ -8,9 +8,13 @@ function M.nmap(bufnr, keys, action)
keys = { keys } keys = { keys }
end end
for _, lhs in ipairs(keys) do for _, lhs in ipairs(keys) do
vim.keymap.set('n', lhs, action, { silent = true, noremap = true, buffer = bufnr }) vim.keymap.set(
'n',
lhs,
action,
{ silent = true, noremap = true, buffer = bufnr }
)
end end
end end

View File

@@ -17,7 +17,12 @@ local function mk_handler(fn)
local client_id = select(4, ...) local client_id = select(4, ...)
local bufnr = select(5, ...) local bufnr = select(5, ...)
local config = select(6, ...) local config = select(6, ...)
fn(err, result, { method = method, client_id = client_id, bufnr = bufnr }, config) fn(
err,
result,
{ method = method, client_id = client_id, bufnr = bufnr },
config
)
end end
end end
end end

View File

@@ -47,7 +47,10 @@ function View:close()
end end
function View:is_open() function View:is_open()
return self.winnr and self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) and vim.api.nvim_win_is_valid(self.winnr) return self.winnr
and self.bufnr
and vim.api.nvim_buf_is_valid(self.bufnr)
and vim.api.nvim_win_is_valid(self.winnr)
end end
return View return View

View File

@@ -26,7 +26,14 @@ end
function M.add_highlights(bufnr, hl_info) function M.add_highlights(bufnr, hl_info)
for line, line_hl in ipairs(hl_info) do for line, line_hl in ipairs(hl_info) do
hl_start, hl_end, hl_type = unpack(line_hl) hl_start, hl_end, hl_type = unpack(line_hl)
vim.api.nvim_buf_add_highlight(bufnr, hlns, hl_type, line - 1, hl_start, hl_end) vim.api.nvim_buf_add_highlight(
bufnr,
hlns,
hl_type,
line - 1,
hl_start,
hl_end
)
end end
end end

View File

@@ -1,4 +1,4 @@
column_width = 120 column_width = 80
line_endings = 'Unix' line_endings = 'Unix'
indent_type = 'Spaces' indent_type = 'Spaces'
indent_width = 2 indent_width = 2