neovim

Personal neovim configuration files
git clone git://gtms.dev:neovim
Log | Files | Refs

commit 1636f4244ba31634509a73b3d3fe2c6fc815a969
parent ad7bc5397783542ee92830a819d8746b06deb46b
Author: Tomas Nemec <nemi@skaut.cz>
Date:   Mon, 11 Apr 2022 08:55:29 +0200

feat: change api of creating commands

Diffstat:
Mftplugin/dart.lua | 22+++++++++++-----------
Mlua/tms/p/hexen.lua | 2+-
Dlua/tms/p/snippets/dart.lua | 148-------------------------------------------------------------------------------
Dlua/tms/p/snippets/html.lua | 9---------
Dlua/tms/p/snippets/init.lua | 38--------------------------------------
Dlua/tms/p/snippets/utils.lua | 32--------------------------------
Mlua/tms/plugins.lua | 18++++--------------
Mplugin/bufremove.lua | 23++++++++++++-----------
Mplugin/git_blame.lua | 15++++-----------
Mplugin/hexen.lua | 7+++++--
Mplugin/scratch.lua | 6++----
Mplugin/terminal.lua | 3++-
Mplugin/trun.lua | 1+
Atest/cmd.lua | 4++++
14 files changed, 46 insertions(+), 282 deletions(-)

