Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<lang>/folds.scm`:

```scheme
(#set! kind "<name>")
```

Then you can use this pattern in a list of kinds that must be automatically closed:

```lua
opts = {
close_fold_kinds_for_ft = {
["<lang>"] = { "<name>" }
},
}
```

### Customize fold text

Adding number suffix of folded lines instead of the default ellipsis, here is the example:
Expand Down
2 changes: 2 additions & 0 deletions lua/ufo/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
10 changes: 6 additions & 4 deletions lua/ufo/provider/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down