Skip to content

Commit

Permalink
feat(tsx): support tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Nov 21, 2024
1 parent f3171eb commit e13306b
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 17 deletions.
22 changes: 6 additions & 16 deletions lua/neolog/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ local function query_log_target_container(lang, range)
local tree = parser:parse()[1]
local root = tree:root()

local query = vim.treesitter.query.get(lang, "neolog")
local query = vim.treesitter.query.get(lang, "neolog-log-container")
if not query then
vim.notify(string.format("logging_framework doesn't support %s language", lang), vim.log.levels.ERROR)
return {}
Expand Down Expand Up @@ -137,15 +137,11 @@ end
---@param lang string
---@return TSNode[]
local function find_log_target(container, lang)
local query = vim.treesitter.query.parse(
lang,
[[
([
(identifier)
(shorthand_property_identifier_pattern)
]) @log_target
]]
)
local query = vim.treesitter.query.get(lang, "neolog-log-target")
if not query then
vim.notify(string.format("logging_framework doesn't support %s language 1", lang), vim.log.levels.ERROR)
return {}
end

local bufnr = vim.api.nvim_get_current_buf()
local log_targets = {}
Expand All @@ -167,12 +163,6 @@ function M.add_log(label_template, position)
return
end

local query = vim.treesitter.query.get(lang, "neolog")
if not query then
vim.notify(string.format("logging_framework doesn't support %s language", lang), vim.log.levels.ERROR)
return
end

local template = M.log_templates[lang]
if not template then
vim.notify(string.format("Log template for %s language is not found", lang), vim.log.levels.ERROR)
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions queries/tsx/neolog-log-target.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; Outside of jsx element
(
([
(identifier)
(shorthand_property_identifier_pattern)
]) @log_target
(#not-has-ancestor? @log_target jsx_element)
(#not-has-ancestor? @log_target jsx_self_closing_element)
)

; Inside of jsx expression but ignore opening and closing tags
(
([
(identifier)
(shorthand_property_identifier_pattern)
]) @log_target
(#has-ancestor? @log_target jsx_expression)
(#not-has-parent? @log_target jsx_opening_element)
(#not-has-parent? @log_target jsx_closing_element)
(#not-has-parent? @log_target jsx_self_closing_element)
)
File renamed without changes.
4 changes: 4 additions & 0 deletions queries/typescript/neolog-log-target.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
([
(identifier)
(shorthand_property_identifier_pattern)
]) @log_target
195 changes: 195 additions & 0 deletions tests/neolog/actions/lang/tsx_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
local neolog = require("neolog")
local helper = require("tests.neolog.helper")

describe("typescript", function()
before_each(function()
neolog.setup()
end)

it("supports jsx expression", function()
local actions = require("neolog.actions")

local input = [[
function foo() {
const a = 1
return (
<div>
<div>{a| + 1}</div>
</div>
)
}
]]

local expected = [[
function foo() {
const a = 1
console.log("a", a)
return (
<div>
<div>{a + 1}</div>
</div>
)
}
]]

helper.assert_scenario({
input = input,
filetype = "typescriptreact",
action = function()
actions.add_log("%identifier", "above")
end,
expected = expected,
})

input = [[
function foo() {
const a = 1
const el = (
<div>
<div>{a| + 1}</div>
</div>
)
}
]]

expected = [[
function foo() {
const a = 1
const el = (
<div>
<div>{a + 1}</div>
</div>
)
console.log("a", a)
}
]]

helper.assert_scenario({
input = input,
filetype = "typescriptreact",
action = function()
actions.add_log("%identifier", "below")
end,
expected = expected,
})
end)

it("supports jsx attribute", function()
local actions = require("neolog.actions")

local input = [[
function foo() {
return (
<div className={a|}>
<div>{b + 1}</div>
</div>
)
}
]]

local expected = [[
function foo() {
console.log("a", a)
return (
<div className={a|}>
<div>{b + 1}</div>
</div>
)
}
]]


helper.assert_scenario({
input = input,
filetype = "typescriptreact",
action = function()
actions.add_log("%identifier", "above")
end,
expected = expected,
})
end)

it("supports visual selection log", function()
local actions = require("neolog.actions")

local input = [[
function foo() {
const a = 1
const b = 1
return (
<div>
<div>{a| + b}</div>
</div>
)
}
]]

local expected = [[
function foo() {
const a = 1
const b = 1
console.log("a", a)
console.log("b", b)
return (
<div>
<div>{a + b}</div>
</div>
)
}
]]

helper.assert_scenario({
input = input,
filetype = "typescriptreact",
action = function()
vim.cmd("normal! vi{")
actions.add_log("%identifier", "above")
end,
expected = expected,
})

input = [[
function foo() {
const a = 1
const b = true
const el = (
<div>
{b && <div>{|a + 1}</div>}
<input className={c} />
</div>
)
}
]]

-- TODO: figure out why indentation is off with inner jsx element
expected = [[
function foo() {
const a = 1
const b = true
const el = (
<div>
{b && <div>{|a + 1}</div>}
<input className={c} />
</div>
)
console.log("b", b)
console.log("a", a)
console.log("c", c)
}
]]

helper.assert_scenario({
input = input,
filetype = "typescriptreact",
action = function()
vim.cmd("normal! Vj")
actions.add_log("%identifier", "below")
end,
expected = expected,
})
end)
end)
5 changes: 4 additions & 1 deletion tests/neolog/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ local function parse_input(input)

for _, line in ipairs(lines) do
-- Count the number of leading whitespaces
-- Don't consider indent of empty lines
local leading_whitespaces = line:match("^%s*")
smallest_indent = smallest_indent and math.min(smallest_indent, #leading_whitespaces) or #leading_whitespaces
if #leading_whitespaces ~= line:len() then
smallest_indent = smallest_indent and math.min(smallest_indent, #leading_whitespaces) or #leading_whitespaces
end
end

local cursor
Expand Down

0 comments on commit e13306b

Please sign in to comment.