add custom snippets

This commit is contained in:
lsanto 2024-10-05 00:28:23 -04:00
parent a788ef0188
commit c858c3f80e
5 changed files with 97 additions and 58 deletions

View File

@ -1,31 +1,36 @@
return {
"hrsh7th/nvim-cmp",
event = "InsertEnter",
'hrsh7th/nvim-cmp',
event = 'InsertEnter',
dependencies = {
"hrsh7th/cmp-buffer", -- source for text in buffer
"hrsh7th/cmp-path", -- source for file system paths
'hrsh7th/cmp-buffer', -- source for text in buffer
'hrsh7th/cmp-path', -- source for file system paths
{
"L3MON4D3/LuaSnip",
'L3MON4D3/LuaSnip',
-- follow latest release.
version = "v2.*", -- Replace <CurrentMajor> by the latest released major (first number of latest release)
version = 'v2.*', -- Replace <CurrentMajor> by the latest released major (first number of latest release)
-- install jsregexp (optional!).
build = "make install_jsregexp",
build = 'make install_jsregexp',
},
"saadparwaiz1/cmp_luasnip", -- for autocompletion
"rafamadriz/friendly-snippets", -- useful snippets
"onsails/lspkind.nvim", -- vs-code like pictograms
'saadparwaiz1/cmp_luasnip', -- for autocompletion
'rafamadriz/friendly-snippets', -- useful snippets
'onsails/lspkind.nvim', -- vs-code like pictograms
},
config = function()
local cmp = require("cmp")
local cmp = require 'cmp'
local luasnip = require("luasnip")
local luasnip = require 'luasnip'
local lspkind = require("lspkind")
local lspkind = require 'lspkind'
-- loads vscode style snippets from installed plugins (e.g. friendly-snippets)
require("luasnip.loaders.from_vscode").lazy_load()
require('luasnip.loaders.from_vscode').lazy_load {}
cmp.setup({
-- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file
for _, ft_path in ipairs(vim.api.nvim_get_runtime_file('mysnippets/*.lua', true)) do
loadfile(ft_path)()
end
cmp.setup {
preselect = cmp.PreselectMode.None,
completion = {
completeopt = 'menu,menuone,noinsert,noselect',
@ -36,9 +41,9 @@ return {
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-e>"] = cmp.mapping.abort(), -- close completion window
["<CR>"] = cmp.mapping.confirm({ select = true }),
mapping = cmp.mapping.preset.insert {
['<C-e>'] = cmp.mapping.abort(), -- close completion window
['<CR>'] = cmp.mapping.confirm { select = true },
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
@ -70,24 +75,29 @@ return {
luasnip.jump(-1)
end
end, { 'i', 's' }),
}),
},
-- sources for autocompletion
sources = cmp.config.sources({
{ name = "nvim_lsp"},
{ name = "luasnip" }, -- snippets
{ name = "buffer" }, -- text within current buffer
{ name = "path" }, -- file system paths
}),
sources = cmp.config.sources {
{ name = 'nvim_lsp' },
{ name = 'luasnip' }, -- snippets
{ name = 'buffer' }, -- text within current buffer
{ name = 'path' }, -- file system paths
},
-- sorting = {
-- comparators = {
-- cmp.config.compare.score,
-- cmp.config.compare.recently_used,
-- },
-- },
-- configure lspkind for vs-code like pictograms in completion menu
formatting = {
expandable_indicator = true,
fields = { cmp.ItemField.Abbr, cmp.ItemField.Kind },
format = lspkind.cmp_format({
format = lspkind.cmp_format {
maxwidth = 50,
ellipsis_char = "...",
}),
ellipsis_char = '...',
},
})
},
}
end,
}

View File

@ -1,50 +1,50 @@
return {
"williamboman/mason.nvim",
'williamboman/mason.nvim',
dependencies = {
"williamboman/mason-lspconfig.nvim",
"WhoIsSethDaniel/mason-tool-installer.nvim",
'williamboman/mason-lspconfig.nvim',
'WhoIsSethDaniel/mason-tool-installer.nvim',
},
config = function()
-- import mason
local mason = require("mason")
local mason = require 'mason'
-- import mason-lspconfig
local mason_lspconfig = require("mason-lspconfig")
local mason_lspconfig = require 'mason-lspconfig'
local mason_tool_installer = require("mason-tool-installer")
local mason_tool_installer = require 'mason-tool-installer'
-- enable mason and configure icons
mason.setup({
mason.setup {
ui = {
icons = {
package_installed = "",
package_pending = "",
package_uninstalled = "",
package_installed = '',
package_pending = '',
package_uninstalled = '',
},
},
})
}
mason_lspconfig.setup({
mason_lspconfig.setup {
-- list of servers for mason to install
ensure_installed = {
"ts_ls",
"html",
"cssls",
"tailwindcss",
"svelte",
"lua_ls",
"graphql",
-- "emmet_ls",
"prismals",
'ts_ls',
'html',
'cssls',
'tailwindcss',
'svelte',
'lua_ls',
'graphql',
'emmet_language_server',
'prismals',
},
})
}
mason_tool_installer.setup({
mason_tool_installer.setup {
ensure_installed = {
"prettier", -- prettier formatter
"stylua", -- lua formatter
"eslint_d",
'prettier', -- prettier formatter
'stylua', -- lua formatter
'eslint_d',
},
})
}
end,
}

View File

@ -47,7 +47,7 @@ vim.keymap.set('n', '<leader>ff', vim.lsp.buf.format, { noremap = true, desc = '
vim.api.nvim_set_keymap('n', '<leader>yr', ":let @+=expand('%:.%:t')<CR>", { noremap = true, silent = true, desc = 'Copy Relative Path' })
vim.api.nvim_set_keymap('n', '<leader>ya', ":let @+=expand('%:P%:t')<CR>", { noremap = true, silent = true, desc = 'Copy Absolute Path' })
vim.g.diagnostics_active = false
vim.g.diagnostics_active = true
function _G.toggle_diagnostics()
if vim.g.diagnostics_active then
vim.g.diagnostics_active = false

15
mysnippets/all.lua Normal file
View File

@ -0,0 +1,15 @@
local ls = require 'luasnip'
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
ls.add_snippets('all', {
s('ter', {
-- equivalent to "${1:cond} ? ${2:then} : ${3:else}"
i(1, 'cond'),
t ' ? ',
i(2, 'then'),
t ' : ',
i(3, 'else'),
}),
})

14
mysnippets/js.lua Normal file
View File

@ -0,0 +1,14 @@
local ls = require 'luasnip'
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
ls.add_snippets('javascriptreact', {
-- set attributes to component
s('jk', {
i(1, 'attr'),
t '={',
i(2, 'value'),
t '}',
}),
})