Skip to content

Commit

Permalink
feat: handle more log cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Nov 29, 2024
1 parent 0584dba commit 2e56b52
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 65 deletions.
13 changes: 12 additions & 1 deletion queries/elixir/timber-log-container.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
target: (identifier) @function_name
(arguments) @log_container
(do_block) @block
(#any-of? @function_name "def" "defp" "defmacro" "if" "unless" "for" "with")
(#any-of? @function_name "def" "defp" "defmacro")
(#make-logable-range! @block "inner" 1 - 1)
)

(
(call
target: (identifier) @function_name
(arguments) @log_container
(do_block) @block
(#any-of? @function_name "if" "unless" "for" "with")
(#make-logable-range! @block "inner" 1 - 1)
) @a
(#make-logable-range! @a "before")
)

(
(call
target: (identifier) @function_name
Expand Down
11 changes: 7 additions & 4 deletions queries/go/timber-log-container.scm
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@
(#make-logable-range! @block "inner" 1 -1)
)

(if_statement
condition: (_) @log_container
consequence: (block) @block
(#make-logable-range! @block "inner" 1 -1)
(
(if_statement
condition: (_) @log_container
consequence: (block) @block
(#make-logable-range! @block "inner" 1 -1)
) @a
(#make-logable-range! @a "before")
)

(for_statement
Expand Down
24 changes: 15 additions & 9 deletions queries/lua/timber-log-container.scm
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@
(#make-logable-range! @a "inner")
)

(if_statement
condition: (_) @log_container
consequence: (block) @a
(#make-logable-range! @a "inner")
)

(if_statement
alternative: (elseif_statement
(
(if_statement
condition: (_) @log_container
consequence: (block) @a
(#make-logable-range! @a "inner")
)
) @b
(#make-logable-range! @b "before")
)

(
(if_statement
alternative: (elseif_statement
condition: (_) @log_container
consequence: (block) @a
(#make-logable-range! @a "inner")
)
) @b
(#make-logable-range! @b "before")
)

(for_statement
Expand Down
33 changes: 21 additions & 12 deletions queries/python/timber-log-container.scm
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
(#make-logable-range! @a "inner" 1 -1)
)

(if_statement
condition: (_) @log_container
consequence: (block) @a
(#make-logable-range! @a "inner")
(
(if_statement
condition: (_) @log_container
consequence: (block) @a
(#make-logable-range! @a "inner")
) @b
(#make-logable-range! @b "before")
)

(elif_clause
Expand All @@ -33,10 +36,13 @@
(#make-logable-range! @a "inner")
)

(for_statement
right: (_) @log_container
body: (block) @a
(#make-logable-range! @a "inner")
(
(for_statement
right: (_) @log_container
body: (block) @a
(#make-logable-range! @a "inner")
) @b
(#make-logable-range! @b "before")
)

(with_statement
Expand All @@ -45,10 +51,13 @@
(#make-logable-range! @a "inner")
)

(while_statement
condition: (_) @log_container
body: (block) @a
(#make-logable-range! @a "inner")
(
(while_statement
condition: (_) @log_container
body: (block) @a
(#make-logable-range! @a "inner")
) @b
(#make-logable-range! @b "before")
)

(
Expand Down
64 changes: 36 additions & 28 deletions queries/ruby/timber-log-container.scm
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,57 @@
(#make-logable-range! @a "inner")
)

(if
condition: (_) @log_container
consequence: (then) @a
(#make-logable-range! @a "inner")
)

(if
alternative: (elsif
(
(if
condition: (_) @log_container
consequence: (then) @a
(#make-logable-range! @a "inner" 1 -1)
)
(#make-logable-range! @a "inner")
) @b
(#make-logable-range! @b "before")
)

(unless
condition: (_) @log_container
consequence: (then) @a
(#make-logable-range! @a "inner")
(
(if
alternative: (elsif
condition: (_) @log_container
consequence: (then) @a
(#make-logable-range! @a "inner" 1 -1)
)
) @b
(#make-logable-range! @b "before")
)

(for
pattern: (identifier) @log_container
body: (do) @a
(#make-logable-range! @a "inner" 1 -1)
(
(unless
condition: (_) @log_container
consequence: (then) @a
(#make-logable-range! @a "inner")
) @b
(#make-logable-range! @b "before")
)

(for
value: (_) @log_container
pattern: (identifier) @log_container
body: (do) @a
(#make-logable-range! @a "inner")
(#make-logable-range! @a "inner" 1 -1)
)

(while
condition: (_) @log_container
body: (do) @a
(#make-logable-range! @a "inner")
(
(while
condition: (_) @log_container
body: (do) @a
(#make-logable-range! @a "inner")
) @b
(#make-logable-range! @b "before")
)

(until
condition: (_) @log_container
body: (do) @a
(#make-logable-range! @a "inner")
(
(until
condition: (_) @log_container
body: (do) @a
(#make-logable-range! @a "inner")
) @b
(#make-logable-range! @b "before")
)

(call
Expand Down
22 changes: 14 additions & 8 deletions queries/rust/timber-log-container.scm
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
(#make-logable-range! @block "inner" 1 -1)
)

(if_expression
condition: (_) @log_container
consequence: (block) @block
(#make-logable-range! @block "inner" 1 -1)
(
(if_expression
condition: (_) @log_container
consequence: (block) @block
(#make-logable-range! @block "inner" 1 -1)
) @a
(#make-logable-range! @a "before")
)

(
Expand Down Expand Up @@ -54,10 +57,13 @@
(#make-logable-range! @block "inner" 1 -1)
)

(while_expression
condition: (_) @log_container
body: (block) @block
(#make-logable-range! @block "inner" 1 -1)
(
(while_expression
condition: (_) @log_container
body: (block) @block
(#make-logable-range! @block "inner" 1 -1)
) @a
(#make-logable-range! @a "before")
)

(
Expand Down
60 changes: 59 additions & 1 deletion tests/timber/actions/lang/elixir_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,31 @@ describe("elixir single log", function()
end
]],
})

helper.assert_scenario({
input = [[
if not fo|o > 1 and bar < baz do
foo + bar
else
foo - baz
end
]],
filetype = "elixir",
action = function()
vim.cmd("normal! V")
actions.insert_log({ position = "above" })
end,
expected = [[
IO.inspect(foo, label: "foo")
IO.inspect(bar, label: "bar")
IO.inspect(baz, label: "baz")
if not foo > 1 and bar < baz do
foo + bar
else
foo - baz
end
]],
})
end)

it("supports unless expression", function()
Expand Down Expand Up @@ -357,6 +382,31 @@ describe("elixir single log", function()
end
]],
})

helper.assert_scenario({
input = [[
unless not fo|o > 1 and bar < baz do
foo + bar
else
foo - baz
end
]],
filetype = "elixir",
action = function()
vim.cmd("normal! V")
actions.insert_log({ position = "above" })
end,
expected = [[
IO.inspect(foo, label: "foo")
IO.inspect(bar, label: "bar")
IO.inspect(baz, label: "baz")
unless not foo > 1 and bar < baz do
foo + bar
else
foo - baz
end
]],
})
end)

it("supports case expression", function()
Expand All @@ -371,10 +421,12 @@ describe("elixir single log", function()
]],
filetype = "elixir",
action = function()
actions.insert_log({ position = "above" })
vim.cmd("normal! vap")
actions.insert_log({ position = "below" })
end,
expected = [[
IO.inspect(foo, label: "foo")
case foo do
^bar ->
IO.inspect(bar, label: "bar")
Expand Down Expand Up @@ -402,10 +454,12 @@ describe("elixir single log", function()
]],
filetype = "elixir",
action = function()
actions.insert_log({ position = "above" })
vim.cmd("normal! vap")
actions.insert_log({ position = "below" })
end,
expected = [[
IO.inspect(foo, label: "foo")
cond foo do
bar > 1 ->
IO.inspect(bar, label: "bar")
Expand All @@ -425,7 +479,7 @@ describe("elixir single log", function()
helper.assert_scenario({
input = [[
with (
%{bar: b|ar} <- foo,
%{bar: bar} <- fo|o,
%{baz: baz} <- bar
) do
nil
Expand All @@ -436,10 +490,12 @@ describe("elixir single log", function()
]],
filetype = "elixir",
action = function()
actions.insert_log({ position = "above" })
vim.cmd("normal! vap")
actions.insert_log({ position = "below" })
end,
expected = [[
IO.inspect(foo, label: "foo")
with (
%{bar: bar} <- foo,
%{baz: baz} <- bar
Expand Down Expand Up @@ -468,10 +524,12 @@ describe("elixir single log", function()
]],
filetype = "elixir",
action = function()
actions.insert_log({ position = "above" })
vim.cmd("normal! V")
actions.insert_log({ position = "below" })
end,
expected = [[
IO.inspect(foo, label: "foo")
for foo <- 1..5, bar <- 1..3, foo > 3 do
IO.inspect(foo, label: "foo")
IO.inspect(bar, label: "bar")
Expand Down
Loading

0 comments on commit 2e56b52

Please sign in to comment.