Skip to content

Commit

Permalink
feat!(template): rename %identifier to %log_target
Browse files Browse the repository at this point in the history
The placeholder %identifier is a term adapted from Javascript language syntax. We'd like to use a more generic term
  • Loading branch information
Goose97 committed Nov 25, 2024
1 parent b103dad commit a5ce3b4
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 88 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ The log statements can be inserted via APIs. See [`:h timber.nvim-actions.api`](
The content of the log statement is specified via templates. See [`:h timber.nvim-config.log-templates`](https://github.com/Goose97/timber.nvim/blob/main/doc/timber.nvim.txt) for more information.

```lua
-- Template: [[print("LOG %line_number %identifier", %identifier)]]
-- Template: [[print("LOG %line_number %log_target", %log_target)]]
local foo = 1
print("LOG 1 foo", foo)
```
Expand All @@ -119,7 +119,7 @@ Here's an example configuration:
require("timber").setup({
log_templates = {
default = {
lua = [[print("%watcher_marker_start" .. %identifier .. "%watcher_marker_end")]],
lua = [[print("%watcher_marker_start" .. %log_target .. "%watcher_marker_end")]],
},
},
log_watcher = {
Expand Down
36 changes: 18 additions & 18 deletions doc/timber.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ actions.insert_log({opts}) *timber.actions.insert_log()*

Insert a log statement. The behavior depends on the template provided:

- If the template contains `%identifier` placeholder, capture the log
- If the template contains `%log_target` placeholder, capture the log
target at the cursor position.
- If the template contains NO `%identifier` placeholder, insert a log
- If the template contains NO `%log_target` placeholder, insert a log
statement at the below and above line, depending on the `position` option.

Parameters: ~
Expand Down Expand Up @@ -270,7 +270,7 @@ by the watcher. Users can use the placeholder `%watcher_marker_start` and

For example, this log template:
>lua
[[print("%watcher_marker_start" .. %identifier .. "%watcher_marker_end")]]
[[print("%watcher_marker_start" .. %log_target .. "%watcher_marker_end")]]
<
Will be expanded to:
>lua
Expand Down Expand Up @@ -327,7 +327,7 @@ between these two markers.
require("timber").setup({
log_templates = {
default = {
lua = [[print("%watcher_marker_start" .. %identifier .. "%watcher_marker_end")]]
lua = [[print("%watcher_marker_start" .. %log_target .. "%watcher_marker_end")]]
}
}
})
Expand Down Expand Up @@ -392,20 +392,20 @@ timber.setup({opts}) *timber.setup()*
{
log_templates = {
default = {
javascript = [[console.log("%identifier", %identifier)]],
typescript = [[console.log("%identifier", %identifier)]],
jsx = [[console.log("%identifier", %identifier)]],
tsx = [[console.log("%identifier", %identifier)]],
lua = [[print("%identifier", %identifier)]],
javascript = [[console.log("%log_target", %log_target)]],
typescript = [[console.log("%log_target", %log_target)]],
jsx = [[console.log("%log_target", %log_target)]],
tsx = [[console.log("%log_target", %log_target)]],
lua = [[print("%log_target", %log_target)]],
},
},
batch_log_templates = {
default = {
javascript = [[console.log({ %repeat<"%identifier": %identifier><, > })]],
typescript = [[console.log({ %repeat<"%identifier": %identifier><, > })]],
jsx = [[console.log({ %repeat<"%identifier": %identifier><, > })]],
tsx = [[console.log({ %repeat<"%identifier": %identifier><, > })]],
lua = [[print(string.format("%repeat<%identifier=%s><, >", %repeat<%identifier><, >))]],
javascript = [[console.log({ %repeat<"%log_target": %log_target><, > })]],
typescript = [[console.log({ %repeat<"%log_target": %log_target><, > })]],
jsx = [[console.log({ %repeat<"%log_target": %log_target><, > })]],
tsx = [[console.log({ %repeat<"%log_target": %log_target><, > })]],
lua = [[print(string.format("%repeat<%log_target=%s><, >", %repeat<%log_target><, >))]],
},
},
keymaps = {
Expand Down Expand Up @@ -448,17 +448,17 @@ Log template string supports special placeholders that will be expanded during
runtime to provide more information about the log context. The supported
placeholders are:

- `%identifier`: the identifier text >lua
- `%log_target`: the identifier text >lua

-- Template: [[print("LOG %identifier", %identifier)]]
-- Template: [[print("LOG %log_target", %log_target)]]
local foo = 1
print("LOG foo", foo)
<
- `%line_number`: the line number of the log target. If the log template
doesn't have a log target, line_number will be the current line at the
cursor >lua

-- Template: [[print("LOG %line_number", %identifier)]]
-- Template: [[print("LOG %line_number", %log_target)]]
local foo = 1
print("LOG 1", foo)
<
Expand All @@ -469,7 +469,7 @@ all placeholders supported by `log_templates`, plus:
batch. The format is: `%repeat<content><separator>`. Inside `content`, you
can use all placeholders supported by `log_templates`. >lua

-- Template: [[print(string.format("LOG %repeat<%identifier=%s><, >", %repeat<%identifier><, >))]]
-- Template: [[print(string.format("LOG %repeat<%log_target=%s><, >", %repeat<%log_target><, >))]]
local foo = bar + baz
print("LOG foo=%s, bar=%s, baz=%s", foo, bar, baz)
<
Expand Down
16 changes: 8 additions & 8 deletions lua/timber/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ local function get_current_indent(insert_line)
end

--- Build the log statement from template. Support special placeholers:
--- %identifier: the identifier text
--- %log_target: the identifier text
--- %line_number: the line_number number
--- %insert_cursor: after inserting the log statement, go to insert mode and place the cursor here.
--- If there's multiple log statements, choose the first one
Expand All @@ -97,9 +97,9 @@ local function resolve_template_placeholders(log_template, handlers)
end
end

if string.find(log_template, "%%identifier") then
if string.find(log_template, "%%log_target") then
local replacement = invoke_handler("identifier")
log_template = string.gsub(log_template, "%%identifier", replacement)
log_template = string.gsub(log_template, "%%log_target", replacement)
end

if string.find(log_template, "%%line_number") then
Expand Down Expand Up @@ -500,8 +500,8 @@ local function build_batch_log_statement(log_template, batch, insert_line)
-- Then resolve the rest
local content, placeholder_id = resolve_template_placeholders(result, {
identifier = function()
utils.notify("Cannot use %identifier placeholder outside %repeat placeholder", "error")
return "%identifier"
utils.notify("Cannot use %log_target placeholder outside %repeat placeholder", "error")
return "%log_target"
end,
line_number = tostring(insert_line + 1),
})
Expand Down Expand Up @@ -536,11 +536,11 @@ function M.__insert_log(motion_type)
end

-- There are two kinds of log statements:
-- 1. Capture log statements: log statements that contain %identifier placeholder
-- 1. Capture log statements: log statements that contain %log_target placeholder
-- We need to capture the log target in the selection range and replace it
-- 2. Non-capture log statements: log statements that don't contain %identifier placeholder
-- 2. Non-capture log statements: log statements that don't contain %log_target placeholder
-- We simply replace the placeholder text
return log_template_lang:find("%%identifier")
return log_template_lang:find("%%log_target")
and build_capture_log_statements(log_template_lang, lang, position, selection_range)
or { build_non_capture_log_statement(log_template_lang, position) }
end
Expand Down
36 changes: 18 additions & 18 deletions lua/timber/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ local utils = require("timber.utils")
local default_config = {
log_templates = {
default = {
javascript = [[console.log("%identifier", %identifier)]],
typescript = [[console.log("%identifier", %identifier)]],
jsx = [[console.log("%identifier", %identifier)]],
tsx = [[console.log("%identifier", %identifier)]],
lua = [[print("%identifier", %identifier)]],
ruby = [[puts("%identifier #{%identifier}")]],
elixir = [[IO.inspect(%identifier, label: "%identifier")]],
go = [[log.Printf("%identifier: %v\n", %identifier)]],
rust = [[println!("%identifier: {:#?}", %identifier);]],
javascript = [[console.log("%log_target", %log_target)]],
typescript = [[console.log("%log_target", %log_target)]],
jsx = [[console.log("%log_target", %log_target)]],
tsx = [[console.log("%log_target", %log_target)]],
lua = [[print("%log_target", %log_target)]],
ruby = [[puts("%log_target #{%log_target}")]],
elixir = [[IO.inspect(%log_target, label: "%log_target")]],
go = [[log.Printf("%log_target: %v\n", %log_target)]],
rust = [[println!("%log_target: {:#?}", %log_target);]],
},
},
batch_log_templates = {
default = {
javascript = [[console.log({ %repeat<"%identifier": %identifier><, > })]],
typescript = [[console.log({ %repeat<"%identifier": %identifier><, > })]],
jsx = [[console.log({ %repeat<"%identifier": %identifier><, > })]],
tsx = [[console.log({ %repeat<"%identifier": %identifier><, > })]],
lua = [[print(string.format("%repeat<%identifier=%s><, >", %repeat<%identifier><, >))]],
ruby = [[puts("%repeat<%identifier: #{%identifier}><, >")]],
elixir = [[IO.inspect({ %repeat<%identifier><, > })]],
go = [[log.Printf("%repeat<%identifier: %v><, >\n", %repeat<%identifier><, >)]],
rust = [[println!("%repeat<%identifier: {:#?}><, >", %repeat<%identifier><, >);]],
javascript = [[console.log({ %repeat<"%log_target": %log_target><, > })]],
typescript = [[console.log({ %repeat<"%log_target": %log_target><, > })]],
jsx = [[console.log({ %repeat<"%log_target": %log_target><, > })]],
tsx = [[console.log({ %repeat<"%log_target": %log_target><, > })]],
lua = [[print(string.format("%repeat<%log_target=%s><, >", %repeat<%log_target><, >))]],
ruby = [[puts("%repeat<%log_target: #{%log_target}><, >")]],
elixir = [[IO.inspect({ %repeat<%log_target><, > })]],
go = [[log.Printf("%repeat<%log_target: %v><, >\n", %repeat<%log_target><, >)]],
rust = [[println!("%repeat<%log_target: {:#?}><, >", %repeat<%log_target><, >);]],
},
},
highlight = {
Expand Down
4 changes: 2 additions & 2 deletions tests/timber/actions/lang/elixir_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("elixir single log", function()
timber.setup({
log_templates = {
default = {
elixir = [[IO.inspect(%identifier, label: "%identifier")]],
elixir = [[IO.inspect(%log_target, label: "%log_target")]],
},
},
})
Expand Down Expand Up @@ -771,7 +771,7 @@ describe("elixir batch log", function()
timber.setup({
batch_log_templates = {
default = {
elixir = [[IO.inspect({ %repeat<%identifier><, > })]],
elixir = [[IO.inspect({ %repeat<%log_target><, > })]],
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions tests/timber/actions/lang/go_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("go single log", function()
timber.setup({
log_templates = {
default = {
go = [[log.Printf("%identifier: %v\n", %identifier)]],
go = [[log.Printf("%log_target: %v\n", %log_target)]],
},
},
})
Expand Down Expand Up @@ -620,7 +620,7 @@ describe("go batch log", function()
timber.setup({
batch_log_templates = {
default = {
go = [[log.Printf("%repeat<%identifier: %v><, >\n", %repeat<%identifier><, >)]],
go = [[log.Printf("%repeat<%log_target: %v><, >\n", %repeat<%log_target><, >)]],
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions tests/timber/actions/lang/javascript_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("javascript single log", function()
timber.setup({
log_templates = {
default = {
javascript = [[console.log("%identifier", %identifier)]],
javascript = [[console.log("%log_target", %log_target)]],
},
},
})
Expand All @@ -25,7 +25,7 @@ describe("javascript batch log", function()
timber.setup({
batch_log_templates = {
default = {
javascript = [[console.log("Testing %line_number", { %repeat<"%identifier": %identifier><, > })]],
javascript = [[console.log("Testing %line_number", { %repeat<"%log_target": %log_target><, > })]],
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion tests/timber/actions/lang/jsx_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("javascriptreact", function()
timber.setup({
log_templates = {
default = {
jsx = [[console.log("%identifier", %identifier)]],
jsx = [[console.log("%log_target", %log_target)]],
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions tests/timber/actions/lang/lua_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("lua single log", function()
timber.setup({
log_templates = {
default = {
lua = [[print("%identifier", %identifier)]],
lua = [[print("%log_target", %log_target)]],
},
},
})
Expand Down Expand Up @@ -717,7 +717,7 @@ describe("lua batch log", function()
timber.setup({
batch_log_templates = {
default = {
lua = [[print(string.format("%repeat<%identifier=%s><, >", %repeat<%identifier><, >))]],
lua = [[print(string.format("%repeat<%log_target=%s><, >", %repeat<%log_target><, >))]],
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions tests/timber/actions/lang/ruby_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("ruby single log", function()
timber.setup({
log_templates = {
default = {
ruby = [[puts("%identifier #{%identifier}")]],
ruby = [[puts("%log_target #{%log_target}")]],
},
},
})
Expand Down Expand Up @@ -977,7 +977,7 @@ describe("ruby batch log", function()
timber.setup({
batch_log_templates = {
default = {
ruby = [[puts("%repeat<%identifier: #{%identifier}><, >")]],
ruby = [[puts("%repeat<%log_target: #{%log_target}><, >")]],
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions tests/timber/actions/lang/rust_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("rust single log", function()
timber.setup({
log_templates = {
default = {
rust = [[println!("%identifier: {:#?}", %identifier);]],
rust = [[println!("%log_target: {:#?}", %log_target);]],
},
},
})
Expand Down Expand Up @@ -701,7 +701,7 @@ describe("rust batch log", function()
timber.setup({
batch_log_templates = {
default = {
rust = [[println!("%repeat<%identifier: {:#?}><, >", %repeat<%identifier><, >);]],
rust = [[println!("%repeat<%log_target: {:#?}><, >", %repeat<%log_target><, >);]],
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion tests/timber/actions/lang/tsx_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("typescriptreact", function()
timber.setup({
log_templates = {
default = {
tsx = [[console.log("%identifier", %identifier)]],
tsx = [[console.log("%log_target", %log_target)]],
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions tests/timber/actions/lang/typescript_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("typescript single log", function()
timber.setup({
log_templates = {
default = {
typescript = [[console.log("%identifier", %identifier)]],
typescript = [[console.log("%log_target", %log_target)]],
},
},
})
Expand All @@ -25,7 +25,7 @@ describe("typescript batch log", function()
timber.setup({
batch_log_templates = {
default = {
typescript = [[console.log("Testing %line_number", { %repeat<"%identifier": %identifier><, > })]],
typescript = [[console.log("Testing %line_number", { %repeat<"%log_target": %log_target><, > })]],
},
},
})
Expand Down
Loading

0 comments on commit a5ce3b4

Please sign in to comment.