refactor: Add a generic provider module

In preperation for coc support
This commit is contained in:
simrat39
2021-10-05 22:46:53 -07:00
parent 28c5b0513a
commit a0d563172d
4 changed files with 74 additions and 17 deletions

View File

@@ -1,12 +1,12 @@
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 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 lsp_utils = require('symbols-outline.utils.lsp_utils') local lsp_utils = require('symbols-outline.utils.lsp_utils')
local utils = require('symbols-outline.utils.init') local utils = require('symbols-outline.utils.init')
local markdown = require('symbols-outline.markdown')
local view = require('symbols-outline.view') local view = require('symbols-outline.view')
local M = {} local M = {}
@@ -29,10 +29,6 @@ local function setup_buffer_autocmd()
end end
local function getParams()
return {textDocument = vim.lsp.util.make_text_document_params()}
end
------------------------- -------------------------
-- STATE -- STATE
------------------------- -------------------------
@@ -48,7 +44,7 @@ local function wipe_state()
M.state = {outline_items = {}, flattened_outline_items = {}} M.state = {outline_items = {}, flattened_outline_items = {}}
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
@@ -70,11 +66,7 @@ local function __refresh ()
M.state.flattened_outline_items) M.state.flattened_outline_items)
end end
if vim.api.nvim_buf_get_option(0, 'ft') == 'markdown' then providers.request_symbols(refresh_handler)
refresh_handler(markdown.handle_markdown())
end
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(),
refresh_handler)
end end
end end
@@ -195,16 +187,12 @@ end
function M.open_outline() function M.open_outline()
if M.state.outline_buf == nil then if M.state.outline_buf == nil then
if vim.api.nvim_buf_get_option(0, 'ft') == 'markdown' then providers.request_symbols(handler)
handler(markdown.handle_markdown())
end
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(),
handler)
end end
end end
function M.close_outline() function M.close_outline()
if M.state.outline_buf ~= nil 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

View File

@@ -0,0 +1,19 @@
local M = {}
local providers = {
'symbols-outline/providers/nvim-lsp',
'symbols-outline/providers/markdown'
}
---@param on_symbols function
function M.request_symbols(on_symbols)
for _, value in ipairs(providers) do
local provider = require(value)
if provider.should_use_provider(0) then
provider.request_symbols(on_symbols)
break
end
end
end
return M

View File

@@ -0,0 +1,15 @@
local md_parser = require('symbols-outline.markdown')
local M = {}
-- probably change this
function M.should_use_provider(bufnr)
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown'
end
---@param on_symbols function
function M.request_symbols(on_symbols)
on_symbols(md_parser.handle_markdown())
end
return M

View File

@@ -0,0 +1,35 @@
local config = require('symbols-outline.config')
local M = {}
local function getParams()
return {textDocument = vim.lsp.util.make_text_document_params()}
end
-- probably change this
function M.should_use_provider(bufnr)
local clients = vim.lsp.buf_get_clients(bufnr)
local ret = false
for id, client in ipairs(clients) do
if config.is_client_blacklisted(id) then
goto continue
else
if client.server_capabilities.documentSymbolProvider then
ret = true
break
end
end
::continue::
end
return ret
end
---@param on_symbols function
function M.request_symbols(on_symbols)
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(),
on_symbols)
end
return M