From c858c3f80e9b84a965a92aafbc9a662d44782d36 Mon Sep 17 00:00:00 2001 From: lsanto Date: Sat, 5 Oct 2024 00:28:23 -0400 Subject: [PATCH] add custom snippets --- lua/custom/plugins/cmp.lua | 70 ++++++++++++++++++++---------------- lua/custom/plugins/mason.lua | 54 ++++++++++++++-------------- lua/myconfigs.lua | 2 +- mysnippets/all.lua | 15 ++++++++ mysnippets/js.lua | 14 ++++++++ 5 files changed, 97 insertions(+), 58 deletions(-) create mode 100644 mysnippets/all.lua create mode 100644 mysnippets/js.lua diff --git a/lua/custom/plugins/cmp.lua b/lua/custom/plugins/cmp.lua index 93b7e856..cf2de985 100644 --- a/lua/custom/plugins/cmp.lua +++ b/lua/custom/plugins/cmp.lua @@ -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 by the latest released major (first number of latest release) + version = 'v2.*', -- Replace 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({ - [""] = cmp.mapping.abort(), -- close completion window - [""] = cmp.mapping.confirm({ select = true }), + mapping = cmp.mapping.preset.insert { + [''] = cmp.mapping.abort(), -- close completion window + [''] = cmp.mapping.confirm { select = true }, [''] = cmp.mapping.select_next_item(), [''] = 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, } diff --git a/lua/custom/plugins/mason.lua b/lua/custom/plugins/mason.lua index c93c7d29..736043f8 100644 --- a/lua/custom/plugins/mason.lua +++ b/lua/custom/plugins/mason.lua @@ -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, } diff --git a/lua/myconfigs.lua b/lua/myconfigs.lua index 56169a43..4743e726 100644 --- a/lua/myconfigs.lua +++ b/lua/myconfigs.lua @@ -47,7 +47,7 @@ vim.keymap.set('n', 'ff', vim.lsp.buf.format, { noremap = true, desc = ' vim.api.nvim_set_keymap('n', 'yr', ":let @+=expand('%:.%:t')", { noremap = true, silent = true, desc = 'Copy Relative Path' }) vim.api.nvim_set_keymap('n', 'ya', ":let @+=expand('%:P%:t')", { 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 diff --git a/mysnippets/all.lua b/mysnippets/all.lua new file mode 100644 index 00000000..2d61c38c --- /dev/null +++ b/mysnippets/all.lua @@ -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'), + }), +}) diff --git a/mysnippets/js.lua b/mysnippets/js.lua new file mode 100644 index 00000000..24c531c9 --- /dev/null +++ b/mysnippets/js.lua @@ -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 '}', + }), +})