spacevim/bundle/splitjoin.vim/autoload/sj/perl.vim
JIe 2bb7059579
Some checks failed
Detach Plugins / check (FlyGrep.vim) (push) Has been cancelled
Detach Plugins / check (GitHub.vim) (push) Has been cancelled
Detach Plugins / check (JavaUnit.vim) (push) Has been cancelled
Detach Plugins / check (SourceCounter.vim) (push) Has been cancelled
Detach Plugins / check (cpicker.nvim) (push) Has been cancelled
Detach Plugins / check (dein-ui.vim) (push) Has been cancelled
Detach Plugins / check (git.vim) (push) Has been cancelled
Detach Plugins / check (iedit.vim) (push) Has been cancelled
Detach Plugins / check (scrollbar.vim) (push) Has been cancelled
Detach Plugins / check (vim-chat) (push) Has been cancelled
Detach Plugins / check (vim-cheat) (push) Has been cancelled
Detach Plugins / check (vim-todo) (push) Has been cancelled
Detach Plugins / check (xmake.vim) (push) Has been cancelled
test / Linux (nvim, nightly) (push) Has been cancelled
test / Linux (nvim, v0.3.8) (push) Has been cancelled
test / Linux (nvim, v0.4.0) (push) Has been cancelled
test / Linux (nvim, v0.4.2) (push) Has been cancelled
test / Linux (nvim, v0.4.3) (push) Has been cancelled
test / Linux (nvim, v0.4.4) (push) Has been cancelled
test / Linux (nvim, v0.5.0) (push) Has been cancelled
test / Linux (nvim, v0.5.1) (push) Has been cancelled
test / Linux (nvim, v0.6.0) (push) Has been cancelled
test / Linux (nvim, v0.6.1) (push) Has been cancelled
test / Linux (nvim, v0.7.0) (push) Has been cancelled
test / Linux (nvim, v0.7.2) (push) Has been cancelled
test / Linux (nvim, v0.8.0) (push) Has been cancelled
test / Linux (nvim, v0.8.1) (push) Has been cancelled
test / Linux (nvim, v0.8.2) (push) Has been cancelled
test / Linux (nvim, v0.8.3) (push) Has been cancelled
test / Linux (nvim, v0.9.0) (push) Has been cancelled
test / Linux (nvim, v0.9.1) (push) Has been cancelled
test / Linux (true, vim, v7.4.052) (push) Has been cancelled
test / Linux (true, vim, v7.4.1689) (push) Has been cancelled
test / Linux (true, vim, v7.4.629) (push) Has been cancelled
test / Linux (true, vim, v8.0.0027) (push) Has been cancelled
test / Linux (true, vim, v8.0.0183) (push) Has been cancelled
test / Linux (vim, nightly) (push) Has been cancelled
test / Linux (vim, v8.0.0184) (push) Has been cancelled
test / Linux (vim, v8.0.1453) (push) Has been cancelled
test / Linux (vim, v8.1.2269) (push) Has been cancelled
test / Linux (vim, v8.2.2434) (push) Has been cancelled
test / Linux (vim, v8.2.3995) (push) Has been cancelled
test / Windows (nvim, nightly) (push) Has been cancelled
test / Windows (nvim, v0.3.8) (push) Has been cancelled
test / Windows (nvim, v0.4.2) (push) Has been cancelled
test / Windows (nvim, v0.4.3) (push) Has been cancelled
test / Windows (nvim, v0.4.4) (push) Has been cancelled
test / Windows (nvim, v0.5.0) (push) Has been cancelled
test / Windows (nvim, v0.5.1) (push) Has been cancelled
test / Windows (nvim, v0.6.0) (push) Has been cancelled
test / Windows (nvim, v0.6.1) (push) Has been cancelled
test / Windows (nvim, v0.7.0) (push) Has been cancelled
test / Windows (nvim, v0.7.2) (push) Has been cancelled
test / Windows (nvim, v0.8.0) (push) Has been cancelled
test / Windows (nvim, v0.8.1) (push) Has been cancelled
test / Windows (nvim, v0.8.2) (push) Has been cancelled
test / Windows (nvim, v0.8.3) (push) Has been cancelled
test / Windows (nvim, v0.9.0) (push) Has been cancelled
test / Windows (nvim, v0.9.1) (push) Has been cancelled
test / Windows (vim, nightly) (push) Has been cancelled
test / Windows (vim, v7.4.1185) (push) Has been cancelled
test / Windows (vim, v7.4.1689) (push) Has been cancelled
test / Windows (vim, v8.0.0027) (push) Has been cancelled
test / Windows (vim, v8.0.1453) (push) Has been cancelled
test / Windows (vim, v8.1.2269) (push) Has been cancelled
test / Windows (vim, v8.2.2434) (push) Has been cancelled
test / Windows (vim, v8.2.3995) (push) Has been cancelled
docker / docker (push) Has been cancelled
mirror / check (coding) (push) Has been cancelled
mirror / check (gitee) (push) Has been cancelled
mirror / check (gitlab) (push) Has been cancelled
init
2024-08-21 14:17:26 +08:00

