Skip to content

Commit

Permalink
perf: add a tiny bit of type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Apr 19, 2023
1 parent 79a0da1 commit 8c5d947
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lua/template-literal-comments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ function M.setup()
vim.treesitter.query.add_directive("set-template-literal-lang-from-comment!", function(match, _, bufnr, pred, metadata)
local comment_node = match[pred[2]]
if comment_node then
local comment = vim.treesitter.get_node_text(comment_node, bufnr)
local tag = comment:match'/%*%s*(%w+)%s*%*/'
if tag then
local language = tag:lower() == 'svg' and 'html'
or vim.filetype.match { filename = 'a.'..tag }
or tag:lower()
metadata.language = language
local success, comment = pcall(vim.treesitter.get_node_text, comment_node, bufnr)
if success then
local tag = comment:match'/%*%s*(%w+)%s*%*/'
if tag then
local language = tag:lower() == 'svg' and 'html'
or vim.filetype.match { filename = 'a.'..tag }
or tag:lower()
metadata.language = language
end
end
end
end)
Expand Down

0 comments on commit 8c5d947

Please # to comment.