From e4d886d4a798208d2c5dd10816cd3f47a8f5f431 Mon Sep 17 00:00:00 2001 From: w0rp Date: Tue, 6 Jun 2017 22:27:20 +0100 Subject: [PATCH] Add a function for computing the number of arguments for a function --- autoload/ale/util.vim | 15 +++++++++++ test/test_function_arg_count.vader | 41 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 test/test_function_arg_count.vader diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim index 03abacb7..90e052a7 100644 --- a/autoload/ale/util.vim +++ b/autoload/ale/util.vim @@ -123,3 +123,18 @@ function! ale#util#GetMatches(lines, patterns) abort return l:matches endfunction + +" Given the name of a function, a Funcref, or a lambda, return the number +" of named arguments for a function. +function! ale#util#FunctionArgCount(function) abort + let l:Function = ale#util#GetFunction(a:function) + + redir => l:output + silent function Function + redir END + + let l:match = matchstr(split(l:output, "\n")[0], '\v\([^)]+\)')[1:-2] + let l:arg_list = filter(split(l:match, ', '), 'v:val !=# ''...''') + + return len(l:arg_list) +endfunction diff --git a/test/test_function_arg_count.vader b/test/test_function_arg_count.vader new file mode 100644 index 00000000..748eed33 --- /dev/null +++ b/test/test_function_arg_count.vader @@ -0,0 +1,41 @@ +Before: + function! Func0() + endfunction + function! Func1(x) + endfunction + function! Func2(x,y) + endfunction + function! Func3(x,y,z) + endfunction + function! Func3a(x,y,z,...) + endfunction + +After: + delfunction Func0 + delfunction Func1 + delfunction Func2 + delfunction Func3 + delfunction Func3a + +Execute(We should be able to compute the argument count for function names): + AssertEqual 0, ale#util#FunctionArgCount('Func0') + AssertEqual 1, ale#util#FunctionArgCount('Func1') + AssertEqual 2, ale#util#FunctionArgCount('Func2') + AssertEqual 3, ale#util#FunctionArgCount('Func3') + AssertEqual 3, ale#util#FunctionArgCount('Func3a') + +Execute(We should be able to compute the argument count for Funcrefs): + AssertEqual 0, ale#util#FunctionArgCount(function('Func0')) + AssertEqual 1, ale#util#FunctionArgCount(function('Func1')) + AssertEqual 2, ale#util#FunctionArgCount(function('Func2')) + AssertEqual 3, ale#util#FunctionArgCount(function('Func3')) + AssertEqual 3, ale#util#FunctionArgCount(function('Func3a')) + +Execute(We should be able to compute the argument count for lambdas): + if has('lambda') + AssertEqual 0, ale#util#FunctionArgCount({->1}) + AssertEqual 1, ale#util#FunctionArgCount({x->1}) + AssertEqual 2, ale#util#FunctionArgCount({x,y->1}) + AssertEqual 3, ale#util#FunctionArgCount({x,y,z->1}) + AssertEqual 3, ale#util#FunctionArgCount({x,y,z,...->1}) + endif