242 lines
5.4 KiB
VimL

function! sj#perl#SplitSuffixIfClause()
let pattern = '\(.*\) \(if\|unless\|while\|until\) \(.*\);\s*$'
if sj#settings#Read('perl_brace_on_same_line')
let replacement = "\\2 (\\3) {\n\\1;\n}"
else
let replacement = "\\2 (\\3) \n{\n\\1;\n}"
endif
return s:Split(pattern, replacement)
endfunction
function! sj#perl#SplitPrefixIfClause()
let pattern = '\<if\s*(.\{-})\s*{.*}'
if search(pattern, 'Wbc') <= 0
return 0
endif
normal! f(
normal %
normal! f{
let body = sj#GetMotion('Va{')
let body = substitute(body, '^{\s*\(.\{-}\)\s*}$', "{\n\\1\n}", '')
call sj#ReplaceMotion('Va{', body)
return 1
endfunction
function! sj#perl#JoinIfClause()
let current_line = getline('.')
let if_clause_pattern = '^\s*\(if\|unless\|while\|until\)\s*(\(.*\))\s*{\=\s*$'
if current_line !~ if_clause_pattern
return 0
endif
let condition = substitute(current_line, if_clause_pattern, '\2', '')
let operation = substitute(current_line, if_clause_pattern, '\1', '')
let start_line = line('.')
call search('{', 'W')
if searchpair('{', '', '}', 'W') <= 0
return 0
endif
let end_line = line('.')
let body = sj#GetMotion('Vi{')
let body = join(split(body, ";\\s*\n"), '; ')
let body = substitute(body, ';\s\+', '; ', 'g')
let body = sj#Trim(body)
let replacement = body.' '.operation.' '.condition.';'
call sj#ReplaceLines(start_line, end_line, replacement)
return 1
endfunction
function! sj#perl#SplitAndClause()
let pattern = '\(.*\) and \(.*\);\s*$'
if sj#settings#Read('perl_brace_on_same_line')
let replacement = "if (\\1) {\n\\2;\n}"
else
let replacement = "if (\\1) \n{\n\\2;\n}"
endif
return s:Split(pattern, replacement)
endfunction
function! sj#perl#SplitOrClause()
let pattern = '\(.*\) or \(.*\);\s*$'
if sj#settings#Read('perl_brace_on_same_line')
let replacement = "unless (\\1) {\n\\2;\n}"
else
let replacement = "unless (\\1) \n{\n\\2;\n}"
endif
return s:Split(pattern, replacement)
endfunction
function! sj#perl#SplitHash()
let [from, to] = sj#LocateBracesOnLine('{', '}')
if from < 0 && to < 0
return 0
endif
let pairs = sj#ParseJsonObjectBody(from + 1, to - 1)
let body = "{\n".join(pairs, ",\n").",\n}"
call sj#ReplaceMotion('Va{', body)
if sj#settings#Read('align')
let body_start = line('.') + 1
let body_end = body_start + len(pairs) - 1
call sj#Align(body_start, body_end, 'hashrocket')
endif
return 1
endfunction
function! sj#perl#JoinHash()
let line = getline('.')
if search('{\s*$', 'c', line('.')) <= 0
return 0
endif
let body = sj#GetMotion('Vi{')
let lines = split(body, ",\n")
let lines = sj#TrimList(lines)
if sj#settings#Read('normalize_whitespace')
let lines = map(lines, 'substitute(v:val, "=>\\s\\+", "=> ", "")')
let lines = map(lines, 'substitute(v:val, "\\s\\+=>", " =>", "")')
endif
let body = join(lines, ', ')
call sj#ReplaceMotion('Va{', '{'.body.'}')
return 1
endfunction
function! sj#perl#SplitSquareBracketedList()
let [from, to] = sj#LocateBracesOnLine('[', ']')
if from < 0 && to < 0
return 0
endif
let items = sj#ParseJsonObjectBody(from + 1, to - 1)
let body = "[\n".join(items, ",\n")
if sj#settings#Read('trailing_comma')
let body .= ","
endif
let body .= "\n]"
call sj#ReplaceMotion('Va[', body)
return 1
endfunction
function! sj#perl#SplitRoundBracketedList()
let [from, to] = sj#LocateBracesOnLine('(', ')')
if from < 0 && to < 0
return 0
endif
let items = sj#ParseJsonObjectBody(from + 1, to - 1)
let body = "(\n".join(items, ",\n")
if sj#settings#Read('trailing_comma')
let body .= ","
endif
let body .= "\n)"
call sj#ReplaceMotion('Va(', body)
return 1
endfunction
function! sj#perl#SplitWordList()
let [from, to] = sj#LocateBracesOnLine('qw(', ')')
if from < 0 && to < 0
return 0
endif
call search('qw\zs(', 'b', line('.'))
let remainder_of_line = getline('.')[col('.') - 1 : -1]
if remainder_of_line !~ '\%(\w\|\s\)\+)'
return 0
endif
let items = split(matchstr(remainder_of_line, '\%(\k\|\s\)\+'), '\s\+')
let body = "(\n".join(items, "\n")."\n)"
call sj#ReplaceMotion('Va(', body)
return 1
endfunction
function! sj#perl#JoinSquareBracketedList()
let line = getline('.')
if search('[\s*$', 'c', line('.')) <= 0
return 0
endif
let body = sj#GetMotion('Vi[')
let lines = split(body, ",\n")
let lines = sj#TrimList(lines)
let body = join(lines, ', ')
call sj#ReplaceMotion('Va[', '['.body.']')
return 1
endfunction
function! sj#perl#JoinRoundBracketedList()
let line = getline('.')
if search('(\s*$', 'c', line('.')) <= 0
return 0
endif
let body = sj#GetMotion('Vi(')
let lines = split(body, ",\n")
let lines = sj#TrimList(lines)
let body = join(lines, ', ')
call sj#ReplaceMotion('Va(', '('.body.')')
return 1
endfunction
function! sj#perl#JoinWordList()
let line = getline('.')
if search('qw\zs(\s*$', 'c', line('.')) <= 0
return 0
endif
let body = sj#GetMotion('Vi(')
let lines = split(body, "\n")
let lines = sj#TrimList(lines)
let body = join(lines, ' ')
call sj#ReplaceMotion('Va(', '('.body.')')
return 1
endfunction
function! s:Split(pattern, replacement_pattern)
let line = getline('.')
if line !~ a:pattern
return 0
endif
call sj#ReplaceMotion('V', substitute(line, a:pattern, a:replacement_pattern, ''))
return 1
endfunction