From d25e1a86485fba5b63206947a0fab1841dbcd2da Mon Sep 17 00:00:00 2001 From: goose Date: Thu, 19 Dec 2024 07:09:31 +0700 Subject: [PATCH] feat(watcher): remove | in the %watcher_marker --- lua/timber/buffers.lua | 3 +- lua/timber/watcher.lua | 4 +-- lua/timber/watcher/sources/filesystem.lua | 4 +-- lua/timber/watcher/sources/neotest.lua | 4 +-- tests/timber/timber_buffers_spec.lua | 38 ++++++++++++++--------- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/lua/timber/buffers.lua b/lua/timber/buffers.lua index d5f7571..ad1c363 100644 --- a/lua/timber/buffers.lua +++ b/lua/timber/buffers.lua @@ -29,7 +29,7 @@ local M = { ---@param line string ---@return string? placeholder_id function M._parse_log_placeholder(line) - local pattern = string.format("🪵(%s)|", string.rep("[A-Z0-9]", 3)) + local pattern = string.format("🪵(%s)", string.rep("[A-Z0-9]", 3)) return string.match(line, pattern) end @@ -152,6 +152,7 @@ function M._new_log_placeholder(log_placeholder) local line = vim.api.nvim_buf_get_lines(log_placeholder.bufnr, log_placeholder.line, log_placeholder.line + 1, false)[1] + -- Find the log marker local marker_pattern = watcher.MARKER .. log_placeholder.id local log_marker_index = line:find(marker_pattern) - 1 diff --git a/lua/timber/watcher.lua b/lua/timber/watcher.lua index 85863b2..56b331b 100644 --- a/lua/timber/watcher.lua +++ b/lua/timber/watcher.lua @@ -27,8 +27,8 @@ end ---@return string start_marker, string end_marker, string marker_id function M.generate_marker_pairs() local id = M.generate_unique_id() - local start = M.MARKER .. id .. "|" - local end_ = "|" .. id + local start = M.MARKER .. id + local end_ = id return start, end_, id end diff --git a/lua/timber/watcher/sources/filesystem.lua b/lua/timber/watcher/sources/filesystem.lua index 118eece..13d9586 100644 --- a/lua/timber/watcher/sources/filesystem.lua +++ b/lua/timber/watcher/sources/filesystem.lua @@ -27,7 +27,7 @@ end function SourceFilesystem:ingest(line) if self.state.state == "initial" then - local start_marker_pattern = string.format("%s(%s)|", watcher.MARKER, string.rep("[A-Z0-9]", watcher.ID_LENGTH)) + local start_marker_pattern = string.format("%s(%s)", watcher.MARKER, string.rep("[A-Z0-9]", watcher.ID_LENGTH)) local _, end_idx, match = string.find(line, start_marker_pattern) if match and end_idx then @@ -41,7 +41,7 @@ function SourceFilesystem:ingest(line) end elseif self.state.state == "pending" then local pending_log_item = self.state.pending_log_item - local end_marker_pattern = string.format("|%s$", pending_log_item) + local end_marker_pattern = string.format("%s$", pending_log_item) local match = string.match(line, end_marker_pattern) local buffer = self.state.buffer diff --git a/lua/timber/watcher/sources/neotest.lua b/lua/timber/watcher/sources/neotest.lua index 1248771..db9a23c 100644 --- a/lua/timber/watcher/sources/neotest.lua +++ b/lua/timber/watcher/sources/neotest.lua @@ -17,8 +17,8 @@ end ---@param text string function SourceNeotest:capture_log_entries(text) - local id_pattern = string.format("🪵(%s)", string.rep("[A-Z0-9]", watcher.ID_LENGTH)) - local pattern = id_pattern .. "%|(.-)%|%1" + local id_pattern = string.format("%s(%s)", watcher.MARKER, string.rep("[A-Z0-9]", watcher.ID_LENGTH)) + local pattern = id_pattern .. "(.-)%1" for id, content in string.gmatch(text, pattern) do self.on_log_capture(id, content) diff --git a/tests/timber/timber_buffers_spec.lua b/tests/timber/timber_buffers_spec.lua index f363ecd..fc6f259 100644 --- a/tests/timber/timber_buffers_spec.lua +++ b/tests/timber/timber_buffers_spec.lua @@ -52,8 +52,8 @@ describe("timber.buffers autocmd", function() input = string.format( [[ const foo = "bar" - console.log("%s%s|") - console.log("%s%s|") + console.log("%s%s") + console.log("%s%s") ]], watcher.MARKER, id1, @@ -80,8 +80,8 @@ describe("timber.buffers autocmd", function() input = string.format( [[ const foo = "bar" - console.log("%s%s|") - console.log("%s%s|") + console.log("%s%s") + console.log("%s%s") const bar = "foo" ]], watcher.MARKER, @@ -113,8 +113,8 @@ describe("timber.buffers autocmd", function() input = string.format( [[ const foo = "bar" - console.log("%s%s|") - console.log("%s%s|") + console.log("%s%s") + console.log("%s%s") const bar = "foo" ]], watcher.MARKER, @@ -157,7 +157,7 @@ describe("timber.buffers autocmd", function() filetype = "typescript", action = function() helper.wait(20) - vim.fn.setreg("a", string.format([[console.log("%s%s|")]], watcher.MARKER, id2), "V") + vim.fn.setreg("a", string.format([[console.log("%s%s")]], watcher.MARKER, id2), "V") vim.cmd([[normal! 2G"ap]]) helper.wait(20) end, @@ -183,7 +183,7 @@ describe("timber.buffers autocmd", function() ]], filetype = "typescript", action = function() - vim.fn.setreg("a", string.format([[console.log("%s%s|")]], watcher.MARKER, id), "V") + vim.fn.setreg("a", string.format([[console.log("%s%s")]], watcher.MARKER, id2), "V") vim.cmd([[normal! "ap]]) helper.wait(20) end, @@ -205,7 +205,7 @@ describe("timber.buffers autocmd", function() input = string.format( [[ const foo = "bar" - console.log("%s%s|") + console.log("%s%s") ]], watcher.MARKER, id @@ -233,7 +233,7 @@ describe("timber.buffers autocmd", function() input = string.format( [[ const foo = "bar" - console.log("%s%s|") + console.log("%s%s") ]], watcher.MARKER, id @@ -273,21 +273,29 @@ describe("timber.buffers.new_log_placeholder", function() it("adds the placeholder to the registry", function() local id = watcher.generate_unique_id() - buffers._new_log_placeholder({ id = id, bufnr = 1, line = 1, entries = {} }) - assert.is.Not.Nil(buffers.log_placeholders:get(id)) + helper.assert_scenario({ + input = string.format([[console.log("%s%s")]], watcher.MARKER, id), + filetype = "typescript", + action = function() + buffers._new_log_placeholder({ id = id, bufnr = 0, line = 0, entries = {} }) + end, + expected = function() + assert.is.Not.Nil(buffers.log_placeholders:get(id)) + end, + }) end) it("attaches to the buffer and reacts to buffer changes", function() local id = watcher.generate_unique_id() helper.assert_scenario({ - input = [[const fo|o = "bar"]], + input = string.format([[console.log("%sfoo")]], watcher.MARKER), filetype = "typescript", action = function() local bufnr = vim.api.nvim_get_current_buf() buffers._new_log_placeholder({ id = "foo", bufnr = bufnr, line = 0, entries = {} }) - vim.fn.setreg("a", string.format([[console.log("%s%s|")]], watcher.MARKER, id), "V") + vim.fn.setreg("a", string.format([[console.log("%s%s")]], watcher.MARKER, id), "V") vim.cmd([[normal! "ap]]) helper.wait(20) vim.cmd("normal! 1Gdd") @@ -330,7 +338,7 @@ describe("timber.buffers._receive_log_entry", function() input = string.format( [[ const foo = "bar" - console.log("%s%s|") + console.log("%s%s") const bar = "foo" ]], watcher.MARKER,