chore(fmt): Finally let's use stylua

Hoping it's as good as go-fmt
This commit is contained in:
hedy
2023-11-17 09:28:33 +08:00
parent d400d4f025
commit dc55a8b942
22 changed files with 247 additions and 307 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
doc/tags doc/tags
*.patch

12
.stylua.toml Normal file
View File

@@ -0,0 +1,12 @@
column_width = 100
line_endings = 'Unix'
indent_type = 'Spaces'
indent_width = 2
# Not very good looking in general, but faster to type
quote_style = 'AutoPreferSingle'
# Since there are ambiguities sometimes let's just not use this shortcut
# alltogether
call_parentheses = 'Always'
[sort_requires]
enabled = true

View File

@@ -1,4 +1,4 @@
local main = require 'outline' local main = require('outline')
local M = {} local M = {}

View File

@@ -1,7 +1,9 @@
local utils = require('outline.utils') local utils = require('outline.utils')
local M = {} local M = {}
local all_kinds = {'File', 'Module', 'Namespace', 'Package', 'Class', 'Method', 'Property', 'Field', 'Constructor', 'Enum', 'Interface', 'Function', 'Variable', 'Constant', 'String', 'Number', 'Boolean', 'Array', 'Object', 'Key', 'Null', 'EnumMember', 'Struct', 'Event', 'Operator', 'TypeParameter', 'Component', 'Fragment', 'TypeAlias', 'Parameter', 'StaticMethod', 'Macro',} -- stylua: ignore start
local all_kinds = {'File', 'Module', 'Namespace', 'Package', 'Class', 'Method', 'Property', 'Field', 'Constructor', 'Enum', 'Interface', 'Function', 'Variable', 'Constant', 'String', 'Number', 'Boolean', 'Array', 'Object', 'Key', 'Null', 'EnumMember', 'Struct', 'Event', 'Operator', 'TypeParameter', 'Component', 'Fragment', 'TypeAlias', 'Parameter', 'StaticMethod', 'Macro'}
-- stylua: ignore end
M.defaults = { M.defaults = {
guides = { guides = {
@@ -31,7 +33,7 @@ M.defaults = {
show_relative_numbers = false, show_relative_numbers = false,
show_cursorline = true, show_cursorline = true,
hide_cursor = false, hide_cursor = false,
winhl = "OutlineDetails:Comment,OutlineLineno:LineNr", winhl = 'OutlineDetails:Comment,OutlineLineno:LineNr',
jump_highlight_duration = 500, jump_highlight_duration = 500,
}, },
preview_window = { preview_window = {
@@ -55,7 +57,7 @@ M.defaults = {
goto_location = '<Cr>', goto_location = '<Cr>',
peek_location = 'o', peek_location = 'o',
goto_and_close = '<S-Cr>', goto_and_close = '<S-Cr>',
restore_location = "<C-g>", restore_location = '<C-g>',
hover_symbol = '<C-space>', hover_symbol = '<C-space>',
toggle_preview = 'K', toggle_preview = 'K',
rename_symbol = 'r', rename_symbol = 'r',
@@ -111,7 +113,7 @@ M.defaults = {
Component = { icon = '󰅴', hl = '@function' }, Component = { icon = '󰅴', hl = '@function' },
Fragment = { icon = '󰅴', hl = '@constant' }, Fragment = { icon = '󰅴', hl = '@constant' },
-- ccls -- ccls
TypeAlias = { icon = '', hl = '@type' }, TypeAlias = { icon = '', hl = '@type' },
Parameter = { icon = '', hl = '@parameter' }, Parameter = { icon = '', hl = '@parameter' },
StaticMethod = { icon = '', hl = '@function' }, StaticMethod = { icon = '', hl = '@function' },
Macro = { icon = '', hl = '@macro' }, Macro = { icon = '', hl = '@macro' },
@@ -248,7 +250,7 @@ function M.get_providers()
end end
function M.show_help() function M.show_help()
print 'Current keymaps:' print('Current keymaps:')
print(vim.inspect(M.o.keymaps)) print(vim.inspect(M.o.keymaps))
end end
@@ -256,7 +258,7 @@ end
-- Does not alter the opts. Might show messages. -- Does not alter the opts. Might show messages.
function M.check_config() function M.check_config()
if M.o.outline_window.hide_cursor and not M.o.outline_window.show_cursorline then if M.o.outline_window.hide_cursor and not M.o.outline_window.show_cursorline then
utils.echo("config", "Warning: hide_cursor enabled without cursorline enabled") utils.echo('config', 'Warning: hide_cursor enabled without cursorline enabled')
end end
end end
@@ -277,7 +279,7 @@ function M.resolve_config()
-- This should not be needed, nor is it failsafe. But in case user only provides -- This should not be needed, nor is it failsafe. But in case user only provides
-- the, eg, "topleft", we append the ' vs'. -- the, eg, "topleft", we append the ' vs'.
if not sc:find(' vs', 1, true) then if not sc:find(' vs', 1, true) then
M.o.outline_window.split_command = sc..' vs' M.o.outline_window.split_command = sc .. ' vs'
end end
end end
----- COMPAT (renaming) ----- ----- COMPAT (renaming) -----
@@ -307,10 +309,16 @@ end
---@return T ---@return T
local function validate_filter_list(l, name) local function validate_filter_list(l, name)
if type(l) == 'boolean' and l then if type(l) == 'boolean' and l then
utils.echo("config", ("Setting %s to true is undefined behaviour. Defaulting to nil."):format(name)) utils.echo(
'config',
('Setting %s to true is undefined behaviour. Defaulting to nil.'):format(name)
)
l = nil l = nil
elseif l and type(l) ~= 'table' and type(l) ~= 'boolean' then elseif l and type(l) ~= 'table' and type(l) ~= 'boolean' then
utils.echo("config", ("%s must either be a table, false, or nil. Defaulting to nil."):format(name)) utils.echo(
'config',
('%s must either be a table, false, or nil. Defaulting to nil.'):format(name)
)
l = nil l = nil
end end
return l return l
@@ -321,7 +329,7 @@ end
function M.resolve_filter_config() function M.resolve_filter_config()
---@type outline.FilterConfig ---@type outline.FilterConfig
local tmp = M.o.symbols.filter local tmp = M.o.symbols.filter
tmp = validate_filter_list(tmp, "symbols.filter") tmp = validate_filter_list(tmp, 'symbols.filter')
---- legacy form -> ft filter list ---- ---- legacy form -> ft filter list ----
if table_has_content(M.o.symbols.blacklist) then if table_has_content(M.o.symbols.blacklist) then
@@ -337,7 +345,7 @@ function M.resolve_filter_config()
if not table_has_content(tmp) then if not table_has_content(tmp) then
tmp = { ['*'] = { exclude = true } } tmp = { ['*'] = { exclude = true } }
-- Lazy filter list -> ft filter list -- Lazy filter list -> ft filter list
elseif tmp[1] then elseif tmp[1] then
if type(tmp[1]) == 'string' then if type(tmp[1]) == 'string' then
tmp = { ['*'] = vim.deepcopy(tmp) } tmp = { ['*'] = vim.deepcopy(tmp) }
@@ -365,7 +373,10 @@ function M.resolve_filter_config()
-- } -- }
for ft, list in pairs(filter) do for ft, list in pairs(filter) do
if type(ft) ~= 'string' then if type(ft) ~= 'string' then
utils.echo("config", "ft (keys) for symbols.filter table can only be string. Skipping this ft.") utils.echo(
'config',
'ft (keys) for symbols.filter table can only be string. Skipping this ft.'
)
goto continue goto continue
end end

View File

@@ -1,5 +1,5 @@
local M = {} local M = {}
local cfg = require 'outline.config' local cfg = require('outline.config')
---@param node outline.SymbolNode|outline.FlatSymbolNode ---@param node outline.SymbolNode|outline.FlatSymbolNode
function M.is_foldable(node) function M.is_foldable(node)

View File

@@ -1,5 +1,5 @@
local outline = require 'outline' local cfg = require('outline.config')
local cfg = require 'outline.config' local outline = require('outline')
local util = vim.lsp.util local util = vim.lsp.util
local M = {} local M = {}
@@ -31,9 +31,7 @@ function M.show_hover()
if not (result and result.contents) then if not (result and result.contents) then
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 return

View File

@@ -1,18 +1,16 @@
local parser = require 'outline.parser' local View = require('outline.view')
local providers = require 'outline.providers.init' local cfg = require('outline.config')
local ui = require 'outline.ui' local folding = require('outline.folding')
local writer = require 'outline.writer' local parser = require('outline.parser')
local cfg = require 'outline.config' local providers = require('outline.providers.init')
local utils = require 'outline.utils.init' local ui = require('outline.ui')
local View = require 'outline.view' local utils = require('outline.utils.init')
local folding = require 'outline.folding' local writer = require('outline.writer')
local M = {} local M = {}
local function setup_global_autocmd() local function setup_global_autocmd()
if if cfg.o.outline_items.highlight_hovered_item or cfg.o.symbol_folding.auto_unfold_hover then
cfg.o.outline_items.highlight_hovered_item or cfg.o.symbol_folding.auto_unfold_hover
then
vim.api.nvim_create_autocmd('CursorHold', { vim.api.nvim_create_autocmd('CursorHold', {
pattern = '*', pattern = '*',
callback = function() callback = function()
@@ -54,24 +52,26 @@ M.state = {
} }
local function wipe_state() local function wipe_state()
M.state = { outline_items = {}, flattened_outline_items = {}, code_win = 0, opts = {} } M.state = {
outline_items = {},
flattened_outline_items = {},
code_win = 0,
opts = {},
}
end end
local function _update_lines() local function _update_lines()
M.state.flattened_outline_items = writer.make_outline(M.view.bufnr, M.state.outline_items, M.state.code_win) M.state.flattened_outline_items =
writer.make_outline(M.view.bufnr, M.state.outline_items, M.state.code_win)
end end
---@param items outline.SymbolNode[] ---@param items outline.SymbolNode[]
local function _merge_items(items) local function _merge_items(items)
utils.merge_items_rec( utils.merge_items_rec({ children = items }, { children = M.state.outline_items })
{ children = items },
{ children = M.state.outline_items }
)
end end
local function __refresh() local function __refresh()
local current_buffer_is_outline = M.view.bufnr local current_buffer_is_outline = M.view.bufnr == vim.api.nvim_get_current_buf()
== vim.api.nvim_get_current_buf()
if M.view:is_open() and not current_buffer_is_outline then if M.view:is_open() and not current_buffer_is_outline 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
@@ -101,10 +101,7 @@ end
---@param change_focus boolean ---@param change_focus boolean
function M.__goto_location(change_focus) function M.__goto_location(change_focus)
local node = M._current_node() local node = M._current_node()
vim.api.nvim_win_set_cursor( vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character })
M.state.code_win,
{ node.line + 1, node.character }
)
if vim.fn.hlexists('OutlineJumpHighlight') == 0 then if vim.fn.hlexists('OutlineJumpHighlight') == 0 then
vim.api.nvim_set_hl(0, 'OutlineJumpHighlight', { link = 'Visual' }) vim.api.nvim_set_hl(0, 'OutlineJumpHighlight', { link = 'Visual' })
@@ -159,8 +156,8 @@ end
local function hide_cursor() local function hide_cursor()
-- Set cursor color to CursorLine in normal mode -- Set cursor color to CursorLine in normal mode
M.state.original_cursor = vim.o.guicursor M.state.original_cursor = vim.o.guicursor
local cur = vim.o.guicursor:match("n.-:(.-)[-,]") local cur = vim.o.guicursor:match('n.-:(.-)[-,]')
vim.opt.guicursor:append("n:"..cur.."-Cursorline") vim.opt.guicursor:append('n:' .. cur .. '-Cursorline')
end end
local function unhide_cursor() local function unhide_cursor()
@@ -187,7 +184,7 @@ local function setup_buffer_autocmd()
callback = function() callback = function()
-- Don't use _goto_location because we don't want to auto-close -- Don't use _goto_location because we don't want to auto-close
M.__goto_location(false) M.__goto_location(false)
end end,
}) })
end end
if cfg.o.outline_window.hide_cursor then if cfg.o.outline_window.hide_cursor then
@@ -196,11 +193,11 @@ local function setup_buffer_autocmd()
hide_cursor() hide_cursor()
vim.api.nvim_create_autocmd('BufEnter', { vim.api.nvim_create_autocmd('BufEnter', {
buffer = 0, buffer = 0,
callback = hide_cursor callback = hide_cursor,
}) })
vim.api.nvim_create_autocmd('BufLeave', { vim.api.nvim_create_autocmd('BufLeave', {
buffer = 0, buffer = 0,
callback = unhide_cursor callback = unhide_cursor,
}) })
end end
end end
@@ -221,15 +218,10 @@ function M._set_folded(folded, move_cursor, node_index)
_update_lines() _update_lines()
elseif node.parent then elseif node.parent then
local parent_node = local parent_node = M.state.flattened_outline_items[node.parent.line_in_outline]
M.state.flattened_outline_items[node.parent.line_in_outline]
if parent_node then if parent_node then
M._set_folded( M._set_folded(folded, not parent_node.folded and folded, parent_node.line_in_outline)
folded,
not parent_node.folded and folded,
parent_node.line_in_outline
)
end end
end end
end end
@@ -271,8 +263,7 @@ end
function M._highlight_current_item(winnr) function M._highlight_current_item(winnr)
local has_provider = M.has_provider() local has_provider = M.has_provider()
local has_outline_open = M.view:is_open() local has_outline_open = M.view:is_open()
local current_buffer_is_outline = M.view.bufnr local current_buffer_is_outline = M.view.bufnr == vim.api.nvim_get_current_buf()
== vim.api.nvim_get_current_buf()
if not has_provider then if not has_provider then
return return
@@ -304,7 +295,11 @@ function M._highlight_current_item(winnr)
-- Must not skip folded nodes so that when user unfolds a parent, they can see the leaf -- Must not skip folded nodes so that when user unfolds a parent, they can see the leaf
-- node highlighted. -- node highlighted.
for value in parser.preorder_iter(M.state.outline_items, function() return true end) do for value in
parser.preorder_iter(M.state.outline_items, function()
return true
end)
do
value.hovered = nil value.hovered = nil
if if
@@ -372,30 +367,15 @@ local function setup_keymaps(bufnr)
M._move_and_jump('up') M._move_and_jump('up')
end) end)
-- hover symbol -- hover symbol
map( map(cfg.o.keymaps.hover_symbol, require('outline.hover').show_hover)
cfg.o.keymaps.hover_symbol,
require('outline.hover').show_hover
)
-- preview symbol -- preview symbol
map( map(cfg.o.keymaps.toggle_preview, require('outline.preview').toggle)
cfg.o.keymaps.toggle_preview,
require('outline.preview').toggle
)
-- rename symbol -- rename symbol
map( map(cfg.o.keymaps.rename_symbol, require('outline.rename').rename)
cfg.o.keymaps.rename_symbol,
require('outline.rename').rename
)
-- code actions -- code actions
map( map(cfg.o.keymaps.code_actions, require('outline.code_action').show_code_actions)
cfg.o.keymaps.code_actions,
require('outline.code_action').show_code_actions
)
-- show help -- show help
map( map(cfg.o.keymaps.show_help, require('outline.config').show_help)
cfg.o.keymaps.show_help,
require('outline.config').show_help
)
-- close outline -- close outline
map(cfg.o.keymaps.close, function() map(cfg.o.keymaps.close, function()
M.view:close() M.view:close()
@@ -502,9 +482,7 @@ end
function M._map_follow_cursor() function M._map_follow_cursor()
if not M.follow_cursor({ focus_outline = true }) then if not M.follow_cursor({ focus_outline = true }) then
utils.echo( utils.echo('Code window no longer active. Try closing and reopening the outline.')
"Code window no longer active. Try closing and reopening the outline."
)
end end
end end
@@ -527,7 +505,7 @@ local function _cmd_open_with_mods(fn)
return function(opts) return function(opts)
local old_sc, use_old_sc local old_sc, use_old_sc
local split = opts.smods.split local split = opts.smods.split
if split ~= "" then if split ~= '' then
old_sc = cfg.o.outline_window.split_command old_sc = cfg.o.outline_window.split_command
use_old_sc = true use_old_sc = true
cfg.o.outline_window.split_command = split .. ' vsplit' cfg.o.outline_window.split_command = split .. ' vsplit'
@@ -545,7 +523,6 @@ local function _cmd_open_with_mods(fn)
else else
fn({ focus_outline = true, on_outline_setup = on_outline_setup }) fn({ focus_outline = true, on_outline_setup = on_outline_setup })
end end
end end
end end
@@ -558,7 +535,7 @@ function M.open_outline(opts)
if not M.view:is_open() then if not M.view:is_open() then
local found = providers.request_symbols(handler, opts) local found = providers.request_symbols(handler, opts)
if not found then if not found then
utils.echo("No providers found for current buffer") utils.echo('No providers found for current buffer')
end end
end end
end end
@@ -618,25 +595,25 @@ function M.show_status()
end end
if p ~= nil then if p ~= nil then
print("Current provider: " .. p.name) print('Current provider: ' .. p.name)
if p.get_status then if p.get_status then
print(p.get_status()) print(p.get_status())
print() print()
end end
if M.view:is_open() then if M.view:is_open() then
print("Outline window is open.") print('Outline window is open.')
else else
print("Outline window is not open.") print('Outline window is not open.')
end end
if require('outline.preview').has_code_win() then if require('outline.preview').has_code_win() then
print("Code window is active.") print('Code window is active.')
else else
print("Code window is either closed or invalid. Please close and reopen the outline window.") print('Code window is either closed or invalid. Please close and reopen the outline window.')
end end
else else
print("No providers") print('No providers')
end end
end end
@@ -659,17 +636,17 @@ end
local function setup_commands() local function setup_commands()
local cmd = function(n, c, o) local cmd = function(n, c, o)
vim.api.nvim_create_user_command('Outline'..n, c, o) vim.api.nvim_create_user_command('Outline' .. n, c, o)
end end
cmd('', _cmd_open_with_mods(M.toggle_outline), { cmd('', _cmd_open_with_mods(M.toggle_outline), {
desc = "Toggle the outline window. \ desc = 'Toggle the outline window. \
With bang, keep focus on initial window after opening.", With bang, keep focus on initial window after opening.',
nargs = 0, nargs = 0,
bang = true, bang = true,
}) })
cmd('Open', _cmd_open_with_mods(M.open_outline), { cmd('Open', _cmd_open_with_mods(M.open_outline), {
desc = "With bang, keep focus on initial window after opening.", desc = 'With bang, keep focus on initial window after opening.',
nargs = 0, nargs = 0,
bang = true, bang = true,
}) })
@@ -678,7 +655,7 @@ With bang, keep focus on initial window after opening.",
cmd('FocusCode', M.focus_code, { nargs = 0 }) cmd('FocusCode', M.focus_code, { nargs = 0 })
cmd('Focus', M.focus_toggle, { nargs = 0 }) cmd('Focus', M.focus_toggle, { nargs = 0 })
cmd('Status', M.show_status, { cmd('Status', M.show_status, {
desc = "Show a message about the current status of the outline window.", desc = 'Show a message about the current status of the outline window.',
nargs = 0, nargs = 0,
}) })
cmd('Follow', _cmd_follow_cursor, { cmd('Follow', _cmd_follow_cursor, {

View File

@@ -1,9 +1,9 @@
local symbols = require 'outline.symbols' local cfg = require('outline.config')
local ui = require 'outline.ui' local folding = require('outline.folding')
local cfg = require 'outline.config' local lsp_utils = require('outline.utils.lsp_utils')
local t_utils = require 'outline.utils.table' local symbols = require('outline.symbols')
local lsp_utils = require 'outline.utils.lsp_utils' local t_utils = require('outline.utils.table')
local folding = require 'outline.folding' local ui = require('outline.ui')
local M = {} local M = {}
@@ -106,7 +106,8 @@ function M.preorder_iter(items, children_check)
end end
if if
node.children and node.traversal_child <= #node.children node.children
and node.traversal_child <= #node.children
and (node.is_root or children_check(node)) and (node.is_root or children_check(node))
then then
prev = node prev = node

View File

@@ -1,6 +1,6 @@
local outline = require 'outline' local cfg = require('outline.config')
local cfg = require 'outline.config' local hover = require('outline.hover')
local hover = require 'outline.hover' local outline = require('outline')
local M = {} local M = {}
@@ -30,8 +30,8 @@ local function get_width_offset()
---@type integer ---@type integer
local outline_winnr = outline.view.winnr local outline_winnr = outline.view.winnr
local width = cfg.get_preview_width() + 3 local width = cfg.get_preview_width() + 3
local has_numbers = vim.api.nvim_win_get_option(outline_winnr, "number") local has_numbers = vim.api.nvim_win_get_option(outline_winnr, 'number')
has_numbers = has_numbers or vim.api.nvim_win_get_option(outline_winnr, "relativenumber") has_numbers = has_numbers or vim.api.nvim_win_get_option(outline_winnr, 'relativenumber')
if has_numbers then if has_numbers then
width = width + 4 width = width + 4
@@ -67,10 +67,7 @@ local function update_preview(code_buf)
if state.preview_buf ~= nil then if state.preview_buf ~= nil then
vim.api.nvim_buf_set_lines(state.preview_buf, 0, -1, 0, lines) vim.api.nvim_buf_set_lines(state.preview_buf, 0, -1, 0, lines)
vim.api.nvim_win_set_cursor( vim.api.nvim_win_set_cursor(state.preview_win, { node.line + 1, node.character })
state.preview_win,
{ node.line + 1, node.character }
)
end end
end end
@@ -79,7 +76,7 @@ local function setup_preview_buf()
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
@@ -139,14 +136,10 @@ end
function M.close() function M.close()
if has_code_win() then if has_code_win() then
if if state.preview_win ~= nil and vim.api.nvim_win_is_valid(state.preview_win) then
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 if state.hover_win ~= nil and vim.api.nvim_win_is_valid(state.hover_win) then
state.hover_win ~= nil and vim.api.nvim_win_is_valid(state.hover_win)
then
vim.api.nvim_win_close(state.hover_win, true) vim.api.nvim_win_close(state.hover_win, true)
end end
end end

View File

@@ -2,9 +2,8 @@ local M = {
name = 'coc', name = 'coc',
} }
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
@@ -28,61 +27,61 @@ end
---@param result table ---@param result table
local function convert_symbols(result) local function convert_symbols(result)
local s = {} local s = {}
local kinds_index = {} local kinds_index = {}
-- create a inverse indexing of symbols.kind -- create a inverse indexing of symbols.kind
local symbols = require("outline.symbols") local symbols = require('outline.symbols')
for k, v in pairs(symbols.kinds) do for k, v in pairs(symbols.kinds) do
kinds_index[v] = k kinds_index[v] = k
end
-- rebuild coc.nvim symbol list hierarchy according to the 'level' key
for _, value in pairs(result) do
value.children = {}
value.kind = kinds_index[value.kind]
if #s == 0 then
table.insert(s, value)
goto continue
end end
-- rebuild coc.nvim symbol list hierarchy according to the 'level' key if value.level == s[#s].level then
for _, value in pairs(result) do if value.level == 0 then
value.children = {} table.insert(s, value)
value.kind = kinds_index[value.kind] goto continue
if #s == 0 then end
table.insert(s, value) local tmp = s[#s]
goto continue table.remove(s)
end table.insert(s[#s].children, tmp)
if value.level == s[#s].level then table.insert(s, value)
if value.level == 0 then elseif value.level == s[#s].level + 1 then
table.insert(s, value) table.insert(s[#s].children, value)
goto continue elseif value.level == s[#s].level + 2 then
end local tmp = s[#s].children[#s[#s].children]
local tmp = s[#s] table.remove(s[#s].children)
table.remove(s) table.insert(s, tmp)
table.insert(s[#s].children, tmp) table.insert(s[#s].children, value)
table.insert(s, value) elseif value.level < s[#s].level then
elseif value.level == s[#s].level + 1 then while value.level < s[#s].level do
table.insert(s[#s].children, value) local tmp = s[#s]
elseif value.level == s[#s].level + 2 then
local tmp = s[#s].children[#(s[#s].children)]
table.remove(s[#s].children)
table.insert(s, tmp)
table.insert(s[#s].children, value)
elseif value.level < s[#s].level then
while value.level < s[#s].level do
local tmp = s[#s]
table.remove(s)
table.insert(s[#s].children, tmp)
end
if s[#s].level ~= 0 then
local tmp = s[#s]
table.remove(s)
table.insert(s[#s].children, tmp)
table.insert(s, value)
else
table.insert(s, value)
end
end
::continue::
end
local top = s[#s]
while top.level ~= 0 do
table.remove(s) table.remove(s)
table.insert(s[#s].children, top) table.insert(s[#s].children, tmp)
top = s[#s] end
if s[#s].level ~= 0 then
local tmp = s[#s]
table.remove(s)
table.insert(s[#s].children, tmp)
table.insert(s, value)
else
table.insert(s, value)
end
end end
return s ::continue::
end
local top = s[#s]
while top.level ~= 0 do
table.remove(s)
table.insert(s[#s].children, top)
top = s[#s]
end
return s
end end
---@param on_symbols function ---@param on_symbols function

View File

@@ -1,14 +1,15 @@
local cfg = require "outline.config" local cfg = require('outline.config')
local M = {} local M = {}
local import_prefix = "outline/providers/" local import_prefix = 'outline/providers/'
_G._outline_current_provider = nil _G._outline_current_provider = nil
function M.find_provider() function M.find_provider()
if not M.providers then if not M.providers then
M.providers = vim.tbl_map(function(p) return import_prefix..p end, cfg.get_providers()) M.providers = vim.tbl_map(function(p)
return import_prefix .. p
end, cfg.get_providers())
end end
for _, name in ipairs(M.providers) do for _, name in ipairs(M.providers) do
local provider = require(name) local provider = require(name)

View File

@@ -1,6 +1,6 @@
local config = require 'outline.config' local config = require('outline.config')
local lsp_utils = require 'outline.utils.lsp_utils' local jsx = require('outline.utils.jsx')
local jsx = require 'outline.utils.jsx' local lsp_utils = require('outline.utils.lsp_utils')
local M = { local M = {
name = 'lsp', name = 'lsp',
@@ -10,9 +10,9 @@ local M = {
function M.get_status() function M.get_status()
if not M.client then if not M.client then
return "No clients" return 'No clients'
end end
return "client: "..M.client.name return 'client: ' .. M.client.name
end end
local function get_params() local function get_params()
@@ -83,15 +83,10 @@ end
---@param on_symbols function ---@param on_symbols function
function M.request_symbols(on_symbols, opts) function M.request_symbols(on_symbols, opts)
vim.lsp.buf_request_all( vim.lsp.buf_request_all(0, 'textDocument/documentSymbol', get_params(), function(response)
0, response = M.postprocess_symbols(response)
'textDocument/documentSymbol', on_symbols(response, opts)
get_params(), end)
function (response)
response = M.postprocess_symbols(response)
on_symbols(response, opts)
end
)
end end
return M return M

View File

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

View File

@@ -1,4 +1,4 @@
local cfg = require 'outline.config' local cfg = require('outline.config')
local M = {} local M = {}
@@ -60,10 +60,13 @@ function M.icon_from_kind(kind)
if cfg.o.symbols.icon_source == 'lspkind' then if cfg.o.symbols.icon_source == 'lspkind' then
local has_lspkind, lspkind = pcall(require, 'lspkind') local has_lspkind, lspkind = pcall(require, 'lspkind')
if not has_lspkind then if not has_lspkind then
vim.notify("[outline]: icon_source set to lspkind but failed to require lspkind!", vim.log.levels.ERROR) vim.notify(
'[outline]: icon_source set to lspkind but failed to require lspkind!',
vim.log.levels.ERROR
)
else else
local icon = lspkind.symbolic(kindstr, { with_text = false }) local icon = lspkind.symbolic(kindstr, { with_text = false })
if icon and icon ~= "" then if icon and icon ~= '' then
return icon return icon
end end
end end

View File

@@ -1,26 +1,19 @@
local M = {} local M = {}
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( vim.api.nvim_buf_add_highlight(bufnr, M.hovered_hl_ns, 'OutlineCurrent', line, col_start, -1)
bufnr,
M.hovered_hl_ns,
'OutlineCurrent',
line,
col_start,
-1
)
end end
function M.setup_highlights() function M.setup_highlights()
-- Setup the OutlineCurrent highlight group if it hasn't been done already by -- Setup the OutlineCurrent highlight group if it hasn't been done already by
-- a theme or manually set -- a theme or manually set
if vim.fn.hlexists 'OutlineCurrent' == 0 then if vim.fn.hlexists('OutlineCurrent') == 0 then
-- TODO: Use nvim_get_hl -- TODO: Use nvim_get_hl
local cline_hl = vim.api.nvim_get_hl_by_name('CursorLine', true) local cline_hl = vim.api.nvim_get_hl_by_name('CursorLine', true)
local string_hl = vim.api.nvim_get_hl_by_name('String', true) local string_hl = vim.api.nvim_get_hl_by_name('String', true)
@@ -36,16 +29,10 @@ 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( local comment_fg_gui = vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID('Comment')), 'fg', 'gui')
vim.fn.synIDtrans(vim.fn.hlID 'Comment'),
'fg',
'gui'
)
if vim.fn.hlexists 'OutlineGuides' == 0 then if vim.fn.hlexists('OutlineGuides') == 0 then
vim.cmd( vim.cmd(string.format('hi OutlineGuides guifg=%s', comment_fg_gui))
string.format('hi OutlineGuides guifg=%s', comment_fg_gui)
)
end end
end end

View File

@@ -9,12 +9,7 @@ function M.nmap(bufnr, keys, action)
end end
for _, lhs in ipairs(keys) do for _, lhs in ipairs(keys) do
vim.keymap.set( vim.keymap.set('n', lhs, action, { silent = true, noremap = true, buffer = bufnr })
'n',
lhs,
action,
{ silent = true, noremap = true, buffer = bufnr }
)
end end
end end
@@ -114,7 +109,7 @@ function M.flash_highlight(winnr, lnum, durationMs, hl_group)
if durationMs == false then if durationMs == false then
return return
end end
hl_group = hl_group or "Visual" hl_group = hl_group or 'Visual'
if durationMs == true or durationMs == 1 then if durationMs == true or durationMs == 1 then
durationMs = 500 durationMs = 500
end end
@@ -131,13 +126,13 @@ end
function M.echo(module, message) function M.echo(module, message)
if not message then if not message then
message = module message = module
module = "" module = ''
end end
local prefix = "outline" local prefix = 'outline'
if module ~= "" then if module ~= '' then
prefix = prefix.."."..module prefix = prefix .. '.' .. module
end end
local prefix_chunk = { '('..prefix..') ', "WarningMsg" } local prefix_chunk = { '(' .. prefix .. ') ', 'WarningMsg' }
-- For now we don't echo much, so add all to history -- For now we don't echo much, so add all to history
vim.api.nvim_echo({ prefix_chunk, { message } }, true, {}) vim.api.nvim_echo({ prefix_chunk, { message } }, true, {})
end end

View File

@@ -5,7 +5,7 @@ local SYMBOL_FRAGMENT = 28
local function get_open_tag(node) local function get_open_tag(node)
if node:type() == 'jsx_element' then if node:type() == 'jsx_element' then
for _, outer in ipairs(node:field 'open_tag') do for _, outer in ipairs(node:field('open_tag')) do
if outer:type() == 'jsx_opening_element' then if outer:type() == 'jsx_opening_element' then
return outer return outer
end end
@@ -18,7 +18,7 @@ end
local function jsx_node_detail(node, buf) local function jsx_node_detail(node, buf)
node = get_open_tag(node) or node node = get_open_tag(node) or node
local param_nodes = node:field 'attribute' local param_nodes = node:field('attribute')
if #param_nodes == 0 then if #param_nodes == 0 then
return nil return nil
end end
@@ -42,7 +42,7 @@ local function jsx_node_tagname(node, buf)
local identifier = nil local identifier = nil
for _, val in ipairs(tagnode:field 'name') do for _, val in ipairs(tagnode:field('name')) do
if val:type() == 'identifier' then if val:type() == 'identifier' then
identifier = val identifier = val
end end
@@ -66,8 +66,7 @@ local function convert_ts(child, children, bufnr)
} }
local converted = { local converted = {
name = (not is_frag and (jsx_node_tagname(child, bufnr) or '<unknown>')) name = (not is_frag and (jsx_node_tagname(child, bufnr) or '<unknown>')) or 'fragment',
or 'fragment',
children = (#children > 0 and children) or nil, children = (#children > 0 and children) or nil,
kind = (is_frag and SYMBOL_FRAGMENT) or SYMBOL_COMPONENT, kind = (is_frag and SYMBOL_FRAGMENT) or SYMBOL_COMPONENT,
detail = jsx_node_detail(child, bufnr), detail = jsx_node_detail(child, bufnr),
@@ -82,12 +81,7 @@ function M.parse_ts(root, children, bufnr)
children = children or {} children = children or {}
for child in root:iter_children() do for child in root:iter_children() do
if if vim.tbl_contains({ 'jsx_element', 'jsx_self_closing_element' }, child:type()) then
vim.tbl_contains(
{ 'jsx_element', 'jsx_self_closing_element' },
child:type()
)
then
local new_children = {} local new_children = {}
M.parse_ts(child, new_children, bufnr) M.parse_ts(child, new_children, bufnr)

View File

@@ -1,5 +1,5 @@
local config = require 'outline.config' local config = require('outline.config')
local tbl_utils = require 'outline.utils.table' local tbl_utils = require('outline.utils.table')
local M = {} local M = {}
@@ -16,7 +16,7 @@ end
function M.flatten_response(response) function M.flatten_response(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)
@@ -51,8 +51,8 @@ end
function M.get_range(token) function M.get_range(token)
if token == nil then if token == nil then
return { return {
start={ line=math.huge, character=math.huge }, start = { line = math.huge, character = math.huge },
['end']={ line=-math.huge, character=-math.huge }, ['end'] = { line = -math.huge, character = -math.huge },
} }
end end
@@ -87,10 +87,7 @@ end
---Recursively sorts all children of each symbol ---Recursively sorts all children of each symbol
function M.sort_symbols(symbols) function M.sort_symbols(symbols)
table.sort(symbols, function(a, b) table.sort(symbols, function(a, b)
return range_compare( return range_compare(M.get_range(a).start, M.get_range(b).start)
M.get_range(a).start,
M.get_range(b).start
)
end) end)
for _, child in ipairs(symbols) do for _, child in ipairs(symbols) do
@@ -132,15 +129,17 @@ function M.symbol_preorder_iter(symbols)
return stk[#stk] return stk[#stk]
end end
return { next=next, is_empty=is_empty, peek=peek } return { next = next, is_empty = is_empty, peek = peek }
end end
local function merge_symbols_rec(iter1, iter2, ub) local function merge_symbols_rec(iter1, iter2, ub)
local res = {} local res = {}
while not (iter1.is_empty() and iter2.is_empty()) do while not (iter1.is_empty() and iter2.is_empty()) do
local bv1 = ((not iter1.is_empty()) and M.get_range(iter1.peek()).start) or { line=math.huge, character=math.huge } local bv1 = ((not iter1.is_empty()) and M.get_range(iter1.peek()).start)
local bv2 = ((not iter2.is_empty()) and M.get_range(iter2.peek()).start) or { line=math.huge, character=math.huge } or { line = math.huge, character = math.huge }
local bv2 = ((not iter2.is_empty()) and M.get_range(iter2.peek()).start)
or { line = math.huge, character = math.huge }
local iter = (range_compare(bv1, bv2) and iter1) or iter2 local iter = (range_compare(bv1, bv2) and iter1) or iter2

View File

@@ -28,14 +28,13 @@ function M.array_copy(t)
return ret return ret
end end
--- Deep copy a table, deeply excluding certain keys --- Deep copy a table, deeply excluding certain keys
function M.deepcopy_excluding(t, keys) function M.deepcopy_excluding(t, keys)
local res = {} local res = {}
for key, value in pairs(t) do for key, value in pairs(t) do
if not vim.tbl_contains(keys, key) then if not vim.tbl_contains(keys, key) then
if type(value) == "table" then if type(value) == 'table' then
res[key] = M.deepcopy_excluding(value, keys) res[key] = M.deepcopy_excluding(value, keys)
else else
res[key] = value res[key] = value

View File

@@ -67,9 +67,9 @@ end
function View:is_open() function View:is_open()
return self.winnr return self.winnr
and self.bufnr and self.bufnr
and vim.api.nvim_buf_is_valid(self.bufnr) and vim.api.nvim_buf_is_valid(self.bufnr)
and vim.api.nvim_win_is_valid(self.winnr) and vim.api.nvim_win_is_valid(self.winnr)
end end
return View return View

View File

@@ -1,18 +1,16 @@
local symbols = require 'outline.symbols' local cfg = require('outline.config')
local parser = require 'outline.parser' local folding = require('outline.folding')
local cfg = require 'outline.config' local parser = require('outline.parser')
local ui = require 'outline.ui' local symbols = require('outline.symbols')
local t_utils = require 'outline.utils.table' local t_utils = require('outline.utils.table')
local folding = require 'outline.folding' local ui = require('outline.ui')
local strlen = vim.fn.strlen local strlen = vim.fn.strlen
local M = {} local M = {}
local hlns = vim.api.nvim_create_namespace 'outline-icon-highlight' local hlns = vim.api.nvim_create_namespace('outline-icon-highlight')
local ns = vim.api.nvim_create_namespace 'outline-virt-text' local ns = vim.api.nvim_create_namespace('outline-virt-text')
---@param bufnr integer ---@param bufnr integer
---@return boolean ---@return boolean
@@ -31,14 +29,7 @@ end
function M.add_highlights(bufnr, hl_info, nodes) function M.add_highlights(bufnr, hl_info, nodes)
for _, line_hl in ipairs(hl_info) do for _, line_hl in ipairs(hl_info) do
local line, hl_start, hl_end, hl_type = unpack(line_hl) local line, hl_start, hl_end, hl_type = unpack(line_hl)
vim.api.nvim_buf_add_highlight( vim.api.nvim_buf_add_highlight(bufnr, hlns, hl_type, line - 1, hl_start, hl_end)
bufnr,
hlns,
hl_type,
line - 1,
hl_start,
hl_end
)
end end
M.add_hover_highlights(bufnr, nodes) M.add_hover_highlights(bufnr, nodes)
end end
@@ -50,7 +41,7 @@ end
---@param bufnr integer ---@param bufnr integer
---@param nodes outline.FlatSymbolNode[] flattened nodes ---@param nodes outline.FlatSymbolNode[] flattened nodes
function M.add_hover_highlights (bufnr, nodes) function M.add_hover_highlights(bufnr, nodes)
if not cfg.o.outline_items.highlight_hovered_item then if not cfg.o.outline_items.highlight_hovered_item then
return return
end end
@@ -63,11 +54,7 @@ function M.add_hover_highlights (bufnr, nodes)
end end
if node.prefix_length then if node.prefix_length then
ui.add_hover_highlight( ui.add_hover_highlight(bufnr, node.line_in_outline - 1, node.prefix_length)
bufnr,
node.line_in_outline - 1,
node.prefix_length
)
end end
::continue:: ::continue::
end end
@@ -101,7 +88,7 @@ function M.make_outline(bufnr, items, codewin)
-- Find the prefix for each line needed for the lineno space -- Find the prefix for each line needed for the lineno space
local lineno_offset = 0 local lineno_offset = 0
local lineno_prefix = "" local lineno_prefix = ''
local lineno_max_width = #tostring(vim.api.nvim_buf_line_count(codebuf) - 1) local lineno_max_width = #tostring(vim.api.nvim_buf_line_count(codebuf) - 1)
if cfg.o.outline_items.show_symbol_lineno then if cfg.o.outline_items.show_symbol_lineno then
-- Use max width-1 plus 1 space padding. -- Use max width-1 plus 1 space padding.
@@ -120,7 +107,7 @@ function M.make_outline(bufnr, items, codewin)
#flattened, #flattened,
from, from,
to, to,
"OutlineGuides" 'OutlineGuides',
}) })
end end
@@ -129,7 +116,7 @@ function M.make_outline(bufnr, items, codewin)
#flattened, #flattened,
from, from,
to, to,
"OutlineFoldMarker" 'OutlineFoldMarker',
}) })
end end
@@ -147,9 +134,9 @@ function M.make_outline(bufnr, items, codewin)
table.insert(flattened, node) table.insert(flattened, node)
node.line_in_outline = #flattened node.line_in_outline = #flattened
table.insert(details, node.detail or '') table.insert(details, node.detail or '')
local lineno = tostring(node.range_start+1) local lineno = tostring(node.range_start + 1)
local leftpad = string.rep(' ', lineno_max_width-#lineno) local leftpad = string.rep(' ', lineno_max_width - #lineno)
table.insert(linenos, leftpad..lineno) table.insert(linenos, leftpad .. lineno)
-- Make the guides for the line prefix -- Make the guides for the line prefix
local pref = t_utils.str_to_table(string.rep(' ', node.depth)) local pref = t_utils.str_to_table(string.rep(' ', node.depth))
@@ -180,7 +167,7 @@ function M.make_outline(bufnr, items, codewin)
-- ancestor is the entire outline itself, it should not have a vertical -- ancestor is the entire outline itself, it should not have a vertical
-- guide). -- guide).
local iternode = node local iternode = node
for i = node.depth-1, 2, -1 do for i = node.depth - 1, 2, -1 do
iternode = iternode.parent_node iternode = iternode.parent_node
if not iternode.isLast then if not iternode.isLast then
pref[i] = guide_markers.vertical pref[i] = guide_markers.vertical
@@ -199,18 +186,18 @@ function M.make_outline(bufnr, items, codewin)
add_fold_hl(total_pref_len - fold_marker_width, total_pref_len + 1) add_fold_hl(total_pref_len - fold_marker_width, total_pref_len + 1)
end end
local line = lineno_prefix..pref_str local line = lineno_prefix .. pref_str
local icon_pref = 0 local icon_pref = 0
if node.icon ~= "" then if node.icon ~= '' then
line = line..' '..node.icon line = line .. ' ' .. node.icon
icon_pref = 1 icon_pref = 1
end end
line = line..' '..node.name line = line .. ' ' .. node.name
-- Highlight for the icon ✨ -- Highlight for the icon ✨
-- Start from icon col -- Start from icon col
local hl_start = #pref_str + #lineno_prefix + icon_pref local hl_start = #pref_str + #lineno_prefix + icon_pref
local hl_end = hl_start + #node.icon -- until after icon local hl_end = hl_start + #node.icon -- until after icon
local hl_type = cfg.o.symbols.icons[symbols.kinds[node.kind]].hl local hl_type = cfg.o.symbols.icons[symbols.kinds[node.kind]].hl
table.insert(hl, { #flattened, hl_start, hl_end, hl_type }) table.insert(hl, { #flattened, hl_start, hl_end, hl_type })
@@ -219,7 +206,7 @@ function M.make_outline(bufnr, items, codewin)
node.prefix_length = hl_end + 1 node.prefix_length = hl_end + 1
-- lines passed to nvim_buf_set_lines cannot contain newlines in each line -- lines passed to nvim_buf_set_lines cannot contain newlines in each line
line = line:gsub("\n", " ") line = line:gsub('\n', ' ')
table.insert(lines, line) table.insert(lines, line)
end end
@@ -278,5 +265,4 @@ end
-- have to call nvim_buf_set_lines n times (each line) rather than add lines -- have to call nvim_buf_set_lines n times (each line) rather than add lines
-- all at once, saving only the need of 1 extra table (lines table) in memory. -- all at once, saving only the need of 1 extra table (lines table) in memory.
return M return M

View File

@@ -1,6 +0,0 @@
column_width = 80
line_endings = 'Unix'
indent_type = 'Spaces'
indent_width = 2
quote_style = 'AutoPreferSingle'
call_parentheses = 'None'