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,197 +1,191 @@
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 "au CursorMoved <buffer> lua require'symbols-outline.preview'.close()"
vim.cmd( end
"au CursorMoved <buffer> lua require'symbols-outline.preview'.close()")
end
end end
------------------------- -------------------------
-- STATE -- STATE
------------------------- -------------------------
M.state = { M.state = {
outline_items = {}, outline_items = {},
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()
if M.state.outline_buf ~= nil then if M.state.outline_buf ~= nil then
local function refresh_handler(response) local function refresh_handler(response)
if response == nil or type(response) ~= 'table' then if response == nil or type(response) ~= 'table' then
return return
end end
local items = parser.parse(response) local items = parser.parse(response)
M.state.code_win = vim.api.nvim_get_current_win() M.state.code_win = vim.api.nvim_get_current_win()
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
providers.request_symbols(refresh_handler)
end end
providers.request_symbols(refresh_handler)
end
end end
M._refresh = utils.debounce(__refresh, 100) 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()
local hovered_line = vim.api.nvim_win_get_cursor(win)[1] - 1 local hovered_line = vim.api.nvim_win_get_cursor(win)[1] - 1
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 end
end
-- 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, end
{value.line_in_outline, 1})
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, ":lua require('symbols-outline')._goto_location(false)<Cr>")
map(config.options.keymaps.focus_location, -- hover symbol
":lua require('symbols-outline')._goto_location(false)<Cr>") map(config.options.keymaps.hover_symbol, ":lua require('symbols-outline.hover').show_hover()<Cr>")
-- hover symbol -- preview symbol
map(config.options.keymaps.hover_symbol, map(config.options.keymaps.toggle_preview, ":lua require('symbols-outline.preview').toggle()<Cr>")
":lua require('symbols-outline.hover').show_hover()<Cr>") -- rename symbol
-- preview symbol map(config.options.keymaps.rename_symbol, ":lua require('symbols-outline.rename').rename()<Cr>")
map(config.options.keymaps.toggle_preview, -- code actions
":lua require('symbols-outline.preview').toggle()<Cr>") map(config.options.keymaps.code_actions, ":lua require('symbols-outline.code_action').show_code_actions()<Cr>")
-- rename symbol -- show help
map(config.options.keymaps.rename_symbol, map(config.options.keymaps.show_help, ":lua require('symbols-outline.config').show_help()<Cr>")
":lua require('symbols-outline.rename').rename()<Cr>") -- close outline
-- code actions map(config.options.keymaps.close, ':bw!<Cr>')
map(config.options.keymaps.code_actions,
":lua require('symbols-outline.code_action').show_code_actions()<Cr>")
-- show help
map(config.options.keymaps.show_help,
":lua require('symbols-outline.config').show_help()<Cr>")
-- close outline
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(_, _)
setup_keymaps(M.state.outline_buf) wipe_state()
setup_buffer_autocmd() end,
})
setup_keymaps(M.state.outline_buf)
setup_buffer_autocmd()
local items = parser.parse(response) local items = parser.parse(response)
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, M.state.flattened_outline_items) writer.parse_and_write(M.state.outline_buf, M.state.flattened_outline_items)
ui.setup_highlights() ui.setup_highlights()
M._highlight_current_item(M.state.code_win) M._highlight_current_item(M.state.code_win)
end end
function M.toggle_outline() function M.toggle_outline()
if M.state.outline_buf == nil then if M.state.outline_buf == nil then
M.open_outline() M.open_outline()
else else
M.close_outline() M.close_outline()
end end
end end
function M.open_outline() function M.open_outline()
if M.state.outline_buf == nil then if M.state.outline_buf == nil then
providers.request_symbols(handler) providers.request_symbols(handler)
end end
end end
function M.close_outline() function M.close_outline()
if M.state.outline_buf then if M.state.outline_buf then
vim.api.nvim_win_close(M.state.outline_win, true) vim.api.nvim_win_close(M.state.outline_win, true)
end end
end end
function M.setup(opts) function M.setup(opts)
config.setup(opts) config.setup(opts)
setup_global_autocmd() setup_global_autocmd()
end end
return M return M

