Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LSP provider if available or tree-sitter as fallback #63

Closed
gbrlsnchs opened this issue Aug 14, 2022 · 1 comment
Closed

Use LSP provider if available or tree-sitter as fallback #63

gbrlsnchs opened this issue Aug 14, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@gbrlsnchs
Copy link

Feature description

I've seen in the examples that you can either use LSP plus regular fallback or tree-sitter plus regular fallback, but can you use LSP when available, then tree-sitter as the fallback? Some filetypes don't have such LSP support but do have tree-sitter support.

Describe the solution you'd like

Use LSP provider if available, otherwise fall back to tree-sitter provider and only if none is available, then fall back to the default one.

Additional context

No response

@kevinhwang91
Copy link
Owner

kevinhwang91 commented Aug 14, 2022

TLDR;

nvim-ufo/doc/example.lua

Lines 23 to 52 in 480209a

-- lsp->treesitter->indent
local function selectProviderWithChainByDefault()
local ftMap = {
vim = 'indent',
python = {'indent'},
git = ''
}
local function customizeSelector(bufnr)
local function handleFallbackException(err, providerName)
if type(err) == 'string' and err:match('UfoFallbackException') then
return require('ufo').getFolds(providerName, bufnr)
else
return require('promise').reject(err)
end
end
return require('ufo').getFolds('lsp', bufnr):catch(function(err)
return handleFallbackException(err, 'treesitter')
end):catch(function(err)
return handleFallbackException(err, 'indent')
end)
end
require('ufo').setup({
provider_selector = function(bufnr, filetype, buftype)
return ftMap[filetype] or customizeSelector
end
})
end

main provider may throw UfoFallbackException error to wake up fallback provider. If you want to use lsp->treesitter->indent chain, you must add the customized provider yourself. ufo has given an example.

Never consider adding this example to README because I haven't seen a fallback chain for a client to request servers that I think it is fragile.

edit: example is changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants