From fd0fd90fe0c718436e805618fcee21ad3e040774 Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Tue, 12 Oct 2021 17:06:35 -0300 Subject: [PATCH] Add vim snippets for many languages --- config/nvim/init.vim | 172 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 165 insertions(+), 7 deletions(-) diff --git a/config/nvim/init.vim b/config/nvim/init.vim index e3b26bd..b13622e 100644 --- a/config/nvim/init.vim +++ b/config/nvim/init.vim @@ -8,9 +8,12 @@ set smartindent set nocp set bo=all colorscheme torte -set ts=8 set list set listchars=tab:*·,lead:·,trail:~,extends:>,precedes:< +set noet +set ts=8 +set sw=8 +filetype indent off hi Whitespace ctermfg=233 hi SignColumn ctermbg=233 @@ -32,18 +35,149 @@ vnoremap " a"``> nnoremap H 0 nnoremap L $ +" Used in abbrevs to remove a space +" append: =Eatchar('\s') +func Eatchar(pat) + let c = nr2char(getchar(0)) + return (c =~ a:pat) ? '' : c +endfunc + augroup html autocmd! autocmd FileType html inoremap > >F<"tyef>"tpa>TT> autocmd FileType html inoremap = ="" autocmd FileType html inoremap \= = autocmd FileType html inoremap \> > - autocmd FileType html iabbrev df> + autocmd FileType html iabbrev df>o2kOA>df><c-r>=Eatchar('\s')<cr> autocmd FileType html nnoremap <buffer> <localleader>f Vatzf autocmd FileType html vnoremap <buffer> H "tdh?><CR>"tpvT> autocmd FileType html vnoremap <buffer> L "td/<<CR>"tPg;vt< augroup END +augroup javascript + autocmd! + autocmd FileType javascript inoremap <buffer> '. ><esc>F<"tyef>"tpa><esc>T<i/<c-o>T> + autocmd FileType javascript nnoremap <buffer> <localleader>f Vatzf + autocmd FileType javascript vnoremap <buffer> H "tdh?><CR>"tpvT> + autocmd FileType javascript vnoremap <buffer> L "td/<<CR>"tPg;vt< + autocmd FileType javascript iabbrev <silent> <buffer> log console.log("")<left><left><c-r>=Eatchar('\s')<cr> + autocmd FileType javascript iabbrev <silent> <buffer> for for(let i = 0; i < ; i++) {<cr>}<up><c-o>f;<c-o>;<c-r>=Eatchar('\s')<cr> + autocmd FileType javascript iabbrev <silent> <buffer> while while() {<cr>}<up><c-o>f)<c-r>=Eatchar('\s')<cr> + autocmd FileType javascript iabbrev <silent> <buffer> if if() {<cr>}<up><c-o>f)<c-r>=Eatchar('\s')<cr> + autocmd FileType javascript iabbrev <silent> <buffer> fn function () {<cr>}<up><c-o>f(<c-r>=Eatchar('\s')<cr> + autocmd FileType javascript iabbrev <silent> <buffer> ret return<c-r>=Eatchar('\s')<cr> + autocmd FileType javascript iabbrev <silent> <buffer> let let = <left><left><left><c-r>=Eatchar('\s')<cr> +augroup javascript + +augroup python + autocmd! + autocmd FileType python iabbrev <silent> <buffer> for for x in :<left><c-r>=Eatchar('\s')<cr> + autocmd FileType python iabbrev <silent> <buffer> while while :<left><c-r>=Eatchar('\s')<cr> + autocmd FileType python iabbrev <silent> <buffer> if if :<left><c-r>=Eatchar('\s')<cr> + autocmd FileType python iabbrev <silent> <buffer> elif elif :<left><c-r>=Eatchar('\s')<cr> + autocmd FileType python iabbrev <silent> <buffer> def def ():<left><left><left><c-r>=Eatchar('\s')<cr> + autocmd FileType python iabbrev <silent> <buffer> met def (self):<c-o>F(<c-r>=Eatchar('\s')<cr> + autocmd FileType python iabbrev <silent> <buffer> class class :<left><c-r>=Eatchar('\s')<cr> + autocmd FileType python iabbrev <silent> <buffer> ret return<c-r>=Eatchar('\s')<cr> + autocmd FileType python iabbrev <silent> <buffer> #! #!/bin/python3<cr><c-r>=Eatchar('\s')<cr> + autocmd FileType python set noet + autocmd FileType python set ts=8 + autocmd FileType python set sw=8 +augroup END + +augroup rust + autocmd! + autocmd FileType rust set noet + autocmd FileType rust set ts=8 + autocmd FileType rust set sw=8 + autocmd FileType rust iabbrev <silent> <buffer> for for x in {<cr>}<up><c-o>f{<left><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> impl impl x for x {<cr>}<up><c-o>fx<delete><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> struct struct {<cr>}<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> match match {<cr>}<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> der #[derive ()]<left><left><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> fn fn () {<cr>}<up><c-o>f(<c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> let let = ;<c-o>F=<left><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> use use ;<left><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> type type = ;<c-o>F=<left><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> if if {<cr>}<up><c-o>f{<left><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> ret return + autocmd FileType rust iabbrev <silent> <buffer> log@ println!("");<left><left><left><c-r>=Eatchar('\s')<cr> + autocmd FileType rust iabbrev <silent> <buffer> main@ fn main() {<cr>}<c-o>O<c-r>=Eatchar('\s')<cr> +augroup END + +augroup c + autocmd! + autocmd FileType c iabbrev <silent> <buffer> main@ int main(int argc, char* argv[]) {<cr><cr><cr>return 0;<cr>}<up><up><up><tab><c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> printf printf("");<left><left><left><c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> log@ fprintf(stderr, "");<left><left><left><c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> for for(int i = 0; i < ; i++) {<cr>}<up><c-o>f;<c-o>;<c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> if if() {<cr>}<up><c-o>f)<c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> while while() {<cr>}<up><c-o>f)<c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> struct struct {<cr>};<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> enum enum {<cr>}<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> fn@ void () {<cr>}<up><c-o>f(<c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> ret return ;<left><c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> retv return;<c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> incl #include <<c-r>=Eatchar('\s')<cr> + autocmd FileType c iabbrev <silent> <buffer> def #define <c-r>=Eatchar('\s')<cr> +augroup END + +augroup cpp + autocmd! + autocmd FileType cpp iabbrev <silent> <buffer> main@ using namespace std;<cr><cr>int main(int argc, char* argv[]) {<cr><cr><cr>return 0;<cr>}<up><up><up><tab><c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> cerr cerr << "" << endl;<c-o>F"<c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> cout cout << "" << endl;<c-o>F"<c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> for for(int i = 0; i < ; i++) {<cr>}<up><c-o>f;<c-o>;<c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> if if() {<cr>}<up><c-o>f)<c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> while while() {<cr>}<up><c-o>f)<c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> struct struct {<cr>};<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> class class {<cr>};<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> pub public:<cr><c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> priv private:<cr><c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> enum enum {<cr>};<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> fn@ void () {<cr>}<up><c-o>f(<c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> templ template <typename T><c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> ret return ;<left><c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> retv return;<c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> incl #include <<c-r>=Eatchar('\s')<cr> + autocmd FileType cpp iabbrev <silent> <buffer> def #define <c-r>=Eatchar('\s')<cr> +augroup END + +augroup cs + autocmd! + autocmd FileType cs iabbrev <silent> <buffer> main@ using System;<cr><cr>class Program {<cr>static void Main(string[] args) {<cr>}<cr>}<up><c-o>O<c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> log@ Console.WriteLine("");<left><left><left><c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> for for(int i = 0; i < ; i++) {<cr>}<up><c-o>f;<c-o>;<c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> if if() {<cr>}<up><c-o>f)<c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> while while() {<cr>}<up><c-o>f)<c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> struct struct {<cr>};<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> class class {<cr>};<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> pub public + autocmd FileType cs iabbrev <silent> <buffer> priv private + autocmd FileType cs iabbrev <silent> <buffer> enum enum {<cr>};<up><c-o>f<space><space><c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> fn@ void () {<cr>}<up><c-o>f(<c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> ret return ;<left><c-r>=Eatchar('\s')<cr> + autocmd FileType cs iabbrev <silent> <buffer> retv return;<c-r>=Eatchar('\s')<cr> +augroup END + +augroup sh + autocmd FileType sh iabbrev <silent> <buffer> #! #!/bin/sh<cr>set -e<cr><c-r>=Eatchar('\s')<cr> + autocmd FileType sh iabbrev <silent> <buffer> if if [ ]; then<cr>fi<up><c-o>f]<left><c-r>=Eatchar('\s')<cr> + autocmd FileType sh iabbrev <silent> <buffer> elif elif [ ]; then<c-o>F]<left><c-r>=Eatchar('\s')<cr> + autocmd FileType sh iabbrev <silent> <buffer> for for x in ; do<cr>done<up><c-o>f;<c-r>=Eatchar('\s')<cr> + autocmd FileType sh iabbrev <silent> <buffer> while while ; do<cr>done<up><c-o>f;<c-r>=Eatchar('\s')<cr> + autocmd FileType sh iabbrev <silent> <buffer> fn@ () {<cr>}<up><c-o>0<c-r>=Eatchar('\s')<cr> +augroup END + +augroup bash + autocmd FileType bash iabbrev <silent> <buffer> while while ; do<cr>done<up><c-o>f;<c-r>=Eatchar('\s')<cr> + autocmd FileType bash iabbrev <silent> <buffer> for for x in ; do<cr>done<up><c-o>f;<c-r>=Eatchar('\s')<cr> + autocmd FileType bash iabbrev <silent> <buffer> fn@ () {<cr>}<up><c-o>0<c-r>=Eatchar('\s')<cr> + autocmd FileType bash iabbrev <silent> <buffer> if if [[ ]]; then<cr>fi<up><c-o>f]<left><c-r>=Eatchar('\s')<cr> + autocmd FileType bash iabbrev <silent> <buffer> elif elif [[ ]]; then<c-o>F]<left><left><c-r>=Eatchar('\s')<cr> + autocmd FileType bash iabbrev <silent> <buffer> #! #!/bin/bash<cr>set -e<cr><c-r>=Eatchar('\s')<cr> +augroup END + call plug#begin('~/.config/nvim/plugged') Plug 'vim-airline/vim-airline' @@ -63,10 +197,6 @@ call plug#end() nnoremap <C-n> :silent NERDTreeToggle<CR> -augroup nerdtree_start - autocmd FileType c silent if !exists('b:NERDTree') | silent NERDTree | au! nerdtree_start | endif -augroup END - augroup nerdtree " Exit Vim if NERDTree is the only window remaining in the only tab. autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif @@ -121,7 +251,7 @@ end -- Use a loop to conveniently call 'setup' on multiple servers and -- map buffer local keybindings when the language server attaches -local servers = { 'pyright', 'clangd' } +local servers = { 'pyright', 'clangd', 'rls', 'tsserver' } for _, lsp in ipairs(servers) do nvim_lsp[lsp].setup { on_attach = on_attach, @@ -130,6 +260,34 @@ for _, lsp in ipairs(servers) do } } end + +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities.textDocument.completion.completionItem.snippetSupport = true + +local html = nvim_lsp.html +local css = nvim_lsp.html +html.setup { + on_attach = on_attach, + capabilities = capabilities, + cmd = { "vscode-html-languageserver", "--stdio" }, + flags = { + debounce_text_changes = 150, + }, + settings = { + html = { + validate = true + } + } +} +css.setup { + on_attach = on_attach, + capabilities = capabilities, + cmd = { "vscode-css-languageserver", "--stdio" }, + flags = { + debounce_text_changes = 150, + }, +} + EOF " auto close scratch buffer (preview window)