View File

@@ -1,32 +1,31 @@
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
function M.show_code_actions() function M.show_code_actions()
local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
local node = main.state.flattened_outline_items[current_line] local node = main.state.flattened_outline_items[current_line]
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

@@ -3,72 +3,72 @@ local vim = vim
local M = {} local M = {}
M.defaults = { M.defaults = {
highlight_hovered_item = true, highlight_hovered_item = true,
show_guides = true, show_guides = true,
position = 'right', position = 'right',
border = 'single', border = 'single',
relative_width = true, relative_width = true,
width = 25, width = 25,
auto_close = false, auto_close = false,
auto_preview = true, auto_preview = true,
show_numbers = false, show_numbers = false,
show_relative_numbers = false, show_relative_numbers = false,
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 = {}
function M.has_numbers() function M.has_numbers()
return M.options.show_numbers or M.options.show_relative_numbers return M.options.show_numbers or M.options.show_relative_numbers
end end
function M.get_position_navigation_direction() function M.get_position_navigation_direction()
if M.options.position == 'left' then if M.options.position == 'left' then
return 'h' return 'h'
else else
return 'l' return 'l'
end end
end end
function M.get_window_width() function M.get_window_width()
@@ -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 has_value(M.options.symbol_blacklist, kind) return false
end
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 has_value(M.options.lsp_blacklist, client.name) return false
end
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,45 +1,42 @@
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
local M = {} local M = {}
local function get_hover_params(node, winnr) local function get_hover_params(node, winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr) local bufnr = vim.api.nvim_win_get_buf(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
-- handler yoinked from the default implementation -- handler yoinked from the default implementation
function M.show_hover() function M.show_hover()
local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
local node = main.state.flattened_outline_items[current_line] local node = main.state.flattened_outline_items[current_line]
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
-- return { 'No information available' }
if not (result and result.contents) then return
-- return { 'No information available' } end
return local markdown_lines = util.convert_input_to_markdown_lines(result.contents)
end markdown_lines = util.trim_empty_lines(markdown_lines)
local markdown_lines = util.convert_input_to_markdown_lines( if vim.tbl_isempty(markdown_lines) then
result.contents) -- return { 'No information available' }
markdown_lines = util.trim_empty_lines(markdown_lines) return
if vim.tbl_isempty(markdown_lines) then end
-- return { 'No information available' } return util.open_floating_preview(markdown_lines, 'markdown', config)
return end)
end
return util.open_floating_preview(markdown_lines, "markdown", config)
end)
end end
return M return M

View File

@@ -6,63 +6,63 @@ local M = {}
-- Note that the headings won't have any hierarchy (as of now). -- Note that the headings won't have any hierarchy (as of now).
---@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
header, title = string.match(value, "^(#+)%s+(.*)$")
if header and not is_inside_code_block then
depth = #header + 1
for i = depth - 1, 1, -1 do
if level_symbols[i] ~= nil then
parent = level_symbols[i].children
break
end
end
for i = depth, max_level do
if level_symbols[i] ~= nil then
level_symbols[i].selectionRange["end"].line = line - 1
level_symbols[i].range["end"].line = line - 1
level_symbols[i] = nil
end
end
max_level = depth
entry = {
kind = 13,
name = title,
selectionRange = {
start = {character = 1, line = line - 1},
["end"] = {character = 1, line = line - 1}
},
range = {
start = {character = 1, line = line - 1},
["end"] = {character = 1, line = line - 1}
},
children = {},
}
parent[#parent + 1] = entry
level_symbols[depth] = entry
end
end end
for i = 2, max_level do header, title = string.match(value, '^(#+)%s+(.*)$')
if header and not is_inside_code_block then
depth = #header + 1
for i = depth - 1, 1, -1 do
if level_symbols[i] ~= nil then if level_symbols[i] ~= nil then
level_symbols[i].selectionRange["end"].line = #lines parent = level_symbols[i].children
level_symbols[i].range["end"].line = #lines break
end end
end end
return {[1000000]={result=level_symbols[1].children}} for i = depth, max_level do
if level_symbols[i] ~= nil then
level_symbols[i].selectionRange['end'].line = line - 1
level_symbols[i].range['end'].line = line - 1
level_symbols[i] = nil
end
end
max_level = depth
entry = {
kind = 13,
name = title,
selectionRange = {
start = { character = 1, line = line - 1 },
['end'] = { character = 1, line = line - 1 },
},
range = {
start = { character = 1, line = line - 1 },
['end'] = { character = 1, line = line - 1 },
},
children = {},
}
parent[#parent + 1] = entry
level_symbols[depth] = entry
end
end
for i = 2, max_level do
if level_symbols[i] ~= nil then
level_symbols[i].selectionRange['end'].line = #lines
level_symbols[i].range['end'].line = #lines
end
end
return { [1000000] = { result = level_symbols[1].children } }
end end
return M return M

View File

@@ -1,14 +1,16 @@
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
return ret table.insert(ret, value)
end
return ret
end end
---Parses result from LSP into a table of symbols ---Parses result from LSP into a table of symbols
@@ -17,201 +19,214 @@ end
---@param hierarchy table A table of booleans which tells if a symbols parent was the last in its group. ---@param hierarchy table A table of booleans which tells if a symbols parent was the last in its group.
---@return table ---@return table
local function parse_result(result, depth, hierarchy) local function parse_result(result, depth, hierarchy)
local ret = {} local ret = {}
for index, value in pairs(result) do for index, value in pairs(result) do
if not config.is_symbol_blacklisted(symbols.kinds[value.kind]) then if not config.is_symbol_blacklisted(symbols.kinds[value.kind]) then
-- the hierarchy is basically a table of booleans which tells whether -- the hierarchy is basically a table of booleans which tells whether
-- the parent was the last in its group or not -- the parent was the last in its group or not
local hir = hierarchy or {} local hir = hierarchy or {}
-- how many parents this node has, 1 is the lowest value because its -- how many parents this node has, 1 is the lowest value because its
-- easier to work it -- easier to work it
local level = depth or 1 local level = depth or 1
-- whether this node is the last in its group -- whether this node is the last in its group
local isLast = index == #result local isLast = index == #result
local children = nil local children = nil
if value.children ~= nil then if value.children ~= nil then
-- copy by value because we dont want it messing with the hir table -- copy by value because we dont want it messing with the hir table
local child_hir = array_copy(hir) local child_hir = array_copy(hir)
table.insert(child_hir, isLast) table.insert(child_hir, isLast)
children = parse_result(value.children, level + 1, child_hir) children = parse_result(value.children, level + 1, child_hir)
end end
-- support SymbolInformation[] -- support SymbolInformation[]
-- https://microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol -- https://microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol
local selectionRange = value.selectionRange local selectionRange = value.selectionRange
if value.selectionRange == nil then if value.selectionRange == nil then
selectionRange = value.location.range selectionRange = value.location.range
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,
kind = value.kind, kind = value.kind,
icon = symbols.icon_from_kind(value.kind), icon = symbols.icon_from_kind(value.kind),
name = value.name or value.text, name = value.name or value.text,
detail = value.detail, detail = value.detail,
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
return ret end
return ret
end end
---Sorts the result from LSP by where the symbols start. ---Sorts the result from LSP by where the symbols start.
---@param result table Result containing symbols returned from textDocument/documentSymbol ---@param result table Result containing symbols returned from textDocument/documentSymbol
---@return table ---@return table
local function sort_result(result) local function sort_result(result)
---Returns the start location for a symbol, or nil if not found. ---Returns the start location for a symbol, or nil if not found.
---@param item table The symbol. ---@param item table The symbol.
---@return table|nil ---@return table|nil
local function get_range_start(item) local function get_range_start(item)
if item.location ~= nil then if item.location ~= nil then
return item.location.range.start return item.location.range.start
elseif item.range ~= nil then elseif item.range ~= nil then
return item.range.start return item.range.start
else else
return nil return nil
end end
end
table.sort(result, function(a, b)
local a_start = get_range_start(a)
local b_start = get_range_start(b)
-- if they both are equal, a should be before b
if a_start == nil and b_start == nil then
return false
end end
table.sort(result, function(a, b) -- those with no start go first
local a_start = get_range_start(a) if a_start == nil then
local b_start = get_range_start(b) return true
end
if b_start == nil then
return false
end
-- if they both are equal, a should be before b -- first try to sort by line. If lines are equal, sort by character instead
if a_start == nil and b_start == nil then return false end if a_start.line ~= b_start.line then
return a_start.line < b_start.line
else
return a_start.character < b_start.character
end
end)
-- those with no start go first return result
if a_start == nil then 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
if a_start.line ~= b_start.line then
return a_start.line < b_start.line
else
return a_start.character < b_start.character
end
end)
return result
end end
---Parses the response from lsp request 'textDocument/documentSymbol' using buf_request_all ---Parses the response from lsp request 'textDocument/documentSymbol' using buf_request_all
---@param response table The result from buf_request_all ---@param response table The result from buf_request_all
---@return table outline items ---@return table outline items
function M.parse(response) function M.parse(response)
local all_results = {} local all_results = {}
-- flatten results to one giant table of symbols -- flatten results to one giant table of symbols
for client_id, client_response in pairs(response) do for client_id, client_response in pairs(response) do
if config.is_client_blacklisted(client_id) then if config.is_client_blacklisted(client_id) then
print('skipping client ' .. client_id) print('skipping client ' .. client_id)
goto continue goto continue
end
local result = client_response['result']
if result == nil or type(result) ~= 'table' then goto continue end
for _, value in pairs(result) do table.insert(all_results, value) end
::continue::
end end
local sorted = sort_result(all_results) local result = client_response['result']
if result == nil or type(result) ~= 'table' then goto continue end
return parse_result(sorted) for _, value in pairs(result) do
table.insert(all_results, value)
end
::continue::
end
local sorted = sort_result(all_results)
return parse_result(sorted)
end end
function M.flatten(outline_items) function M.flatten(outline_items)
local ret = {} local ret = {}
for _, value in ipairs(outline_items) do for _, value in ipairs(outline_items) do
table.insert(ret, value) table.insert(ret, value)
if value.children ~= nil then if value.children ~= nil then
local inner = M.flatten(value.children) local inner = M.flatten(value.children)
for _, value_inner in ipairs(inner) do for _, value_inner in ipairs(inner) do
table.insert(ret, value_inner) table.insert(ret, value_inner)
end end
end
end end
return ret end
return ret
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
return ret ret = ret .. tostring(value)
end
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
return t t[i] = str:sub(i, i)
end
return t
end end
function M.get_lines(flattened_outline_items) 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
if value.isLast then if value.isLast then
line[index] = ui.markers.bottom line[index] = ui.markers.bottom
else else
line[index] = ui.markers.middle line[index] = ui.markers.middle
end end
-- else if the parent was not the last in its group, add a -- else if the parent was not the last in its group, add a
-- vertical marker because there are items under us and we need -- vertical marker because there are items under us and we need
-- to point to those -- to point to those
elseif not value.hierarchy[index] then elseif not value.hierarchy[index] then
line[index] = ui.markers.vertical line[index] = ui.markers.vertical
end
end
end end
end
local final_prefix = {}
-- Add 1 space between the guides
for _, v in ipairs(line) do
table.insert(final_prefix, v)
table.insert(final_prefix, " ")
end
local string_prefix = table_to_str(final_prefix)
local hl_start = #string_prefix
local hl_end = #string_prefix + #value.icon
table.insert(lines, string_prefix .. value.icon .. " " ..
value.name)
hl_type = config.options.symbols[symbols.kinds[value.kind]].hl
table.insert(hl_info, {hl_start, hl_end, hl_type})
end end
return lines, hl_info
local final_prefix = {}
-- Add 1 space between the guides
for _, v in ipairs(line) do
table.insert(final_prefix, v)
table.insert(final_prefix, ' ')
end
local string_prefix = table_to_str(final_prefix)
local hl_start = #string_prefix
local hl_end = #string_prefix + #value.icon
table.insert(lines, string_prefix .. value.icon .. ' ' .. value.name)
hl_type = config.options.symbols[symbols.kinds[value.kind]].hl
table.insert(hl_info, { hl_start, hl_end, hl_type })
end
return lines, hl_info
end 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
return M return M

View File

@@ -1,236 +1,236 @@
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 = {}
local state = { local state = {
preview_buf = nil, preview_buf = nil,
preview_win = nil, preview_win = nil,
hover_buf = nil, hover_buf = nil,
hover_win = nil, hover_win = nil,
} }
local function is_current_win_outline() local function is_current_win_outline()
local curwin = vim.api.nvim_get_current_win() local curwin = vim.api.nvim_get_current_win()
return curwin == main.state.outline_win return curwin == main.state.outline_win
end end
local function has_code_win() local function has_code_win()
local isWinValid = vim.api.nvim_win_is_valid(main.state.code_win) local isWinValid = vim.api.nvim_win_is_valid(main.state.code_win)
if not isWinValid then if not isWinValid then
return false return false
end end
local bufnr = vim.api.nvim_win_get_buf(main.state.code_win) local bufnr = vim.api.nvim_win_get_buf(main.state.code_win)
local isBufValid = vim.api.nvim_buf_is_valid(bufnr) local isBufValid = vim.api.nvim_buf_is_valid(bufnr)
return isBufValid return isBufValid
end end
local function get_offset() local function get_offset()
local outline_winnr = main.state.outline_win local outline_winnr = main.state.outline_win
local width = 53 local width = 53
local height = 0 local height = 0
if config.has_numbers() then if config.has_numbers() then
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
end end
return { height, width } return { height, width }
end end
local function get_height() local function get_height()
local uis = vim.api.nvim_list_uis() local uis = vim.api.nvim_list_uis()
return math.ceil(uis[1].height / 3) return math.ceil(uis[1].height / 3)
end end
local function get_hovered_node() local function get_hovered_node()
local hovered_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] local hovered_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
local node = main.state.flattened_outline_items[hovered_line] local node = main.state.flattened_outline_items[hovered_line]
return node return node
end end
local function update_preview(code_buf) local function update_preview(code_buf)
code_buf = code_buf or vim.api.nvim_win_get_buf(main.state.code_win) code_buf = code_buf or vim.api.nvim_win_get_buf(main.state.code_win)
local node = get_hovered_node() local node = get_hovered_node()
if not node then if not node then
return return
end end
local lines = vim.api.nvim_buf_get_lines(code_buf, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(code_buf, 0, -1, false)
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
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
-- 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
local function get_hover_params(node, winnr) local function get_hover_params(node, winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr) local bufnr = vim.api.nvim_win_get_buf(winnr)
local uri = vim.uri_from_bufnr(bufnr) local uri = vim.uri_from_bufnr(bufnr)
return { return {
textDocument = { uri = uri }, textDocument = { uri = uri },
position = { line = node.line, character = node.character }, position = { line = node.line, character = node.character },
bufnr = bufnr, bufnr = bufnr,
} }
end end
local function update_hover() local function update_hover()
if not has_code_win() then if not has_code_win() then
return return
end end
local node = get_hovered_node() local node = get_hovered_node()
if not node then if not node then
return return
end end
local provider = _G._symbols_outline_current_provider local provider = _G._symbols_outline_current_provider
local params = get_hover_params(node, main.state.code_win) local params = get_hover_params(node, main.state.code_win)
provider.hover_info(params.bufnr, params, function(err, result) provider.hover_info(params.bufnr, params, function(err, result)
if err then if err then
print(vim.inspect(err)) print(vim.inspect(err))
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)
end end
end) end)
end end
local function setup_hover_buf() local function setup_hover_buf()
if not has_code_win() then if not has_code_win() then
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()
if state.preview_win == nil and state.preview_buf == nil then if state.preview_win == nil and state.preview_buf == nil then
state.preview_buf = vim.api.nvim_create_buf(false, true) state.preview_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_attach(state.preview_buf, false, { vim.api.nvim_buf_attach(state.preview_buf, false, {
on_detach = function() on_detach = function()
state.preview_buf = nil state.preview_buf = nil
state.preview_win = nil state.preview_win = nil
end, end,
}) })
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 },
row = offsets[1], row = offsets[1],
col = offsets[2], col = offsets[2],
border = config.options.border, border = config.options.border,
}) })
setup_preview_buf() setup_preview_buf()
else else
update_preview() update_preview()
end end
end end
local function show_hover() local function show_hover()
if state.hover_win == nil and state.hover_buf == nil then if state.hover_win == nil and state.hover_buf == nil then
state.hover_buf = vim.api.nvim_create_buf(false, true) state.hover_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_attach(state.hover_buf, false, { vim.api.nvim_buf_attach(state.hover_buf, false, {
on_detach = function() on_detach = function()
state.hover_buf = nil state.hover_buf = nil
state.hover_win = nil state.hover_win = nil
end, end,
}) })
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 },
row = offsets[1] + height + 2, row = offsets[1] + height + 2,
col = offsets[2], col = offsets[2],
border = config.options.border, border = config.options.border,
}) })
setup_hover_buf() setup_hover_buf()
else else
update_hover() update_hover()
end end
end end
function M.show() function M.show()
if not is_current_win_outline() or #vim.api.nvim_list_wins() < 2 then if not is_current_win_outline() or #vim.api.nvim_list_wins() < 2 then
return return
end end
show_preview() show_preview()
show_hover() show_hover()
set_bg_hl() set_bg_hl()
end 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
end end
function M.toggle() function M.toggle()
if state.preview_win ~= nil then if state.preview_win ~= nil then
M.close() M.close()
else else
M.show() M.show()
end end
end end
return M return M

