Skip to content

Commit

Permalink
Merge branch 'line-continuations'
Browse files Browse the repository at this point in the history
* line-continuations:
  Implement line continuations for sh
  • Loading branch information
AndrewRadev committed Nov 18, 2023
2 parents 8b00772 + 1f73572 commit 2f2d200
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
18 changes: 18 additions & 0 deletions autoload/sj/sh.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ function! sj#sh#SplitBySemicolon()
return 1
endfunction

function! sj#sh#SplitWithBackslash()
if !search('\S', 'Wc', line('.'))
return 0
endif

exe "normal! i\\\<cr>"
return 1
endfunction

function! sj#sh#JoinWithSemicolon()
if !nextnonblank(line('.') + 1)
return 0
Expand All @@ -20,3 +29,12 @@ function! sj#sh#JoinWithSemicolon()
call sj#Keeppatterns('s/;\=\s*\n\_s*/; /e')
return 1
endfunction

function! sj#sh#JoinBackslashedLine()
if getline('.') !~ '\\\s*$'
return 0
endif

call sj#Keeppatterns('s/\\\=\s*\n\_s*//e')
return 1
endfunction
11 changes: 11 additions & 0 deletions doc/splitjoin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,17 @@ for the `sh` and `zsh` filetypes.
echo "one"
echo "two"
<
If the line is not made up of semicolon-separated commands, it gets broken up
with a backslash at the cursor position, for example with the cursor on the
pipe:
>
echo "one" | wc -c
echo "one" \
| wc -l
<
If there is a broken line like that, joining should always join it first,
before trying anything else.

==============================================================================
TEX *splitjoin-tex*
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/sh/splitjoin.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
let b:splitjoin_split_callbacks = [
\ 'sj#sh#SplitBySemicolon',
\ 'sj#sh#SplitWithBackslash',
\ ]

let b:splitjoin_join_callbacks = [
\ 'sj#sh#JoinBackslashedLine',
\ 'sj#sh#JoinWithSemicolon',
\ ]
50 changes: 48 additions & 2 deletions spec/plugin/sh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
describe "sh" do
let(:filename) { 'test.sh' }

describe "splitjoining by semicolon" do
before :each do
vim.set(:expandtab)
vim.set(:shiftwidth, 2)
end

describe "by semicolon" do
specify "simple case" do
set_file_contents <<~EOF
echo "one"; echo "two"
Expand Down Expand Up @@ -51,5 +56,46 @@
EOF
end
end
end

describe "with backslash" do
specify "simple case" do
set_file_contents <<~EOF
echo "one" | wc -c
EOF

vim.search('|')
split

assert_file_contents <<~EOF
echo "one" \\
| wc -c
EOF

join

assert_file_contents <<~EOF
echo "one" | wc -c
EOF
end

specify "between words, finds closest non-whitespace forward" do
set_file_contents <<~EOF
echo "one" | wc -c
EOF

vim.search(' w')
split

assert_file_contents <<~EOF
echo "one" | \\
wc -c
EOF

join

assert_file_contents <<~EOF
echo "one" | wc -c
EOF
end
end
end

0 comments on commit 2f2d200

Please sign in to comment.