xref: /netbsd-src/tests/usr.bin/indent/opt_ci.c (revision f6ead5d9af63af69d21f824743545a69030e217f)
1 /* $NetBSD: opt_ci.c,v 1.11 2023/06/10 08:17:04 rillig Exp $ */
2 
3 /*
4  * Tests for the option '-ci', which controls the indentation of continuation
5  * lines in statements and declarations, but only inside a function.
6  */
7 
8 /*
9  * Top level expressions with and without parentheses.
10  */
11 //indent input
12 int top_level = 1 +
13  2;
14 int top_level = (1 +
15  2 + (
16   3));
17 //indent end
18 
19 //indent run -ci0
20 int		top_level = 1 +
21 2;
22 int		top_level = (1 +
23 			     2 + (
24 				  3));
25 //indent end
26 
27 //indent run-equals-prev-output -ci2
28 
29 //indent run-equals-prev-output -ci4
30 
31 //indent run-equals-prev-output -ci8
32 
33 //indent run -ci0 -nlp
34 int		top_level = 1 +
35 2;
36 int		top_level = (1 +
37 	2 + (
38 		3));
39 //indent end
40 
41 //indent run -ci2 -nlp
42 int		top_level = 1 +
43 2;
44 int		top_level = (1 +
45   2 + (
46     3));
47 //indent end
48 
49 /*
50  * Between 2019-04-04 and 2023-06-09, there was a special rule that prevented
51  * indentation based on the number of open parentheses, in the case that the
52  * continuation indentation is half an indentation level, maybe to prevent that
53  * the continuation line has the same indentation as a follow-up statement,
54  * such as in 'if' statements. To prevent such ambiguities, see '-eei'.
55  */
56 //indent run -ci4 -nlp
57 int		top_level = 1 +
58 2;
59 int		top_level = (1 +
60     2 + (
61 	3));
62 //indent end
63 
64 
65 /*
66  * Declarations in functions without parentheses.
67  */
68 //indent input
69 int
sum(int a,int b)70 sum(int a, int b)
71 {
72 	return a +
73 	 b;
74 	return first +
75 	 second;
76 }
77 //indent end
78 
79 //indent run -ci0
80 int
sum(int a,int b)81 sum(int a, int b)
82 {
83 	return a +
84 		b;
85 	return first +
86 		second;
87 }
88 //indent end
89 
90 //indent run -ci2
91 int
sum(int a,int b)92 sum(int a, int b)
93 {
94 	return a +
95 	  b;
96 	return first +
97 	  second;
98 }
99 //indent end
100 
101 //indent run -ci4
102 int
sum(int a,int b)103 sum(int a, int b)
104 {
105 	return a +
106 	    b;
107 	return first +
108 	    second;
109 }
110 //indent end
111 
112 //indent run -ci8
113 int
sum(int a,int b)114 sum(int a, int b)
115 {
116 	return a +
117 		b;
118 	return first +
119 		second;
120 }
121 //indent end
122 
123 
124 /*
125  * Continued statements with expressions in parentheses.
126  */
127 //indent input
128 int
sum(int a,int b)129 sum(int a, int b)
130 {
131 	return (a +
132 	b);
133 	return (first +
134 	second + (
135 	third));
136 }
137 //indent end
138 
139 //indent run -ci0
140 int
sum(int a,int b)141 sum(int a, int b)
142 {
143 	return (a +
144 		b);
145 	return (first +
146 		second + (
147 			  third));
148 }
149 //indent end
150 
151 //indent run-equals-prev-output -ci2
152 
153 //indent run-equals-prev-output -ci4
154 
155 //indent run-equals-prev-output -ci8
156 
157 //indent run -ci2 -nlp
158 int
sum(int a,int b)159 sum(int a, int b)
160 {
161 	return (a +
162 	  b);
163 	return (first +
164 	  second + (
165 	    third));
166 }
167 //indent end
168 
169 //indent run -ci4 -nlp
170 int
sum(int a,int b)171 sum(int a, int b)
172 {
173 	return (a +
174 	    b);
175 	return (first +
176 	    second + (
177 		third));
178 }
179 //indent end
180 
181 //indent run -ci8 -nlp
182 int
sum(int a,int b)183 sum(int a, int b)
184 {
185 	return (a +
186 		b);
187 	return (first +
188 		second + (
189 			third));
190 }
191 //indent end
192 
193 
194 /*
195  * In the default configuration, the indentation level from '-i' is the same
196  * as the continuation indentation from '-ci'.  The difference between these
197  * becomes visible for structural macros like 'forever' or 'foreach'.
198  */
199 //indent input
200 #define forever for (;;)
201 void
function(void)202 function(void)
203 {
204 	forever
205 		stmt();
206 
207 	forever {
208 		stmt();
209 	}
210 }
211 //indent end
212 
213 //indent run-equals-input
214 
215 /*
216  * The difference between the block indentation and the continuation
217  * indentation only becomes visible when these two differ.
218  */
219 //indent run -i8 -ci4
220 #define forever for (;;)
221 void
function(void)222 function(void)
223 {
224 	forever
225 	    stmt();
226 
227 	forever {
228 		stmt();
229 	}
230 }
231 //indent end
232 
233 
234 //indent input
235 {
236 	size_t last_word_len = com.len
237 	    - (size_t)(last_blank + 1);
238 }
239 //indent end
240 
241 //indent run-equals-input -ldi0 -ci4
242