View File

@@ -1,31 +1,31 @@
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
return M return M

View File

@@ -1,35 +1,35 @@
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
function M.has_provider() function M.has_provider()
local ret = false local ret = false
for _, value in ipairs(providers) do for _, value in ipairs(providers) do
local provider = require(value) local provider = require(value)
if provider.should_use_provider(0) then if provider.should_use_provider(0) then
ret = true ret = true
break break
end end
end end
return ret return ret
end end
---@param on_symbols function ---@param on_symbols function
function M.request_symbols(on_symbols) function M.request_symbols(on_symbols)
for _, value in ipairs(providers) do for _, value in ipairs(providers) do
local provider = require(value) local provider = require(value)
if provider.should_use_provider(0) then if provider.should_use_provider(0) then
_G._symbols_outline_current_provider = provider _G._symbols_outline_current_provider = provider
provider.request_symbols(on_symbols) provider.request_symbols(on_symbols)
break break
end end
end end
end end
return M return M

View File

@@ -1,19 +1,19 @@
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
function M.request_symbols(on_symbols) function M.request_symbols(on_symbols)
on_symbols(md_parser.handle_markdown()) on_symbols(md_parser.handle_markdown())
end end
return M return M

View File

@@ -1,57 +1,57 @@
local config = require("symbols-outline.config") local config = require 'symbols-outline.config'
local M = {} local M = {}
local function getParams() local function getParams()
return { textDocument = vim.lsp.util.make_text_document_params() } return { textDocument = vim.lsp.util.make_text_document_params() }
end end
function M.hover_info(bufnr, params, on_info) function M.hover_info(bufnr, params, on_info)
local clients = vim.lsp.buf_get_clients(bufnr) local clients = vim.lsp.buf_get_clients(bufnr)
local used_client local used_client
for id, client in pairs(clients) do for id, client in pairs(clients) do
if config.is_client_blacklisted(id) then if config.is_client_blacklisted(id) then
goto continue goto continue
else else
if client.server_capabilities.hoverProvider then if client.server_capabilities.hoverProvider then
used_client = client used_client = client
break break
end end
end end
::continue:: ::continue::
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
function M.should_use_provider(bufnr) function M.should_use_provider(bufnr)
local clients = vim.lsp.buf_get_clients(bufnr) local clients = vim.lsp.buf_get_clients(bufnr)
local ret = false local ret = false
for id, client in pairs(clients) do for id, client in pairs(clients) do
if config.is_client_blacklisted(id) then if config.is_client_blacklisted(id) then
goto continue goto continue
else else
if client.server_capabilities.documentSymbolProvider then if client.server_capabilities.documentSymbolProvider then
ret = true ret = true
break break
end end
end end
::continue:: ::continue::
end end
return ret return ret
end 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,38 +1,39 @@
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
function M.rename() function M.rename()
local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
local node = main.state.flattened_outline_items[current_line] local node = main.state.flattened_outline_items[current_line]
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 end)
end)
end end
return M return M

