Skip to content

Commit

Permalink
Use Treesitter for folding (nvim-orgmode#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteBlackGoose authored Mar 11, 2023
1 parent dd5c132 commit ca3993c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
6 changes: 1 addition & 5 deletions ftplugin/org.vim
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
lua require('orgmode.config'):setup_mappings('org')
lua require('orgmode.config'):setup_mappings('text_objects')

function! OrgmodeFoldExpr()
return luaeval('require("orgmode.org.indent").foldexpr()')
endfunction

function! OrgmodeFoldText()
return luaeval('require("orgmode.org.indent").foldtext()')
endfunction
Expand All @@ -20,7 +16,7 @@ endfunction
setlocal nomodeline
setlocal fillchars+=fold:\
setlocal foldmethod=expr
setlocal foldexpr=OrgmodeFoldExpr()
setlocal foldexpr=nvim_treesitter#foldexpr()
setlocal foldtext=OrgmodeFoldText()
setlocal formatexpr=OrgmodeFormatExpr()
setlocal foldlevel=0
Expand Down
6 changes: 4 additions & 2 deletions lua/orgmode/utils/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ function M.get_node_at_cursor(cursor, winnr, ignore_injected_langs)
winnr = winnr or 0
local buf = vim.api.nvim_win_get_buf(winnr)
-- TODO: Use only this function when 0.8 is released
if vim.treesitter.get_node_at_pos then
return vim.treesitter.get_node_at_pos(buf, cursor[1] - 1, cursor[2], {
if vim.treesitter.get_node then
return vim.treesitter.get_node({
bufrn = buf,
pos = { cursor[1] - 1, cursor[2] },
ignore_injections = ignore_injected_langs,
})
end
Expand Down
8 changes: 8 additions & 0 deletions queries/org/folds.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
([
(section)
(table)
(drawer)
(property_drawer)
(block)
] @fold
(#trim! @fold))

0 comments on commit ca3993c

Please sign in to comment.