feat(nvim): add some new useful autocmds on ftplugin

This commit is contained in:
Sergio Laín 2024-01-17 09:52:25 +01:00
parent 038974661b
commit 60e69ab7d3
No known key found for this signature in database
GPG key ID: 14C9B8080681777B
5 changed files with 58 additions and 1 deletions

View file

@ -0,0 +1,16 @@
-- Automatically end a self-closing tag when pressing /
vim.keymap.set("i", "/", function()
local node = vim.treesitter.get_node()
if not node then
return "/"
end
if node:type() == "jsx_opening_element" then
local char_at_cursor = vim.fn.strcharpart(vim.fn.strpart(vim.fn.getline("."), vim.fn.col(".") - 2), 0, 1)
local already_have_space = char_at_cursor == " "
return already_have_space and "/>" or " />"
end
return "/"
end, { expr = true, buffer = true })

View file

@ -1 +1,13 @@
vim.opt.conceallevel = 0
-- When creating a new line with o, make sure there is a trailing comma on the current line
vim.keymap.set("n", "o", function()
local line = vim.api.nvim_get_current_line()
local should_add_comma = string.find(line, "[^,{[]$")
if should_add_comma then
return "A,<cr>"
else
return "o"
end
end, { buffer = true, expr = true })

View file

@ -1 +1,13 @@
vim.opt.conceallevel = 0
-- When creating a new line with o, make sure there is a trailing comma on the current line
vim.keymap.set("n", "o", function()
local line = vim.api.nvim_get_current_line()
local should_add_comma = string.find(line, "[^,{[]$")
if should_add_comma then
return "A,<cr>"
else
return "o"
end
end, { buffer = true, expr = true })

View file

@ -0,0 +1,16 @@
-- Automatically end a self-closing tag when pressing /
vim.keymap.set("i", "/", function()
local node = vim.treesitter.get_node()
if not node then
return "/"
end
if node:type() == "jsx_opening_element" then
local char_at_cursor = vim.fn.strcharpart(vim.fn.strpart(vim.fn.getline("."), vim.fn.col(".") - 2), 0, 1)
local already_have_space = char_at_cursor == " "
return already_have_space and "/>" or " />"
end
return "/"
end, { expr = true, buffer = true })

View file

@ -107,4 +107,5 @@
"NEWS.md": "2123"
},
"version": 2
}
}