Skip to content

Commit a8370b1

Browse files
committed
Fix joining single line Ruby methods without arguments
Joining single-line Ruby method _without_ arguments into an endless method definition was not working as the regexp pattern expected the `def` always to accept parenthesized arguments. Example: ```ruby def foo() bar end ``` Would be joined to: ```ruby def foo() = bar ``` However, the following, which is the idiomatic way to write Ruby methods without arguments, would not: ```ruby def foo bar end ```
1 parent 3bfd24e commit a8370b1

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

autoload/sj/ruby.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ endfunction
11131113

11141114
function! sj#ruby#JoinOnelineDef()
11151115
" adapted from vim-ruby
1116-
if search('\<def\s\+\%(\k\+\.\)\=\k\+[!?]\=\%((.*)\)\s*\%(#.*\)\=$', 'Wbc', line('.')) <= 0
1116+
if search('\<def\s\+\%(\k\+\.\)\=\k\+[!?]\=\s*\%((.*)\)\=\s*\%(#.*\)\=$', 'Wbc', line('.')) <= 0
11171117
return 0
11181118
endif
11191119

spec/plugin/ruby_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,5 +1901,47 @@ def foo(one, two)
19011901
def foo(one, two) = bar
19021902
EOF
19031903
end
1904+
1905+
specify "with the cursor on a definition without arguments" do
1906+
set_file_contents <<~EOF
1907+
def foo = bar
1908+
EOF
1909+
1910+
vim.search 'def'
1911+
split
1912+
1913+
assert_file_contents <<~EOF
1914+
def foo
1915+
bar
1916+
end
1917+
EOF
1918+
1919+
join
1920+
1921+
assert_file_contents <<~EOF
1922+
def foo = bar
1923+
EOF
1924+
end
1925+
1926+
specify "with the cursor on a definition arguments defaults" do
1927+
set_file_contents <<~EOF
1928+
def self.foo(bar = quux(42), ...) = bar
1929+
EOF
1930+
1931+
vim.search 'def'
1932+
split
1933+
1934+
assert_file_contents <<~EOF
1935+
def self.foo(bar = quux(42), ...)
1936+
bar
1937+
end
1938+
EOF
1939+
1940+
join
1941+
1942+
assert_file_contents <<~EOF
1943+
def self.foo(bar = quux(42), ...) = bar
1944+
EOF
1945+
end
19041946
end
19051947
end

0 commit comments

Comments
 (0)