diff --git a/README.md b/README.md index f9f5d00..510d6a3 100644 --- a/README.md +++ b/README.md @@ -338,6 +338,32 @@ vim.keymap.set('n', 'K', function() end) ``` +### Fold specific Treesitter patterns + +The default folds provided by treesitter are usually too generic to be useful +for automatically closing everything (except perhaps import_declaration). + +Instead, you can define more specialized patterns using treesitter queries and +target them explicitly. + +To assign a special kind to a query you use `#set!` directive inside the query: + +`nvim/after/queries//folds.scm`: + +```scheme +(#set! kind "") +``` + +Then you can use this pattern in a list of kinds that must be automatically closed: + +```lua +opts = { + close_fold_kinds_for_ft = { + [""] = { "" } + }, +} +``` + ### Customize fold text Adding number suffix of folded lines instead of the default ellipsis, here is the example: diff --git a/lua/ufo/main.lua b/lua/ufo/main.lua index 7fe3870..9a7dace 100644 --- a/lua/ufo/main.lua +++ b/lua/ufo/main.lua @@ -130,6 +130,8 @@ function M.inspectBuf(bufnr) table.insert(msg, 'Fallback provider: ' .. fb.providers[2]) end table.insert(msg, 'Selected provider: ' .. (fb.selectedProvider or 'nil')) + -- TODO: Should we print a table of all nodes that share the same {start, stop}? + -- This way, we can display both the standard node type and the custom kind. local curKind local curStartLine, curEndLine = -1, -1 local kindSet = {} diff --git a/lua/ufo/provider/treesitter.lua b/lua/ufo/provider/treesitter.lua index 19677d8..b02ff1a 100644 --- a/lua/ufo/provider/treesitter.lua +++ b/lua/ufo/provider/treesitter.lua @@ -112,18 +112,20 @@ local function iterFoldMatches(bufnr, parser, root, rootLang) -- Extract capture names from each match for id, nodes in pairs(match) do local m = metadata[id] + local nKind = metadata['kind'] + local node, nType if m and m.range then nType = getNodesType(nodes) - node = MetaNode:new(m.range, nType) + node = MetaNode:new(m.range, nKind or nType) elseif type(nodes) ~= 'table' then + -- TODO: support custom kinds + -- old behaviou before 0.11 node = nodes - elseif #nodes == 1 then - node = nodes[1] else nType = getNodesType(nodes) - node = MetaNode.from_nodes(nodes[1], nodes[#nodes], nType) + node = MetaNode.from_nodes(nodes[1], nodes[#nodes], nKind or nType) end table.insert(matches, node)