Skip to content

Commit 30b64b8

Browse files
committed
clangformat.vroom: Fix textprops dependency and split out cursor tests
Fixes clangformat.vroom failing against some versions of vim since #145 with `E117: Unknown function: prop_type_add`. Moves all the intricate cursor position checks from clangformat.vroom into a separate file clangformat-cursor.vroom so the flow of the basic testing scenarios makes makes more sense and is less brittle.
1 parent 68f49da commit 30b64b8

File tree

2 files changed

+92
-73
lines changed

2 files changed

+92
-73
lines changed

vroom/clangformat-cursor.vroom

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
These examples demonstrate specific cursor positioning behavior related to the
2+
overall clang-format formatting demonstrated in clangformat.vroom. See the
3+
examples in that file first if you're unfamiliar with formatting behavior.
4+
5+
We'll set up codefmt and configure the vroom environment, then jump into some
6+
examples.
7+
8+
:source $VROOMDIR/setupvroom.vim
9+
10+
:let g:repeat_calls = []
11+
:function FakeRepeat(...)<CR>
12+
| call add(g:repeat_calls, a:000)<CR>
13+
:endfunction
14+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
15+
16+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
17+
18+
19+
It will keep the cursor in the correct position when formatting code (assuming
20+
that clang-format has a version >= 3.4).
21+
22+
:Glaive codefmt clang_format_executable='clang-format-3.3'
23+
% int f() { int i = 1; return 1234567890; }
24+
25+
:FormatCode clang-format
26+
! clang-format-3.3 --version .*
27+
$ clang-format version 3.3.0 (tags/testing)
28+
! clang-format-3.3 -style file -lines 1:1 2>.*
29+
$ int f() {
30+
$ int i = 1;
31+
$ return 1234567890;
32+
$ }
33+
int f() {
34+
int i = 1;
35+
return 1234567890;
36+
}
37+
@end
38+
39+
:Glaive codefmt clang_format_executable='clang-format'
40+
@clear
41+
% int f() { int i = 1; return 1234567890; }
42+
43+
:goto 33
44+
:echomsg getline('.')[col('.')-1]
45+
~ 5
46+
:FormatCode clang-format
47+
! clang-format --version .*
48+
$ clang-format version 3.7.0 (tags/testing)
49+
! clang-format -style file .* -cursor 32 .*2>.*
50+
$ { "Cursor": 36 }
51+
$ int f() {
52+
$ int i = 1;
53+
$ return 1234567890;
54+
$ }
55+
:echomsg getline('.')[col('.')-1]
56+
~ 5
57+
58+
Some vim builds that support textprops had a bug with determining cursor
59+
position (see https://github.com/vim/vim/issues/5930), but codefmt's cursor
60+
positioning should still maintain cursor position correctly for those cases.
61+
62+
Note: The following test scenario will skip the relevant textprops setup and run
63+
harmlessly without doing any interesting verification in vim versions that don't
64+
support textprops.
65+
66+
@clear
67+
:function PlaceTextpropIfSupported(lnum, col, length) abort<CR>
68+
| if !has('textprops')<CR>
69+
| return<CR>
70+
| endif<CR>
71+
| call prop_type_add('keyword', {})<CR>
72+
| call prop_add(a:lnum, a:col, {'length': a:length, 'type': 'keyword'})<CR>
73+
|endfunction
74+
% int f() {<CR>
75+
| int i=1;<CR>
76+
| return 1234567890; }<CR>
77+
:call PlaceTextpropIfSupported(1, 1, 3)
78+
:call cursor(2, 10)
79+
:echomsg getline('.')[col('.')-1]
80+
~ =
81+
:FormatCode clang-format
82+
! clang-format -style file .* -cursor 23 .*2>.*
83+
$ { "Cursor": 18 }
84+
$ int f() {
85+
$ int i = 1;
86+
$ return 1234567890;
87+
$ }
88+
:echomsg getline('.')[col('.')-1]
89+
~ =
90+

vroom/clangformat.vroom

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -132,79 +132,8 @@ It can format specific line ranges of code using :FormatLines.
132132
}
133133
@end
134134

135-
136-
137-
It will keep the cursor in the correct position when formatting code (assuming
138-
that clang-format has a version >= 3.4).
139-
140-
:Glaive codefmt clang_format_executable='clang-format-3.3'
141-
@clear
142-
% int f() { int i = 1; return 1234567890; }
143-
144-
:FormatCode clang-format
145-
! clang-format-3.3 --version .*
146-
$ clang-format version 3.3.0 (tags/testing)
147-
! clang-format-3.3 -style file -lines 1:1 2>.*
148-
$ int f() {
149-
$ int i = 1;
150-
$ return 1234567890;
151-
$ }
152-
int f() {
153-
int i = 1;
154-
return 1234567890;
155-
}
156-
@end
157-
:Glaive codefmt clang_format_executable='clang-format'
158-
@clear
159-
% int f() { int i = 1; return 1234567890; }
160-
161-
:goto 33
162-
:echomsg getline('.')[col('.')-1]
163-
~ 5
164-
:FormatCode clang-format
165-
! clang-format --version .*
166-
$ clang-format version 3.7.0 (tags/testing)
167-
! clang-format -style file .* -cursor 32 .*2>.*
168-
$ { "Cursor": 36 }
169-
$ int f() {
170-
$ int i = 1;
171-
$ return 1234567890;
172-
$ }
173-
int f() {
174-
int i = 1;
175-
return 1234567890;
176-
}
177-
@end
178-
:echomsg getline('.')[col('.')-1]
179-
~ 5
180-
181-
This should work when with textprops set in the file, despite various functions
182-
like line2byte() and :goto being buggy in that case.
183-
(See https://github.com/vim/vim/issues/5930 for bug details)
184-
185-
@clear
186-
% int f() {<CR>
187-
| int i=1;<CR>
188-
| return 1234567890; }<CR>
189-
:call prop_type_add('keyword', {})
190-
:call prop_add(1, 1, {'length': 3, 'type': 'keyword'})
191-
:call cursor(2, 10)
192-
:echomsg getline('.')[col('.')-1]
193-
~ =
194-
:FormatCode clang-format
195-
! clang-format -style file .* -cursor 23 .*2>.*
196-
$ { "Cursor": 18 }
197-
$ int f() {
198-
$ int i = 1;
199-
$ return 1234567890;
200-
$ }
201-
int f() {
202-
int i = 1;
203-
return 1234567890;
204-
}
205-
@end
206-
:echomsg getline('.')[col('.')-1]
207-
~ =
135+
Note this will usually maintain cursor position correctly even when the code
136+
under the cursor moves. See clangformat-cursor.vroom for examples.
208137

209138
You might have wondered where the "-style file" above comes from. The
210139
clang-format tool accepts a "style" option to control the formatting style. By

0 commit comments

Comments
 (0)