Skip to content

Commit

Permalink
fix: Ignore heading patterns in fenced code blocks
Browse files Browse the repository at this point in the history
- Addresses issue #145
- Fixes folding and jump-to-section behavior
- If folding or jump-to-section is initiated with the cursor IN a code
  block, the behavior will occur in the code block itself (i.e. the
  cursor will jump to the next heading inside the code block, if there
  is one, or it will fold the section inside of the code block)
  • Loading branch information
jakewvincent committed Jun 3, 2024
1 parent b1cea92 commit 4864c6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 8 additions & 2 deletions lua/mkdnflow/cursor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,21 @@ local go_to_heading = function(anchor_text, reverse)
-- Record which line we're on; chances are the link goes to something later,
-- so we'll start looking from here onwards and then circle back to the beginning
local position = vim.api.nvim_win_get_cursor(0)
local starting_row, continue = position[1], true
local starting_row, continue, in_fenced_code_block = position[1], true, false
local row = (reverse and starting_row - 1) or starting_row + 1
while continue do
local line = (reverse and vim.api.nvim_buf_get_lines(0, row - 1, row, false))
or vim.api.nvim_buf_get_lines(0, row - 1, row, false)
-- If the line has contents, do the thing
if line[1] then
-- Are we in a code block?
if string.find(line[1], "^```") then
-- Flip the truth value
in_fenced_code_block = not in_fenced_code_block
end
-- Does the line start with a hash?
local has_heading = string.find(line[1], '^#')
if has_heading then
if has_heading and not in_fenced_code_block then
if anchor_text == nil then
-- Send the cursor to the heading
vim.api.nvim_win_set_cursor(0, { row, 0 })
Expand Down Expand Up @@ -191,6 +196,7 @@ local go_to_heading = function(anchor_text, reverse)
-- If the line does not have contents, start searching from the beginning
if anchor_text ~= nil or wrap == true then
row = (reverse and vim.api.nvim_buf_line_count(0)) or 1
in_fenced_code_block = false
else
continue = nil
local place = (reverse and 'beginning') or 'end'
Expand Down
16 changes: 12 additions & 4 deletions lua/mkdnflow/folds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ local get_section_range = function(start_row)
vim.api.nvim_buf_line_count(0)
local heading_level = M.getHeadingLevel(line)
if heading_level > 0 then
local continue = true
local continue, in_fenced_code_block = true, false
local end_row = start_row + 1
while continue do
local next_line = vim.api.nvim_buf_get_lines(0, end_row - 1, end_row, false)
if next_line[1] then
if M.getHeadingLevel(next_line[1]) <= heading_level then
if string.find(next_line[1], "^```") then
-- Flip the truth value
in_fenced_code_block = not in_fenced_code_block
end
if M.getHeadingLevel(next_line[1]) <= heading_level and not in_fenced_code_block then
continue = false
else
end_row = end_row + 1
Expand All @@ -53,10 +57,14 @@ end

local get_nearest_heading = function()
local row = vim.api.nvim_win_get_cursor(0)[1] - 1
local continue = true
local continue, in_fenced_code_block = true, false
while continue and row > 0 do
local prev_line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1]
if M.getHeadingLevel(prev_line) < 99 then
if string.find(prev_line, "^```") then
-- Flip the truth value
in_fenced_code_block = not in_fenced_code_block
end
if M.getHeadingLevel(prev_line) < 99 and not in_fenced_code_block then
continue = false
return row
else
Expand Down

0 comments on commit 4864c6b

Please sign in to comment.