Support header hierarchy in Markdown outlines

This commit is contained in:
Matthew Schauer
2022-02-02 17:26:12 -08:00
parent 0347928385
commit 49b9aa24e9

View File

@@ -7,8 +7,8 @@ local M = {}
---@return table ---@return table
function M.handle_markdown() function M.handle_markdown()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local results = {} local level_symbols = {{children = {}}}
local max_level = 1
local is_inside_code_block = false local is_inside_code_block = false
for line, value in ipairs(lines) do for line, value in ipairs(lines) do
@@ -16,15 +16,29 @@ function M.handle_markdown()
is_inside_code_block = not is_inside_code_block is_inside_code_block = not is_inside_code_block
end end
if string.find(value, "^#+ ") and not is_inside_code_block then header, title = string.match(value, "^(#+)%s+(.*)$")
if #results > 0 then if header and not is_inside_code_block then
results[#results].selectionRange["end"].line = line - 1 depth = #header + 1
results[#results].range["end"].line = line - 1
for i = depth - 1, 1, -1 do
if level_symbols[i] ~= nil then
parent = level_symbols[i].children
break
end
end end
table.insert(results, { for i = depth, max_level do
if level_symbols[i] ~= nil then
level_symbols[i].selectionRange["end"].line = line - 1
level_symbols[i].range["end"].line = line - 1
level_symbols[i] = nil
end
end
max_level = depth
entry = {
kind = 13, kind = 13,
name = value, name = title,
selectionRange = { selectionRange = {
start = {character = 1, line = line - 1}, start = {character = 1, line = line - 1},
["end"] = {character = 1, line = line - 1} ["end"] = {character = 1, line = line - 1}
@@ -32,12 +46,23 @@ function M.handle_markdown()
range = { range = {
start = {character = 1, line = line - 1}, start = {character = 1, line = line - 1},
["end"] = {character = 1, line = line - 1} ["end"] = {character = 1, line = line - 1}
} },
}) children = {},
}
parent[#parent + 1] = entry
level_symbols[depth] = entry
end end
end end
return {[1000000]={result=results}} for i = 2, max_level do
if level_symbols[i] ~= nil then
level_symbols[i].selectionRange["end"].line = #lines
level_symbols[i].range["end"].line = #lines
end
end
return {[1000000]={result=level_symbols[1].children}}
end end
return M return M