Format with stylua

This commit is contained in:
FollieHiyuki
2022-02-22 22:29:24 +07:00
parent 142963e97c
commit d980dbf947
18 changed files with 875 additions and 830 deletions

View File

@@ -1,31 +1,27 @@
local vim = vim local vim = vim
local parser = require('symbols-outline.parser') local parser = require 'symbols-outline.parser'
local providers = require('symbols-outline.providers.init') local providers = require 'symbols-outline.providers.init'
local ui = require('symbols-outline.ui') local ui = require 'symbols-outline.ui'
local writer = require('symbols-outline.writer') local writer = require 'symbols-outline.writer'
local config = require('symbols-outline.config') local config = require 'symbols-outline.config'
local utils = require('symbols-outline.utils.init') local utils = require 'symbols-outline.utils.init'
local view = require('symbols-outline.view') local view = require 'symbols-outline.view'
local M = {} local M = {}
local function setup_global_autocmd() local function setup_global_autocmd()
if config.options.highlight_hovered_item then if config.options.highlight_hovered_item then
vim.cmd( vim.cmd "au CursorHold * :lua require('symbols-outline')._highlight_current_item()"
"au CursorHold * :lua require('symbols-outline')._highlight_current_item()")
end end
end end
local function setup_buffer_autocmd() local function setup_buffer_autocmd()
if config.options.auto_preview then if config.options.auto_preview then
vim.cmd( vim.cmd "au CursorHold <buffer> lua require'symbols-outline.preview'.show()"
"au CursorHold <buffer> lua require'symbols-outline.preview'.show()")
else else
vim.cmd( vim.cmd "au CursorMoved <buffer> lua require'symbols-outline.preview'.close()"
"au CursorMoved <buffer> lua require'symbols-outline.preview'.close()")
end end
end end
------------------------- -------------------------
@@ -36,11 +32,11 @@ M.state = {
flattened_outline_items = {}, flattened_outline_items = {},
outline_win = nil, outline_win = nil,
outline_buf = nil, outline_buf = nil,
code_win = 0 code_win = 0,
} }
local function wipe_state() local function wipe_state()
M.state = {outline_items = {}, flattened_outline_items = {}, code_win = 0} M.state = { outline_items = {}, flattened_outline_items = {}, code_win = 0 }
end end
local function __refresh() local function __refresh()
@@ -56,8 +52,7 @@ local function __refresh()
M.state.outline_items = items M.state.outline_items = items
M.state.flattened_outline_items = parser.flatten(items) M.state.flattened_outline_items = parser.flatten(items)
writer.parse_and_write(M.state.outline_buf, writer.parse_and_write(M.state.outline_buf, M.state.flattened_outline_items)
M.state.flattened_outline_items)
end end
providers.request_symbols(refresh_handler) providers.request_symbols(refresh_handler)
@@ -69,30 +64,34 @@ M._refresh = utils.debounce(__refresh, 100)
function M._goto_location(change_focus) function M._goto_location(change_focus)
local current_line = vim.api.nvim_win_get_cursor(M.state.outline_win)[1] local current_line = vim.api.nvim_win_get_cursor(M.state.outline_win)[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, vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character })
{node.line + 1, node.character}) if change_focus then
if change_focus then vim.fn.win_gotoid(M.state.code_win) end vim.fn.win_gotoid(M.state.code_win)
if config.options.auto_close then M.close_outline() end end
if config.options.auto_close then
M.close_outline()
end
end 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 = local is_current_buffer_the_outline = M.state.outline_buf == vim.api.nvim_get_current_buf()
M.state.outline_buf == vim.api.nvim_get_current_buf()
local doesnt_have_outline_buf = not M.state.outline_buf local doesnt_have_outline_buf = not M.state.outline_buf
local should_exit = (not has_provider) or local should_exit = not has_provider or doesnt_have_outline_buf or is_current_buffer_the_outline
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
-- function -- function
if winnr then should_exit = false end if winnr then
should_exit = false
end
if should_exit then return end if should_exit then
return
end
local win = winnr or vim.api.nvim_get_current_win() local win = winnr or vim.api.nvim_get_current_win()
@@ -100,8 +99,7 @@ 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 if value.line == hovered_line or (hovered_line > value.range_start and hovered_line < value.range_end) then
(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
@@ -110,51 +108,47 @@ function M._highlight_current_item(winnr)
-- clear old highlight -- clear old highlight
ui.clear_hover_highlight(M.state.outline_buf) ui.clear_hover_highlight(M.state.outline_buf)
for _, value in ipairs(nodes) do for _, value in ipairs(nodes) do
ui.add_hover_highlight(M.state.outline_buf, value.line_in_outline - 1, ui.add_hover_highlight(M.state.outline_buf, value.line_in_outline - 1, value.depth * 2)
value.depth * 2) vim.api.nvim_win_set_cursor(M.state.outline_win, { value.line_in_outline, 1 })
vim.api.nvim_win_set_cursor(M.state.outline_win,
{value.line_in_outline, 1})
end end
end end
local function setup_keymaps(bufnr) local function setup_keymaps(bufnr)
local map = function (...) local map = function(...)
utils.nmap(bufnr, ...) utils.nmap(bufnr, ...)
end end
-- goto_location of symbol and focus that window -- goto_location of symbol and focus that window
map(config.options.keymaps.goto_location, map(config.options.keymaps.goto_location, ":lua require('symbols-outline')._goto_location(true)<Cr>")
":lua require('symbols-outline')._goto_location(true)<Cr>")
-- goto_location of symbol but stay in outline -- goto_location of symbol but stay in outline
map(config.options.keymaps.focus_location, map(config.options.keymaps.focus_location, ":lua require('symbols-outline')._goto_location(false)<Cr>")
":lua require('symbols-outline')._goto_location(false)<Cr>")
-- hover symbol -- hover symbol
map(config.options.keymaps.hover_symbol, map(config.options.keymaps.hover_symbol, ":lua require('symbols-outline.hover').show_hover()<Cr>")
":lua require('symbols-outline.hover').show_hover()<Cr>")
-- preview symbol -- preview symbol
map(config.options.keymaps.toggle_preview, map(config.options.keymaps.toggle_preview, ":lua require('symbols-outline.preview').toggle()<Cr>")
":lua require('symbols-outline.preview').toggle()<Cr>")
-- rename symbol -- rename symbol
map(config.options.keymaps.rename_symbol, map(config.options.keymaps.rename_symbol, ":lua require('symbols-outline.rename').rename()<Cr>")
":lua require('symbols-outline.rename').rename()<Cr>")
-- code actions -- code actions
map(config.options.keymaps.code_actions, map(config.options.keymaps.code_actions, ":lua require('symbols-outline.code_action').show_code_actions()<Cr>")
":lua require('symbols-outline.code_action').show_code_actions()<Cr>")
-- show help -- show help
map(config.options.keymaps.show_help, map(config.options.keymaps.show_help, ":lua require('symbols-outline.config').show_help()<Cr>")
":lua require('symbols-outline.config').show_help()<Cr>")
-- close outline -- close outline
map(config.options.keymaps.close, ":bw!<Cr>") map(config.options.keymaps.close, ':bw!<Cr>')
end end
local function handler(response) local function handler(response)
if response == nil or type(response) ~= 'table' then return end if response == nil or type(response) ~= 'table' then
return
end
M.state.code_win = vim.api.nvim_get_current_win() M.state.code_win = vim.api.nvim_get_current_win()
M.state.outline_buf, M.state.outline_win = view.setup_view() M.state.outline_buf, M.state.outline_win = view.setup_view()
-- clear state when buffer is closed -- clear state when buffer is closed
vim.api.nvim_buf_attach(M.state.outline_buf, false, vim.api.nvim_buf_attach(M.state.outline_buf, false, {
{on_detach = function(_, _) wipe_state() end}) on_detach = function(_, _)
wipe_state()
end,
})
setup_keymaps(M.state.outline_buf) setup_keymaps(M.state.outline_buf)
setup_buffer_autocmd() setup_buffer_autocmd()

View File

@@ -1,21 +1,21 @@
local vim = vim local vim = vim
local main = require('symbols-outline') local main = require 'symbols-outline'
local buf_request = require('symbols-outline.utils.lsp_utils').request local buf_request = require('symbols-outline.utils.lsp_utils').request
local M = {} local M = {}
local function get_action_params(node, winnr) local function get_action_params(node, winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr) local bufnr = vim.api.nvim_win_get_buf(winnr)
local fn = "file://" .. vim.api.nvim_buf_get_name(bufnr) local fn = 'file://' .. vim.api.nvim_buf_get_name(bufnr)
local pos = {line = node.line, character = node.character} local pos = { line = node.line, character = node.character }
local diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr, node.line) local diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr, node.line)
return { return {
textDocument = {uri = fn}, textDocument = { uri = fn },
range = {start = pos, ["end"] = pos}, range = { start = pos, ['end'] = pos },
context = {diagnostics = diagnostics}, context = { diagnostics = diagnostics },
bufnr = bufnr bufnr = bufnr,
} }
end end
@@ -25,8 +25,7 @@ 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, buf_request(params.bufnr, 'textDocument/codeAction', params, vim.lsp.handlers['textDocument/codeAction'])
vim.lsp.handlers["textDocument/codeAction"])
end end
return M return M

