Skip to content
This repository was archived by the owner on May 23, 2025. It is now read-only.

Commit 1d7981b

Browse files
author
Cristhian Melo
committed
feat: refactor all code and enable auto setup
1 parent f354654 commit 1d7981b

File tree

8 files changed

+103
-84
lines changed

8 files changed

+103
-84
lines changed

lua/template-string.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
local M = {}
22

3-
local debounce = require("utils.debounce")
4-
local quotes = require("utils.replace_quotes")
3+
local debounce = require("template-string.debounce")
4+
local quotes = require("template-string.replace_quotes")
55

6-
--- Configures the plugin behavior.
6+
--- Configures the behavior of the plugin.
77
function M.setup()
8+
-- Create a debounced version of the replace_quotes_in_line function
89
local debounced_replace = debounce.debounce(quotes.replace_quotes_in_line, 100)
10+
11+
-- Set up an autocmd to trigger the debounced function on TextChanged and TextChangedI events
912
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
1013
callback = debounced_replace,
1114
})
1215
end
1316

17+
--- @export
1418
return M

lua/utils/allowed_filetypes.lua renamed to lua/template-string/allowed_filetypes.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ local vim = vim
22

33
local M = {}
44

5+
-- List of allowed file types for syntax checking
56
local allowed_filetypes = {
67
javascript = true,
78
typescript = true,
89
javascriptreact = true,
910
typescriptreact = true,
1011
}
1112

13+
--- Checks if the current buffer's file type is allowed for syntax checking.
14+
-- @return boolean indicating if the file type is allowed.
1215
function M.is_allowed_filetype()
1316
local filetype = vim.bo.filetype
1417
return allowed_filetypes[filetype] or false

lua/template-string/debounce.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local vim = vim
2+
3+
local M = {}
4+
5+
--- Creates a debounced version of a function.
6+
-- @param fn The function to debounce.
7+
-- @param ms The debounce time in milliseconds.
8+
-- @return The debounced function.
9+
function M.debounce(fn, ms)
10+
local timer = vim.loop.new_timer() -- Create a new timer from Vim's event loop
11+
12+
return function(...)
13+
timer:stop() -- Stop the timer to reset debounce time
14+
local argv = { ... } -- Capture arguments passed to the debounced function
15+
16+
timer:start(
17+
ms,
18+
0,
19+
vim.schedule_wrap(function()
20+
fn(unpack(argv)) -- Call the original function after debounce time
21+
end)
22+
)
23+
end
24+
end
25+
26+
return M

lua/utils/react_utils.lua renamed to lua/template-string/react_utils.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ local ts = require("nvim-treesitter.ts_utils")
22

33
local M = {}
44

5-
-- Función para verificar si el cursor está dentro de una prop de React
5+
--- Checks if the cursor is inside a React prop or JSX/TSX opening element.
6+
-- @return boolean indicating if the cursor is inside a React prop or JSX/TSX opening element.
67
function M.is_inside_react_opening_element()
78
local node = ts.get_node_at_cursor()
89

910
if not node then
1011
return false
1112
end
1213

13-
-- Verificar si el nodo es un elemento de apertura JSX/TSX or prop
14+
-- Check if the node is a JSX/TSX string or template string
1415
if node:type() == "string" or node:type() == "template_string" then
1516
local prev_node = ts.get_previous_node(node)
1617

1718
if not prev_node then
1819
return false
1920
end
2021

22+
-- Check if the previous node is a property identifier
2123
if prev_node:type() == "property_identifier" then
2224
return true
2325
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
local filetypes = require("template-string.allowed_filetypes")
2+
local u = require("template-string.react_utils")
3+
local M = {}
4+
5+
--- Replaces quotes in a line based on the presence of template literals and context.
6+
-- @param line The line of text to process.
7+
-- @param use_brackets A boolean indicating if brackets should be used.
8+
-- @return The processed line with appropriate quote replacements.
9+
local function replace_quotes(line, use_brackets)
10+
-- Replace quotes with backticks and brackets if ${} is found and use_brackets is true
11+
line = line:gsub("(['\"])(.-)(%${.-})(.-)%1", function(quote, before, inside, after)
12+
if use_brackets then
13+
return "{`" .. before .. inside .. after .. "`}"
14+
else
15+
return "`" .. before .. inside .. after .. "`"
16+
end
17+
end)
18+
19+
-- Revert backticks and brackets to original quotes if ${} is not found in the content
20+
line = line:gsub("{`([^`]*)`}", function(content)
21+
if not content:find("%${.-}") then
22+
local original_quote = line:match("[\"']") or '"'
23+
return original_quote .. content .. original_quote
24+
end
25+
return "{`" .. content .. "`}"
26+
end)
27+
28+
-- Revert solitary backticks to original quotes if ${} is not found in the content
29+
line = line:gsub("`([^`]*)`", function(content)
30+
if not content:find("%${.-}") then
31+
local original_quote = line:match("[\"']") or '"'
32+
return original_quote .. content .. original_quote
33+
end
34+
return "`" .. content .. "`"
35+
end)
36+
37+
return line
38+
end
39+
40+
--- Replaces quotes in the current line of the buffer if the filetype is allowed.
41+
function M.replace_quotes_in_line()
42+
if not filetypes.is_allowed_filetype() then
43+
return
44+
end
45+
46+
local row = vim.api.nvim_win_get_cursor(0)[1]
47+
local line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1]
48+
local new_line = replace_quotes(line, u.is_inside_react_opening_element())
49+
50+
-- Update the buffer if there were changes
51+
if new_line ~= line then
52+
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { new_line })
53+
end
54+
end
55+
56+
return M

lua/utils/debounce.lua

Lines changed: 0 additions & 20 deletions
This file was deleted.

lua/utils/replace_quotes.lua

Lines changed: 0 additions & 59 deletions
This file was deleted.

plugin/template-string.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if exists('g:loaded_template_string')
2+
finish
3+
endif
4+
5+
lua require'template-string'.setup()
6+
7+
let g:loaded_template_string = 1

0 commit comments

Comments
 (0)