Skip to content

Commit 1aa617d

Browse files
authored
Merge pull request #209 from perelo/main
add CUE initial support, based on js.vim
2 parents 3e60a0b + cf178b0 commit 1aa617d

3 files changed

Lines changed: 206 additions & 0 deletions

File tree

autoload/sj/cue.vim

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+

doc/splitjoin.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONTENTS *splitjoin* *splitjoin-content
1010
Clojure................................: |splitjoin-clojure|
1111
Coffeescript...........................: |splitjoin-coffee|
1212
CSS....................................: |splitjoin-css|
13+
CUE....................................: |splitjoin-cue|
1314
Elixir.................................: |splitjoin-elixir|
1415
Elm....................................: |splitjoin-elm|
1516
Eruby..................................: |splitjoin-eruby|
@@ -356,6 +357,52 @@ Multiline selectors ~
356357
}
357358
<
358359

360+
==============================================================================
361+
CUE *splitjoin-cue*
362+
363+
CUE Structs are JSON objects but with a cleaner syntax. Lists and Function
364+
arguments behave like JSON's.
365+
See |splitjoin-json|.
366+
367+
Structs ~
368+
369+
Structs are first class, so the cursor can precede the first curly brace.
370+
>
371+
a: foo: { x: bar: baz: bool, y: bar: baz: int, z: bar: baz: string }
372+
373+
a: foo: {
374+
x: bar: baz: bool
375+
y: bar: baz: int
376+
z: bar: baz: string
377+
}
378+
<
379+
Lists ~
380+
381+
The same applies to lists.
382+
>
383+
foo: [ 'x:y:z', "\xFFFF0000", a.foo.y ]
384+
385+
foo: [
386+
'x:y:z',
387+
"\xFFFF0000",
388+
a.foo.y
389+
]
390+
<
391+
Function Arguments ~
392+
393+
Function splitting requires the cursor to be positioned inside the
394+
parenthesis, preferably near the closing one.
395+
>
396+
bar: m.Baz(foo[2].bar.baz, 42, true)
397+
398+
bar: m.Baz(
399+
foo[2].bar.baz,
400+
42,
401+
true
402+
)
403+
<
404+
405+
359406
==============================================================================
360407
ELIXIR *splitjoin-elixir*
361408

ftplugin/cue/splitjoin.vim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
if !exists('b:splitjoin_split_callbacks')
2+
let b:splitjoin_split_callbacks = [
3+
\ 'sj#cue#SplitStructLiteral',
4+
\ 'sj#cue#SplitArray',
5+
\ 'sj#cue#SplitArgs',
6+
\ 'sj#cue#SplitImports',
7+
\ ]
8+
endif
9+
10+
if !exists('b:splitjoin_join_callbacks')
11+
let b:splitjoin_join_callbacks = [
12+
\ 'sj#cue#JoinStructLiteral',
13+
\ 'sj#cue#JoinArray',
14+
\ 'sj#cue#JoinArgs',
15+
\ ]
16+
endif

0 commit comments

Comments
 (0)