View File

@@ -16,45 +16,45 @@ M.defaults = {
show_symbol_details = true, show_symbol_details = true,
preview_bg_highlight = 'Pmenu', preview_bg_highlight = 'Pmenu',
keymaps = { -- These keymaps can be a string or a table for multiple keys keymaps = { -- These keymaps can be a string or a table for multiple keys
close = {"<Esc>", "q"}, close = { '<Esc>', 'q' },
goto_location = "<Cr>", goto_location = '<Cr>',
focus_location = "o", focus_location = 'o',
hover_symbol = "<C-space>", hover_symbol = '<C-space>',
toggle_preview = "K", toggle_preview = 'K',
rename_symbol = "r", rename_symbol = 'r',
code_actions = "a", code_actions = 'a',
show_help = "?" show_help = '?',
}, },
lsp_blacklist = {}, lsp_blacklist = {},
symbol_blacklist = {}, symbol_blacklist = {},
symbols = { symbols = {
File = {icon = "", hl = "TSURI"}, File = { icon = '', hl = 'TSURI' },
Module = {icon = "", hl = "TSNamespace"}, Module = { icon = '', hl = 'TSNamespace' },
Namespace = {icon = "", hl = "TSNamespace"}, Namespace = { icon = '', hl = 'TSNamespace' },
Package = {icon = "", hl = "TSNamespace"}, Package = { icon = '', hl = 'TSNamespace' },
Class = {icon = "𝓒", hl = "TSType"}, Class = { icon = '𝓒', hl = 'TSType' },
Method = {icon = "ƒ", hl = "TSMethod"}, Method = { icon = 'ƒ', hl = 'TSMethod' },
Property = {icon = "", hl = "TSMethod"}, Property = { icon = '', hl = 'TSMethod' },
Field = {icon = "", hl = "TSField"}, Field = { icon = '', hl = 'TSField' },
Constructor = {icon = "", hl = "TSConstructor"}, Constructor = { icon = '', hl = 'TSConstructor' },
Enum = {icon = "", hl = "TSType"}, Enum = { icon = '', hl = 'TSType' },
Interface = {icon = "", hl = "TSType"}, Interface = { icon = '', hl = 'TSType' },
Function = {icon = "", hl = "TSFunction"}, Function = { icon = '', hl = 'TSFunction' },
Variable = {icon = "", hl = "TSConstant"}, Variable = { icon = '', hl = 'TSConstant' },
Constant = {icon = "", hl = "TSConstant"}, Constant = { icon = '', hl = 'TSConstant' },
String = {icon = "𝓐", hl = "TSString"}, String = { icon = '𝓐', hl = 'TSString' },
Number = {icon = "#", hl = "TSNumber"}, Number = { icon = '#', hl = 'TSNumber' },
Boolean = {icon = "", hl = "TSBoolean"}, Boolean = { icon = '', hl = 'TSBoolean' },
Array = {icon = "", hl = "TSConstant"}, Array = { icon = '', hl = 'TSConstant' },
Object = {icon = "⦿", hl = "TSType"}, Object = { icon = '⦿', hl = 'TSType' },
Key = {icon = "🔐", hl = "TSType"}, Key = { icon = '🔐', hl = 'TSType' },
Null = {icon = "NULL", hl = "TSType"}, Null = { icon = 'NULL', hl = 'TSType' },
EnumMember = {icon = "", hl = "TSField"}, EnumMember = { icon = '', hl = 'TSField' },
Struct = {icon = "𝓢", hl = "TSType"}, Struct = { icon = '𝓢', hl = 'TSType' },
Event = {icon = "🗲", hl = "TSType"}, Event = { icon = '🗲', hl = 'TSType' },
Operator = {icon = "+", hl = "TSOperator"}, Operator = { icon = '+', hl = 'TSOperator' },
TypeParameter = {icon = "𝙏", hl = "TSParameter"} TypeParameter = { icon = '𝙏', hl = 'TSParameter' },
} },
} }
M.options = {} M.options = {}
@@ -79,40 +79,47 @@ function M.get_window_width()
end end
end end
function M.get_split_command() function M.get_split_command()
if M.options.position == 'left' then if M.options.position == 'left' then
return "topleft vs" return 'topleft vs'
else else
return "botright vs" return 'botright vs'
end end
end end
local function has_value(tab, val) local function has_value(tab, val)
for _, value in ipairs(tab) do if value == val then return true end end for _, value in ipairs(tab) do
if value == val then
return true
end
end
return false return false
end end
function M.is_symbol_blacklisted(kind) function M.is_symbol_blacklisted(kind)
if kind == nil then return false end if kind == nil then
return false
end
return has_value(M.options.symbol_blacklist, kind) return has_value(M.options.symbol_blacklist, kind)
end end
function M.is_client_blacklisted(client_id) function M.is_client_blacklisted(client_id)
local client = vim.lsp.get_client_by_id(client_id) local client = vim.lsp.get_client_by_id(client_id)
if not client then return false end if not client then
return false
end
return has_value(M.options.lsp_blacklist, client.name) return has_value(M.options.lsp_blacklist, client.name)
end end
function M.show_help() function M.show_help()
print "Current keymaps:" print 'Current keymaps:'
print(vim.inspect(M.options.keymaps)) print(vim.inspect(M.options.keymaps))
end end
function M.setup(options) function M.setup(options)
vim.g.symbols_outline_loaded = 1 vim.g.symbols_outline_loaded = 1
M.options = vim.tbl_deep_extend("force", {}, M.defaults, options or {}) M.options = vim.tbl_deep_extend('force', {}, M.defaults, options or {})
end end
return M return M

