Skip to content

Commit

Permalink
feat(folds): Add org_cycle_separator_lines
Browse files Browse the repository at this point in the history
Closes #711
  • Loading branch information
kristijanhusak committed Jan 27, 2025
1 parent 98bf44d commit ab87a9b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 110 deletions.
39 changes: 39 additions & 0 deletions docs/configuration.org
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,45 @@ require('orgmode').setup({
Number of minutes to increase/decrease when using
[[#org_timestamp_up][org_timestamp_up]]/[[#org_timestamp_down][org_timestamp_down]]

*** =org_cycle_separator_lines=
:PROPERTIES:
:CUSTOM_ID: org_cycle_separator_lines
:END:
- Type =number=
- Default: =2=
Minimum number of empty lines needed at the end of the headline to show a single empty line when headline is folded.

For example, given this structure:
#+begin_src org
* One empty space headline
Content

* Two empty space headline
Content


* Three empty space headline
Content



* Last headline
Content
#+end_src

When folded, it will appear like this:
#+begin_src org
* One empty space headline ...
* Two empty space headline ...

* Three empty space headline ...

* Last headline ...
#+end_src

When value is =0=, all empty lines are folded together with headline.

Cannot be negative.
*** =org_blank_before_new_entry=
:PROPERTIES:
:CUSTOM_ID: org_blank_before_new_entry
Expand Down
2 changes: 1 addition & 1 deletion ftplugin/org.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require('orgmode.org.indent').setup_virtual_indent()
vim.bo.modeline = false
vim.opt_local.fillchars:append('fold: ')
vim.opt_local.foldmethod = 'expr'
vim.opt_local.foldexpr = 'v:lua.require("orgmode.org.fold").foldexpr()'
vim.opt_local.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
if utils.has_version_10() and config.ui.folds.colored then
vim.opt_local.foldtext = ''
else
Expand Down
1 change: 1 addition & 0 deletions lua/orgmode/config/_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
---@field org_indent_mode_turns_off_org_adapt_indentation? boolean If true, turning on indent mode will turn off `org_adapt_indentation`. Default: true
---@field org_indent_mode_turns_on_hiding_stars? boolean If true, turning on indent mode will hide leading stars. Default: true
---@field org_time_stamp_rounding_minutes? number Rounding minutes for time stamps. Default: 5
---@field org_cycle_separator_lines? number Min number of spaces are needed at the end of headline to show empty line between folds. Default: 2
---@field org_blank_before_new_entry? { heading: boolean, plain_list_item: boolean } Should blank line be prepended. Default: { heading = true, plain_list_item = false }
---@field org_src_window_setup? string | fun() How to open "special edit" buffer window. Default: 'top 16new'
---@field org_edit_src_content_indentation? number Addditional ndentation number applied when editing a SRC block through special edit. Default: 0
Expand Down
1 change: 1 addition & 0 deletions lua/orgmode/config/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ local DefaultConfig = {
org_indent_mode_turns_off_org_adapt_indentation = true,
org_indent_mode_turns_on_hiding_stars = true,
org_time_stamp_rounding_minutes = 5,
org_cycle_separator_lines = 2,
org_blank_before_new_entry = {
heading = true,
plain_list_item = false,
Expand Down
36 changes: 36 additions & 0 deletions lua/orgmode/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,42 @@ function Config:setup_ts_predicates()
return false
end, { force = true, all = false })

local org_cycle_separator_lines = math.max(self.opts.org_cycle_separator_lines, 0)

vim.treesitter.query.add_directive('org-set-fold-offset!', function(match, _, bufnr, pred, metadata)
if org_cycle_separator_lines == 0 then
return
end
---@type TSNode | nil
local capture_id = pred[2]
local section_node = match[capture_id]
if not capture_id or not section_node or section_node:type() ~= 'section' then
return
end
if not metadata[capture_id] then
metadata[capture_id] = {}
end
local range = metadata[capture_id].range or { section_node:range() }
local start_row = range[1]
local end_row = range[3]

local empty_lines = 0
while end_row > start_row do
local line = vim.api.nvim_buf_get_lines(bufnr, end_row - 1, end_row, false)[1]
if vim.trim(line) ~= '' then
break
end
empty_lines = empty_lines + 1
end_row = end_row - 1
end

if empty_lines < org_cycle_separator_lines then
return
end
range[3] = range[3] - 1
metadata[capture_id].range = range
end, { force = true, all = false })

vim.treesitter.query.add_predicate('org-is-valid-priority?', function(match, _, source, predicate)
local node = match[predicate[2]]
local type = predicate[3]
Expand Down
108 changes: 0 additions & 108 deletions lua/orgmode/org/fold.lua

This file was deleted.

2 changes: 1 addition & 1 deletion queries/org/folds.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
(drawer)
(property_drawer)
(block)
] @fold)
] @fold (#org-set-fold-offset! @fold))

0 comments on commit ab87a9b

Please sign in to comment.