From b876991043da316e756c782018a8876d1c3351fb Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Fri, 15 Jul 2022 22:47:49 +0200 Subject: [PATCH] nvim: add own snippets and some keybinds --- files/nvim/.config/nvim/lua/mappings.lua | 26 +++++ files/nvim/.config/nvim/lua/plugins.lua | 25 ++--- files/nvim/.config/nvim/snippets/LICENSE | 21 +++++ files/nvim/.config/nvim/snippets/README.md | 94 +++++++++++++++++++ files/nvim/.config/nvim/snippets/package.json | 16 ++++ .../.config/nvim/snippets/snippets/latex.json | 87 +++++++++++++++++ 6 files changed, 258 insertions(+), 11 deletions(-) create mode 100644 files/nvim/.config/nvim/snippets/LICENSE create mode 100644 files/nvim/.config/nvim/snippets/README.md create mode 100644 files/nvim/.config/nvim/snippets/package.json create mode 100644 files/nvim/.config/nvim/snippets/snippets/latex.json diff --git a/files/nvim/.config/nvim/lua/mappings.lua b/files/nvim/.config/nvim/lua/mappings.lua index 7bae17c..06f18ff 100644 --- a/files/nvim/.config/nvim/lua/mappings.lua +++ b/files/nvim/.config/nvim/lua/mappings.lua @@ -79,8 +79,33 @@ wk.register({ -- cmp local cmp = require 'cmp' +local luasnip = require 'luasnip' +local has_words_before = function() + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil +end cmp.setup({ mapping = { + [""] = cmp.mapping(function(fallback) -- Advance to next parameter + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), + [""] = cmp.mapping(function(fallback) -- Got back to last parameter + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), [""] = cmp.mapping.select_prev_item(), [""] = cmp.mapping.select_next_item(), [""] = cmp.mapping.confirm({ @@ -89,3 +114,4 @@ cmp.setup({ }), }, }) + diff --git a/files/nvim/.config/nvim/lua/plugins.lua b/files/nvim/.config/nvim/lua/plugins.lua index e3427d7..04f9cd9 100644 --- a/files/nvim/.config/nvim/lua/plugins.lua +++ b/files/nvim/.config/nvim/lua/plugins.lua @@ -1,6 +1,8 @@ -vim.cmd [[packadd packer.nvim]] - local fn = vim.fn + +-- This should auto install packer, if it is not installed on system +-- Otherwise use: +-- git clone --depth 1 https://github.com/wbthomason/packer.nvim ~/.local/share/nvim/site/pack/packer/start/packer.nvim local install_path = fn.stdpath('data') .. '/site/pack/packer/start/packer.nvim' if fn.empty(fn.glob(install_path)) > 0 then packer_bootstrap = fn.system({ 'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', @@ -137,6 +139,7 @@ return require('packer').startup(function(use) "make", "python", "rust", + "yaml", }, auto_intall = true, highlight = { @@ -188,13 +191,8 @@ return require('packer').startup(function(use) }) -- Load friendly-snippets require('luasnip.loaders.from_vscode').lazy_load() - -- TODO: Add own snippets - --[[ require("luasnip.loaders.from_vscode").lazy_load({ - paths = { - "~/.config/nvim/snippets", - "/home/marc/.local/share/nvim/site/pack/packer/start/friendly-snippets" - } - }) ]] + -- Load own snippets + require("luasnip.loaders.from_vscode").lazy_load({ paths = { "./snippets" }}) end, }) @@ -209,11 +207,16 @@ return require('packer').startup(function(use) -- Align with `:Align ` use 'RRethy/nvim-align' - -- Easily comment out stuff (gc, gb) + -- Easily comment out stuff use({ "numToStr/Comment.nvim", config = function() - require('Comment').setup({}) + require('Comment').setup({ + opleader = { + line = 'cl', + block = 'cb', + } + }) end, }) diff --git a/files/nvim/.config/nvim/snippets/LICENSE b/files/nvim/.config/nvim/snippets/LICENSE new file mode 100644 index 0000000..b9783b9 --- /dev/null +++ b/files/nvim/.config/nvim/snippets/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Rafael Madriz + +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. diff --git a/files/nvim/.config/nvim/snippets/README.md b/files/nvim/.config/nvim/snippets/README.md new file mode 100644 index 0000000..0a81e36 --- /dev/null +++ b/files/nvim/.config/nvim/snippets/README.md @@ -0,0 +1,94 @@ +# Friendly Snippets + +Snippets collection for a set of different programming languages for faster development. + +The only goal is to have one community driven repository for all kinds of +snippets in all programming languages, this way you can have it all in one +place. + +### Usage + +This collection of snippets should work with any plugin that supports loading +vscode snippets. Like for example: + +- [vim-vsnip](https://github.com/hrsh7th/vim-vsnip) +- [LuaSnip](https://github.com/L3MON4D3/LuaSnip) +- [coc-snippets](https://github.com/neoclide/coc-snippets) + +### Add snippets from a framework to a filetype. + +There's extra snippets included in this repo but they are not added by default, +since it would be irrelevant for people not using those frameworks. See +[`snippets/frameworks`](https://github.com/rafamadriz/friendly-snippets/tree/main/snippets/frameworks) + +For example: if you want to add rails snippets to ruby. + +With LuaSnip: + +```lua +require'luasnip'.filetype_extend("ruby", {"rails"}) +``` + +This method is going to work globally on all open buffers with `ruby` filetype. +Alternatively you can do `set filetype=ruby.rails` so it only works on a +specific buffer, but this is going to mess up with syntax highlighting. + +With vim-vsnip: + +```viml +let g:vsnip_filetypes.ruby = ['rails'] +``` + +For more info related to this change see [#88](https://github.com/rafamadriz/friendly-snippets/issues/88) + +### Install + +Use your plugin manager of choice, e.g. + +```lua +-- Packer +use "rafamadriz/friendly-snippets" + +-- Plug +Plug 'rafamadriz/friendly-snippets' + +-- If you're using coc.nvim, you can use: +CocInstall https://github.com/rafamadriz/friendly-snippets@main +``` + +#### HTML + +![HTML gif](https://user-images.githubusercontent.com/67771985/131255337-d53f3408-b60d-44a2-93ba-9a3240a7436e.gif) + +#### JS + +![JS gif](https://user-images.githubusercontent.com/67771985/131255342-e393165a-e4b1-401e-9084-a782b9dd3fef.gif) + +##### NOTE: Using [nvim-compe](https://github.com/hrsh7th/nvim-compe) with [vim-vsnip](https://github.com/hrsh7th/vim-vsnip) on the videos. + +## TODO + +- Add all included snippets to the + [Wiki](https://github.com/rafamadriz/friendly-snippets/wiki). + +## Thanks to all contributors + + + + + +## Credits + +A good portion of the snippets have been forked from the following repositories: + +- [vscode-standardjs-snippets](https://github.com/capaj/vscode-standardjs-snippets) +- [python-snippets](https://github.com/cstrap/python-snippets) +- [vs-snippets](https://github.com/kitagry/vs-snippets) +- [Wscats/html-snippets](https://github.com/Wscats/html-snippets) +- [Harry-Ross/vscode-c-snippets](https://github.com/Harry-Ross/vscode-c-snippets) +- [vscode-jekyll-snippets](https://github.com/edheltzel/vscode-jekyll-snippets) +- [vscode-fortran-support](https://github.com/krvajal/vscode-fortran-support) +- [vscode_cobol](https://github.com/spgennard/vscode_cobol) +- [VSCode-LaTeX-Snippets](https://github.com/JeffersonQin/VSCode-LaTeX-Snippets) +- [vscode-react-javascript-snippets](https://github.com/dsznajder/vscode-react-javascript-snippets) +- [honza/vim-snippets - Verilog](https://github.com/honza/vim-snippets/blob/master/snippets/verilog.snippets) diff --git a/files/nvim/.config/nvim/snippets/package.json b/files/nvim/.config/nvim/snippets/package.json new file mode 100644 index 0000000..448a737 --- /dev/null +++ b/files/nvim/.config/nvim/snippets/package.json @@ -0,0 +1,16 @@ +{ + "name": "friendly-snippets", + "engines": { + "vscode": "^1.11.0" + }, + "contributes": { + "snippets": [ + { + "language": [ + "tex" + ], + "path": "./snippets/latex.json" + } + ] + } +} diff --git a/files/nvim/.config/nvim/snippets/snippets/latex.json b/files/nvim/.config/nvim/snippets/snippets/latex.json new file mode 100644 index 0000000..bbb981d --- /dev/null +++ b/files/nvim/.config/nvim/snippets/snippets/latex.json @@ -0,0 +1,87 @@ +{ + "notes": { + "prefix": "notes", + "body": [ + "\\input{/home/marc/.dots/presets/notes.tex}", + "", + "\\title{$1}", + "\\subtitle{$1}", + "\\author{Marco Thomas}", + "", + "\\begin{document}", + "", + "\\maketitle", + "\\newpage", + "\\tableofcontents", + "\\newpage", + "", + "\\end{document}" + ], + "description": "Preset to create new notes document" + }, + "presentations": { + "prefix": "presentations", + "body": [ + "\\input{/home/marc/.dots/presets/presentations.tex}", + "", + "\\title{$1}", + "\\subtitle{$2}", + "\\author{Marco Thomas}", + "", + "\\begin{document}", + "", + "\\maketitle", + "\\newpage", + "\\begin{frame}", + " \\frametitle{Inhaltsübersicht}", + " \\tableofcontents[hideallsubsections]", + "\\end{frame}", + "\\newpage", + "", + "\\end{document}" + ], + "description": "Preset to create new presentation" + }, + "wrapfigure": { + "prefix": "wrapfigure", + "body": [ + "\\begin{wrapfigure}{${1:l or r}}{0.4\\textwidth}", + " \\centering", + " \\includegraphics[width=0.2\\textwidth]{figures/$2}", + " \\caption{$3}", + " \\label{fig:$4}", + "\\end{wrapfigure}" + ], + "description": "Wrap figure in text context" + }, + "source": { + "prefix": "src", + "body": [ + "\\inputminted[breaklines=true, bgcolor=bg]{${1:language}}{${2:file}}" + ], + "description": "Include a source code file" + }, + "source-direct": { + "prefix": "srcd", + "body": [ + "\\begin{minted}[breaklines=true, bgcolor=bg]{${1:language}}", + "\\end{minted}" + ], + "description": "Directly write a code snippet" + }, + "source-inline": { + "prefix": "srci", + "body": ["\\mintinline{${1:language}}{${2:code}}"], + "description": "Write code inline" + }, + "german quote": { + "prefix": "gqq", + "body": ["\\glqq{}$1\\grqq{}"], + "description": "German quotes" + }, + "paragraph": { + "prefix": "paragraph", + "body": ["\\paragraph{$1}"], + "description": "Insert a new paragraph" + } +}