|
78 | 78 |
|
79 | 79 | M.foldSection = function() |
80 | 80 | local row, line = vim.api.nvim_win_get_cursor(0)[1], vim.api.nvim_get_current_line() |
81 | | - local in_fenced_code_block = utils.cursorInCodeBlock(row) |
82 | | - if M.getHeadingLevel(line) < 99 and not in_fenced_code_block then |
83 | | - local range = get_section_range() |
84 | | - if range then |
85 | | - vim.cmd(tostring(range[1]) .. ',' .. tostring(range[2]) .. 'fold') |
86 | | - end |
87 | | - else |
88 | | - local start_row = get_nearest_heading() |
89 | | - if start_row then |
90 | | - local range = get_section_range(start_row) |
| 81 | + -- See if the cursor is in an open fold. If so, and if it is not also on a heading, close the |
| 82 | + -- open fold. |
| 83 | + if vim.fn.foldlevel(row) > 0 and not (M.getHeadingLevel(line) < 99) then |
| 84 | + vim.cmd.foldclose() |
| 85 | + else -- Otherwise, create a fold |
| 86 | + local in_fenced_code_block = utils.cursorInCodeBlock(row) |
| 87 | + -- See if the cursor is on a heading |
| 88 | + if M.getHeadingLevel(line) < 99 and not in_fenced_code_block then |
| 89 | + local range = get_section_range() |
91 | 90 | if range then |
92 | 91 | vim.cmd(tostring(range[1]) .. ',' .. tostring(range[2]) .. 'fold') |
93 | 92 | end |
| 93 | + else -- The cursor isn't on a heading, so find what the range of the fold should be |
| 94 | + local start_row = get_nearest_heading() |
| 95 | + if start_row then |
| 96 | + local range = get_section_range(start_row) |
| 97 | + if range then |
| 98 | + vim.cmd(tostring(range[1]) .. ',' .. tostring(range[2]) .. 'fold') |
| 99 | + end |
| 100 | + end |
94 | 101 | end |
95 | 102 | end |
96 | 103 | end |
97 | 104 |
|
98 | 105 | M.unfoldSection = function(row) |
99 | 106 | row = row or vim.api.nvim_win_get_cursor(0)[1] |
100 | | - local foldstart = vim.fn.foldclosed(tostring(row)) |
101 | | - if foldstart > -1 then |
102 | | - local foldend = vim.fn.foldclosedend(tostring(row)) |
103 | | - vim.cmd(tostring(foldstart) .. ',' .. tostring(foldend) .. 'foldopen') |
| 107 | + -- If the cursor is on a closed fold, open the fold. |
| 108 | + if vim.fn.foldlevel(row) > 0 then |
| 109 | + vim.cmd.foldopen() |
104 | 110 | end |
105 | 111 | end |
106 | 112 |
|
|
0 commit comments