|
| 1 | +function! sj#cue#SplitImports() |
| 2 | + let pattern = '^import\s\+\(\%(\k\+\s\+\)\=\%(".*"\)\)$' |
| 3 | + |
| 4 | + if getline('.') =~ pattern |
| 5 | + call sj#Keeppatterns('s/' . pattern . '/import (\r\1\r)/') |
| 6 | + normal! k== |
| 7 | + return 1 |
| 8 | + else |
| 9 | + return 0 |
| 10 | + endif |
| 11 | +endfunction |
| 12 | + |
| 13 | +function! sj#cue#SplitStructLiteral() |
| 14 | + let [from, to] = sj#LocateBracesOnLine('{', '}') |
| 15 | + |
| 16 | + if from < 0 && to < 0 |
| 17 | + return 0 |
| 18 | + endif |
| 19 | + |
| 20 | + let pairs = sj#ParseJsonObjectBody(from + 1, to - 1) |
| 21 | + let body = join(pairs, "\n") |
| 22 | + let body = "{\n".body."\n}" |
| 23 | + call sj#ReplaceMotion('Va{', body) |
| 24 | + |
| 25 | + if sj#settings#Read('align') |
| 26 | + let body_start = line('.') + 1 |
| 27 | + let body_end = body_start + len(pairs) - 1 |
| 28 | + call sj#Align(body_start, body_end, 'json_object') |
| 29 | + endif |
| 30 | + |
| 31 | + return 1 |
| 32 | +endfunction |
| 33 | + |
| 34 | +function! sj#cue#JoinStructLiteral() |
| 35 | + let line = getline('.') |
| 36 | + |
| 37 | + if line =~ '{\s*$' |
| 38 | + call search('{', 'c', line('.')) |
| 39 | + let body = sj#GetMotion('Vi{') |
| 40 | + |
| 41 | + let lines = split(body, "\n") |
| 42 | + let lines = sj#TrimList(lines) |
| 43 | + if sj#settings#Read('normalize_whitespace') |
| 44 | + let lines = map(lines, 'substitute(v:val, ":\\s\\+", ": ", "")') |
| 45 | + endif |
| 46 | + |
| 47 | + let body = join(lines, ', ') |
| 48 | + let body = substitute(body, ',$', '', '') |
| 49 | + |
| 50 | + if sj#settings#Read('curly_brace_padding') |
| 51 | + let body = '{ '.body.' }' |
| 52 | + else |
| 53 | + let body = '{'.body.'}' |
| 54 | + endif |
| 55 | + |
| 56 | + call sj#ReplaceMotion('Va{', body) |
| 57 | + |
| 58 | + return 1 |
| 59 | + else |
| 60 | + return 0 |
| 61 | + endif |
| 62 | +endfunction |
| 63 | + |
| 64 | +function! sj#cue#SplitArray() |
| 65 | + return s:SplitList(['[', ']'], 'cursor_on_line') |
| 66 | +endfunction |
| 67 | + |
| 68 | +function! sj#cue#JoinArray() |
| 69 | + return s:JoinList(['[', ']'], 'padding') |
| 70 | +endfunction |
| 71 | + |
| 72 | +function! sj#cue#SplitArgs() |
| 73 | + return s:SplitList(['(', ')'], 'cursor_inside') |
| 74 | +endfunction |
| 75 | + |
| 76 | +function! sj#cue#JoinArgs() |
| 77 | + return s:JoinList(['(', ')'], 'no_padding') |
| 78 | +endfunction |
| 79 | + |
| 80 | +function! s:SplitList(delimiter, cursor_position) |
| 81 | + let start = a:delimiter[0] |
| 82 | + let end = a:delimiter[1] |
| 83 | + |
| 84 | + let lineno = line('.') |
| 85 | + let indent = indent('.') |
| 86 | + |
| 87 | + if a:cursor_position == 'cursor_inside' |
| 88 | + let [from, to] = sj#LocateBracesAroundCursor(start, end) |
| 89 | + elseif a:cursor_position == 'cursor_on_line' |
| 90 | + let [from, to] = sj#LocateBracesOnLine(start, end) |
| 91 | + else |
| 92 | + echoerr "Invalid value for a:cursor_position: ".a:cursor_position |
| 93 | + return |
| 94 | + endif |
| 95 | + |
| 96 | + if from < 0 && to < 0 |
| 97 | + return 0 |
| 98 | + endif |
| 99 | + |
| 100 | + let items = sj#ParseJsonObjectBody(from + 1, to - 1) |
| 101 | + if empty(items) |
| 102 | + return 0 |
| 103 | + endif |
| 104 | + |
| 105 | + let body = start."\n".join(items, ",\n")."\n".end |
| 106 | + |
| 107 | + call sj#ReplaceMotion('Va'.start, body) |
| 108 | + |
| 109 | + let end_line = lineno + len(items) + 1 |
| 110 | + call sj#SetIndent(end_line, indent) |
| 111 | + |
| 112 | + return 1 |
| 113 | +endfunction |
| 114 | + |
| 115 | +function! s:JoinList(delimiter, delimiter_padding) |
| 116 | + let start = a:delimiter[0] |
| 117 | + let end = a:delimiter[1] |
| 118 | + |
| 119 | + let line = getline('.') |
| 120 | + |
| 121 | + if line !~ start . '\s*$' |
| 122 | + return 0 |
| 123 | + endif |
| 124 | + |
| 125 | + call search(start, 'c', line('.')) |
| 126 | + let body = sj#GetMotion('Vi'.start) |
| 127 | + |
| 128 | + let lines = split(body, "\n") |
| 129 | + let lines = sj#TrimList(lines) |
| 130 | + let body = sj#Trim(join(lines, ' ')) |
| 131 | + let body = substitute(body, ',\s*$', '', '') |
| 132 | + |
| 133 | + if a:delimiter_padding == 'padding' |
| 134 | + let body = start.' '.body.' '.end |
| 135 | + else |
| 136 | + let body = start.body.end |
| 137 | + endif |
| 138 | + |
| 139 | + call sj#ReplaceMotion('Va'.start, body) |
| 140 | + |
| 141 | + return 1 |
| 142 | +endfunction |
| 143 | + |
0 commit comments