View File

@@ -1,24 +1,48 @@
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)
local symbols = config.options.symbols local symbols = config.options.symbols
if type(kind) == 'string' then if type(kind) == 'string' then
return symbols[kind].icon return symbols[kind].icon
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
return symbols[M.kinds[kind]].icon kind = 19
end
return symbols[M.kinds[kind]].icon
end end
return M return M

View File

@@ -1,57 +1,54 @@
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
-- Some colorschemes do some funky things with the comment highlight, most -- Some colorschemes do some funky things with the comment highlight, most
-- 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,28 +4,33 @@ 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
--- @param f function --- @param f function
--- @param delay number --- @param delay number
--- @return function --- @return function
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(
timer:stop() delay,
f(unpack(args)) 0,
end)) vim.schedule_wrap(function()
end timer:stop()
f(unpack(args))
end)
)
end
end end
return M return M

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
@@ -28,12 +28,12 @@ function M.request(bufnr, method, params, handler)
end end
function M.is_buf_attached_to_lsp(bufnr) function M.is_buf_attached_to_lsp(bufnr)
local clients = vim.lsp.buf_get_clients(bufnr or 0) local clients = vim.lsp.buf_get_clients(bufnr or 0)
return clients ~= nil and #clients > 0 return clients ~= nil and #clients > 0
end end
function M.is_buf_markdown(bufnr) function M.is_buf_markdown(bufnr)
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown' return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown'
end end
return M return M

