Skip to content

Commit 937f216

Browse files
committed
Fix function argument detection for C
1 parent cace6c9 commit 937f216

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

autoload/sj.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,13 @@ function! sj#LocateBracesOnLine(open, close, ...)
705705

706706
" optional skip parameter
707707
if a:0 > 0
708-
let skip = sj#SkipSyntax(a:1)
708+
if type(a:1) == type([])
709+
" it's a list of groups, convert it to a skip expression:
710+
let skip = sj#SkipSyntax(a:1)
711+
else
712+
" use the skip expression as-is
713+
let skip = a:1
714+
endif
709715
else
710716
let skip = ''
711717
endif

autoload/sj/c.vim

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22
let s:skip = sj#SkipSyntax(['cComment', 'cCommentL', 'cString', 'cCppString', 'cBlock'])
33

44
function! sj#c#SplitFuncall()
5-
if sj#SearchUnderCursor('(.\{-})', '', s:skip) <= 0
5+
let [from, to] = sj#LocateBracesAroundCursor('(', ')', s:skip)
6+
if from < 0 && to < 0
67
return 0
78
endif
89

9-
call sj#PushCursor()
10-
11-
normal! l
12-
let start = col('.')
13-
normal! h%h
14-
let end = col('.')
15-
16-
let items = sj#ParseJsonObjectBody(start, end)
10+
let items = sj#ParseJsonObjectBody(from + 1, to - 1)
1711

1812
let body = "("
1913
if sj#settings#Read('c_argument_split_first_newline')
@@ -28,8 +22,6 @@ function! sj#c#SplitFuncall()
2822
let body .= ")"
2923
endif
3024

31-
call sj#PopCursor()
32-
3325
call sj#ReplaceMotion('va(', body)
3426
return 1
3527
endfunction

spec/plugin/c_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
assert_file_contents "if (val1 && val2 || val3);"
2626
end
2727

28-
specify "function_call" do
28+
specify "function call" do
2929
set_file_contents "myfunction(lots, of, different, parameters)"
3030

3131
vim.search '('
@@ -43,6 +43,24 @@
4343
assert_file_contents "myfunction(lots, of, different, parameters)"
4444
end
4545

46+
specify "nested function call" do
47+
set_file_contents "myfunction(other_function(lots, of, different, parameters))"
48+
49+
vim.search 'lots'
50+
split
51+
52+
assert_file_contents <<~EOF
53+
myfunction(other_function(lots,
54+
of,
55+
different,
56+
parameters))
57+
EOF
58+
59+
join
60+
61+
assert_file_contents "myfunction(other_function(lots, of, different, parameters))"
62+
end
63+
4664
specify "ignores strings" do
4765
set_file_contents "\"myfunction(several, parameters)\""
4866

0 commit comments

Comments
 (0)