-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fbb916c
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Cristhian Fernando Melo Montilla | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# 😸 template-string.nvim | ||
|
||
`template-string.nvim` is a simple Neovim plugin for handling template strings in JavaScript and TypeScript files. It provides functionality to wrap template literals with `{``}` when inside JSX/TSX components and revert them back to their original form when necessary. | ||
|
||
## Features | ||
|
||
- Wrap template literals with `{``}` when inside JSX/TSX components. | ||
- Revert template literals to their original form when necessary. | ||
- Configurable options to enable/disable wrapping with `{``}`. | ||
|
||
## Supported Languages | ||
|
||
- JavaScript | ||
- TypeScript | ||
- JSX | ||
- TSX | ||
|
||
## Installation | ||
|
||
Install using your favorite package manager for Neovim. For example, using [lazy.nvim](https://github.com/folke/lazy.nvim): | ||
|
||
```lua | ||
{ | ||
"rxtsel/template-string.nvim", | ||
event = "BufReadPost", | ||
dependencies = { | ||
"nvim-lua/plenary.nvim", | ||
}, | ||
config = function() | ||
require("template-string").setup() | ||
end, | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
Once installed, the plugin automatically wraps template literals with **``** when editing JavaScript or TypeScript files. To use template literals, simply enclose your JavaScript or TypeScript expressions in `${}`. For example: | ||
|
||
```javascript | ||
// Javascript/TypeScript | ||
const name = "World"; | ||
const greeting = `Hello, ${name}!`; | ||
console.log(greeting); | ||
``` | ||
|
||
On JSX/TSX components, the plugin will wrap template literals with `{``}`. For example: | ||
|
||
```jsx | ||
const props = { | ||
name: "World", | ||
}; | ||
|
||
<Test greeting={`Hello, ${props.name}!`} />; | ||
``` | ||
|
||
## Configuration | ||
|
||
The `setup` function accepts an optional configuration object with the following options: | ||
|
||
- **jsx_brackets** `boolean | nil`: Enable/disable wrapping template literals with `{``}` inside JSX/TSX components. Defaults to `true` if not provided. | ||
|
||
```lua | ||
-- Default configuration | ||
require('template-string').setup({ | ||
jsx_brackets = true, -- Wrap template literals with {``} inside JSX/TSX components | ||
}) | ||
``` | ||
|
||
## License | ||
|
||
This plugin is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
---@class TemplateStringConfig | ||
---@field jsx_brackets boolean | nil | ||
|
||
local M = {} | ||
|
||
local allowed_filetypes = { | ||
javascript = true, | ||
typescript = true, | ||
javascriptreact = true, | ||
typescriptreact = true, | ||
} | ||
|
||
local function is_allowed_filetype() | ||
local filetype = vim.bo.filetype | ||
return allowed_filetypes[filetype] or false | ||
end | ||
|
||
-- Function to wrap with {``} if inside a JSX/TSX component | ||
local function wrap_with_brackets_if_necessary(content) | ||
if content:find("%${") then | ||
return "={`" .. content .. "`}" | ||
else | ||
return '="' .. content .. '"' | ||
end | ||
end | ||
|
||
-- Function to replace quotes in the current line | ||
local function replace_quotes_in_line() | ||
if not is_allowed_filetype() then | ||
return | ||
end | ||
|
||
local row = vim.api.nvim_win_get_cursor(0)[1] | ||
local line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1] | ||
|
||
if not line then | ||
return | ||
end | ||
|
||
local new_line = line | ||
|
||
-- Replace quotes with backticks when ${ is found | ||
new_line = new_line:gsub("(['\"])(.-)%${(.-)}(.-)%1", function(quote, before, inside, after) | ||
return "`" .. before .. "${" .. inside .. "}" .. after .. "`" | ||
end) | ||
|
||
if M.jsx_brackets then | ||
-- Wrap with {``} if inside a JSX/TSX component | ||
new_line = new_line:gsub("=%s*`([^`]*)`", wrap_with_brackets_if_necessary) | ||
|
||
-- Revert backticks to original quotes if ${ is not found | ||
new_line = new_line:gsub("={[`{]+([^`]*)[`}]+}", function(content) | ||
if not content:find("%${") then | ||
-- Determine the original type of quotes, double or single | ||
local original_quote = line:match("=[\"']") and '"' or line:match("=['\"]") and "'" or '"' | ||
return "=" .. original_quote .. content .. original_quote | ||
end | ||
return "={" .. "`" .. content .. "`" .. "}" | ||
end) | ||
end | ||
|
||
-- Also handle reverting solitary backticks on normal lines | ||
new_line = new_line:gsub("`([^`]*)`", function(content) | ||
if not content:find("%${") then | ||
-- Determine the original type of quotes, double or single | ||
local original_quote = line:match("[\"']") or '"' | ||
return original_quote .. content .. original_quote | ||
end | ||
return "`" .. content .. "`" | ||
end) | ||
|
||
if new_line ~= line then | ||
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { new_line }) | ||
end | ||
end | ||
|
||
-- Function to execute update with debounce | ||
local function debounce(fn, ms) | ||
local timer = vim.loop.new_timer() | ||
return function(...) | ||
timer:stop() | ||
local argv = { ... } | ||
timer:start( | ||
ms, | ||
0, | ||
vim.schedule_wrap(function() | ||
fn(unpack(argv)) | ||
end) | ||
) | ||
end | ||
end | ||
|
||
--- Configures the plugin behavior. | ||
---@param opts TemplateStringConfig | nil Optional plugin configuration. | ||
function M.setup(opts) | ||
opts = opts or {} | ||
-- Enable brackets for JSX/TSX | ||
local jsx_brackets = opts.jsx_brackets == nil or opts.jsx_brackets | ||
M.jsx_brackets = jsx_brackets | ||
|
||
-- Enable debounce | ||
local debounced_replace = debounce(replace_quotes_in_line, 100) | ||
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { | ||
callback = debounced_replace, | ||
}) | ||
end | ||
|
||
return M |