From ba1422f7e93c2f6d4dc63a86f8c50f24f91a4c3a Mon Sep 17 00:00:00 2001 From: Aquiles Gomez Date: Sun, 28 Jul 2024 21:38:59 -0500 Subject: [PATCH] Move stack implementation to collections.lua --- lua/obsidian/collections.lua | 35 +++++++++++++++++++++++++++++++++++ lua/obsidian/ui.lua | 7 ++++--- lua/obsidian/util.lua | 35 ----------------------------------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/lua/obsidian/collections.lua b/lua/obsidian/collections.lua index 9f826806a..5e45d68ba 100644 --- a/lua/obsidian/collections.lua +++ b/lua/obsidian/collections.lua @@ -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 diff --git a/lua/obsidian/ui.lua b/lua/obsidian/ui.lua index c9703313c..141f4e245 100644 --- a/lua/obsidian/ui.lua +++ b/lua/obsidian/ui.lua @@ -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 = {} @@ -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 @@ -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 diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 5b38b120e..1db3d044e 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -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