dist: transpile cfilter.vim => cfilter.lua #21662

Vim has changed cfilter.vim from vimscript to vim9script.
Nvim supports only vimscript, not vim9script.
This commit is contained in:
TJ DeVries 2023-01-08 15:31:38 -05:00 committed by GitHub
parent 266a1c61b9
commit 904c13e6b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 243 additions and 62 deletions

View File

@ -27,6 +27,29 @@ local vim9 = (function()
end
end
M.fn_ref = function(module, name, copied, ...)
for _, val in ipairs({ ... }) do
table.insert(copied, val)
end
local funcref = name
if type(funcref) == 'function' then
return funcref(unpack(copied))
elseif type(funcref) == 'string' then
if vim.fn.exists('*' .. funcref) == 1 then
return vim.fn[funcref](unpack(copied))
end
if module[funcref] then
module[funcref](unpack(copied))
end
error('unknown function: ' .. funcref)
else
error(string.format('unable to call funcref: %s', funcref))
end
end
M.fn_mut = function(name, args, info)
local result = vim.fn._Vim9ScriptFn(name, args)
for idx, val in pairs(result[2]) do

View File

@ -0,0 +1,114 @@
----------------------------------------
-- This file is generated via github.com/tjdevries/vim9jit
-- For any bugs, please first consider reporting there.
----------------------------------------
-- Ignore "value assigned to a local variable is unused" because
-- we can't guarantee that local variables will be used by plugins
-- luacheck: ignore 311
local vim9 = require('_vim9script')
local M = {}
local Qf_filter = nil
-- vim9script
-- # cfilter.vim: Plugin to filter entries from a quickfix/location list
-- # Last Change: Jun 30, 2022
-- # Maintainer: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
-- # Version: 2.0
-- #
-- # Commands to filter the quickfix list:
-- # :Cfilter[!] /{pat}/
-- # Create a new quickfix list from entries matching {pat} in the current
-- # quickfix list. Both the file name and the text of the entries are
-- # matched against {pat}. If ! is supplied, then entries not matching
-- # {pat} are used. The pattern can be optionally enclosed using one of
-- # the following characters: ', ", /. If the pattern is empty, then the
-- # last used search pattern is used.
-- # :Lfilter[!] /{pat}/
-- # Same as :Cfilter but operates on the current location list.
-- #
Qf_filter = function(qf, searchpat, bang)
qf = vim9.bool(qf)
local Xgetlist = function() end
local Xsetlist = function() end
local cmd = ''
local firstchar = ''
local lastchar = ''
local pat = ''
local title = ''
local Cond = function() end
local items = {}
if vim9.bool(qf) then
Xgetlist = function(...)
return vim.fn['getqflist'](...)
end
Xsetlist = function(...)
return vim.fn['setqflist'](...)
end
cmd = ':Cfilter' .. bang
else
Xgetlist = function(...)
return vim9.fn_ref(M, 'getloclist', vim.deepcopy({ 0 }), ...)
end
Xsetlist = function(...)
return vim9.fn_ref(M, 'setloclist', vim.deepcopy({ 0 }), ...)
end
cmd = ':Lfilter' .. bang
end
firstchar = vim9.index(searchpat, 0)
lastchar = vim9.slice(searchpat, -1, nil)
if firstchar == lastchar and (firstchar == '/' or firstchar == '"' or firstchar == "'") then
pat = vim9.slice(searchpat, 1, -2)
if pat == '' then
-- # Use the last search pattern
pat = vim.fn.getreg('/')
end
else
pat = searchpat
end
if pat == '' then
return
end
if bang == '!' then
Cond = function(_, val)
return vim9.ops.NotRegexpMatches(val.text, pat)
and vim9.ops.NotRegexpMatches(vim9.fn.bufname(val.bufnr), pat)
end
else
Cond = function(_, val)
return vim9.ops.RegexpMatches(val.text, pat)
or vim9.ops.RegexpMatches(vim9.fn.bufname(val.bufnr), pat)
end
end
items = vim9.fn_mut('filter', { Xgetlist(), Cond }, { replace = 0 })
title = cmd .. ' /' .. pat .. '/'
Xsetlist({}, ' ', { ['title'] = title, ['items'] = items })
end
vim.api.nvim_create_user_command('Cfilter', function(__vim9_arg_1)
Qf_filter(true, __vim9_arg_1.args, (__vim9_arg_1.bang and '!' or ''))
end, {
bang = true,
nargs = '+',
complete = nil,
})
vim.api.nvim_create_user_command('Lfilter', function(__vim9_arg_1)
Qf_filter(false, __vim9_arg_1.args, (__vim9_arg_1.bang and '!' or ''))
end, {
bang = true,
nargs = '+',
complete = nil,
})
-- # vim: shiftwidth=2 sts=2 expandtab
return M

View File

@ -1,62 +0,0 @@
" cfilter.vim: Plugin to filter entries from a quickfix/location list
" Last Change: Aug 23, 2018
" Maintainer: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
" Version: 1.1
"
" Commands to filter the quickfix list:
" :Cfilter[!] /{pat}/
" Create a new quickfix list from entries matching {pat} in the current
" quickfix list. Both the file name and the text of the entries are
" matched against {pat}. If ! is supplied, then entries not matching
" {pat} are used. The pattern can be optionally enclosed using one of
" the following characters: ', ", /. If the pattern is empty, then the
" last used search pattern is used.
" :Lfilter[!] /{pat}/
" Same as :Cfilter but operates on the current location list.
"
if exists("loaded_cfilter")
finish
endif
let loaded_cfilter = 1
func s:Qf_filter(qf, searchpat, bang)
if a:qf
let Xgetlist = function('getqflist')
let Xsetlist = function('setqflist')
let cmd = ':Cfilter' . a:bang
else
let Xgetlist = function('getloclist', [0])
let Xsetlist = function('setloclist', [0])
let cmd = ':Lfilter' . a:bang
endif
let firstchar = a:searchpat[0]
let lastchar = a:searchpat[-1:]
if firstchar == lastchar &&
\ (firstchar == '/' || firstchar == '"' || firstchar == "'")
let pat = a:searchpat[1:-2]
if pat == ''
" Use the last search pattern
let pat = @/
endif
else
let pat = a:searchpat
endif
if pat == ''
return
endif
if a:bang == '!'
let cond = 'v:val.text !~# pat && bufname(v:val.bufnr) !~# pat'
else
let cond = 'v:val.text =~# pat || bufname(v:val.bufnr) =~# pat'
endif
let items = filter(Xgetlist(), cond)
let title = cmd . ' /' . pat . '/'
call Xsetlist([], ' ', {'title' : title, 'items' : items})
endfunc
com! -nargs=+ -bang Cfilter call s:Qf_filter(1, <q-args>, <q-bang>)
com! -nargs=+ -bang Lfilter call s:Qf_filter(0, <q-args>, <q-bang>)

View File

@ -0,0 +1,106 @@
local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local command = helpers.command
local eq = helpers.eq
local funcs = helpers.funcs
describe('cfilter.lua', function()
before_each(function()
clear()
command('packadd cfilter')
end)
for _, list in ipairs({
{
name = 'Cfilter',
get = funcs.getqflist,
set = funcs.setqflist,
},
{
name = 'Lfilter',
get = function()
return funcs.getloclist(0)
end,
set = function(items)
return funcs.setloclist(0, items)
end,
},
}) do
local filter = function(s, bang)
if not bang then
bang = ''
else
bang = '!'
end
command(string.format('%s%s %s', list.name, bang, s))
end
describe((':%s'):format(list.name), function()
it('does not error on empty list', function()
filter('nothing')
eq({}, funcs.getqflist())
end)
it('requires an argument', function()
local ok = pcall(filter, '')
eq(false, ok)
end)
local test = function(name, s, res, map, bang)
it(('%s (%s)'):format(name, s), function()
list.set({
{ filename = 'foo', lnum = 1, text = 'bar' },
{ filename = 'foo', lnum = 2, text = 'baz' },
{ filename = 'foo', lnum = 3, text = 'zed' },
})
filter(s, bang)
local got = list.get()
if map then
got = map(got)
end
eq(res, got)
end)
end
local toname = function(qflist)
return funcs.map(qflist, 'v:val.text')
end
test('filters with no matches', 'does not match', {})
test('filters with matches', 'ba', { 'bar', 'baz' }, toname)
test('filters with matches', 'z', { 'baz', 'zed' }, toname)
test('filters with matches', '^z', { 'zed' }, toname)
test('filters with not matches', '^z', { 'bar', 'baz' }, toname, true)
it('also supports using the / register', function()
list.set({
{ filename = 'foo', lnum = 1, text = 'bar' },
{ filename = 'foo', lnum = 2, text = 'baz' },
{ filename = 'foo', lnum = 3, text = 'zed' },
})
funcs.setreg('/', 'ba')
filter('/')
eq({ 'bar', 'baz' }, toname(list.get()))
end)
it('also supports using the / register with bang', function()
list.set({
{ filename = 'foo', lnum = 1, text = 'bar' },
{ filename = 'foo', lnum = 2, text = 'baz' },
{ filename = 'foo', lnum = 3, text = 'zed' },
})
funcs.setreg('/', 'ba')
filter('/', true)
eq({ 'zed' }, toname(list.get()))
end)
end)
end
end)