Skip to content

Commit

Permalink
feat(watcher): remove | in the %watcher_marker
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Dec 19, 2024
1 parent a47f09b commit d25e1a8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lua/timber/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/timber/watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lua/timber/watcher/sources/filesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lua/timber/watcher/sources/neotest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 23 additions & 15 deletions tests/timber/timber_buffers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d25e1a8

Please sign in to comment.