Skip to content

Commit 1857bbb

Browse files
committed
TeX support for individual \commands{}
1 parent 6af1cdc commit 1857bbb

4 files changed

Lines changed: 198 additions & 82 deletions

File tree

autoload/sj.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ function! sj#SearchSkip(pattern, skip, ...)
548548
return match
549549
endfunction
550550

551+
function! sj#GetCursorSyntax() abort
552+
return synIDattr(synID(line('.'), col('.'), 1), 'name')
553+
endfunction
554+
551555
function! sj#SkipSyntax(syntax_groups)
552556
let syntax_groups = a:syntax_groups
553557
let skip_pattern = '\%('.join(syntax_groups, '\|').'\)'

autoload/sj/tex.vim

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ function! sj#tex#JoinBlock()
4646

4747
let block = sj#GetByPosition(start, end)
4848

49-
let pattern = '^\(\\begin{'.arg_pattern.'\{-}}'.opts_pattern.'\)\_s\+\(.\{-}\)\_s\+\(\\end{'.arg_pattern.'\{-}}\)$'
49+
let pattern =
50+
\ '^\(\\begin{'..arg_pattern..'\{-}}'..
51+
\ opts_pattern..
52+
\ '\)\_s\+\(.\{-}\)\_s\+\(\\end{'..
53+
\ arg_pattern..
54+
\ '\{-}}\)$'
55+
5056
let match = matchlist(block, pattern)
5157
if empty(match)
5258
return 0
@@ -67,3 +73,39 @@ function! sj#tex#JoinBlock()
6773
call sj#ReplaceByPosition(start, end, replacement)
6874
return 1
6975
endfunction
76+
77+
function! sj#tex#SplitCommand()
78+
" If on currently on a command, find opening bracket:
79+
if sj#GetCursorSyntax() == 'texStatement'
80+
call search('\%#\\\=\k\+{', 'e')
81+
endif
82+
83+
let [from, to] = sj#LocateBracesAroundCursor('{', '}', ['texSpecialChar'])
84+
if from < 0 && to < 0
85+
return 0
86+
endif
87+
88+
let body = sj#GetCols(from + 1, to - 1)
89+
call sj#ReplaceCols(from + 1, to - 1, "\n"..sj#Trim(body).."\n")
90+
normal! =2j
91+
92+
return 1
93+
endfunction
94+
95+
function! sj#tex#JoinCommand()
96+
" If on currently on a command, find opening bracket:
97+
if sj#GetCursorSyntax() == 'texStatement'
98+
call search('\%#\\\=\k\+{', 'e')
99+
endif
100+
101+
" Check if we're on a '{' that is really a delimiter:
102+
if sj#GetCursorSyntax() != 'texDelimiter'
103+
return
104+
endif
105+
106+
let body = sj#GetMotion('vi{')
107+
let replacement = join(sj#TrimList(split(body, "\n")), ' ')
108+
call sj#ReplaceMotion('va{', '{'..replacement..'}')
109+
110+
return 1
111+
endfunction

ftplugin/tex/splitjoin.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
let b:splitjoin_split_callbacks = [
2+
\ 'sj#tex#SplitCommand',
23
\ 'sj#tex#SplitBlock',
34
\ ]
45

56
let b:splitjoin_join_callbacks = [
67
\ 'sj#tex#JoinBlock',
8+
\ 'sj#tex#JoinCommand',
79
\ ]

spec/plugin/tex_spec.rb

Lines changed: 149 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,162 @@
11
require 'spec_helper'
22

33
describe "tex" do
4-
let(:filename) { 'test.tex' }
4+
let(:filename) { 'test.latex' }
55

66
before :each do
77
vim.set :expandtab
88
vim.set :shiftwidth, 2
99
end
1010

11-
specify "simple blocks" do
12-
set_file_contents <<~EOF
13-
\\begin{center} Hello World \\end{center}
14-
EOF
15-
16-
split
17-
18-
assert_file_contents <<~EOF
19-
\\begin{center}
20-
Hello World
21-
\\end{center}
22-
EOF
23-
24-
vim.search 'begin'
25-
join
26-
27-
assert_file_contents <<~EOF
28-
\\begin{center} Hello World \\end{center}
29-
EOF
30-
end
31-
32-
specify "multiline block" do
33-
set_file_contents <<~EOF
34-
\\begin{center} x = y\\\\ y = z \\end{center}
35-
EOF
36-
37-
split
38-
39-
assert_file_contents <<~EOF
40-
\\begin{center}
41-
x = y\\\\
42-
y = z
43-
\\end{center}
44-
EOF
45-
46-
join
47-
48-
assert_file_contents <<~EOF
49-
\\begin{center} x = y\\\\ y = z \\end{center}
50-
EOF
11+
describe "commands" do
12+
specify "simple commands" do
13+
set_file_contents <<~EOF
14+
\\caption{Example figure}
15+
EOF
16+
17+
vim.search '{'
18+
split
19+
20+
assert_file_contents <<~EOF
21+
\\caption{
22+
Example figure
23+
}
24+
EOF
25+
26+
join
27+
28+
assert_file_contents <<~EOF
29+
\\caption{Example figure}
30+
EOF
31+
end
32+
33+
specify "nested escaped brackets" do
34+
set_file_contents <<~EOF
35+
\\caption{Example \\{nested\\} figure}
36+
EOF
37+
38+
vim.search 'nested'
39+
split
40+
41+
assert_file_contents <<~EOF
42+
\\caption{
43+
Example \\{nested\\} figure
44+
}
45+
EOF
46+
47+
join
48+
49+
assert_file_contents <<~EOF
50+
\\caption{Example \\{nested\\} figure}
51+
EOF
52+
end
53+
54+
specify "with cursor on command" do
55+
set_file_contents <<~EOF
56+
\\caption{Example figure}
57+
EOF
58+
59+
vim.search 'caption'
60+
split
61+
62+
assert_file_contents <<~EOF
63+
\\caption{
64+
Example figure
65+
}
66+
EOF
67+
68+
vim.search 'caption'
69+
join
70+
71+
assert_file_contents <<~EOF
72+
\\caption{Example figure}
73+
EOF
74+
end
5175
end
5276

53-
specify "block with parameters" do
54-
set_file_contents <<~EOF
55-
\\begin{tabular}[]{cc} row1 \\\\ row2 \\end{tabular}
56-
EOF
57-
58-
split
59-
60-
assert_file_contents <<~EOF
61-
\\begin{tabular}[]{cc}
62-
row1 \\\\
63-
row2
64-
\\end{tabular}
65-
EOF
66-
67-
join
68-
69-
assert_file_contents <<~EOF
70-
\\begin{tabular}[]{cc} row1 \\\\ row2 \\end{tabular}
71-
EOF
72-
end
73-
74-
specify "itemized blocks" do
75-
set_file_contents <<~EOF
76-
\\begin{enumerate}\\item item1 \\item item2\\end{enumerate}
77-
EOF
78-
79-
split
80-
81-
assert_file_contents <<~EOF
82-
\\begin{enumerate}
83-
\\item item1
84-
\\item item2
85-
\\end{enumerate}
86-
EOF
87-
88-
join
89-
90-
assert_file_contents <<~EOF
91-
\\begin{enumerate} \\item item1 \\item item2 \\end{enumerate}
92-
EOF
77+
describe "blocks" do
78+
specify "simple blocks" do
79+
set_file_contents <<~EOF
80+
\\begin{center} Hello World \\end{center}
81+
EOF
82+
83+
split
84+
85+
assert_file_contents <<~EOF
86+
\\begin{center}
87+
Hello World
88+
\\end{center}
89+
EOF
90+
91+
vim.search 'begin'
92+
join
93+
94+
assert_file_contents <<~EOF
95+
\\begin{center} Hello World \\end{center}
96+
EOF
97+
end
98+
99+
specify "multiline block" do
100+
set_file_contents <<~EOF
101+
\\begin{center} x = y\\\\ y = z \\end{center}
102+
EOF
103+
104+
split
105+
106+
assert_file_contents <<~EOF
107+
\\begin{center}
108+
x = y\\\\
109+
y = z
110+
\\end{center}
111+
EOF
112+
113+
join
114+
115+
assert_file_contents <<~EOF
116+
\\begin{center} x = y\\\\ y = z \\end{center}
117+
EOF
118+
end
119+
120+
specify "block with parameters" do
121+
set_file_contents <<~EOF
122+
\\begin{tabular}[]{cc} row1 \\\\ row2 \\end{tabular}
123+
EOF
124+
125+
split
126+
127+
assert_file_contents <<~EOF
128+
\\begin{tabular}[]{cc}
129+
row1 \\\\
130+
row2
131+
\\end{tabular}
132+
EOF
133+
134+
join
135+
136+
assert_file_contents <<~EOF
137+
\\begin{tabular}[]{cc} row1 \\\\ row2 \\end{tabular}
138+
EOF
139+
end
140+
141+
specify "itemized blocks" do
142+
set_file_contents <<~EOF
143+
\\begin{enumerate}\\item item1 \\item item2\\end{enumerate}
144+
EOF
145+
146+
split
147+
148+
assert_file_contents <<~EOF
149+
\\begin{enumerate}
150+
\\item item1
151+
\\item item2
152+
\\end{enumerate}
153+
EOF
154+
155+
join
156+
157+
assert_file_contents <<~EOF
158+
\\begin{enumerate} \\item item1 \\item item2 \\end{enumerate}
159+
EOF
160+
end
93161
end
94162
end

0 commit comments

Comments
 (0)