View File

@@ -1,6 +1,6 @@
local vim = vim local vim = vim
local main = require('symbols-outline') local main = require 'symbols-outline'
local util = vim.lsp.util local util = vim.lsp.util
local buf_request = require('symbols-outline.utils.lsp_utils').request local buf_request = require('symbols-outline.utils.lsp_utils').request
@@ -11,9 +11,9 @@ local function get_hover_params(node, winnr)
local fn = vim.uri_from_bufnr(bufnr) local fn = vim.uri_from_bufnr(bufnr)
return { return {
textDocument = {uri = fn}, textDocument = { uri = fn },
position = {line = node.line, character = node.character}, position = { line = node.line, character = node.character },
bufnr = bufnr bufnr = bufnr,
} }
end end
@@ -24,21 +24,18 @@ function M.show_hover()
local hover_params = get_hover_params(node, main.state.code_win) local hover_params = get_hover_params(node, main.state.code_win)
buf_request(hover_params.bufnr, "textDocument/hover", hover_params, buf_request(hover_params.bufnr, 'textDocument/hover', hover_params, function(_, result, _, config)
function(_, result, _, config)
if not (result and result.contents) then if not (result and result.contents) then
-- return { 'No information available' } -- return { 'No information available' }
return return
end end
local markdown_lines = util.convert_input_to_markdown_lines( local markdown_lines = util.convert_input_to_markdown_lines(result.contents)
result.contents)
markdown_lines = util.trim_empty_lines(markdown_lines) markdown_lines = util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then if vim.tbl_isempty(markdown_lines) then
-- return { 'No information available' } -- return { 'No information available' }
return return
end end
return util.open_floating_preview(markdown_lines, "markdown", config) return util.open_floating_preview(markdown_lines, 'markdown', config)
end) end)
end end

