|
| 1 | +---@class TemplateStringConfig |
| 2 | +---@field jsx_brackets boolean | nil |
| 3 | + |
| 4 | +local M = {} |
| 5 | + |
| 6 | +local allowed_filetypes = { |
| 7 | + javascript = true, |
| 8 | + typescript = true, |
| 9 | + javascriptreact = true, |
| 10 | + typescriptreact = true, |
| 11 | +} |
| 12 | + |
| 13 | +local function is_allowed_filetype() |
| 14 | + local filetype = vim.bo.filetype |
| 15 | + return allowed_filetypes[filetype] or false |
| 16 | +end |
| 17 | + |
| 18 | +-- Function to wrap with {``} if inside a JSX/TSX component |
| 19 | +local function wrap_with_brackets_if_necessary(content) |
| 20 | + if content:find("%${") then |
| 21 | + return "={`" .. content .. "`}" |
| 22 | + else |
| 23 | + return '="' .. content .. '"' |
| 24 | + end |
| 25 | +end |
| 26 | + |
| 27 | +-- Function to replace quotes in the current line |
| 28 | +local function replace_quotes_in_line() |
| 29 | + if not is_allowed_filetype() then |
| 30 | + return |
| 31 | + end |
| 32 | + |
| 33 | + local row = vim.api.nvim_win_get_cursor(0)[1] |
| 34 | + local line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1] |
| 35 | + |
| 36 | + if not line then |
| 37 | + return |
| 38 | + end |
| 39 | + |
| 40 | + local new_line = line |
| 41 | + |
| 42 | + -- Replace quotes with backticks when ${ is found |
| 43 | + new_line = new_line:gsub("(['\"])(.-)%${(.-)}(.-)%1", function(quote, before, inside, after) |
| 44 | + return "`" .. before .. "${" .. inside .. "}" .. after .. "`" |
| 45 | + end) |
| 46 | + |
| 47 | + if M.jsx_brackets then |
| 48 | + -- Wrap with {``} if inside a JSX/TSX component |
| 49 | + new_line = new_line:gsub("=%s*`([^`]*)`", wrap_with_brackets_if_necessary) |
| 50 | + |
| 51 | + -- Revert backticks to original quotes if ${ is not found |
| 52 | + new_line = new_line:gsub("={[`{]+([^`]*)[`}]+}", function(content) |
| 53 | + if not content:find("%${") then |
| 54 | + -- Determine the original type of quotes, double or single |
| 55 | + local original_quote = line:match("=[\"']") and '"' or line:match("=['\"]") and "'" or '"' |
| 56 | + return "=" .. original_quote .. content .. original_quote |
| 57 | + end |
| 58 | + return "={" .. "`" .. content .. "`" .. "}" |
| 59 | + end) |
| 60 | + end |
| 61 | + |
| 62 | + -- Also handle reverting solitary backticks on normal lines |
| 63 | + new_line = new_line:gsub("`([^`]*)`", function(content) |
| 64 | + if not content:find("%${") then |
| 65 | + -- Determine the original type of quotes, double or single |
| 66 | + local original_quote = line:match("[\"']") or '"' |
| 67 | + return original_quote .. content .. original_quote |
| 68 | + end |
| 69 | + return "`" .. content .. "`" |
| 70 | + end) |
| 71 | + |
| 72 | + if new_line ~= line then |
| 73 | + vim.api.nvim_buf_set_lines(0, row - 1, row, false, { new_line }) |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +-- Function to execute update with debounce |
| 78 | +local function debounce(fn, ms) |
| 79 | + local timer = vim.loop.new_timer() |
| 80 | + return function(...) |
| 81 | + timer:stop() |
| 82 | + local argv = { ... } |
| 83 | + timer:start( |
| 84 | + ms, |
| 85 | + 0, |
| 86 | + vim.schedule_wrap(function() |
| 87 | + fn(unpack(argv)) |
| 88 | + end) |
| 89 | + ) |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +--- Configures the plugin behavior. |
| 94 | +---@param opts TemplateStringConfig | nil Optional plugin configuration. |
| 95 | +function M.setup(opts) |
| 96 | + opts = opts or {} |
| 97 | + -- Enable brackets for JSX/TSX |
| 98 | + local jsx_brackets = opts.jsx_brackets == nil or opts.jsx_brackets |
| 99 | + M.jsx_brackets = jsx_brackets |
| 100 | + |
| 101 | + -- Enable debounce |
| 102 | + local debounced_replace = debounce(replace_quotes_in_line, 100) |
| 103 | + vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { |
| 104 | + callback = debounced_replace, |
| 105 | + }) |
| 106 | +end |
| 107 | + |
| 108 | +return M |
0 commit comments