Skip to content

Commit

Permalink
Move stack implementation to collections.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
aquilesg committed Jul 29, 2024
1 parent 5ca3dc7 commit ba1422f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
35 changes: 35 additions & 0 deletions lua/obsidian/collections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,39 @@ end
-- return self
-- end

-------------------------
--- Stack implementation
-------------------------

-- Create new stack
function M.new_stack()
return {}
end

-- Push an item onto the stack
function M.push(stack, item)
table.insert(stack, item)
end

-- Pop an item from the stack
function M.pop(stack)
if #stack == 0 then
return nil
end
return table.remove(stack)
end

-- Check the top item on the stack
function M.peek(stack)
if #stack == 0 then
return nil
end
return stack[#stack]
end

-- Check if the stack is empty
function M.is_empty(stack)
return #stack == 0
end

return M
7 changes: 4 additions & 3 deletions lua/obsidian/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local util = require "obsidian.util"
local log = require "obsidian.log"
local search = require "obsidian.search"
local DefaultTbl = require("obsidian.collections").DefaultTbl
local collections = require "obsidian.collections"
local iter = require("obsidian.itertools").iter

local M = {}
Expand Down Expand Up @@ -608,12 +609,12 @@ local function get_callout_extmarks(marks, line, lnum, ui_opts, callout_hl_group
end
end
end
elseif string.find(line, ">") and not util.is_empty(callout_hl_group_stack) then
elseif string.find(line, ">") and not collections.is_empty(callout_hl_group_stack) then
log.debug "Callout stack available, generating marks for callout body"
-- If we have a current stack, then we're in a callout group and should treat the lone
-- > character as part of a callout block
generate_callout_extmarks_body(marks, line, lnum, callout_hl_group_stack)
elseif not string.match(line, "%s*>(.+)") and not util.is_empty(callout_hl_group_stack) then
elseif not string.match(line, "%s*>(.+)") and not collections.is_empty(callout_hl_group_stack) then
log.debug "Clearing callout stack"
-- If we have a current stack, but the we don't match the > block, then we should remove all of the items from the stack
-- as this inidcates we've exited the existing callout block
Expand Down Expand Up @@ -675,7 +676,7 @@ local function update_extmarks(bufnr, ns_id, ui_opts)

-- Iterate over lines (skipping code blocks) and update marks.
local inside_code_block = false
local callout_block_highlights = util.new_stack()
local callout_block_highlights = collections.new_stack()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
for i, line in ipairs(lines) do
local lnum = i - 1
Expand Down
35 changes: 0 additions & 35 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1341,39 +1341,4 @@ util.buffer_is_empty = function(bufnr)
end
end

-------------------------
--- Stack implementation
-------------------------

-- Create new stack
function util.new_stack()
return {}
end

-- Push an item onto the stack
function util.push(stack, item)
table.insert(stack, item)
end

-- Pop an item from the stack
function util.pop(stack)
if #stack == 0 then
return nil
end
return table.remove(stack)
end

-- Check the top item on the stack
function util.peek(stack)
if #stack == 0 then
return nil
end
return stack[#stack]
end

-- Check if the stack is empty
function util.is_empty(stack)
return #stack == 0
end

return util

0 comments on commit ba1422f

Please sign in to comment.