View File

@@ -1,4 +1,4 @@
local config = require('symbols-outline.config') local config = require 'symbols-outline.config'
local M = {} local M = {}
@@ -6,38 +6,38 @@ local M = {}
---@return string bufnr ---@return string bufnr
---@return string bufnr ---@return string bufnr
function M.setup_view() function M.setup_view()
-- create a scratch unlisted buffer -- create a scratch unlisted buffer
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
end end
return M return M

View File

@@ -1,62 +1,68 @@
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
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) end
vim.api.nvim_buf_set_option(bufnr, "modifiable", false) 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_option(bufnr, 'modifiable', false)
end 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
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
local function clear_virt_text(bufnr) local function clear_virt_text(bufnr)
vim.api.nvim_buf_clear_namespace(bufnr, -1, 0, -1) vim.api.nvim_buf_clear_namespace(bufnr, -1, 0, -1)
end end
-- runs the whole writing routine where the text is cleared, new data is parsed -- runs the whole writing routine where the text is cleared, new data is parsed
-- and then written -- and then written
function M.parse_and_write(bufnr, flattened_outline_items) function M.parse_and_write(bufnr, flattened_outline_items)
local lines, hl_info = parser.get_lines(flattened_outline_items) local lines, hl_info = parser.get_lines(flattened_outline_items)
M.write_outline(bufnr, lines) M.write_outline(bufnr, lines)
clear_virt_text(bufnr) clear_virt_text(bufnr)
local details = parser.get_details(flattened_outline_items) local details = parser.get_details(flattened_outline_items)
M.add_highlights(bufnr, hl_info) M.add_highlights(bufnr, hl_info)
M.write_details(bufnr, details) M.write_details(bufnr, details)
end end
return M return M