refactor: Reorganize provider API

This commit is contained in:
hedy
2023-11-22 20:55:10 +08:00
parent 3c9f267126
commit ec4f727631
5 changed files with 7 additions and 7 deletions

View File

@@ -2,7 +2,7 @@ local M = {
name = 'coc',
}
function M.should_use_provider(_)
function M.supports_buffer(_)
local not_coc_installed = vim.fn.exists('*CocActionAsync') == 0
local not_coc_service_initialized = vim.g.coc_service_initialized == 0

View File

@@ -11,7 +11,7 @@ function M.find_provider()
end
for _, name in ipairs(M.providers) do
local provider = require(name)
if provider.should_use_provider(0) then
if provider.supports_buffer(0) then
return provider, name
end
end

View File

@@ -0,0 +1,119 @@
local M = {}
local SYMBOL_COMPONENT = 27
local SYMBOL_FRAGMENT = 28
local function get_open_tag(node)
if node:type() == 'jsx_element' then
for _, outer in ipairs(node:field('open_tag')) do
if outer:type() == 'jsx_opening_element' then
return outer
end
end
end
return nil
end
local function jsx_node_detail(node, buf)
node = get_open_tag(node) or node
local param_nodes = node:field('attribute')
if #param_nodes == 0 then
return nil
end
local res = '{ '
.. table.concat(
vim.tbl_map(function(el)
local a, b, c, d = el:range()
local text = vim.api.nvim_buf_get_text(buf, a, b, c, d, {})
return text[1]
end, param_nodes),
' '
)
.. ' }'
return res
end
local function jsx_node_tagname(node, buf)
local tagnode = get_open_tag(node) or node
local identifier = nil
for _, val in ipairs(tagnode:field('name')) do
if val:type() == 'identifier' then
identifier = val
end
end
if identifier then
local a, b, c, d = identifier:range()
local text = vim.api.nvim_buf_get_text(buf, a, b, c, d, {})
local name = table.concat(text)
return name
end
end
local function convert_ts(child, children, bufnr)
local is_frag = (child:type() == 'jsx_fragment')
local a, b, c, d = child:range()
local range = {
start = { line = a, character = b },
['end'] = { line = c, character = d },
}
local converted = {
name = (not is_frag and (jsx_node_tagname(child, bufnr) or '<unknown>')) or 'fragment',
children = (#children > 0 and children) or nil,
kind = (is_frag and SYMBOL_FRAGMENT) or SYMBOL_COMPONENT,
detail = jsx_node_detail(child, bufnr),
range = range,
selectionRange = range,
}
return converted
end
function M.parse_ts(root, children, bufnr)
children = children or {}
for child in root:iter_children() do
if vim.tbl_contains({ 'jsx_element', 'jsx_self_closing_element' }, child:type()) then
local new_children = {}
M.parse_ts(child, new_children, bufnr)
table.insert(children, convert_ts(child, new_children, bufnr))
else
M.parse_ts(child, children, bufnr)
end
end
return children
end
function M.get_symbols(bufnr)
bufnr = bufnr or 0
local status, parser = pcall(vim.treesitter.get_parser, bufnr)
if not status then
return {}
end
if parser == nil then
return {}
end
local root = parser:parse()[1]:root()
if root == nil then
return {}
end
return M.parse_ts(root, nil, bufnr)
end
return M

View File

@@ -17,7 +17,7 @@ local M = {
---@return boolean ft_is_markdown
function M.should_use_provider(bufnr)
function M.supports_buffer(bufnr)
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown'
end

View File

@@ -1,5 +1,5 @@
local config = require('outline.config')
local jsx = require('outline.utils.jsx')
local jsx = require('outline.providers.jsx')
local lsp_utils = require('outline.utils.lsp_utils')
local M = {
@@ -49,7 +49,7 @@ function M.hover_info(bufnr, params, on_info)
use_client.request('textDocument/hover', params, on_info, bufnr)
end
function M.should_use_provider(bufnr)
function M.supports_buffer(bufnr)
local clients = vim.lsp.get_active_clients({ bufnr = bufnr })
local ret = false
@@ -69,7 +69,7 @@ function M.should_use_provider(bufnr)
return ret
end
function M.postprocess_symbols(response)
local function postprocess_symbols(response)
local symbols = lsp_utils.flatten_response(response)
local jsx_symbols = jsx.get_symbols()
@@ -84,7 +84,7 @@ end
---@param on_symbols function
function M.request_symbols(on_symbols, opts)
vim.lsp.buf_request_all(0, 'textDocument/documentSymbol', get_params(), function(response)
response = M.postprocess_symbols(response)
response = postprocess_symbols(response)
on_symbols(response, opts)
end)
end