From 15a7ac4890dea48be140350c295d4d9ea05292ee Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Tue, 2 Nov 2021 17:30:24 +0100 Subject: vim: switch from ultisnips+snippets.nvim to snippy --- .vim/lua/plugins.lua | 30 +++++------ .vim/lua/snips.lua | 150 +++++++++++---------------------------------------- 2 files changed, 45 insertions(+), 135 deletions(-) (limited to '.vim/lua') diff --git a/.vim/lua/plugins.lua b/.vim/lua/plugins.lua index 7b15a7a..7d19781 100644 --- a/.vim/lua/plugins.lua +++ b/.vim/lua/plugins.lua @@ -1,4 +1,6 @@ --- vim: foldenable +-- vim: fen fdm=marker fdl=0 +---@diagnostic disable: undefined-global + -- load by calling `lua require('plugins')` from your init.vim local g = vim.g @@ -332,25 +334,19 @@ return require('packer').startup({function() use {'tjdevries/astronauta.nvim'} -- snippets - use {'SirVer/ultisnips', -- {{{ - disable = true, - requires = {'honza/vim-snippets'}, + use {'dcampos/nvim-snippy', -- {{{} + requires = { + 'honza/vim-snippets' + }, config = function() - vim.g.UltiSnipsJumpForwardTrigger = "" - vim.g.UltiSnipsJumpBackwardTrigger = "" + local S = require('snippy') + local K = require('astronauta.keymap') + K.inoremap { '', function() if S.can_jump(1) then S.next() else return '' end end } + K.inoremap { '', function() if S.can_jump(-1) then S.previous() else return '' end end } + K.snoremap { '', function() if S.can_jump(1) then S.next() else return '' end end } + K.snoremap { '', function() if S.can_jump(-1) then S.previous() else return '' end end } end} -- }}} - use {'norcalli/snippets.nvim', - config = function() - require('snippets').use_suggested_mappings() - -- NOTE(robert): write your own inserter using the plenary helpers - require('snips') - end} - - -- use {'L3MON4D3/LuaSnip', - -- config = "require('plugins._ultisnips')", - -- } - use {'junegunn/vim-easy-align'} use {'editorconfig/editorconfig-vim', -- {{{ diff --git a/.vim/lua/snips.lua b/.vim/lua/snips.lua index b15ffbf..b8c8b77 100644 --- a/.vim/lua/snips.lua +++ b/.vim/lua/snips.lua @@ -1,122 +1,36 @@ --- vim: foldenable +local M = {} -local system = function(cmd) - return function() - local args = vim.split(cmd, ' ') - local command = args[1] - table.remove(args, 1) - local output = require('plenary.job'):new({ - command = command, - args = args, - enable_recording = true, - }):sync() - if #output > 1 then - return table.concat(output, '\n') - else - return output[1] - end - end +M.exec = function(cmd) + return io.popen(cmd):read'*l' end -local U = require'snippets.utils' -require('snippets').snippets = { - _global = { - today = [[${=os.date("%Y-%m-%d")}]], - nowutc = [[${=os.date("!%Y-%m-%dT%H:%M:%SZ")}]], - nowloc = [[${=os.date("%Y-%m-%dT%H:%M:%S%z")}]], - - me = [[${=io.popen('git config --get user.name'):read"*l"} <${=io.popen('git config --get user.email'):read"*l"}>]], - - ok = "✔", - bad = "✘", - - todo = U.force_comment [[TODO(${=io.popen("id -un"):read"*l"}): ]], - note = U.force_comment [[NOTE(${=io.popen("id -un"):read"*l"}): ]], - }, - - sh = { -- {{{ - skel = U.match_indentation [[ -#!/bin/sh -set -e -[ -n "\$DEBUG" ] && set -x -]], - - deps = U.match_indentation [[ -# check requirements -for tool in ${1}; do - if ! command -v "\$tool" >/dev/null; then - printf "%s: %s not installed but required\n" "\$(basename "\$0")" "\$tool" >&2 - exit 1 - fi -done -]], - - getopt = U.match_indentation [[ -${2}= -while getopts ${1}h opt; do - case "\$opt" in - ${1}) ${2}=1 ;; - h|?) - printf "usage: %s [-h]\n" "\$(basename "\$0")" - printf "\n" - printf " -${1} \t ${2}\n" - printf "\n" - exit 2 ;; - esac -done -shift \$((OPTIND - 1)) -]], - }, -- }}} - - tex = { -- {{{ - brief = U.match_indentation [[ -\documentclass{dinbrief} -\usepackage{graphicx} -\usepackage[a4paper,margin=30truemm]{geometry} -% language -\usepackage{polyglossia} -\setdefaultlanguage[spelling=new]{german} -% font -\usepackage{fontspec} -\setmainfont{IBM Plex Sans} -\setmonofont{IBM Plex Mono} -% dinbrief options -\nowindowtics -\nowindowrules - -% unterschrift -\newcommand{\setsign}{\includegraphics[height=30truemm]{/home/robert/Pictures/sign.png}} -\signature{Robert Günzler} -\address{ -% absender - Robert Günzler \\\\ - ${4:STREET} \\\\ - ${5:PLZ} Berlin} - -\date{\today} -\place{Berlin} - -% betreff -% \subject{{\bf anliegen}} - -\begin{document} -\begin{letter}{ -% empfänger - ${1} - -\opening{${2:Sehr geehrte Damen und Herren,}} - -${0} - -\vspace{10truemm} -\closing[\protect\setsign]{${3:Mit freundlichen Grüßen}} -\end{letter} -\end{document} -]], - }, -- }}} +local function find_sub(s, replacement, pattern, start, special) + local x1, x2 = s:find(pattern, start, special) + if x1 then + return s:sub(1, x1-1) .. replacement .. s:sub(x2+1), x1, x2 + end + return s +end +-- Only works with prefix comments. +local function get_line_comment() + local cms = vim.bo.commentstring + -- This whole pesc dance is a bummer. + local pattern = '^(%s*)' .. + (find_sub(vim.pesc((find_sub(cms, '\0', '%s', 1, true))), '(%s*).*', '\0', 1, true)) + local pre_ws, inner_ws = vim.api.nvim_get_current_line():match(pattern) + if pre_ws then + return pre_ws .. cms:format('') .. inner_ws + end + return '' +end +M.comment = function(str) + -- add space + local prefix = vim.bo.commentstring:format(''):gsub('%S$', '%0 ') + local comment = get_line_comment() + if comment ~= '' then + prefix = '' + end + return prefix .. str +end - org = { - todo = [[* TODO ]], - note = [[* ]], - } -} +return M -- cgit 1.4.1