refactor: Provider symbol actions

It makes sense to store the provider each sidebar is using with own
self.provider fields, no way that did not occur to me before this.

The old `_G._outline_current_provider` ironically, can now be replaced
by `require('outline').current.provider`.
This commit is contained in:
hedy
2023-11-26 15:14:42 +08:00
parent 1743ee7c66
commit b90174c6c2
8 changed files with 78 additions and 91 deletions

View File

@@ -28,7 +28,7 @@ end
---@param result table
local function convert_symbols(result)
local s = {}
local kinds_index = require('outline.symbol').str_to_kind
local kinds_index = require('outline.symbols').str_to_kind
-- rebuild coc.nvim symbol list hierarchy according to the 'level' key
for _, value in pairs(result) do
value.children = {}

View File

@@ -3,19 +3,19 @@ local cfg = require('outline.config')
local M = {}
local import_prefix = 'outline/providers/'
---@return outline.Provider?
function M.find_provider()
if not M.providers then
M.providers = vim.tbl_map(function(p)
return import_prefix .. p
end, cfg.get_providers())
end
for _, name in ipairs(M.providers) do
local provider = require(name)
for _, path in ipairs(M.providers) do
local provider = require(path)
if provider.supports_buffer(0) then
return provider, name
return provider
end
end
return nil, nil
end
---@return boolean found_provider
@@ -23,20 +23,15 @@ function M.has_provider()
return M.find_provider() ~= nil
end
---@param on_symbols function
---@param opts outline.OutlineOpts?
---@return boolean found_provider
function M.request_symbols(on_symbols, opts)
local provider, name = M.find_provider()
if not provider then
return false
---Call `sidebar.provider[method]` with args. NOP if no provider or no defined `method`
---@param sidebar outline.Sidebar
---@param method string
---@param args any[]
function M.action(sidebar, method, args)
if not sidebar.provider or not sidebar.provider[method] then
return
end
_G._outline_current_provider = provider
if not provider.name then
_G._outline_current_provider.name = name
end
provider.request_symbols(on_symbols, opts)
return true
return sidebar.provider[method](unpack(args))
end
return M

View File

@@ -89,4 +89,20 @@ function M.request_symbols(on_symbols, opts)
end)
end
-- No good way to update outline when LSP action complete for now
---@param sidebar outline.Sidebar
function M.code_actions(sidebar)
sidebar:wrap_goto_location(function()
vim.lsp.buf.code_action()
end)
end
---@param sidebar outline.Sidebar
function M.rename_symbol(sidebar)
sidebar:wrap_goto_location(function()
vim.lsp.buf.rename()
end)
end
return M