Recipes
December 9, 2024 ยท View on GitHub
The purpose of this document is to showcase some common use cases of timber.nvim.
Advanced logging use cases
Log current line
Show config
vim.keymap.set("n", "gll", function()
return require("timber.actions").insert_log({ position = "below", operator = true }) .. "_"
end, {
desc = "[G]o [L]og: Insert below log statements the current line",
expr = true,
})
Surround log statements
Show config
vim.keymap.set("n", "gls", function()
require("timber.actions").insert_log({
templates = { before = "default", after = "default" },
position = "surround",
})
end, { desc = "[G]o [L]og [S]urround: Insert surround log statements below the current line" })
This will insert two log statements, one above and one below for the target at the cursor. This is useful in scenarios when functions mutate the variables and you want to track the changes:
-- "|" denotes the cursor position
-- Before
mutate_foo(fo|o)
-- After
print("foo", foo)
mutate_foo(fo|o)
print("foo", foo)
Time log statements
Show config
require("timber").setup({
log_templates = {
time_start = {
lua = [[local _start = os.time()]],
},
time_end = {
lua = [[print("Elapsed time: " .. tostring(os.time() - _start) .. " seconds")]],
},
},
})
vim.keymap.set("n", "glt", function()
require("timber.actions").insert_log({
templates = { before = "time_start", after = "time_end" },
position = "surround",
})
end, {
desc = "[G]o [L]og [T]ime: Insert a time log statement surround the cursor position",
})
This is useful when you want to measure execution time of a code segment.
-- "|" denotes the cursor position
-- Before
long_operation(fo|o)
-- After
local _start = os.time()
long_operation(fo|o)
print("Elapsed time: " .. tostring(os.time() - _start) .. " seconds")
Pretty captured log buffer
You can add syntax highlighting the captured log buffer. Here are some examples:
Javascript (console.log)
- Use
javascriptsyntax for the log buffer
-- After
opts = {
watcher = {
sources = {
enabled = true,
javascript_log = {
type = "filesystem",
name = "Log file",
path = "/tmp/debug.log",
buffer = {
syntax = "javascript",
}
}
}
}
}
- Optionally, you can extend the syntax file. For example, in your config root,
after/syntax/javascript.vim
syntax sync fromstart
" Special characters
syntax region consoleLogObject start="{" end="}" fold transparent contains=ALL
" Keywords
syntax match consoleLogSpecial "\V\([Promise]\|[Function]\|[Reference]\|[Circular]\)"
syntax match consoleLogSpecial "\[Array(\d\+)\]"
syntax match consoleLogSpecial "Symbol(.\+)"
" Object keys
syntax match consoleLogObjectKey "\<\w\+\>:" contained contains=consoleLogColon
syntax match consoleLogColon ":" contained
" Define highlighting
highlight default link consoleLogSpecial Type
highlight default link consoleLogObjectKey Identifier
highlight default link consoleLogColon Operator