From b64c0c66d41e66ede1c71f15f11ea145fff113d8 Mon Sep 17 00:00:00 2001 From: hedy Date: Sat, 16 Mar 2024 08:12:50 +0800 Subject: [PATCH] feat(providers): Support independent configs for each provider to handle Closes #58 --- CHANGELOG.md | 4 ++++ README.md | 11 ++++++++++- lua/outline/config.lua | 5 +++++ lua/outline/providers/init.lua | 4 +++- lua/outline/providers/markdown.lua | 14 ++++++++++++-- lua/outline/providers/norg.lua | 4 +++- lua/outline/types/outline.lua | 3 ++- 7 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 647c975..2c4dbbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,10 @@ the file and jumping elsewhere. - If auto-preview is enabled the preview window will automatically resize and reposition +- Each provider can now handle its own configuration via the + `providers["provider-name"]` table. The first provider to make use of this will + be the markdown provider, which looks at `providers.markdown.filetypes` for + the list of filetypes to be supported for markdown outline symbols. ### Fixes diff --git a/README.md b/README.md index fc735f8..3182707 100644 --- a/README.md +++ b/README.md @@ -359,10 +359,15 @@ Pass a table to the setup call with your configuration options. providers = { priority = { 'lsp', 'coc', 'markdown', 'norg' }, + -- Configuration for each provider (3rd party providers are supported) lsp = { -- Lsp client names to ignore blacklist_clients = {}, }, + markdown = { + -- List of supported ft's to use the markdown provider + filetypes = {'markdown'}, + }, }, symbols = { @@ -494,10 +499,14 @@ path, for use as a provider. External providers from plugins should define the provider module at `lua/outline/providers/.lua` with these functions: -- `supports_buffer(bufnr: integer) -> boolean` +- `supports_buffer(bufnr: integer, config: table?) -> boolean` This function could check buffer filetype, existence of required modules, etc. + The config table comes from the user's configuration in the + `providers['provider-name']` table where `provider-name` is the + `require('outline.providers.').name`. + - `get_status() -> string[]` (optional) Return a list of lines to be included in `:OutlineStatus` as supplementary diff --git a/lua/outline/config.lua b/lua/outline/config.lua index d60340c..1abb51c 100644 --- a/lua/outline/config.lua +++ b/lua/outline/config.lua @@ -94,6 +94,9 @@ M.defaults = { lsp = { blacklist_clients = {}, }, + markdown = { + filetypes = { 'markdown' }, + }, }, symbols = { ---@type outline.FilterConfig? @@ -325,6 +328,8 @@ function M.resolve_config() map[client] = true end M.o.providers.lsp.blacklist_clients = map + ----- LSP PROVIDER CONFIG NAME ----- + M.o.providers['nvim-lsp'] = M.o.providers.lsp end ---Ensure l is either table, false, or nil. If not, print warning using given diff --git a/lua/outline/providers/init.lua b/lua/outline/providers/init.lua index 517df51..ec8c9bb 100644 --- a/lua/outline/providers/init.lua +++ b/lua/outline/providers/init.lua @@ -3,6 +3,8 @@ local import_prefix = 'outline/providers/' ---@return outline.Provider?, table? function M.find_provider() + local configs = require('outline.config').o.providers + if not M.providers then M.providers = vim.tbl_map(function(p) return import_prefix .. p @@ -11,7 +13,7 @@ function M.find_provider() for _, path in ipairs(M.providers) do local provider = require(path) - local ok, info = provider.supports_buffer(0) + local ok, info = provider.supports_buffer(0, configs[provider.name]) if ok then return provider, info end diff --git a/lua/outline/providers/markdown.lua b/lua/outline/providers/markdown.lua index 441f8c8..8678838 100644 --- a/lua/outline/providers/markdown.lua +++ b/lua/outline/providers/markdown.lua @@ -15,9 +15,19 @@ local M = { name = 'markdown', } +---@param bufnr integer +---@param config table? ---@return boolean ft_is_markdown -function M.supports_buffer(bufnr) - return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown' +function M.supports_buffer(bufnr, config) + local ft = vim.api.nvim_buf_get_option(bufnr, 'ft') + if config and config.filetypes then + for _, ft_check in ipairs(config.filetypes) do + if ft_check == ft then + return true + end + end + end + return ft == "markdown" end -- Parses markdown files and returns a table of SymbolInformation[] which is diff --git a/lua/outline/providers/norg.lua b/lua/outline/providers/norg.lua index 0e0b0f0..5ad44d9 100644 --- a/lua/outline/providers/norg.lua +++ b/lua/outline/providers/norg.lua @@ -18,7 +18,9 @@ local M = { ]], } -function M.supports_buffer(bufnr) +---@param bufnr integer +---@param config table? +function M.supports_buffer(bufnr, config) if vim.api.nvim_buf_get_option(bufnr, 'ft') ~= 'norg' then return false end diff --git a/lua/outline/types/outline.lua b/lua/outline/types/outline.lua index da94d82..a1312fa 100644 --- a/lua/outline/types/outline.lua +++ b/lua/outline/types/outline.lua @@ -63,8 +63,9 @@ ---@class outline.Provider ---@field name string +---@field config table? ---@field get_status? fun(info:table?):string[] ----@field supports_buffer fun(bufnr:integer):boolean,table? +---@field supports_buffer fun(bufnr:integer, config:table?):boolean,table? ---@field request_symbols fun(on_symbols:fun(symbols?:outline.ProviderSymbol[], opts:table?, provider_info:table?), opts:table?) ---@field show_hover? fun(sidebar:outline.Sidebar):boolean ---@field rename_symbol? fun(sidebar:outline.Sidebar):boolean