1717
1818--- @param bufnr ? number defaults to 0 for the current buffer
1919--- @param filetype ? string
20- local function checkForTreesitter (bufnr , filetype )
20+ local function checkForTreesitterWithFallback (bufnr , filetype )
2121 if not bufnr then bufnr = 0 end
2222 -- always prioritize treesitter parser over LSP for folding
2323 if vim .b [bufnr ].origami_folding_provider == " lsp" then return end
@@ -32,9 +32,12 @@ local function checkForTreesitter(bufnr, filetype)
3232 vim .wo [win ][0 ].foldexpr = " v:lua.vim.treesitter.foldexpr()"
3333 vim .b [bufnr ].origami_folding_provider = " treesitter"
3434 else
35- vim .wo [win ][0 ].foldmethod = " indent"
35+ local fallback =
36+ require (" origami.config" ).config .useLspFoldsWithTreesitterFallback .foldmethodIfNeitherIsAvailable
37+ if type (fallback ) == " function" then fallback = fallback (bufnr ) end
38+ vim .wo [win ][0 ].foldmethod = fallback
3639 vim .wo [win ][0 ].foldexpr = " "
37- vim .b [bufnr ].origami_folding_provider = " indent "
40+ vim .b [bufnr ].origami_folding_provider = fallback
3841 end
3942 end )
4043end
@@ -52,12 +55,12 @@ vim.api.nvim_create_autocmd("LspAttach", {
5255vim .api .nvim_create_autocmd (" FileType" , {
5356 desc = " Origami: Use Treesitter as folding provider if there is a parser for it" ,
5457 group = group ,
55- callback = function (ctx ) checkForTreesitter (ctx .buf , ctx .match ) end ,
58+ callback = function (ctx ) checkForTreesitterWithFallback (ctx .buf , ctx .match ) end ,
5659})
5760
5861-- initialize on the existing buffer in case of lazy-loading
5962local listedBufs = vim .fn .getbufinfo { buflisted = 1 }
6063for _ , buf in ipairs (listedBufs ) do
6164 checkForLsp (buf .bufnr )
62- checkForTreesitter (buf .bufnr )
65+ checkForTreesitterWithFallback (buf .bufnr )
6366end
0 commit comments