View File

@@ -7,16 +7,16 @@ local M = {}
---@return table ---@return table
function M.handle_markdown() function M.handle_markdown()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local level_symbols = {{children = {}}} local level_symbols = { { children = {} } }
local max_level = 1 local max_level = 1
local is_inside_code_block = false local is_inside_code_block = false
for line, value in ipairs(lines) do for line, value in ipairs(lines) do
if string.find(value, "^```") then if string.find(value, '^```') then
is_inside_code_block = not is_inside_code_block is_inside_code_block = not is_inside_code_block
end end
header, title = string.match(value, "^(#+)%s+(.*)$") header, title = string.match(value, '^(#+)%s+(.*)$')
if header and not is_inside_code_block then if header and not is_inside_code_block then
depth = #header + 1 depth = #header + 1
@@ -29,8 +29,8 @@ function M.handle_markdown()
for i = depth, max_level do for i = depth, max_level do
if level_symbols[i] ~= nil then if level_symbols[i] ~= nil then
level_symbols[i].selectionRange["end"].line = line - 1 level_symbols[i].selectionRange['end'].line = line - 1
level_symbols[i].range["end"].line = line - 1 level_symbols[i].range['end'].line = line - 1
level_symbols[i] = nil level_symbols[i] = nil
end end
end end
@@ -40,12 +40,12 @@ function M.handle_markdown()
kind = 13, kind = 13,
name = title, name = title,
selectionRange = { selectionRange = {
start = {character = 1, line = line - 1}, start = { character = 1, line = line - 1 },
["end"] = {character = 1, line = line - 1} ['end'] = { character = 1, line = line - 1 },
}, },
range = { range = {
start = {character = 1, line = line - 1}, start = { character = 1, line = line - 1 },
["end"] = {character = 1, line = line - 1} ['end'] = { character = 1, line = line - 1 },
}, },
children = {}, children = {},
} }
@@ -57,12 +57,12 @@ function M.handle_markdown()
for i = 2, max_level do for i = 2, max_level do
if level_symbols[i] ~= nil then if level_symbols[i] ~= nil then
level_symbols[i].selectionRange["end"].line = #lines level_symbols[i].selectionRange['end'].line = #lines
level_symbols[i].range["end"].line = #lines level_symbols[i].range['end'].line = #lines
end end
end end
return {[1000000]={result=level_symbols[1].children}} return { [1000000] = { result = level_symbols[1].children } }
end end
return M return M

View File

@@ -1,13 +1,15 @@
local symbols = require('symbols-outline.symbols') local symbols = require 'symbols-outline.symbols'
local ui = require('symbols-outline.ui') local ui = require 'symbols-outline.ui'
local config = require('symbols-outline.config') local config = require 'symbols-outline.config'
local M = {} local M = {}
-- copies an array and returns it because lua usually does references -- copies an array and returns it because lua usually does references
local function array_copy(t) local function array_copy(t)
local ret = {} local ret = {}
for _, value in ipairs(t) do table.insert(ret, value) end for _, value in ipairs(t) do
table.insert(ret, value)
end
return ret return ret
end end
@@ -46,7 +48,9 @@ local function parse_result(result, depth, hierarchy)
end end
local range = value.range local range = value.range
if value.range == nil then range = value.location.range end if value.range == nil then
range = value.location.range
end
table.insert(ret, { table.insert(ret, {
deprecated = value.deprecated, deprecated = value.deprecated,
@@ -57,12 +61,12 @@ local function parse_result(result, depth, hierarchy)
line = selectionRange.start.line, line = selectionRange.start.line,
character = selectionRange.start.character, character = selectionRange.start.character,
range_start = range.start.line, range_start = range.start.line,
range_end = range["end"].line, range_end = range['end'].line,
children = children, children = children,
depth = level, depth = level,
isLast = isLast, isLast = isLast,
hierarchy = hir hierarchy = hir,
}); })
end end
end end
return ret return ret
@@ -90,11 +94,17 @@ local function sort_result(result)
local b_start = get_range_start(b) local b_start = get_range_start(b)
-- if they both are equal, a should be before b -- if they both are equal, a should be before b
if a_start == nil and b_start == nil then return false end if a_start == nil and b_start == nil then
return false
end
-- those with no start go first -- those with no start go first
if a_start == nil then return true end if a_start == nil then
if b_start == nil then return false end return true
end
if b_start == nil then
return false
end
-- first try to sort by line. If lines are equal, sort by character instead -- first try to sort by line. If lines are equal, sort by character instead
if a_start.line ~= b_start.line then if a_start.line ~= b_start.line then
@@ -123,7 +133,9 @@ function M.parse(response)
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 table.insert(all_results, value) end for _, value in pairs(result) do
table.insert(all_results, value)
end
::continue:: ::continue::
end end
@@ -148,14 +160,18 @@ function M.flatten(outline_items)
end end
local function table_to_str(t) local function table_to_str(t)
local ret = "" local ret = ''
for _, value in ipairs(t) do ret = ret .. tostring(value) end for _, value in ipairs(t) do
ret = ret .. tostring(value)
end
return ret return ret
end end
local function str_to_table(str) local function str_to_table(str)
local t = {} local t = {}
for i = 1, #str do t[i] = str:sub(i, i) end for i = 1, #str do
t[i] = str:sub(i, i)
end
return t return t
end end
@@ -163,14 +179,14 @@ function M.get_lines(flattened_outline_items)
local lines = {} local lines = {}
local hl_info = {} local hl_info = {}
for _, value in ipairs(flattened_outline_items) do for _, value in ipairs(flattened_outline_items) do
local line = str_to_table(string.rep(" ", value.depth)) local line = str_to_table(string.rep(' ', value.depth))
if config.options.show_guides then if config.options.show_guides then
-- makes the guides -- makes the guides
for index, _ in ipairs(line) do for index, _ in ipairs(line) do
-- all items start with a space (or two) -- all items start with a space (or two)
if index == 1 then if index == 1 then
line[index] = " " line[index] = ' '
-- if index is last, add a bottom marker if current item is last, -- if index is last, add a bottom marker if current item is last,
-- else add a middle marker -- else add a middle marker
elseif index == #line then elseif index == #line then
@@ -192,16 +208,15 @@ function M.get_lines(flattened_outline_items)
-- Add 1 space between the guides -- Add 1 space between the guides
for _, v in ipairs(line) do for _, v in ipairs(line) do
table.insert(final_prefix, v) table.insert(final_prefix, v)
table.insert(final_prefix, " ") table.insert(final_prefix, ' ')
end end
local string_prefix = table_to_str(final_prefix) local string_prefix = table_to_str(final_prefix)
local hl_start = #string_prefix local hl_start = #string_prefix
local hl_end = #string_prefix + #value.icon local hl_end = #string_prefix + #value.icon
table.insert(lines, string_prefix .. value.icon .. " " .. table.insert(lines, string_prefix .. value.icon .. ' ' .. value.name)
value.name)
hl_type = config.options.symbols[symbols.kinds[value.kind]].hl hl_type = config.options.symbols[symbols.kinds[value.kind]].hl
table.insert(hl_info, {hl_start, hl_end, hl_type}) table.insert(hl_info, { hl_start, hl_end, hl_type })
end end
return lines, hl_info return lines, hl_info
end end
@@ -209,7 +224,7 @@ end
function M.get_details(flattened_outline_items) function M.get_details(flattened_outline_items)
local lines = {} local lines = {}
for _, value in ipairs(flattened_outline_items) do for _, value in ipairs(flattened_outline_items) do
table.insert(lines, value.detail or "") table.insert(lines, value.detail or '')
end end
return lines return lines
end end

View File

@@ -1,7 +1,7 @@
local vim = vim local vim = vim
local main = require("symbols-outline") local main = require 'symbols-outline'
local config = require("symbols-outline.config") local config = require 'symbols-outline.config'
local buf_request = require("symbols-outline.utils.lsp_utils").request local buf_request = require('symbols-outline.utils.lsp_utils').request
local M = {} local M = {}
@@ -36,7 +36,7 @@ local function get_offset()
width = width + 4 width = width + 4
end end
if config.options.position == "right" then if config.options.position == 'right' then
width = 0 - width width = 0 - width
else else
width = vim.api.nvim_win_get_width(outline_winnr) + 1 width = vim.api.nvim_win_get_width(outline_winnr) + 1
@@ -72,10 +72,10 @@ end
local function setup_preview_buf() local function setup_preview_buf()
local code_buf = vim.api.nvim_win_get_buf(main.state.code_win) local code_buf = vim.api.nvim_win_get_buf(main.state.code_win)
local ft = vim.api.nvim_buf_get_option(code_buf, "filetype") local ft = vim.api.nvim_buf_get_option(code_buf, 'filetype')
local function treesitter_attach() local function treesitter_attach()
local ts_highlight = require("nvim-treesitter.highlight") local ts_highlight = require 'nvim-treesitter.highlight'
ts_highlight.attach(state.preview_buf, ft) ts_highlight.attach(state.preview_buf, ft)
end end
@@ -83,9 +83,9 @@ local function setup_preview_buf()
-- user might not have tree sitter installed -- user might not have tree sitter installed
pcall(treesitter_attach) pcall(treesitter_attach)
vim.api.nvim_buf_set_option(state.preview_buf, "syntax", ft) vim.api.nvim_buf_set_option(state.preview_buf, 'syntax', ft)
vim.api.nvim_buf_set_option(state.preview_buf, "bufhidden", "delete") vim.api.nvim_buf_set_option(state.preview_buf, 'bufhidden', 'delete')
vim.api.nvim_win_set_option(state.preview_win, "cursorline", true) vim.api.nvim_win_set_option(state.preview_win, 'cursorline', true)
update_preview(code_buf) update_preview(code_buf)
end end
@@ -123,7 +123,7 @@ local function update_hover()
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, {})
@@ -139,18 +139,18 @@ local function setup_hover_buf()
return return
end end
local code_buf = vim.api.nvim_win_get_buf(main.state.code_win) local code_buf = vim.api.nvim_win_get_buf(main.state.code_win)
local ft = vim.api.nvim_buf_get_option(code_buf, "filetype") local ft = vim.api.nvim_buf_get_option(code_buf, 'filetype')
vim.api.nvim_buf_set_option(state.hover_buf, "syntax", ft) vim.api.nvim_buf_set_option(state.hover_buf, 'syntax', ft)
vim.api.nvim_buf_set_option(state.hover_buf, "bufhidden", "delete") vim.api.nvim_buf_set_option(state.hover_buf, 'bufhidden', 'delete')
vim.api.nvim_win_set_option(state.hover_win, "wrap", true) vim.api.nvim_win_set_option(state.hover_win, 'wrap', true)
vim.api.nvim_win_set_option(state.hover_win, "cursorline", false) vim.api.nvim_win_set_option(state.hover_win, 'cursorline', false)
update_hover() update_hover()
end end
local function set_bg_hl() local function set_bg_hl()
local winhi = "Normal:" .. config.options.preview_bg_highlight local winhi = 'Normal:' .. config.options.preview_bg_highlight
vim.api.nvim_win_set_option(state.preview_win, "winhighlight", winhi) vim.api.nvim_win_set_option(state.preview_win, 'winhighlight', winhi)
vim.api.nvim_win_set_option(state.hover_win, "winhighlight", winhi) vim.api.nvim_win_set_option(state.hover_win, 'winhighlight', winhi)
end end
local function show_preview() local function show_preview()
@@ -164,7 +164,7 @@ local function show_preview()
}) })
local offsets = get_offset() local offsets = get_offset()
state.preview_win = vim.api.nvim_open_win(state.preview_buf, false, { state.preview_win = vim.api.nvim_open_win(state.preview_buf, false, {
relative = "win", relative = 'win',
width = 50, width = 50,
height = get_height(), height = get_height(),
bufpos = { 0, 0 }, bufpos = { 0, 0 },
@@ -190,7 +190,7 @@ local function show_hover()
local offsets = get_offset() local offsets = get_offset()
local height = get_height() local height = get_height()
state.hover_win = vim.api.nvim_open_win(state.hover_buf, false, { state.hover_win = vim.api.nvim_open_win(state.hover_buf, false, {
relative = "win", relative = 'win',
width = 50, width = 50,
height = height, height = height,
bufpos = { 0, 0 }, bufpos = { 0, 0 },

View File

@@ -1,29 +1,29 @@
local M = {} local M = {}
function M.should_use_provider(_) function M.should_use_provider(_)
local not_coc_installed = vim.fn.exists("*CocActionAsync") == 0 local not_coc_installed = vim.fn.exists '*CocActionAsync' == 0
local not_coc_service_initialized = vim.g.coc_service_initialized == 0 local not_coc_service_initialized = vim.g.coc_service_initialized == 0
if not_coc_installed or not_coc_service_initialized then if not_coc_installed or not_coc_service_initialized then
return return
end end
local coc_attached = vim.fn.call("CocAction", { "ensureDocument" }) local coc_attached = vim.fn.call('CocAction', { 'ensureDocument' })
local has_symbols = vim.fn.call("CocHasProvider", { "documentSymbol" }) local has_symbols = vim.fn.call('CocHasProvider', { 'documentSymbol' })
return coc_attached and has_symbols return coc_attached and has_symbols
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
function M.request_symbols(on_symbols) function M.request_symbols(on_symbols)
vim.fn.call("CocActionAsync", { vim.fn.call('CocActionAsync', {
"documentSymbols", 'documentSymbols',
function(_, symbols) function(_, symbols)
on_symbols({ [1000000] = { result = symbols } }) on_symbols { [1000000] = { result = symbols } }
end, end,
}) })
end end

View File

@@ -1,9 +1,9 @@
local M = {} local M = {}
local providers = { local providers = {
"symbols-outline/providers/nvim-lsp", 'symbols-outline/providers/nvim-lsp',
"symbols-outline/providers/coc", 'symbols-outline/providers/coc',
"symbols-outline/providers/markdown", 'symbols-outline/providers/markdown',
} }
_G._symbols_outline_current_provider = nil _G._symbols_outline_current_provider = nil

View File

@@ -1,14 +1,14 @@
local md_parser = require("symbols-outline.markdown") local md_parser = require 'symbols-outline.markdown'
local M = {} local M = {}
-- probably change this -- probably change this
function M.should_use_provider(bufnr) function M.should_use_provider(bufnr)
return string.match(vim.api.nvim_buf_get_option(bufnr, "ft"), "markdown") return string.match(vim.api.nvim_buf_get_option(bufnr, 'ft'), 'markdown')
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,4 +1,4 @@
local config = require("symbols-outline.config") local config = require 'symbols-outline.config'
local M = {} local M = {}
@@ -23,10 +23,10 @@ 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)
end end
-- probably change this -- probably change this
@@ -51,7 +51,7 @@ 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

@@ -1,18 +1,18 @@
local vim = vim local vim = vim
local main = require('symbols-outline') local main = require 'symbols-outline'
local buf_request = require('symbols-outline.utils.lsp_utils').request local buf_request = require('symbols-outline.utils.lsp_utils').request
local M = {} local M = {}
local function get_rename_params(node, winnr) local function get_rename_params(node, winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr) local bufnr = vim.api.nvim_win_get_buf(winnr)
local fn = "file://" .. vim.api.nvim_buf_get_name(bufnr) local fn = 'file://' .. vim.api.nvim_buf_get_name(bufnr)
return { return {
textDocument = {uri = fn}, textDocument = { uri = fn },
position = {line = node.line, character = node.character}, position = { line = node.line, character = node.character },
bufnr = bufnr bufnr = bufnr,
} }
end end
@@ -22,13 +22,14 @@ function M.rename()
local params = get_rename_params(node, main.state.code_win) local params = get_rename_params(node, main.state.code_win)
local new_name = vim.fn.input("New Name: ", node.name) local new_name = vim.fn.input('New Name: ', node.name)
if not new_name or new_name == "" or new_name == node.name then return end if not new_name or new_name == '' or new_name == node.name then
return
end
params.newName = new_name params.newName = new_name
buf_request(params.bufnr, "textDocument/rename", params, buf_request(params.bufnr, 'textDocument/rename', params, function(_, result)
function(_, result)
if result ~= nil then if result ~= nil then
vim.lsp.util.apply_workspace_edit(result) vim.lsp.util.apply_workspace_edit(result)
end end

View File

@@ -1,12 +1,34 @@
local config = require('symbols-outline.config') local config = require 'symbols-outline.config'
local M = {} local M = {}
M.kinds = { M.kinds = {
"File", "Module", "Namespace", "Package", "Class", "Method", "Property", 'File',
"Field", "Constructor", "Enum", "Interface", "Function", "Variable", 'Module',
"Constant", "String", "Number", "Boolean", "Array", "Object", "Key", "Null", 'Namespace',
"EnumMember", "Struct", "Event", "Operator", "TypeParameter" 'Package',
'Class',
'Method',
'Property',
'Field',
'Constructor',
'Enum',
'Interface',
'Function',
'Variable',
'Constant',
'String',
'Number',
'Boolean',
'Array',
'Object',
'Key',
'Null',
'EnumMember',
'Struct',
'Event',
'Operator',
'TypeParameter',
} }
function M.icon_from_kind(kind) function M.icon_from_kind(kind)
@@ -17,7 +39,9 @@ function M.icon_from_kind(kind)
end end
-- If the kind is higher than the available ones then default to 'Object' -- If the kind is higher than the available ones then default to 'Object'
if kind > #M.kinds then kind = 19 end if kind > #M.kinds then
kind = 19
end
return symbols[M.kinds[kind]].icon return symbols[M.kinds[kind]].icon
end end

View File

@@ -1,35 +1,34 @@
local vim = vim local vim = vim
local config = require('symbols-outline.config') local config = require 'symbols-outline.config'
local symbol_kinds = require('symbols-outline.symbols').kinds local symbol_kinds = require('symbols-outline.symbols').kinds
local M = {} local M = {}
M.markers = { M.markers = {
bottom = "", bottom = '',
middle = "", middle = '',
vertical = "", vertical = '',
horizontal = "" horizontal = '',
} }
M.hovered_hl_ns = vim.api.nvim_create_namespace("hovered_item") M.hovered_hl_ns = vim.api.nvim_create_namespace 'hovered_item'
function M.clear_hover_highlight(bufnr) function M.clear_hover_highlight(bufnr)
vim.api.nvim_buf_clear_namespace(bufnr, M.hovered_hl_ns, 0, -1) vim.api.nvim_buf_clear_namespace(bufnr, M.hovered_hl_ns, 0, -1)
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", vim.api.nvim_buf_add_highlight(bufnr, M.hovered_hl_ns, 'FocusedSymbol', line, col_start, -1)
line, col_start, -1)
end end
local function highlight_text(name, text, hl_group) local function highlight_text(name, text, hl_group)
vim.cmd(string.format("syn match %s /%s/", name, text)) vim.cmd(string.format('syn match %s /%s/', name, text))
vim.cmd(string.format("hi def link %s %s", name, hl_group)) vim.cmd(string.format('hi def link %s %s', name, hl_group))
end end
function M.setup_highlights() function M.setup_highlights()
-- Setup the FocusedSymbol highlight group if it hasn't been done already by -- Setup the FocusedSymbol highlight group if it hasn't been done already by
-- a theme or manually set -- a theme or manually set
if vim.fn.hlexists('FocusedSymbol') == 0 then if vim.fn.hlexists 'FocusedSymbol' == 0 then
vim.cmd 'hi FocusedSymbol term=italic,bold cterm=italic ctermbg=yellow ctermfg=darkblue gui=bold,italic guibg=yellow guifg=darkblue' vim.cmd 'hi FocusedSymbol term=italic,bold cterm=italic ctermbg=yellow ctermfg=darkblue gui=bold,italic guibg=yellow guifg=darkblue'
end end
@@ -37,21 +36,19 @@ 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, highlight_text('marker_vertical', M.markers.vertical, 'SymbolsOutlineConnector')
"SymbolsOutlineConnector") highlight_text('markers_horizontal', M.markers.horizontal, 'SymbolsOutlineConnector')
highlight_text("markers_horizontal", M.markers.horizontal, highlight_text('markers_bottom', M.markers.bottom, 'SymbolsOutlineConnector')
"SymbolsOutlineConnector")
highlight_text("markers_bottom", M.markers.bottom, "SymbolsOutlineConnector")
end end
return M return M

View File

@@ -4,11 +4,12 @@ local M = {}
---@param keys table|string ---@param keys table|string
---@param action string ---@param action string
function M.nmap(bufnr, keys, action) function M.nmap(bufnr, keys, action)
if type(keys) == 'string' then keys = {keys} end if type(keys) == 'string' then
keys = { keys }
end
for _, value in ipairs(keys) do for _, value in ipairs(keys) do
vim.api.nvim_buf_set_keymap(bufnr, "n", value, action, vim.api.nvim_buf_set_keymap(bufnr, 'n', value, action, { silent = true, noremap = true })
{silent = true, noremap = true})
end end
end end
@@ -18,13 +19,17 @@ end
function M.debounce(f, delay) function M.debounce(f, delay)
local timer = vim.loop.new_timer() local timer = vim.loop.new_timer()
return function (...) return function(...)
local args = { ... } local args = { ... }
timer:start(delay, 0, vim.schedule_wrap(function () timer:start(
delay,
0,
vim.schedule_wrap(function()
timer:stop() timer:stop()
f(unpack(args)) f(unpack(args))
end)) end)
)
end end
end end

View File

@@ -7,7 +7,7 @@ local M = {}
local function mk_handler(fn) local function mk_handler(fn)
return function(...) return function(...)
local config_or_client_id = select(4, ...) local config_or_client_id = select(4, ...)
local is_new = type(config_or_client_id) ~= "number" local is_new = type(config_or_client_id) ~= 'number'
if is_new then if is_new then
fn(...) fn(...)
else else

View File

@@ -1,4 +1,4 @@
local config = require('symbols-outline.config') local config = require 'symbols-outline.config'
local M = {} local M = {}
@@ -10,31 +10,31 @@ function M.setup_view()
local bufnr = vim.api.nvim_create_buf(false, true) local bufnr = vim.api.nvim_create_buf(false, true)
-- delete buffer when window is closed / buffer is hidden -- delete buffer when window is closed / buffer is hidden
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "delete") vim.api.nvim_buf_set_option(bufnr, 'bufhidden', 'delete')
-- create a split -- create a split
vim.cmd(config.get_split_command()) vim.cmd(config.get_split_command())
-- resize to a % of the current window size -- resize to a % of the current window size
vim.cmd("vertical resize " .. config.get_window_width()) vim.cmd('vertical resize ' .. config.get_window_width())
-- get current (outline) window and attach our buffer to it -- get current (outline) window and attach our buffer to it
local winnr = vim.api.nvim_get_current_win() local winnr = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(winnr, bufnr) vim.api.nvim_win_set_buf(winnr, bufnr)
-- window stuff -- window stuff
vim.api.nvim_win_set_option(winnr, "number", false) vim.api.nvim_win_set_option(winnr, 'number', false)
vim.api.nvim_win_set_option(winnr, "relativenumber", false) vim.api.nvim_win_set_option(winnr, 'relativenumber', false)
vim.api.nvim_win_set_option(winnr, "winfixwidth", true) vim.api.nvim_win_set_option(winnr, 'winfixwidth', true)
-- buffer stuff -- buffer stuff
vim.api.nvim_buf_set_name(bufnr, "OUTLINE") vim.api.nvim_buf_set_name(bufnr, 'OUTLINE')
vim.api.nvim_buf_set_option(bufnr, "filetype", "Outline") vim.api.nvim_buf_set_option(bufnr, 'filetype', 'Outline')
vim.api.nvim_buf_set_option(bufnr, "modifiable", false) vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
if config.options.show_numbers or config.options.show_relative_numbers then if config.options.show_numbers or config.options.show_relative_numbers then
vim.api.nvim_win_set_option(winnr, "nu", true) vim.api.nvim_win_set_option(winnr, 'nu', true)
end end
if config.options.show_relative_numbers then if config.options.show_relative_numbers then
vim.api.nvim_win_set_option(winnr, "rnu", true) vim.api.nvim_win_set_option(winnr, 'rnu', true)
end end
return bufnr, winnr return bufnr, winnr

View File

@@ -1,24 +1,26 @@
local vim = vim local vim = vim
local parser = require('symbols-outline.parser') local parser = require 'symbols-outline.parser'
local config = require('symbols-outline.config') local config = require 'symbols-outline.config'
local M = {} local M = {}
local function is_buffer_outline(bufnr) local function is_buffer_outline(bufnr)
local isValid = vim.api.nvim_buf_is_valid(bufnr) local isValid = vim.api.nvim_buf_is_valid(bufnr)
local name = vim.api.nvim_buf_get_name(bufnr) local name = vim.api.nvim_buf_get_name(bufnr)
local ft = vim.api.nvim_buf_get_option(bufnr, "filetype") local ft = vim.api.nvim_buf_get_option(bufnr, 'filetype')
return string.match(name, "OUTLINE") ~= nil and ft == "Outline" and isValid return string.match(name, 'OUTLINE') ~= nil and ft == 'Outline' and isValid
end end
local hlns = vim.api.nvim_create_namespace("symbols-outline-icon-highlight") local hlns = vim.api.nvim_create_namespace 'symbols-outline-icon-highlight'
function M.write_outline(bufnr, lines) function M.write_outline(bufnr, lines)
if not is_buffer_outline(bufnr) then return end if not is_buffer_outline(bufnr) then
vim.api.nvim_buf_set_option(bufnr, "modifiable", true) return
end
vim.api.nvim_buf_set_option(bufnr, 'modifiable', true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.api.nvim_buf_set_option(bufnr, "modifiable", false) vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
end end
function M.add_highlights(bufnr, hl_info) function M.add_highlights(bufnr, hl_info)
@@ -28,17 +30,21 @@ function M.add_highlights(bufnr, hl_info)
end end
end end
local ns = vim.api.nvim_create_namespace("symbols-outline-virt-text") local ns = vim.api.nvim_create_namespace 'symbols-outline-virt-text'
function M.write_details(bufnr, lines) function M.write_details(bufnr, lines)
if not is_buffer_outline(bufnr) then return end if not is_buffer_outline(bufnr) then
if not config.options.show_symbol_details then return end return
end
if not config.options.show_symbol_details then
return
end
for index, value in ipairs(lines) do for index, value in ipairs(lines) do
vim.api.nvim_buf_set_extmark(bufnr, ns, index - 1, -1, { vim.api.nvim_buf_set_extmark(bufnr, ns, index - 1, -1, {
virt_text = {{value, "Comment"}}, virt_text = { { value, 'Comment' } },
virt_text_pos = "eol", virt_text_pos = 'eol',
hl_mode = "combine" hl_mode = 'combine',
}) })
end end
end end