Skip to content

Commit

Permalink
feat!: switch to native snippets and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rafi committed May 22, 2024
1 parent c09d0fb commit f437c53
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 123 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,7 @@ _Note_ that 95% of the plugins are **lazy-loaded**.
| [hrsh7th/cmp-buffer] | nvim-cmp source for buffer words
| [hrsh7th/cmp-path] | nvim-cmp source for path
| [hrsh7th/cmp-emoji] | nvim-cmp source for emoji
| [L3MON4D3/LuaSnip] | Snippet Engine written in Lua
| [rafamadriz/friendly-snippets] | Preconfigured snippets for different languages
| [saadparwaiz1/cmp_luasnip] | Luasnip completion source for nvim-cmp
| [windwp/nvim-autopairs] | Powerful auto-pair plugin with multiple characters support
| [echasnovski/mini.surround] | Fast and feature-rich surround actions
| [JoosepAlviste/nvim-ts-context-commentstring] | Set the commentstring based on the cursor location
Expand Down Expand Up @@ -607,9 +605,7 @@ _Note_ that 95% of the plugins are **lazy-loaded**.
[hrsh7th/cmp-buffer]: https://github.com/hrsh7th/cmp-buffer
[hrsh7th/cmp-path]: https://github.com/hrsh7th/cmp-path
[hrsh7th/cmp-emoji]: https://github.com/hrsh7th/cmp-emoji
[L3MON4D3/LuaSnip]: https://github.com/L3MON4D3/LuaSnip
[rafamadriz/friendly-snippets]: https://github.com/rafamadriz/friendly-snippets
[saadparwaiz1/cmp_luasnip]: https://github.com/saadparwaiz1/cmp_luasnip
[windwp/nvim-autopairs]: https://github.com/windwp/nvim-autopairs
[echasnovski/mini.surround]: https://github.com/echasnovski/mini.surround
[JoosepAlviste/nvim-ts-context-commentstring]: https://github.com/JoosepAlviste/nvim-ts-context-commentstring
Expand Down Expand Up @@ -693,7 +689,7 @@ Spec: `rafi.plugins.extras.coding.<name>`
| `chainsaw` | [chrisgrieser/nvim-chainsaw] | Create log statements on the fly
| `editorconfig` | [sgur/vim-editorconfig] | EditorConfig plugin written entirely in Vimscript
| `emmet` | [mattn/emmet-vim] | Provides support for expanding abbreviations alá emmet
| `mini.comment` | [echasnovski/mini.comment] | Fast and familiar per-line commenting
| `luasnip` | [L3MON4D3/LuaSnip] [saadparwaiz1/cmp_luasnip] | Snippet Engine and cmp source
| `minipairs` | [echasnovski/mini.pairs] | Automatically manage character pairs
| `neogen` | [danymat/neogen] | Annotation generator
| `sandwich` | [machakann/vim-sandwich] | Search, select, and edit sandwich text objects
Expand All @@ -702,7 +698,8 @@ Spec: `rafi.plugins.extras.coding.<name>`
[chrisgrieser/nvim-chainsaw]: https://github.com/chrisgrieser/nvim-chainsaw
[sgur/vim-editorconfig]: https://github.com/sgur/vim-editorconfig
[mattn/emmet-vim]: https://github.com/mattn/emmet-vim
[echasnovski/mini.comment]: https://github.com/echasnovski/mini.comment
[L3MON4D3/LuaSnip]: https://github.com/L3MON4D3/LuaSnip
[saadparwaiz1/cmp_luasnip]: https://github.com/saadparwaiz1/cmp_luasnip
[echasnovski/mini.pairs]: https://github.com/echasnovski/mini.pairs
[danymat/neogen]: https://github.com/danymat/neogen
[machakann/vim-sandwich]: https://github.com/machakann/vim-sandwich
Expand Down
4 changes: 4 additions & 0 deletions lua/rafi/config/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ end, { silent = true, desc = 'Yank absolute path' })
--- }}}
-- Coding {{{

-- Comment
map('n', '<Leader>v', 'gcc', { remap = true, desc = 'Comment Line' })
map('x', '<Leader>v', 'gc', { remap = true, desc = 'Comment Selection' })

-- Macros
map('n', '<C-q>', 'q', { desc = 'Macro Prefix' })

Expand Down
90 changes: 40 additions & 50 deletions lua/rafi/plugins/coding.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ return {
for _, source in ipairs(opts.sources) do
source.group_index = source.group_index or 1
end

local parse = require('cmp.utils.snippet').parse
---@diagnostic disable-next-line: duplicate-set-field
require('cmp.utils.snippet').parse = function(input)
local ok, ret = pcall(parse, input)
if ok then
return ret
end
return LazyVim.cmp.snippet_preview(input)
end

local cmp = require('cmp')
cmp.setup(opts)
cmp.event:on('confirm_done', function(event)
Expand All @@ -131,57 +142,36 @@ return {
},

-----------------------------------------------------------------------------
-- Snippet Engine written in Lua
{
'L3MON4D3/LuaSnip',
event = 'InsertEnter',
build = (not LazyVim.is_win())
and "echo 'NOTE: jsregexp is optional, so not a big deal if it fails to build'; make install_jsregexp"
or nil,
dependencies = {
-- Preconfigured snippets for different languages
{
'rafamadriz/friendly-snippets',
config = function()
require('luasnip.loaders.from_vscode').lazy_load()
require('luasnip.loaders.from_lua').load({ paths = { './snippets' } })
end,
},
-- Adds luasnip source to nvim-cmp.
{
-- Native snippets
vim.fn.has('nvim-0.10') == 1
and {
'nvim-cmp',
dependencies = {
-- Luasnip completion source for nvim-cmp
'saadparwaiz1/cmp_luasnip',
{
'garymjr/nvim-snippets',
opts = {
friendly_snippets = true,
global_snippets = { 'all', 'global' },
},
dependencies = {
-- Preconfigured snippets for different languages
'rafamadriz/friendly-snippets',
},
},
},
opts = function(_, opts)
opts.snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
expand = function(item)
return LazyVim.cmp.expand(item.body)
end,
}
table.insert(opts.sources, { name = 'luasnip', keyword_length = 2 })
table.insert(opts.sources, { name = 'snippets' })
end,
},
},
-- stylua: ignore
keys = {
{ '<C-l>', function() require('luasnip').expand_or_jump() end, mode = { 'i', 's' } },
},
opts = {
history = true,
delete_check_events = 'TextChanged',
-- ft_func = function()
-- return vim.split(vim.bo.filetype, '.', { plain = true })
-- end,
}
or {
import = 'rafi.plugins.extras.coding.luasnip',
enabled = vim.fn.has('nvim-0.10') == 0,
},
config = function(_, opts)
require('luasnip').setup(opts)
vim.api.nvim_create_user_command('LuaSnipEdit', function()
require('luasnip.loaders').edit_snippet_files()
end, {})
end,
},

-----------------------------------------------------------------------------
-- Powerful auto-pair plugin with multiple characters support
Expand Down Expand Up @@ -258,23 +248,23 @@ return {
-----------------------------------------------------------------------------
-- Set the commentstring based on the cursor location
{
'JoosepAlviste/nvim-ts-context-commentstring',
opts = {
enable = true,
enable_autocmd = false,
},
'folke/ts-comments.nvim',
event = 'VeryLazy',
enabled = vim.fn.has('nvim-0.10') == 1,
opts = {},
},
{
import = 'rafi.plugins.extras.coding.minicomment',
enabled = vim.fn.has('nvim-0.10') == 0,
},

-----------------------------------------------------------------------------
-- Powerful line and block-wise commenting
{
'numToStr/Comment.nvim',
event = 'VeryLazy',
dependencies = { 'JoosepAlviste/nvim-ts-context-commentstring' },
-- stylua: ignore
keys = {
{ '<Leader>v', '<Plug>(comment_toggle_linewise_current)', mode = 'n', desc = 'Comment' },
{ '<Leader>v', '<Plug>(comment_toggle_linewise_visual)', mode = 'x', desc = 'Comment' },
{ '<Leader>V', '<Plug>(comment_toggle_blockwise_current)', mode = 'n', desc = 'Comment' },
{ '<Leader>V', '<Plug>(comment_toggle_blockwise_visual)', mode = 'x', desc = 'Comment' },
},
Expand Down
55 changes: 55 additions & 0 deletions lua/rafi/plugins/extras/coding/luasnip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
return {

-----------------------------------------------------------------------------
-- Snippet Engine written in Lua
{
'L3MON4D3/LuaSnip',
event = 'InsertEnter',
build = (not LazyVim.is_win())
and "echo 'NOTE: jsregexp is optional, so not a big deal if it fails to build'; make install_jsregexp"
or nil,
dependencies = {
-- Preconfigured snippets for different languages
{
'rafamadriz/friendly-snippets',
config = function()
require('luasnip.loaders.from_vscode').lazy_load()
require('luasnip.loaders.from_lua').load({ paths = { './snippets' } })
end,
},
-- Adds luasnip source to nvim-cmp.
{
'nvim-cmp',
dependencies = {
-- Luasnip completion source for nvim-cmp
'saadparwaiz1/cmp_luasnip',
},
opts = function(_, opts)
opts.snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
}
table.insert(opts.sources, { name = 'luasnip' })
end,
},
},
-- stylua: ignore
keys = {
{ '<C-l>', function() require('luasnip').expand_or_jump() end, mode = { 'i', 's' } },
},
opts = {
history = true,
delete_check_events = 'TextChanged',
-- ft_func = function()
-- return vim.split(vim.bo.filetype, '.', { plain = true })
-- end,
},
config = function(_, opts)
require('luasnip').setup(opts)
vim.api.nvim_create_user_command('LuaSnipEdit', function()
require('luasnip.loaders').edit_snippet_files()
end, {})
end,
},
}
24 changes: 0 additions & 24 deletions lua/rafi/plugins/extras/coding/minicomment.lua

This file was deleted.

42 changes: 0 additions & 42 deletions lua/rafi/plugins/extras/coding/native_snippets.lua

This file was deleted.

2 changes: 1 addition & 1 deletion lua/rafi/plugins/extras/coding/neogen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ return {
keys = {
{ '<leader>cg', function() require('neogen').generate({}) end, desc = 'Neogen Comment' },
},
opts = { snippet_engine = 'luasnip' },
opts = {},
},
}

0 comments on commit f437c53

Please sign in to comment.