From 8c5d947d07468b8f7eaff6f0f005159b791ade1b Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Wed, 19 Apr 2023 07:41:38 +0300 Subject: [PATCH] perf: add a tiny bit of type safety --- lua/template-literal-comments.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lua/template-literal-comments.lua b/lua/template-literal-comments.lua index f0c7042..75aa8f0 100644 --- a/lua/template-literal-comments.lua +++ b/lua/template-literal-comments.lua @@ -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)