diff --git a/ftplugin/dart.lua b/ftplugin/dart.lua @@ -1,19 +1,19 @@ vim.g.dart_style_guide = 2 vim.g.dart_html_in_string = true +local opts = {buffer = true} -- vim.cmd [[command! -buffer DartAnalyzer lua require('tms.ft.dart.analyze').qf2131()]] -vim.cmd [[command! -buffer DartDebug lua require('tms.ft.dart.debug').func()]] -vim.cmd [[command! -buffer DartPrint lua require('tms.ft.dart.debug').print()]] -vim.cmd [[command! -buffer DartOrganizeImports lua require('tms.ft.dart.lsp').organize_imports()]] -vim.cmd [[command! -buffer DartFixAll lua require('tms.ft.dart.lsp').fix_all()]] -vim.cmd [[command! -buffer DartExtract lua require('tms.ft.dart.lsp').extract()]] +vim.api.nvim_create_user_command('DartDebug', require('tms.ft.dart.debug').func, opts) +vim.api.nvim_create_user_command('DartPrint', require('tms.ft.dart.debug').print, opts) +vim.api.nvim_create_user_command('DartOrganizeImports', require('tms.ft.dart.lsp').organize_imports, opts) +vim.api.nvim_create_user_command('DartFixAll', require('tms.ft.dart.lsp').fix_all, opts) +vim.api.nvim_create_user_command('DartExtract', require('tms.ft.dart.lsp').extract, opts) -vim.keymap.set('n', '<leader>pp', function() require('tms.ft.dart.debug').print() end, {buffer = true, noremap = true}) -vim.keymap.set('n', '<leader>pa', function() require('tms.ft.dart.analyze').qf2131() end, - {buffer = true, noremap = true}) -vim.keymap.set('n', '<leader>po', function() require('tms.ft.dart.lsp').organize_imports() end, - {buffer = true, noremap = true}) -vim.keymap.set('n', '<leader>pf', function() require('tms.ft.dart.lsp').fix_all() end, {buffer = true, noremap = true}) +opts = {buffer = true, noremap = true} +vim.keymap.set('n', '<leader>pp', function() require('tms.ft.dart.debug').print() end, opts) +vim.keymap.set('n', '<leader>pa', function() require('tms.ft.dart.analyze').qf2131() end, opts) +vim.keymap.set('n', '<leader>po', function() require('tms.ft.dart.lsp').organize_imports() end, opts) +vim.keymap.set('n', '<leader>pf', function() require('tms.ft.dart.lsp').fix_all() end, opts) if vim.fn.getline(1):match('^#!.*dcli') then vim.cmd [[comp dcli]] diff --git a/lua/tms/p/hexen.lua b/lua/tms/p/hexen.lua @@ -1,4 +1,4 @@ -local utf8 = require('lua-utf8') +Local utf8 = require('lua-utf8') local api = vim.api local fn = vim.fn diff --git a/lua/tms/p/snippets/dart.lua b/lua/tms/p/snippets/dart.lua @@ -1,148 +0,0 @@ -local U = require 'snippets.utils' - --- function test_func(name) --- return function() U.match_indentation(string.format('%s(\'$1\', () {\n$0\n});', name)) end --- end - -local function test_func_snippet(name, nodesc) - local S - if nodesc then - S = [[ -${-1}(() { - $0 -});]] - else - S = [[ -${-1}('$1', () { - $0 -});]] - end - S = U.match_indentation(S) - return U.iterate_variables_by_id(S, -1, function(v) v.default = name end) -end - -return { - ['lib'] = [[library ${1};]], - ['im'] = [[import 'package:${1}/${2}.dart';]], - ['rgx'] = U.match_indentation [[new RegExp(r'${1}')]], - ['var'] = U.match_indentation [[var ${1} = ${2};]], - ['st'] = U.match_indentation [[static ${0}]], - ['fi'] = U.match_indentation [[final ${0}]], - ['re'] = U.match_indentation [[return ${0}]], - ['br'] = U.match_indentation [[break;]], - ['th'] = [[throw ${0}]], - ['cl'] = [[class ${1=camel_case(vim.fn.expand("%:t:r"))} ${0}]], - ['in'] = [[interface ${1=camel_case(vim.fn.expand("%:t:r"))} ${0}]], - ['imp'] = [[implements ${0}]], - ['ext'] = [[extends ${0}]], - - ['if'] = U.match_indentation [[ - if (${1:true}) { - ${0} - }]], - - ['ife'] = U.match_indentation [[ - if (${1:true}) { - ${2} - } else { - ${0} - } - ]], - - ['el'] = U.match_indentation [[ - else - ]], - - ['sw'] = U.match_indentation [[ - switch (${1}) { - ${0} - } - ]], - - ['cs'] = U.match_indentation [[ - case ${1}: - ${0}]], - - ['de'] = U.match_indentation [[ - default: - ${0}]], - - ['wh'] = U.match_indentation [[ - while (${1:/* condition */}) { - ${0} - }]], - - ['dowh'] = U.match_indentation [[ - do { - ${0} - } while (${0:/* condition */});]], - - ['as'] = U.match_indentation [[assert(${0:/* condition */});]], - - ['try'] = [[ - try { - ${0:${VISUAL}} - } catch (${1:Exception e}) { - }]], - - ['tryf'] = [[ - try { - ${0:${VISUAL}} - } catch (${1:Exception e}) { - } finally { - }]], - - ['main'] = [[ - main() { - ${0} - }]], - - ['for'] = U.match_indentation [[ - for (var ${1:i} = 0; $1 < ${2}; $1++) { - $0 - }]], - - ['fore'] = U.match_indentation [[ - for (final $1 in $2) { - $0 - }]], - - -- test - ['ex'] = U.match_indentation [[expect($1, $0);]], - ['exr'] = U.match_indentation [[expect($2, $0, reason: '$1');]], - ['tst'] = test_func_snippet('test'), - ['grp'] = test_func_snippet('group'), - ['stp'] = test_func_snippet('setUp', true), - ['td'] = test_func_snippet('tearDown', true), - - -- Angular - ['ngc'] = U.match_indentation [[ -@Component( - selector: '${1=vim.fn.expand("%:t:r"):gsub("_", "-")}', - templateUrl: '${2=vim.fn.expand("%:t:r")}.html', - styleUrls: ['$2.css'], - directives: [coreDirectives], - providers: [], - changeDetection: ChangeDetectionStrategy.OnPush, -) -class ${3=camel_case(vim.fn.expand("%:t:r"))} { - $0 -}]], - - ['ngcp'] = U.match_indentation [[ -@Component( - selector: '${1=vim.fn.expand("%:t:r"):gsub("_", "-")}', - templateUrl: '${2=vim.fn.expand("%:t:r")}.html', - styleUrls: ['$2.css'], - directives: [coreDirectives], - providers: [], - changeDetection: ChangeDetectionStrategy.OnPush, -) -class ${3=camel_case(vim.fn.expand("%:t:r"))} { - final ChangeDetectorRef changeDetector; - - $3(this.changeDetector); - - $0 -}]], -} diff --git a/lua/tms/p/snippets/html.lua b/lua/tms/p/snippets/html.lua @@ -1,9 +0,0 @@ -return { - -- Angular - ['ngif'] = [[*ngIf="${1}"]], - ['ngf'] = [[*ngFor="let ${1} of ${2}"]], - ['ngfi'] = [[*ngFor="let ${1} of ${2}; let i=index"]], - ['ngb'] = [[[${1}]="${2}"]], - ['nge'] = [[(${1})="${2}"]], - ['ngbe'] = [[[(${1})]="${2}"]], -} diff --git a/lua/tms/p/snippets/init.lua b/lua/tms/p/snippets/init.lua @@ -1,38 +0,0 @@ -local snip = require('snippets') -local U = require('snippets.utils') - -require('tms.p.snippets.utils') - -local M = {} - -M.setup = function() - snip.use_suggested_mappings() - snip.set_ux(require 'snippets.inserters.vim_input') - - snip.snippets = { - _global = { - date = os.date('%d.%m.%Y'), - todo = U.force_comment [[TODO(${=io.popen("id -un"):read("*l")}) ${=os.date('%d.%m.%Y')}: ]], - note = U.force_comment [[NOTE(${=io.popen("id -un"):read("*l")}): ]], - bang = [[#!/usr/bin/env ${1}]], - copyright = U.force_comment [[Copyright (C) Tomáš Němec ${=os.date("%Y")}]], - }, - go = {bang = [[//usr/bin/go run \$0 \$@; exit \$?]]}, - dart = require('tms.p.snippets.dart'), - html = require('tms.p.snippets.html'), - lua = { - req = [[local ${2:${1|S.v:match"([^.()]+)[()]*$"}} = require('$1')]], - func = [[function${1|vim.trim(S.v):gsub("^%S"," %0")}(${2|vim.trim(S.v)})$0 end]], - ['loc'] = [[local ${2:${1|S.v:match"([^.()]+)[()]*$"}} = ${1}]], - ['for'] = U.match_indentation [[ -for ${1:i}, ${2:v} in ipairs(${3:t}) do - $0 -end]], - sl = [[{${2|create_list_of_strings(S.v)}}]], - key = [[['${1}'] = $0]], - }, - scss = {['v'] = [[var(--${1})]], ['ngif'] = [[*ngIf="${1}"]]}, - } -end - -return M diff --git a/lua/tms/p/snippets/utils.lua b/lua/tms/p/snippets/utils.lua @@ -1,32 +0,0 @@ -function create_list_of_strings(input) - local str = '' - local function wrap_quotes(x) return '\'' .. x .. '\'' end - for item, _ in input:gmatch('([%s%w%.%_]+),') do - local str_end = (' '):rep(vim.bo.shiftwidth) .. wrap_quotes(item) - if str == '' then - str = str .. str_end - else - str = str .. ',' .. str_end - end - end - - return str -end - -function camel_case(s) - local upper = true - local output = {} - for c in string.gmatch(s, '.') do - if c == '_' then - upper = true - else - if upper then - table.insert(output, c:upper()) - upper = false - else - table.insert(output, c) - end - end - end - return table.concat(output, '') -end diff --git a/lua/tms/plugins.lua b/lua/tms/plugins.lua @@ -293,9 +293,9 @@ return packer.startup({ requires = {'nvim-lua/plenary.nvim'}, config = function() require('rest-nvim').setup() - vim.cmd [[command! Rest :lua require("rest-nvim").run()]] - vim.cmd [[command! RestPreview :lua require("rest-nvim").run(true)]] - vim.cmd [[command! RestLast :lua require("rest-nvim").last()]] + vim.api.nvim_create_user_command('Rest', require('rest-nvim').run) + vim.api.nvim_create_user_command('RestPreview', function() require('rest-nvim').run(true) end) + vim.api.nvim_create_user_command('RestLast', require('rest-nvim').last) end, } @@ -311,7 +311,6 @@ return packer.startup({ use {'dart-lang/dart-vim-plugin', ft = {'dart'}} -- snippets - use {'norcalli/snippets.nvim', config = function() require('tms.p.snippets').setup() end} use {'L3MON4D3/LuaSnip', config = function() require('tms.p.luasnip').setup() end} -- completion @@ -420,7 +419,7 @@ return packer.startup({ } require('tms.p.treesitter').setup() -- hack - vim.cmd [[command! TSHRefresh execute 'write|TSBufEnable highlight']] + vim.api.nvim_create_user_command('TSHRefresh', 'write|TSBufEnable highlight') vim.keymap.set('n', '<leader>su', '<cmd>TSHRefresh<cr>', {noremap = true}) end, } @@ -471,15 +470,6 @@ return packer.startup({ config = function() require('telescope').load_extension('dap') end, } use { - 'nvim-telescope/telescope-snippets.nvim', - after = {'telescope.nvim'}, - config = function() - require('telescope').load_extension('snippets') - vim.keymap.set('i', '<c-j>', '<cmd>lua require"snippets".expand_or_advance()<cr>', {noremap = true}) - vim.keymap.set('i', '<c-k>', '<cmd>lua require"snippets".advance_snippet(-1)<cr>', {noremap = true}) - end, - } - use { 'nvim-telescope/telescope-media-files.nvim', after = {'telescope.nvim'}, config = function() require('telescope').load_extension('media_files') end, diff --git a/plugin/bufremove.lua b/plugin/bufremove.lua @@ -142,19 +142,20 @@ end ---- Compute 'true' buffer id (strictly positive integer). Treat `nil` and 0 as ---- current buffer. function H.normalize_buf_id(buf_id) - if buf_id == nil or buf_id == 0 then return vim.api.nvim_get_current_buf() end + if buf_id == nil or buf_id == 0 or buf_id == '' then return vim.api.nvim_get_current_buf() end return buf_id end function H.notify(msg) vim.notify(string.format('(bufremove) %s', msg)) end -_G.BufRemove = BufRemove - -vim.cmd [[command! -nargs=? BufDelete lua BufRemove.delete(<args>)]] -vim.cmd [[command! -nargs=? BufWipeout lua BufRemove.wipeout(<args>)]] -vim.cmd [[command! -nargs=? BufUnshow lua BufRemove.unshow(<args>)]] -vim.cmd [[command! -nargs=? BufWinUnshow lua BufRemove.unshow_in_window(<args>)]] -vim.keymap.set('n', '<leader>bd', _G.BufRemove.delete, {noremap = true}) -vim.keymap.set('n', '<leader>bw', _G.BufRemove.wipeout, {noremap = true}) -vim.keymap.set('n', '<leader>bu', _G.BufRemove.unshow, {noremap = true}) -vim.keymap.set('n', '<leader>bU', _G.BufRemove.unshow_in_window, {noremap = true}) +vim.api.nvim_create_user_command('BufDelete', function(data) BufRemove.delete(data.args, data.bang) end, + {nargs = '?', bang = true}) +vim.api.nvim_create_user_command('BufWipeout', function(data) BufRemove.wipeout(data.args, data.bang) end, + {nargs = '?', bang = true}) +vim.api.nvim_create_user_command('BufUnshow', function(data) BufRemove.unshow(data.args) end, {nargs = '?'}) +vim.api + .nvim_create_user_command('BufWinUnshow', function(data) BufRemove.unshow_in_window(data.args) end, {nargs = '?'}) +vim.keymap.set('n', '<leader>bd', BufRemove.delete, {noremap = true}) +vim.keymap.set('n', '<leader>bw', BufRemove.wipeout, {noremap = true}) +vim.keymap.set('n', '<leader>bu', BufRemove.unshow, {noremap = true}) +vim.keymap.set('n', '<leader>bU', BufRemove.unshow_in_window, {noremap = true}) diff --git a/plugin/git_blame.lua b/plugin/git_blame.lua @@ -35,8 +35,7 @@ local create_win = function() -- the grep is an ugly but super performant way to remove everything up until the first occurance of " (" -- which strips off the commit hash and filename from the git blame log - vim.api.nvim_command('read!git blame --date human ' .. startingBuf .. - ' | grep -o " (.* [0-9]\\+)" | cut -c 3- ') + vim.api.nvim_command('read!git blame --date human ' .. startingBuf .. ' | grep -o " (.* [0-9]\\+)" | cut -c 3- ') -- vim.api.nvim_command('read!git blame --date human ' .. startingBuf .. ' | sed -n "s/ (/&\n/;s/.*\n//p"' ) vim.cmd('normal gg') vim.cmd('normal dd') -- there is an empty line at the top of the file - remove it @@ -44,13 +43,9 @@ end local get_current_window = function() return vim.api.nvim_get_current_win() end -local function get_current_cursor_location(window) - return vim.api.nvim_win_get_cursor(window) -end +local function get_current_cursor_location(window) return vim.api.nvim_win_get_cursor(window) end -local function set_cursor_position(window, position) - return vim.api.nvim_win_set_cursor(window, position) -end +local function set_cursor_position(window, position) return vim.api.nvim_win_set_cursor(window, position) end local blame = function() local starting_window = get_current_window() @@ -74,6 +69,4 @@ local blame = function() set_cursor_position(starting_window, starting_cursor_location) end -_G.GitBlame = blame - -vim.cmd [[command! GitBlame lua GitBlame()]] +vim.api.nvim_create_user_command('GitBlame', blame) diff --git a/plugin/hexen.lua b/plugin/hexen.lua @@ -1,2 +1,5 @@ -vim.cmd [[command! -range HexEncode lua require('tms.p.hexen').encode()]] -vim.cmd [[command! -range HexDecode lua require('tms.p.hexen').decode()]] +-- TODO Arguments andrange passed +vim.api.nvim_create_user_command('HexEncode', require('tms.p.hexen').encode, {range = true}) +vim.api.nvim_create_user_command('HexDecode', require('tms.p.hexen').decode, {range = true}) +-- vim.cmd [[command! -range HexEncode lua require('tms.p.hexen').encode()]] +-- vim.cmd [[command! -range HexDecode lua require('tms.p.hexen').decode()]] diff --git a/plugin/scratch.lua b/plugin/scratch.lua @@ -6,7 +6,5 @@ local open = function() vim.api.nvim_win_set_buf(win, buf) end -_G.Scratch = {open = open} - -vim.cmd [[command! Scratch lua Scratch.open()]] -vim.keymap.set('n', '<space>j', Scratch.open, {noremap = true}) +vim.api.nvim_create_user_command('Scratch', open) +vim.keymap.set('n', '<space>j', open, {noremap = true}) diff --git a/plugin/terminal.lua b/plugin/terminal.lua @@ -26,4 +26,5 @@ vim.api.nvim_create_autocmd('BufLeave', { command = 'let g:SexyScroller_ScrollTime=10|let g:SexyScroller_CursorTime=5', }) -vim.cmd [[command! -nargs=1 TerminalRun lua require('tms.p.terminal').run('<args>')]] +vim.api.nvim_create_user_command('TerminalRun', function(data) require('tms.p.terminal').run(data.args) end, + {nargs = '1'}) diff --git a/plugin/trun.lua b/plugin/trun.lua @@ -58,6 +58,7 @@ end -- Make functions globally accessible _G.Trun = {complete = complete, qf = to_qf} +-- TODO nvim_create_user_command vim.cmd [[ fun! TrunComplete(A,L,P) return v:lua.Trun.complete() diff --git a/test/cmd.lua b/test/cmd.lua @@ -0,0 +1,4 @@ +local test = function(data) + D(data) +end +vim.api.nvim_create_user_command('Asdf', test, {nargs = '?', bang = true, range=true})