Skip to content

Commit ea122d4

Browse files
committed
feat: foldmethod customizable if neither TS nor LSP available (#48)
1 parent 3dda1df commit ea122d4

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ manner and adds some features that `nvim-ufo` does not provide.
7575
```lua
7676
-- default settings
7777
require("origami").setup {
78-
useLspFoldsWithTreesitterFallback = true,
78+
useLspFoldsWithTreesitterFallback = {
79+
enabled = true,
80+
foldmethodIfNeitherIsAvailable = "indent", ---@type string|fun(bufnr: number): string
81+
},
7982
pauseFoldsOnSearch = true,
8083
foldtext = {
8184
enabled = true,

lua/origami/config.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ local warn = require("origami.utils").warn
44

55
---@class Origami.config
66
local defaultConfig = {
7-
useLspFoldsWithTreesitterFallback = true,
7+
useLspFoldsWithTreesitterFallback = {
8+
enabled = true,
9+
foldmethodIfNeitherIsAvailable = "indent", ---@type string|fun(bufnr: number): string
10+
},
811
pauseFoldsOnSearch = true,
912
foldtext = {
1013
enabled = true,
@@ -62,6 +65,14 @@ function M.setup(userConfig)
6265
)
6366
M.config.foldKeymaps.closeOnlyOnFirstColumn = M.config.foldKeymaps.hOnlyOpensOnFirstColumn
6467
end
68+
69+
-- DEPRECATION (2025-12-27)
70+
if type(M.config.useLspFoldsWithTreesitterFallback) == "boolean" then
71+
warn(
72+
"nvim-origami config `useLspFoldsWithTreesitterFallback` was renamed to `useLspFoldsWithTreesitterFallback.enabled`."
73+
)
74+
M.config.useLspFoldsWithTreesitterFallback.enabled = M.config.useLspFoldsWithTreesitterFallback
75+
end
6576
---@diagnostic enable: undefined-field
6677
-----------------------------------------------------------------------------
6778

lua/origami/features/lsp-and-treesitter-foldexpr.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ end
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)
4043
end
@@ -52,12 +55,12 @@ vim.api.nvim_create_autocmd("LspAttach", {
5255
vim.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
5962
local listedBufs = vim.fn.getbufinfo { buflisted = 1 }
6063
for _, buf in ipairs(listedBufs) do
6164
checkForLsp(buf.bufnr)
62-
checkForTreesitter(buf.bufnr)
65+
checkForTreesitterWithFallback(buf.bufnr)
6366
end

0 commit comments

Comments
 (0)