Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@ available options when this plugin is in your editor's Lua workspace.
checkbox_markers = " .oOX",
checkbox_partials_toggle = 1,
auto_indent_after_colon = true,
markdown_headings = false,
}
```

`custom_mappings` entries are passed to `vim.keymap.set` as `{ mode, lhs, rhs }` for each configured buffer.

When `markdown_headings = true`, the promote and demote actions also change ATX heading levels in `markdown`
buffers: demoting `## Title` gives `### Title` and promoting it gives `# Title`. Levels stay between 1 and 6, and
`#` lines inside fenced code blocks or YAML front matter are left alone.

## Commands

The plugin defines these commands:
Expand Down
69 changes: 69 additions & 0 deletions lua/bullets/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,71 @@ local function fallback_style_for_indent(indent)
return config.options.outline_levels[indent_depth(indent) + 1]
end

local function parse_heading(line)
local indent, hashes, rest = line:match '^( ? ? ?)(#+)(.*)$'
if not hashes or #hashes > 6 or not (rest == '' or rest:match '^[ \t]') then
return nil
end

return { indent = indent, level = #hashes, rest = rest }
end

local function in_markdown_code(lnum)
local lines = vim.api.nvim_buf_get_lines(0, 0, lnum - 1, false)
local front_matter = false
local fence
for index, line in ipairs(lines) do
if index == 1 and line == '---' then
front_matter = true
elseif front_matter then
front_matter = not (line == '---' or line == '...')
else
local run, rest = line:match '^ ? ? ?([`~][`~][`~]+)(.*)$'
if run and run:match('^' .. vim.pesc(run:sub(1, 1)) .. '+$') then
if not fence then
fence = run
elseif run:sub(1, 1) == fence:sub(1, 1) and #run >= #fence and rest:match '^%s*$' then
fence = nil
end
end
end
end

return front_matter or fence ~= nil
end

-- Returns nil when the line is not a heading the plugin owns, so callers can try list items.
local function change_heading_level(lnum, direction)
if not config.options.markdown_headings or vim.bo.filetype ~= 'markdown' then
return nil
end

local line = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, false)[1]
local heading = parse_heading(line)
if not heading or in_markdown_code(lnum) then
return nil
end

local level = heading.level + (direction == 'demote' and 1 or -1)
if level < 1 or level > 6 then
-- Handled as a no-op so mappings do not fall back to native indentation.
return true
end

vim.api.nvim_buf_set_lines(0, lnum - 1, lnum, false, { heading.indent .. string.rep('#', level) .. heading.rest })
local cursor = vim.api.nvim_win_get_cursor(0)
if cursor[1] == lnum then
vim.api.nvim_win_set_cursor(0, { lnum, math.max(0, cursor[2] + level - heading.level) })
end
return true
end

local function change_line_level(lnum, direction)
local heading_changed = change_heading_level(lnum, direction)
if heading_changed ~= nil then
return heading_changed
end

local line = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, false)[1]
local bullet = resolve_bullet(parse_line(line), lnum)
if not bullet then
Expand Down Expand Up @@ -599,6 +663,11 @@ end

local function change_current_line_level(direction)
local lnum = vim.api.nvim_win_get_cursor(0)[1]
local heading_changed = change_heading_level(lnum, direction)
if heading_changed ~= nil then
return heading_changed
end

local changed = change_line_level(lnum, direction)
if changed and config.options.renumber_on_change then
M.renumber_list()
Expand Down
2 changes: 2 additions & 0 deletions lua/bullets/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ local M = {}
---@field checkbox_markers? string Characters used as checkbox states.
---@field checkbox_partials_toggle? 0|1 How partial checkbox states toggle.
---@field auto_indent_after_colon? boolean Indent a new child item after a bullet ending in `:`.
---@field markdown_headings? boolean Promote and demote Markdown ATX headings in `markdown` buffers.

---@type bullets.Config
M.defaults = {
Expand All @@ -48,6 +49,7 @@ M.defaults = {
checkbox_markers = ' .oOX',
checkbox_partials_toggle = 1,
auto_indent_after_colon = true,
markdown_headings = false,
}

---@type bullets.Config
Expand Down
48 changes: 48 additions & 0 deletions openspec/specs/outline-nesting/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,54 @@ The plugin SHALL promote or demote every list item in a visual range.
- WHEN visual demotion is triggered
- THEN each selected list item moves to the next configured outline level

### Requirement: Change Markdown heading levels

The plugin SHALL promote and demote Markdown ATX headings with the outline level actions when `markdown_headings` is enabled.

#### Scenario: Demote a Markdown heading {#ON-019}

- GIVEN `markdown_headings` is enabled
- AND the current buffer has the `markdown` filetype
- AND the current line is an ATX heading
- WHEN demotion is triggered
- THEN the heading gains one `#` and its text is preserved

#### Scenario: Promote a Markdown heading {#ON-020}

- GIVEN `markdown_headings` is enabled
- AND the current buffer has the `markdown` filetype
- AND the current line is an ATX heading below level 1
- WHEN promotion is triggered
- THEN the heading loses one `#` and its text is preserved

#### Scenario: Keep headings within valid levels {#ON-021}

- GIVEN `markdown_headings` is enabled
- AND the current line is a level 1 heading or a level 6 heading
- WHEN promotion of the level 1 heading or demotion of the level 6 heading is triggered
- THEN the line is unchanged and is not indented

#### Scenario: Ignore heading-like lines in code and front matter {#ON-022}

- GIVEN `markdown_headings` is enabled
- AND a line starting with `#` is inside a fenced code block or YAML front matter
- WHEN demotion is triggered on that line
- THEN the line is treated as plain text instead of a heading

#### Scenario: Markdown headings disabled by default {#ON-023}

- GIVEN `markdown_headings` is not configured
- AND the current line is an ATX heading in a `markdown` buffer
- WHEN demotion is triggered
- THEN the heading level is unchanged

#### Scenario: Change headings in visual ranges {#ON-024}

- GIVEN `markdown_headings` is enabled
- AND a visual range contains ATX headings and list items
- WHEN visual promotion or demotion is triggered
- THEN headings change level and list items change outline level

### Requirement: Auto-indent children after colons

The plugin SHALL insert child list items after continued list items ending in a colon when `auto_indent_after_colon` is enabled.
Expand Down
104 changes: 104 additions & 0 deletions test/markdown_headings_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
local helpers = require 'test.helpers'

describe('markdown headings', function()
local function markdown_buffer(lines)
helpers.new_buffer(lines)
vim.bo.filetype = 'markdown'
end

before_each(function()
helpers.reset_config()
require('bullets').setup { markdown_headings = true }
end)

it('demotes a heading #ON-019', function()
markdown_buffer { '# Title', '## Section' }

helpers.feedkeys 'G>>'

assert.are.same({ '# Title', '### Section' }, helpers.get_lines())
end)

it('demotes a heading with the insert mapping #ON-019', function()
markdown_buffer { '## Section' }

helpers.feedkeys 'A<C-t>'

assert.are.same({ '### Section' }, helpers.get_lines())
end)

it('promotes a heading #ON-020', function()
markdown_buffer { '# Title', '### Section' }

helpers.feedkeys 'G<<'

assert.are.same({ '# Title', '## Section' }, helpers.get_lines())
end)

it('promotes an empty heading #ON-020', function()
markdown_buffer { '##' }

vim.cmd 'BulletPromote'

assert.are.same({ '#' }, helpers.get_lines())
end)

it('does not promote past level 1 or demote past level 6 #ON-021', function()
markdown_buffer { '# Title', '###### Deepest' }

helpers.feedkeys 'gg<<'
helpers.feedkeys 'G>>'

assert.are.same({ '# Title', '###### Deepest' }, helpers.get_lines())
end)

it('ignores heading-like lines in fenced code blocks #ON-022', function()
markdown_buffer { '```sh', '# comment', '```' }

helpers.feedkeys 'ggj'
vim.cmd 'BulletDemote'

assert.are.same({ '```sh', '# comment', '```' }, helpers.get_lines())
end)

it('ignores heading-like lines in front matter #ON-022', function()
markdown_buffer { '---', '# comment', '---', '# Title' }

helpers.feedkeys 'ggj'
vim.cmd 'BulletDemote'
helpers.feedkeys 'G'
vim.cmd 'BulletDemote'

assert.are.same({ '---', '# comment', '---', '## Title' }, helpers.get_lines())
end)

it('does not treat hashtags as headings #ON-022', function()
markdown_buffer { '#tag' }

vim.cmd 'BulletDemote'

assert.are.same({ '#tag' }, helpers.get_lines())
end)

it('leaves headings unchanged when disabled #ON-023', function()
helpers.reset_config()
markdown_buffer { '## Section' }

vim.cmd 'BulletDemote'

assert.are.same({ '## Section' }, helpers.get_lines())
end)

it('changes headings and list items in visual ranges #ON-024', function()
require('bullets').setup { markdown_headings = true, outline_levels = { 'std-', 'std*' } }
markdown_buffer { '## Section', '- item' }
vim.bo.expandtab = true
vim.bo.shiftwidth = 2

vim.cmd '1,2BulletDemoteVisual'
assert.are.same({ '### Section', ' * item' }, helpers.get_lines())

vim.cmd '1,2BulletPromoteVisual'
assert.are.same({ '## Section', '- item' }, helpers.get_lines())
end)
end)