author | dadubrow |
Thu, 29 Apr 2010 11:30:21 -0500 | |
branch | RCL_2_4 |
changeset 140 | 6d6cf3bdff43 |
parent 138 | e0657161c0cc |
permissions | -rw-r--r-- |
37 | 1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2009 IBM Corporation and others. |
|
3 |
* All rights reserved. This program and the accompanying materials |
|
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|
5 |
* which accompanies this distribution, and is available at |
|
6 |
* http://www.eclipse.org/legal/epl-v10.html |
|
7 |
* |
|
8 |
* Contributors: |
|
9 |
* IBM Corporation - initial API and implementation |
|
10 |
* Sergey Prigogin (Google) |
|
11 |
* Anton Leherbauer (Wind River Systems) |
|
12 |
*******************************************************************************/ |
|
13 |
package org.eclipse.cdt.internal.ui.text; |
|
14 |
||
15 |
import org.eclipse.core.runtime.Assert; |
|
16 |
import org.eclipse.core.runtime.Platform; |
|
17 |
import org.eclipse.core.runtime.preferences.IPreferencesService; |
|
18 |
import org.eclipse.jface.text.BadLocationException; |
|
19 |
import org.eclipse.jface.text.IDocument; |
|
20 |
import org.eclipse.jface.text.IRegion; |
|
21 |
||
22 |
import org.eclipse.cdt.core.CCorePlugin; |
|
23 |
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; |
|
24 |
import org.eclipse.cdt.core.model.ICProject; |
|
25 |
||
26 |
import org.eclipse.cdt.internal.corext.util.CodeFormatterUtil; |
|
27 |
||
28 |
/** |
|
29 |
* Uses the {@link org.eclipse.cdt.internal.ui.text.CHeuristicScanner} to |
|
30 |
* get the indentation level for a certain position in a document. |
|
31 |
* |
|
32 |
* <p> |
|
33 |
* An instance holds some internal position in the document and is therefore |
|
34 |
* not thread-safe. |
|
35 |
* </p> |
|
36 |
*/ |
|
37 |
public final class CIndenter { |
|
38 |
||
39 |
/** |
|
40 |
* The CDT Core preferences. |
|
41 |
*/ |
|
42 |
private final class CorePrefs { |
|
43 |
final boolean prefUseTabs; |
|
44 |
final int prefTabSize; |
|
45 |
final int prefIndentationSize; |
|
46 |
final boolean prefArrayDimensionsDeepIndent; |
|
47 |
final int prefArrayIndent; |
|
48 |
final boolean prefArrayDeepIndent; |
|
49 |
final boolean prefTernaryDeepAlign; |
|
50 |
final int prefTernaryIndent; |
|
51 |
final int prefCaseIndent; |
|
52 |
final int prefCaseBlockIndent; |
|
53 |
final int prefAssignmentIndent; |
|
54 |
final int prefSimpleIndent; |
|
55 |
final int prefBracketIndent; |
|
56 |
final boolean prefMethodDeclDeepIndent; |
|
57 |
final boolean prefMethodDeclFirstParameterDeepIndent; |
|
58 |
final int prefMethodDeclIndent; |
|
59 |
final boolean prefMethodCallDeepIndent; |
|
60 |
final boolean prefMethodCallFirstParameterDeepIndent; |
|
61 |
final int prefMethodCallIndent; |
|
62 |
final boolean prefParenthesisDeepIndent; |
|
63 |
final int prefParenthesisIndent; |
|
64 |
final int prefBlockIndent; |
|
65 |
final int prefMethodBodyIndent; |
|
66 |
final int prefTypeIndent; |
|
67 |
final int prefAccessSpecifierIndent; |
|
68 |
final int prefAccessSpecifierExtraSpaces; |
|
69 |
final int prefNamespaceBodyIndent; |
|
70 |
final boolean prefIndentBracesForBlocks; |
|
71 |
final boolean prefIndentBracesForArrays; |
|
72 |
final boolean prefIndentBracesForMethods; |
|
73 |
final boolean prefIndentBracesForTypes; |
|
74 |
final int prefContinuationIndent; |
|
75 |
final boolean prefHasTemplates; |
|
76 |
final String prefTabChar; |
|
77 |
||
78 |
private final ICProject fProject; |
|
79 |
||
80 |
/** |
|
81 |
* Returns the possibly project-specific core preference defined under <code>key</code>. |
|
82 |
* |
|
83 |
* @param key the key of the preference |
|
84 |
* @return the value of the preference |
|
85 |
*/ |
|
86 |
private String getCoreFormatterOption(String key) { |
|
87 |
if (fProject == null) |
|
88 |
return CCorePlugin.getOption(key); |
|
89 |
return fProject.getOption(key, true); |
|
90 |
} |
|
91 |
||
92 |
CorePrefs(ICProject project) { |
|
93 |
fProject= project; |
|
94 |
prefUseTabs= prefUseTabs(); |
|
95 |
prefTabSize= prefTabSize(); |
|
96 |
prefIndentationSize= prefIndentationSize(); |
|
97 |
prefArrayDimensionsDeepIndent= prefArrayDimensionsDeepIndent(); |
|
98 |
prefContinuationIndent= prefContinuationIndent(); |
|
99 |
prefBlockIndent= prefBlockIndent(); |
|
100 |
prefArrayIndent= prefArrayIndent(); |
|
101 |
prefArrayDeepIndent= prefArrayDeepIndent(); |
|
102 |
prefTernaryDeepAlign= prefTernaryDeepAlign(); |
|
103 |
prefTernaryIndent= prefTernaryIndent(); |
|
104 |
prefCaseIndent= prefCaseIndent(); |
|
105 |
prefCaseBlockIndent= prefCaseBlockIndent(); |
|
106 |
prefAssignmentIndent= prefAssignmentIndent(); |
|
107 |
prefIndentBracesForBlocks= prefIndentBracesForBlocks(); |
|
108 |
prefSimpleIndent= prefSimpleIndent(); |
|
109 |
prefBracketIndent= prefBracketIndent(); |
|
110 |
prefMethodDeclDeepIndent= prefMethodDeclDeepIndent(); |
|
111 |
prefMethodDeclFirstParameterDeepIndent= prefMethodDeclFirstParameterDeepIndent(); |
|
112 |
prefMethodDeclIndent= prefMethodDeclIndent(); |
|
113 |
prefMethodCallDeepIndent= prefMethodCallDeepIndent(); |
|
114 |
prefMethodCallFirstParameterDeepIndent= prefMethodCallFirstParameterDeepIndent(); |
|
115 |
prefMethodCallIndent= prefMethodCallIndent(); |
|
116 |
prefParenthesisDeepIndent= prefParenthesisDeepIndent(); |
|
117 |
prefParenthesisIndent= prefParenthesisIndent(); |
|
118 |
prefMethodBodyIndent= prefMethodBodyIndent(); |
|
119 |
prefTypeIndent= prefTypeIndent(); |
|
120 |
prefAccessSpecifierIndent= prefAccessSpecifierIndent(); |
|
121 |
prefAccessSpecifierExtraSpaces= prefAccessSpecifierExtraSpaces(); |
|
122 |
prefNamespaceBodyIndent= prefNamespaceBodyIndent(); |
|
123 |
prefIndentBracesForArrays= prefIndentBracesForArrays(); |
|
124 |
prefIndentBracesForMethods= prefIndentBracesForMethods(); |
|
125 |
prefIndentBracesForTypes= prefIndentBracesForTypes(); |
|
126 |
prefHasTemplates= hasTemplates(); |
|
127 |
prefTabChar= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR); |
|
128 |
} |
|
129 |
||
130 |
private boolean prefUseTabs() { |
|
131 |
return !CCorePlugin.SPACE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR)); |
|
132 |
} |
|
133 |
||
134 |
private int prefTabSize() { |
|
135 |
return CodeFormatterUtil.getTabWidth(fProject); |
|
136 |
} |
|
137 |
||
138 |
private int prefIndentationSize() { |
|
139 |
return CodeFormatterUtil.getIndentWidth(fProject); |
|
140 |
} |
|
141 |
||
142 |
private boolean prefArrayDimensionsDeepIndent() { |
|
143 |
return true; // sensible default, no formatter setting |
|
144 |
} |
|
145 |
||
146 |
private int prefArrayIndent() { |
|
147 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_INITIALIZER_LIST); |
|
148 |
try { |
|
149 |
if (DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_BY_ONE) |
|
150 |
return 1; |
|
151 |
} catch (IllegalArgumentException e) { |
|
152 |
// ignore and return default |
|
153 |
} |
|
154 |
||
155 |
return prefContinuationIndent(); // default |
|
156 |
} |
|
157 |
||
158 |
private boolean prefArrayDeepIndent() { |
|
159 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_INITIALIZER_LIST); |
|
160 |
try { |
|
161 |
return DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_ON_COLUMN; |
|
162 |
} catch (IllegalArgumentException e) { |
|
163 |
// ignore and return default |
|
164 |
} |
|
165 |
||
166 |
return true; |
|
167 |
} |
|
168 |
||
169 |
private boolean prefTernaryDeepAlign() { |
|
170 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION); |
|
171 |
try { |
|
172 |
return DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_ON_COLUMN; |
|
173 |
} catch (IllegalArgumentException e) { |
|
174 |
// ignore and return default |
|
175 |
} |
|
176 |
return false; |
|
177 |
} |
|
178 |
||
179 |
private int prefTernaryIndent() { |
|
180 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION); |
|
181 |
try { |
|
182 |
if (DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_BY_ONE) |
|
183 |
return 1; |
|
184 |
else |
|
185 |
return prefContinuationIndent(); |
|
186 |
} catch (IllegalArgumentException e) { |
|
187 |
// ignore and return default |
|
188 |
} |
|
189 |
||
190 |
return prefContinuationIndent(); |
|
191 |
} |
|
192 |
||
193 |
private int prefCaseIndent() { |
|
194 |
if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH))) |
|
195 |
return 1; |
|
196 |
else |
|
197 |
return 0; |
|
198 |
} |
|
199 |
||
200 |
private int prefCaseBlockIndent() { |
|
201 |
if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES))) |
|
202 |
return 1; |
|
203 |
else |
|
204 |
return 0; |
|
205 |
} |
|
206 |
||
207 |
private int prefAssignmentIndent() { |
|
208 |
return prefContinuationIndent(); |
|
209 |
} |
|
210 |
||
211 |
private int prefSimpleIndent() { |
|
212 |
if (prefIndentBracesForBlocks() && prefBlockIndent() == 0) |
|
213 |
return 1; |
|
214 |
else |
|
215 |
return prefBlockIndent(); |
|
216 |
} |
|
217 |
||
218 |
private int prefBracketIndent() { |
|
219 |
return prefBlockIndent(); |
|
220 |
} |
|
221 |
||
222 |
private boolean prefMethodDeclDeepIndent() { |
|
223 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION); |
|
224 |
try { |
|
225 |
int indentStyle = DefaultCodeFormatterConstants.getIndentStyle(option); |
|
226 |
return indentStyle == DefaultCodeFormatterConstants.INDENT_ON_COLUMN; |
|
227 |
} catch (IllegalArgumentException e) { |
|
228 |
// ignore and return default |
|
229 |
} |
|
230 |
||
231 |
return false; |
|
232 |
} |
|
233 |
||
234 |
private boolean prefMethodDeclFirstParameterDeepIndent() { |
|
235 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION); |
|
236 |
try { |
|
237 |
int indentStyle = DefaultCodeFormatterConstants.getIndentStyle(option); |
|
238 |
int wrappingStyle = DefaultCodeFormatterConstants.getWrappingStyle(option); |
|
239 |
return indentStyle == DefaultCodeFormatterConstants.INDENT_ON_COLUMN && |
|
240 |
(wrappingStyle == DefaultCodeFormatterConstants.WRAP_COMPACT_FIRST_BREAK || |
|
241 |
wrappingStyle == DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE); |
|
242 |
} catch (IllegalArgumentException e) { |
|
243 |
// ignore and return default |
|
244 |
} |
|
245 |
||
246 |
return false; |
|
247 |
} |
|
248 |
||
249 |
private int prefMethodDeclIndent() { |
|
250 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION); |
|
251 |
try { |
|
252 |
if (DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_BY_ONE) |
|
253 |
return 1; |
|
254 |
else |
|
255 |
return prefContinuationIndent(); |
|
256 |
} catch (IllegalArgumentException e) { |
|
257 |
// ignore and return default |
|
258 |
} |
|
259 |
return 1; |
|
260 |
} |
|
261 |
||
262 |
private boolean prefMethodCallDeepIndent() { |
|
263 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION); |
|
264 |
try { |
|
265 |
int indentStyle = DefaultCodeFormatterConstants.getIndentStyle(option); |
|
266 |
return indentStyle == DefaultCodeFormatterConstants.INDENT_ON_COLUMN; |
|
267 |
} catch (IllegalArgumentException e) { |
|
268 |
// ignore and return default |
|
269 |
} |
|
270 |
return false; // sensible default |
|
271 |
} |
|
272 |
||
273 |
private boolean prefMethodCallFirstParameterDeepIndent() { |
|
274 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION); |
|
275 |
try { |
|
276 |
int indentStyle = DefaultCodeFormatterConstants.getIndentStyle(option); |
|
277 |
int wrappingStyle = DefaultCodeFormatterConstants.getWrappingStyle(option); |
|
278 |
return indentStyle == DefaultCodeFormatterConstants.INDENT_ON_COLUMN && |
|
279 |
(wrappingStyle == DefaultCodeFormatterConstants.WRAP_COMPACT_FIRST_BREAK || |
|
280 |
wrappingStyle == DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE); |
|
281 |
} catch (IllegalArgumentException e) { |
|
282 |
// ignore and return default |
|
283 |
} |
|
284 |
return false; // sensible default |
|
285 |
} |
|
286 |
||
287 |
private int prefMethodCallIndent() { |
|
288 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION); |
|
289 |
try { |
|
290 |
if (DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_BY_ONE) |
|
291 |
return 1; |
|
292 |
else |
|
293 |
return prefContinuationIndent(); |
|
294 |
} catch (IllegalArgumentException e) { |
|
295 |
// ignore and return default |
|
296 |
} |
|
297 |
||
298 |
return 1; // sensible default |
|
299 |
} |
|
300 |
||
301 |
private boolean prefParenthesisDeepIndent() { |
|
302 |
// don't do parenthesis deep indentation |
|
303 |
// String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION); |
|
304 |
// try { |
|
305 |
// return DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_ON_COLUMN; |
|
306 |
// } catch (IllegalArgumentException e) { |
|
307 |
// // ignore and return default |
|
308 |
// } |
|
309 |
||
310 |
return false; |
|
311 |
} |
|
312 |
||
313 |
private int prefParenthesisIndent() { |
|
314 |
return prefContinuationIndent(); |
|
315 |
} |
|
316 |
||
317 |
private int prefBlockIndent() { |
|
318 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BLOCK); |
|
319 |
if (DefaultCodeFormatterConstants.FALSE.equals(option)) |
|
320 |
return 0; |
|
321 |
||
322 |
return 1; // sensible default |
|
323 |
} |
|
324 |
||
325 |
private int prefMethodBodyIndent() { |
|
326 |
if (DefaultCodeFormatterConstants.FALSE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BODY))) |
|
327 |
return 0; |
|
328 |
||
329 |
return 1; // sensible default |
|
330 |
} |
|
331 |
||
332 |
private int prefTypeIndent() { |
|
333 |
String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER); |
|
334 |
if (DefaultCodeFormatterConstants.FALSE.equals(option)) |
|
335 |
return 0; |
|
336 |
||
337 |
return 1; // sensible default |
|
338 |
} |
|
339 |
||
340 |
private int prefAccessSpecifierIndent() { |
|
341 |
if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER))) |
|
342 |
return 1; |
|
343 |
else |
|
344 |
return 0; |
|
345 |
} |
|
346 |
||
347 |
private int prefAccessSpecifierExtraSpaces() { |
|
348 |
// Hidden option that enables fractional indent of access specifiers. |
|
349 |
IPreferencesService prefs = Platform.getPreferencesService(); |
|
350 |
return prefs.getInt(CCorePlugin.PLUGIN_ID, CCorePlugin.PLUGIN_ID + ".formatter.indent_access_specifier_extra_spaces", 0, null); //$NON-NLS-1$ |
|
351 |
} |
|
352 |
||
353 |
private int prefNamespaceBodyIndent() { |
|
354 |
if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_NAMESPACE_HEADER))) |
|
355 |
return prefBlockIndent(); |
|
356 |
else |
|
357 |
return 0; |
|
358 |
} |
|
359 |
||
360 |
private boolean prefIndentBracesForBlocks() { |
|
361 |
return DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_BLOCK)); |
|
362 |
} |
|
363 |
||
364 |
private boolean prefIndentBracesForArrays() { |
|
365 |
return DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_INITIALIZER_LIST)); |
|
366 |
} |
|
367 |
||
368 |
private boolean prefIndentBracesForMethods() { |
|
369 |
return DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_METHOD_DECLARATION)); |
|
370 |
} |
|
371 |
||
372 |
private boolean prefIndentBracesForTypes() { |
|
373 |
return DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION)); |
|
374 |
} |
|
375 |
||
376 |
private int prefContinuationIndent() { |
|
377 |
try { |
|
378 |
return Integer.parseInt(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION)); |
|
379 |
} catch (NumberFormatException e) { |
|
380 |
// ignore and return default |
|
381 |
} |
|
382 |
||
383 |
return 2; // sensible default |
|
384 |
} |
|
385 |
||
386 |
private boolean hasTemplates() { |
|
387 |
return true; |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
/** The document being scanned. */ |
|
392 |
private final IDocument fDocument; |
|
393 |
/** The indentation accumulated by <code>findReferencePosition</code>. */ |
|
394 |
private int fIndent; |
|
395 |
/** Extra spaces to add on top of fIndent */ |
|
396 |
private int fExtraSpaces; |
|
397 |
/** |
|
398 |
* The absolute (character-counted) indentation offset for special cases |
|
399 |
* (method defs, array initializers) |
|
400 |
*/ |
|
401 |
private int fAlign; |
|
402 |
/** The stateful scan position for the indentation methods. */ |
|
403 |
private int fPosition; |
|
404 |
/** The previous position. */ |
|
405 |
private int fPreviousPos; |
|
406 |
/** The most recent token. */ |
|
407 |
private int fToken; |
|
408 |
/** The line of <code>fPosition</code>. */ |
|
409 |
private int fLine; |
|
410 |
/** |
|
411 |
* The scanner we will use to scan the document. It has to be installed |
|
412 |
* on the same document as the one we get. |
|
413 |
*/ |
|
414 |
private final CHeuristicScanner fScanner; |
|
415 |
/** |
|
416 |
* The CDT Core preferences. |
|
417 |
*/ |
|
418 |
private final CorePrefs fPrefs; |
|
419 |
||
420 |
/** |
|
421 |
* Creates a new instance. |
|
422 |
* |
|
423 |
* @param document the document to scan |
|
424 |
* @param scanner the {@link CHeuristicScanner} to be used for scanning |
|
425 |
* the document. It must be installed on the same <code>IDocument</code>. |
|
426 |
*/ |
|
427 |
public CIndenter(IDocument document, CHeuristicScanner scanner) { |
|
428 |
this(document, scanner, null); |
|
429 |
} |
|
430 |
||
431 |
/** |
|
432 |
* Creates a new instance. |
|
433 |
* |
|
434 |
* @param document the document to scan |
|
435 |
* @param scanner the {@link CHeuristicScanner} to be used for scanning |
|
436 |
* the document. It must be installed on the same |
|
437 |
* <code>IDocument</code>. |
|
438 |
* @param project the C/C++ project to get the formatter preferences from, or |
|
439 |
* <code>null</code> to use the workspace settings |
|
440 |
*/ |
|
441 |
public CIndenter(IDocument document, CHeuristicScanner scanner, ICProject project) { |
|
442 |
Assert.isNotNull(document); |
|
443 |
Assert.isNotNull(scanner); |
|
444 |
fDocument= document; |
|
445 |
fScanner= scanner; |
|
446 |
fPrefs= new CorePrefs(project); |
|
447 |
} |
|
448 |
||
449 |
/** |
|
450 |
* Computes the indentation at the reference point of <code>position</code>. |
|
451 |
* |
|
452 |
* @param offset the offset in the document |
|
453 |
* @return a String which reflects the indentation at the line in which the |
|
454 |
* reference position to <code>offset</code> resides, or <code>null</code> |
|
455 |
* if it cannot be determined |
|
456 |
*/ |
|
457 |
public StringBuilder getReferenceIndentation(int offset) { |
|
458 |
return getReferenceIndentation(offset, false); |
|
459 |
} |
|
460 |
||
461 |
/** |
|
462 |
* Computes the indentation at the reference point of <code>position</code>. |
|
463 |
* |
|
464 |
* @param offset the offset in the document |
|
465 |
* @param assumeOpeningBrace <code>true</code> if an opening brace should be assumed |
|
466 |
* @return a String which reflects the indentation at the line in which the |
|
467 |
* reference position to <code>offset</code> resides, or <code>null</code> |
|
468 |
* if it cannot be determined |
|
469 |
*/ |
|
470 |
private StringBuilder getReferenceIndentation(int offset, boolean assumeOpeningBrace) { |
|
471 |
int unit; |
|
472 |
if (assumeOpeningBrace) |
|
473 |
unit= findReferencePosition(offset, Symbols.TokenLBRACE); |
|
474 |
else |
|
475 |
unit= findReferencePosition(offset, peekToken(offset)); |
|
476 |
||
477 |
// if we were unable to find anything, return null |
|
478 |
if (unit == CHeuristicScanner.NOT_FOUND) |
|
479 |
return null; |
|
480 |
||
481 |
return getLeadingWhitespace(unit); |
|
482 |
} |
|
483 |
||
484 |
/** |
|
485 |
* Computes the indentation at <code>offset</code>. |
|
486 |
* |
|
487 |
* @param offset the offset in the document |
|
488 |
* @return a String which reflects the correct indentation for the line in |
|
489 |
* which offset resides, or <code>null</code> if it cannot be |
|
490 |
* determined |
|
491 |
*/ |
|
492 |
public StringBuilder computeIndentation(int offset) { |
|
493 |
return computeIndentation(offset, false); |
|
494 |
} |
|
495 |
||
496 |
/** |
|
497 |
* Computes the indentation at <code>offset</code>. |
|
498 |
* |
|
499 |
* @param offset the offset in the document |
|
500 |
* @param assumeOpeningBrace <code>true</code> if an opening brace should be assumed |
|
501 |
* @return a String which reflects the correct indentation for the line in |
|
502 |
* which offset resides, or <code>null</code> if it cannot be |
|
503 |
* determined |
|
504 |
*/ |
|
505 |
public StringBuilder computeIndentation(int offset, boolean assumeOpeningBrace) { |
|
506 |
StringBuilder reference= getReferenceIndentation(offset, assumeOpeningBrace); |
|
507 |
||
508 |
// handle special alignment |
|
509 |
if (fAlign != CHeuristicScanner.NOT_FOUND) { |
|
510 |
try { |
|
511 |
// a special case has been detected. |
|
512 |
IRegion line= fDocument.getLineInformationOfOffset(fAlign); |
|
513 |
int lineOffset= line.getOffset(); |
|
514 |
return createIndent(lineOffset, fAlign, false); |
|
515 |
} catch (BadLocationException e) { |
|
516 |
return null; |
|
517 |
} |
|
518 |
} |
|
519 |
||
520 |
if (reference == null) |
|
521 |
return null; |
|
522 |
||
523 |
// Add additional indent |
|
524 |
return createReusingIndent(reference, fIndent, fExtraSpaces); |
|
525 |
} |
|
526 |
||
527 |
/** |
|
528 |
* Computes the indentation for a continuation line at <code>offset</code>. |
|
529 |
* |
|
530 |
* @param offset the offset in the document |
|
531 |
* @return a StringBuilder which reflects the correct indentation for |
|
532 |
* the line in which offset resides, or <code>null</code> if it cannot be |
|
533 |
* determined. |
|
534 |
* @throws BadLocationException |
|
535 |
*/ |
|
536 |
public StringBuilder computeContinuationLineIndentation(int offset) throws BadLocationException { |
|
537 |
StringBuilder reference= getLeadingWhitespace(offset); |
|
538 |
IRegion line= fDocument.getLineInformationOfOffset(offset); |
|
539 |
String string= fDocument.get(line.getOffset(), offset - line.getOffset()); |
|
540 |
if (string.trim().length() == 0) |
|
541 |
return reference; |
|
542 |
// Add additional indent |
|
543 |
return createReusingIndent(reference, fPrefs.prefContinuationIndent, 0); |
|
544 |
} |
|
545 |
||
546 |
/** |
|
547 |
* Computes the length of a <code>CharacterSequence</code>, counting |
|
548 |
* a tab character as the size until the next tab stop and every other |
|
549 |
* character as one. |
|
550 |
* |
|
551 |
* @param indent the string to measure |
|
552 |
* @return the visual length in characters |
|
553 |
*/ |
|
554 |
private int computeVisualLength(CharSequence indent) { |
|
555 |
final int tabSize= fPrefs.prefTabSize; |
|
556 |
int length= 0; |
|
557 |
for (int i= 0; i < indent.length(); i++) { |
|
558 |
char ch= indent.charAt(i); |
|
559 |
switch (ch) { |
|
560 |
case '\t': |
|
561 |
if (tabSize > 0) { |
|
562 |
int reminder= length % tabSize; |
|
563 |
length += tabSize - reminder; |
|
564 |
} |
|
565 |
break; |
|
566 |
case ' ': |
|
567 |
length++; |
|
568 |
break; |
|
569 |
} |
|
570 |
} |
|
571 |
return length; |
|
572 |
} |
|
573 |
||
574 |
/** |
|
575 |
* Strips any characters off the end of <code>reference</code> that exceed |
|
576 |
* <code>indentLength</code>. |
|
577 |
* |
|
578 |
* @param reference the string to measure |
|
579 |
* @param indentLength the maximum visual indentation length |
|
580 |
* @return the stripped <code>reference</code> |
|
581 |
*/ |
|
582 |
private StringBuilder stripExceedingChars(StringBuilder reference, int indentLength) { |
|
583 |
final int tabSize= fPrefs.prefTabSize; |
|
584 |
int measured= 0; |
|
585 |
int chars= reference.length(); |
|
586 |
int i= 0; |
|
587 |
for (; measured < indentLength && i < chars; i++) { |
|
588 |
char ch= reference.charAt(i); |
|
589 |
switch (ch) { |
|
590 |
case '\t': |
|
591 |
if (tabSize > 0) { |
|
592 |
int reminder= measured % tabSize; |
|
593 |
measured += tabSize - reminder; |
|
594 |
} |
|
595 |
break; |
|
596 |
case ' ': |
|
597 |
measured++; |
|
598 |
break; |
|
599 |
} |
|
600 |
} |
|
601 |
int deleteFrom= measured > indentLength ? i - 1 : i; |
|
602 |
||
603 |
return reference.delete(deleteFrom, chars); |
|
604 |
} |
|
605 |
||
606 |
/** |
|
607 |
* Returns the indentation of the line at <code>offset</code> as a |
|
608 |
* <code>StringBuilder</code>. If the offset is not valid, the empty string |
|
609 |
* is returned. |
|
610 |
* |
|
611 |
* @param offset the offset in the document |
|
612 |
* @return the indentation (leading whitespace) of the line in which |
|
613 |
* <code>offset</code> is located |
|
614 |
*/ |
|
615 |
private StringBuilder getLeadingWhitespace(int offset) { |
|
616 |
StringBuilder indent= new StringBuilder(); |
|
617 |
try { |
|
618 |
IRegion line= fDocument.getLineInformationOfOffset(offset); |
|
619 |
int lineOffset= line.getOffset(); |
|
620 |
int nonWS= fScanner.findNonWhitespaceForwardInAnyPartition(lineOffset, lineOffset + line.getLength()); |
|
621 |
indent.append(fDocument.get(lineOffset, nonWS - lineOffset)); |
|
622 |
return indent; |
|
623 |
} catch (BadLocationException e) { |
|
624 |
return indent; |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* Creates an indentation string of the length indent - start, consisting of |
|
630 |
* the content in <code>fDocument</code> in the range [start, indent), |
|
631 |
* with every character replaced by a space except for tabs, which are kept |
|
632 |
* as such. |
|
633 |
* <p> |
|
634 |
* If <code>convertSpaceRunsToTabs</code> is <code>true</code>, every |
|
635 |
* run of the number of spaces that make up a tab are replaced by a tab |
|
636 |
* character. If it is not set, no conversion takes place, but tabs in the |
|
637 |
* original range are still copied verbatim. |
|
638 |
* </p> |
|
639 |
* |
|
640 |
* @param start the start of the document region to copy the indent from |
|
641 |
* @param indent the exclusive end of the document region to copy the indent |
|
642 |
* from |
|
643 |
* @param convertSpaceRunsToTabs whether to convert consecutive runs of |
|
644 |
* spaces to tabs |
|
645 |
* @return the indentation corresponding to the document content specified |
|
646 |
* by <code>start</code> and <code>indent</code> |
|
647 |
*/ |
|
648 |
private StringBuilder createIndent(int start, final int indent, final boolean convertSpaceRunsToTabs) { |
|
649 |
final boolean convertTabs= fPrefs.prefUseTabs && convertSpaceRunsToTabs; |
|
650 |
final int tabLen= fPrefs.prefTabSize; |
|
651 |
final StringBuilder ret= new StringBuilder(); |
|
652 |
try { |
|
653 |
int spaces= 0; |
|
654 |
while (start < indent) { |
|
655 |
char ch= fDocument.getChar(start); |
|
656 |
if (ch == '\t') { |
|
657 |
ret.append('\t'); |
|
658 |
spaces= 0; |
|
659 |
} else if (convertTabs) { |
|
660 |
spaces++; |
|
661 |
if (spaces == tabLen) { |
|
662 |
ret.append('\t'); |
|
663 |
spaces= 0; |
|
664 |
} |
|
665 |
} else { |
|
666 |
ret.append(' '); |
|
667 |
} |
|
668 |
||
669 |
start++; |
|
670 |
} |
|
671 |
// remainder |
|
672 |
while (spaces-- > 0) |
|
673 |
ret.append(' '); |
|
674 |
} catch (BadLocationException e) { |
|
675 |
} |
|
676 |
||
677 |
return ret; |
|
678 |
} |
|
679 |
||
680 |
/** |
|
681 |
* Creates a string with a visual length of the given |
|
682 |
* <code>indentationSize</code>. |
|
683 |
* |
|
684 |
* @param buffer the original indent to reuse if possible |
|
685 |
* @param additional the additional indentation units to add or subtract to |
|
686 |
* reference |
|
687 |
* @param extraSpaces additional spaces to add to indentation. |
|
688 |
* @return the modified <code>buffer</code> reflecting the indentation |
|
689 |
* adapted to <code>additional</code> |
|
690 |
*/ |
|
691 |
public StringBuilder createReusingIndent(StringBuilder buffer, int additional, int extraSpaces) { |
|
692 |
int refLength= computeVisualLength(buffer); |
|
693 |
int addLength= fPrefs.prefIndentationSize * additional + extraSpaces; // may be < 0 |
|
694 |
int totalLength= Math.max(0, refLength + addLength); |
|
695 |
||
696 |
// copy the reference indentation for the indent up to the last tab |
|
697 |
// stop within the maxCopy area |
|
698 |
int minLength= Math.min(totalLength, refLength); |
|
699 |
int tabSize= fPrefs.prefTabSize; |
|
700 |
int maxCopyLength= tabSize > 0 ? minLength - minLength % tabSize : minLength; // maximum indent to copy |
|
701 |
stripExceedingChars(buffer, maxCopyLength); |
|
702 |
||
703 |
// add additional indent |
|
704 |
int missing= totalLength - maxCopyLength; |
|
705 |
final int tabs, spaces; |
|
706 |
if (CCorePlugin.SPACE.equals(fPrefs.prefTabChar)) { |
|
707 |
tabs= 0; |
|
708 |
spaces= missing; |
|
709 |
} else { |
|
710 |
tabs= tabSize > 0 ? missing / tabSize : 0; |
|
711 |
spaces= tabSize > 0 ? missing % tabSize : missing; |
|
712 |
} |
|
713 |
for (int i= 0; i < tabs; i++) |
|
714 |
buffer.append('\t'); |
|
715 |
for (int i= 0; i < spaces; i++) |
|
716 |
buffer.append(' '); |
|
717 |
return buffer; |
|
718 |
} |
|
719 |
||
720 |
/** |
|
721 |
* Returns relative indent of continuation lines. |
|
722 |
* @return a number of indentation units. |
|
723 |
*/ |
|
724 |
public int getContinuationLineIndent() { |
|
725 |
return fPrefs.prefContinuationIndent; |
|
726 |
} |
|
727 |
||
728 |
/** |
|
729 |
* Returns the reference position regarding to indentation for <code>offset</code>, |
|
730 |
* or <code>NOT_FOUND</code>. This method calls |
|
731 |
* {@link #findReferencePosition(int, int) findReferencePosition(offset, nextChar)} where |
|
732 |
* <code>nextChar</code> is the next character after <code>offset</code>. |
|
733 |
* |
|
734 |
* @param offset the offset for which the reference is computed |
|
735 |
* @return the reference statement relative to which <code>offset</code> |
|
736 |
* should be indented, or {@link CHeuristicScanner#NOT_FOUND} |
|
737 |
*/ |
|
738 |
public int findReferencePosition(int offset) { |
|
739 |
return findReferencePosition(offset, peekToken(offset)); |
|
740 |
} |
|
741 |
||
742 |
/** |
|
743 |
* Peeks the next token in the document that comes after <code>offset</code> |
|
744 |
* on the same line as <code>offset</code>. |
|
745 |
* |
|
746 |
* @param offset the offset into document |
|
747 |
* @return the token symbol of the next element, or TokenEOF if there is none |
|
748 |
*/ |
|
749 |
private int peekToken(int offset) { |
|
750 |
if (offset < fDocument.getLength()) { |
|
751 |
try { |
|
752 |
IRegion line= fDocument.getLineInformationOfOffset(offset); |
|
753 |
int lineOffset= line.getOffset(); |
|
754 |
int next= fScanner.nextToken(offset, lineOffset + line.getLength()); |
|
755 |
return next; |
|
756 |
} catch (BadLocationException e) { |
|
757 |
} |
|
758 |
} |
|
759 |
return Symbols.TokenEOF; |
|
760 |
} |
|
761 |
||
762 |
/** |
|
763 |
* Returns the reference position regarding to indentation for <code>position</code>, |
|
764 |
* or <code>NOT_FOUND</code>. |
|
765 |
* |
|
766 |
* <p>If <code>peekNextChar</code> is <code>true</code>, the next token after |
|
767 |
* <code>offset</code> is read and taken into account when computing the |
|
768 |
* indentation. Currently, if the next token is the first token on the line |
|
769 |
* (i.e. only preceded by whitespace), the following tokens are specially |
|
770 |
* handled: |
|
771 |
* <ul> |
|
772 |
* <li><code>switch</code> labels are indented relative to the switch block</li> |
|
773 |
* <li>opening curly braces are aligned correctly with the introducing code</li> |
|
774 |
* <li>closing curly braces are aligned properly with the introducing code of |
|
775 |
* the matching opening brace</li> |
|
776 |
* <li>closing parenthesis' are aligned with their opening peer</li> |
|
777 |
* <li>the <code>else</code> keyword is aligned with its <code>if</code>, anything |
|
778 |
* else is aligned normally (i.e. with the base of any introducing statements).</li> |
|
779 |
* <li>if there is no token on the same line after <code>offset</code>, the indentation |
|
780 |
* is the same as for an <code>else</code> keyword</li> |
|
781 |
* </ul> |
|
782 |
* |
|
783 |
* @param offset the offset for which the reference is computed |
|
784 |
* @param nextToken the next token to assume in the document |
|
785 |
* @return the reference statement relative to which <code>offset</code> |
|
786 |
* should be indented, or {@link CHeuristicScanner#NOT_FOUND} |
|
787 |
*/ |
|
788 |
public int findReferencePosition(int offset, int nextToken) { |
|
789 |
boolean danglingElse= false; |
|
790 |
boolean cancelIndent= false; // If set to true, fIndent is ignored. |
|
791 |
int extraIndent= 0; // Can be either positive or negative. |
|
792 |
boolean matchBrace= false; |
|
793 |
boolean matchParen= false; |
|
794 |
boolean matchCase= false; |
|
795 |
boolean matchAccessSpecifier= false; |
|
796 |
||
797 |
// Account for un-indentation characters already typed in, but after position. |
|
798 |
// If they are on a line by themselves, the indentation gets adjusted accordingly. |
|
799 |
// |
|
800 |
// Also account for a dangling else. |
|
801 |
if (offset < fDocument.getLength()) { |
|
802 |
try { |
|
803 |
IRegion line= fDocument.getLineInformationOfOffset(offset); |
|
804 |
int lineOffset= line.getOffset(); |
|
805 |
int prevPos= Math.max(offset - 1, 0); |
|
806 |
boolean isFirstTokenOnLine= fDocument.get(lineOffset, prevPos + 1 - lineOffset).trim().length() == 0; |
|
807 |
int prevToken= fScanner.previousToken(prevPos, CHeuristicScanner.UNBOUND); |
|
808 |
boolean bracelessBlockStart= fScanner.isBracelessBlockStart(prevPos, CHeuristicScanner.UNBOUND); |
|
809 |
||
810 |
switch (nextToken) { |
|
811 |
case Symbols.TokenELSE: |
|
812 |
danglingElse= true; |
|
813 |
break; |
|
814 |
||
815 |
case Symbols.TokenCASE: |
|
816 |
case Symbols.TokenDEFAULT: |
|
817 |
if (isFirstTokenOnLine) |
|
818 |
matchCase= true; |
|
819 |
break; |
|
820 |
||
821 |
case Symbols.TokenPUBLIC: |
|
822 |
case Symbols.TokenPROTECTED: |
|
823 |
case Symbols.TokenPRIVATE: |
|
824 |
if (isFirstTokenOnLine) |
|
825 |
matchAccessSpecifier= true; |
|
826 |
break; |
|
827 |
||
828 |
case Symbols.TokenLBRACE: // for opening-brace-on-new-line style |
|
829 |
if (bracelessBlockStart) { |
|
830 |
extraIndent= fPrefs.prefIndentBracesForBlocks ? 0 : -1; |
|
831 |
} else if (prevToken == Symbols.TokenCOLON && !fPrefs.prefIndentBracesForBlocks) { |
|
832 |
extraIndent= -1; |
|
833 |
} else if ((prevToken == Symbols.TokenEQUAL || prevToken == Symbols.TokenRBRACKET) && |
|
834 |
!fPrefs.prefIndentBracesForArrays) { |
|
835 |
cancelIndent= true; |
|
138
e0657161c0cc
bug 11072 - fix problems with formatting around const functions
dadubrow
parents:
37
diff
changeset
|
836 |
} else if ((prevToken == Symbols.TokenRPAREN || prevToken == Symbols.TokenCONST) && fPrefs.prefIndentBracesForMethods) { |
37 | 837 |
extraIndent= 1; |
838 |
} else if (prevToken == Symbols.TokenIDENT && fPrefs.prefIndentBracesForTypes) { |
|
839 |
extraIndent= 1; |
|
840 |
} |
|
841 |
break; |
|
842 |
||
843 |
case Symbols.TokenRBRACE: // closing braces get unindented |
|
844 |
if (isFirstTokenOnLine || prevToken != Symbols.TokenLBRACE) |
|
845 |
matchBrace= true; |
|
846 |
break; |
|
847 |
||
848 |
case Symbols.TokenRPAREN: |
|
849 |
if (isFirstTokenOnLine) |
|
850 |
matchParen= true; |
|
851 |
break; |
|
852 |
} |
|
853 |
} catch (BadLocationException e) { |
|
854 |
} |
|
855 |
} else { |
|
856 |
// don't assume an else could come if we are at the end of file |
|
857 |
danglingElse= false; |
|
858 |
} |
|
859 |
||
860 |
int ref= findReferencePosition(offset, danglingElse, matchBrace, matchParen, matchCase, |
|
861 |
matchAccessSpecifier); |
|
862 |
if (cancelIndent) { |
|
863 |
fIndent = 0; |
|
864 |
} else if (extraIndent > 0) { |
|
865 |
fAlign= CHeuristicScanner.NOT_FOUND; |
|
866 |
fIndent += extraIndent; |
|
867 |
} else { |
|
868 |
fIndent += extraIndent; |
|
869 |
} |
|
870 |
return ref; |
|
871 |
} |
|
872 |
||
873 |
/** |
|
874 |
* Returns the reference position regarding to indentation for <code>position</code>, |
|
875 |
* or <code>NOT_FOUND</code>.<code>fIndent</code> will contain the |
|
876 |
* relative indentation (in indentation units, not characters) after the |
|
877 |
* call. If there is a special alignment (e.g. for a method declaration |
|
878 |
* where parameters should be aligned), <code>fAlign</code> will contain |
|
879 |
* the absolute position of the alignment reference in <code>fDocument</code>, |
|
880 |
* otherwise <code>fAlign</code> is set to <code>CHeuristicScanner.NOT_FOUND</code>. |
|
881 |
* |
|
882 |
* @param offset the offset for which the reference is computed |
|
883 |
* @param danglingElse whether a dangling else should be assumed at <code>position</code> |
|
884 |
* @param matchBrace whether the position of the matching brace should be |
|
885 |
* returned instead of doing code analysis |
|
886 |
* @param matchParen whether the position of the matching parenthesis |
|
887 |
* should be returned instead of doing code analysis |
|
888 |
* @param matchCase whether the position of a switch statement reference |
|
889 |
* should be returned (either an earlier case statement or the |
|
890 |
* switch block brace) |
|
891 |
* @param matchAccessSpecifier whether the position of a class body reference |
|
892 |
* should be returned (either an earlier public/protected/private |
|
893 |
* or the class body brace) |
|
894 |
* @return the reference statement relative to which <code>position</code> |
|
895 |
* should be indented, or {@link CHeuristicScanner#NOT_FOUND} |
|
896 |
*/ |
|
897 |
public int findReferencePosition(int offset, boolean danglingElse, boolean matchBrace, boolean matchParen, |
|
898 |
boolean matchCase, boolean matchAccessSpecifier) { |
|
899 |
fIndent= 0; // the indentation modification |
|
900 |
fAlign= CHeuristicScanner.NOT_FOUND; |
|
901 |
fPosition= offset; |
|
902 |
||
903 |
// forward cases |
|
904 |
// An unindentation happens sometimes if the next token is special, namely on braces, parens and case |
|
905 |
// labels align braces, but handle the case where we align with the method declaration start instead |
|
906 |
// of the opening brace. |
|
907 |
if (matchBrace) { |
|
908 |
if (skipScope(Symbols.TokenLBRACE, Symbols.TokenRBRACE)) { |
|
909 |
try { |
|
910 |
// Align with the opening brace that is on a line by its own |
|
911 |
int lineOffset= fDocument.getLineOffset(fLine); |
|
912 |
if (lineOffset <= fPosition && fDocument.get(lineOffset, fPosition - lineOffset).trim().length() == 0) |
|
913 |
return fPosition; |
|
914 |
} catch (BadLocationException e) { |
|
915 |
// Concurrent modification - walk default path |
|
916 |
} |
|
917 |
// If the opening brace is not on the start of the line, skip to the start |
|
918 |
int pos= skipToStatementStart(true, true); |
|
919 |
fIndent= 0; // indent is aligned with reference position |
|
920 |
return pos; |
|
921 |
} else { |
|
922 |
// If we can't find the matching brace, the heuristic is to unindent |
|
923 |
// by one against the normal position |
|
924 |
int pos= findReferencePosition(offset, danglingElse, false, matchParen, matchCase, |
|
925 |
matchAccessSpecifier); |
|
926 |
fIndent--; |
|
927 |
return pos; |
|
928 |
} |
|
929 |
} |
|
930 |
||
931 |
// Align parentheses |
|
932 |
if (matchParen) { |
|
933 |
if (skipScope(Symbols.TokenLPAREN, Symbols.TokenRPAREN)) { |
|
934 |
return fPosition; |
|
935 |
} else { |
|
936 |
// if we can't find the matching paren, the heuristic is to unindent |
|
937 |
// by one against the normal position |
|
938 |
int pos= findReferencePosition(offset, danglingElse, matchBrace, false, matchCase, |
|
939 |
matchAccessSpecifier); |
|
940 |
fIndent--; |
|
941 |
return pos; |
|
942 |
} |
|
943 |
} |
|
944 |
||
945 |
// The only reliable way to get case labels aligned (due to many different styles of using braces in |
|
946 |
// a block) is to go for another case statement, or the scope opening brace. |
|
947 |
if (matchCase) { |
|
948 |
return matchCaseAlignment(); |
|
949 |
} |
|
950 |
||
951 |
// the only reliable way to get access specifiers aligned (due to many different styles of using |
|
952 |
// braces in a block) is to go for another access specifier, or the scope opening brace. |
|
953 |
if (matchAccessSpecifier) { |
|
954 |
return matchAccessSpecifierAlignment(); |
|
955 |
} |
|
956 |
||
957 |
nextToken(); |
|
958 |
// Skip access specifiers |
|
959 |
while (fToken == Symbols.TokenCOLON && isAccessSpecifier()) { |
|
960 |
nextToken(); |
|
961 |
} |
|
962 |
||
963 |
int line= fLine; |
|
964 |
switch (fToken) { |
|
965 |
case Symbols.TokenGREATERTHAN: |
|
966 |
case Symbols.TokenRBRACE: |
|
967 |
// skip the block and fall through |
|
968 |
// if we can't complete the scope, reset the scan position |
|
969 |
int pos= fPosition; |
|
970 |
if (!skipScope()) |
|
971 |
fPosition= pos; |
|
972 |
return skipToStatementStart(danglingElse, false); |
|
973 |
case Symbols.TokenSEMICOLON: |
|
974 |
// this is the 90% case: after a statement block |
|
975 |
// the end of the previous statement / block previous.end |
|
976 |
// search to the end of the statement / block before the previous; |
|
977 |
// the token just after that is previous.start |
|
978 |
return skipToStatementStart(danglingElse, false); |
|
979 |
||
980 |
// scope introduction: special treat who special is |
|
981 |
case Symbols.TokenLPAREN: |
|
982 |
case Symbols.TokenLBRACE: |
|
983 |
case Symbols.TokenLBRACKET: |
|
984 |
return handleScopeIntroduction(Math.min(offset + 1, fDocument.getLength()), true); |
|
985 |
||
986 |
case Symbols.TokenEOF: |
|
987 |
// trap when hitting start of document |
|
988 |
return CHeuristicScanner.NOT_FOUND; |
|
989 |
||
990 |
case Symbols.TokenEQUAL: |
|
991 |
// indent assignments |
|
992 |
fIndent= fPrefs.prefAssignmentIndent; |
|
993 |
return fPosition; |
|
994 |
||
995 |
case Symbols.TokenCOLON: |
|
996 |
pos= fPosition; |
|
997 |
if (looksLikeCaseStatement()) { |
|
998 |
fIndent= fPrefs.prefCaseBlockIndent; |
|
999 |
return pos; |
|
1000 |
} |
|
1001 |
fPosition= pos; |
|
1002 |
if (looksLikeTypeInheritanceDecl()) { |
|
1003 |
fIndent= fPrefs.prefBlockIndent; |
|
1004 |
return pos; |
|
1005 |
} |
|
1006 |
fPosition= pos; |
|
1007 |
if (looksLikeConstructorInitializer()) { |
|
1008 |
fIndent= fPrefs.prefBlockIndent; |
|
1009 |
return pos; |
|
1010 |
} |
|
1011 |
fPosition= pos; |
|
1012 |
if (isConditional()) { |
|
1013 |
fPosition= offset; |
|
1014 |
fLine= line; |
|
1015 |
return skipToPreviousListItemOrListStart(); |
|
1016 |
} |
|
1017 |
fPosition= pos; |
|
1018 |
return skipToStatementStart(danglingElse, false); |
|
1019 |
||
1020 |
case Symbols.TokenQUESTIONMARK: |
|
1021 |
if (fPrefs.prefTernaryDeepAlign) { |
|
1022 |
setFirstElementAlignment(fPosition, offset + 1); |
|
1023 |
} else { |
|
1024 |
fIndent= fPrefs.prefTernaryIndent; |
|
1025 |
} |
|
1026 |
return fPosition; |
|
1027 |
||
1028 |
// indentation for blockless introducers: |
|
1029 |
case Symbols.TokenDO: |
|
1030 |
case Symbols.TokenWHILE: |
|
1031 |
case Symbols.TokenELSE: |
|
1032 |
fIndent= fPrefs.prefSimpleIndent; |
|
1033 |
return fPosition; |
|
1034 |
||
1035 |
case Symbols.TokenTRY: |
|
1036 |
return skipToStatementStart(danglingElse, false); |
|
1037 |
||
138
e0657161c0cc
bug 11072 - fix problems with formatting around const functions
dadubrow
parents:
37
diff
changeset
|
1038 |
case Symbols.TokenCONST: |
e0657161c0cc
bug 11072 - fix problems with formatting around const functions
dadubrow
parents:
37
diff
changeset
|
1039 |
nextToken(); |
e0657161c0cc
bug 11072 - fix problems with formatting around const functions
dadubrow
parents:
37
diff
changeset
|
1040 |
if (fToken != Symbols.TokenRPAREN) { |
e0657161c0cc
bug 11072 - fix problems with formatting around const functions
dadubrow
parents:
37
diff
changeset
|
1041 |
return skipToPreviousListItemOrListStart(); |
e0657161c0cc
bug 11072 - fix problems with formatting around const functions
dadubrow
parents:
37
diff
changeset
|
1042 |
} |
e0657161c0cc
bug 11072 - fix problems with formatting around const functions
dadubrow
parents:
37
diff
changeset
|
1043 |
// could be const method decl |
e0657161c0cc
bug 11072 - fix problems with formatting around const functions
dadubrow
parents:
37
diff
changeset
|
1044 |
//$FALL-THROUGH$ |
37 | 1045 |
case Symbols.TokenRPAREN: |
1046 |
if (skipScope(Symbols.TokenLPAREN, Symbols.TokenRPAREN)) { |
|
1047 |
int scope= fPosition; |
|
1048 |
nextToken(); |
|
1049 |
if (fToken == Symbols.TokenIF || fToken == Symbols.TokenWHILE || fToken == Symbols.TokenFOR) { |
|
1050 |
fIndent= fPrefs.prefSimpleIndent; |
|
1051 |
return fPosition; |
|
1052 |
} |
|
1053 |
if (fToken == Symbols.TokenSWITCH) { |
|
1054 |
return fPosition; |
|
1055 |
} |
|
1056 |
fPosition= scope; |
|
1057 |
if (looksLikeMethodDecl()) { |
|
1058 |
return skipToStatementStart(danglingElse, false); |
|
1059 |
} |
|
1060 |
if (fToken == Symbols.TokenCATCH) { |
|
1061 |
return skipToStatementStart(danglingElse, false); |
|
1062 |
} |
|
1063 |
fPosition= scope; |
|
1064 |
if (looksLikeAnonymousTypeDecl()) { |
|
1065 |
return skipToStatementStart(danglingElse, false); |
|
1066 |
} |
|
1067 |
} |
|
1068 |
// restore |
|
1069 |
fPosition= offset; |
|
1070 |
fLine= line; |
|
1071 |
// else: fall through to default |
|
1072 |
return skipToPreviousListItemOrListStart(); |
|
1073 |
||
1074 |
case Symbols.TokenCOMMA: |
|
1075 |
// inside a list of some type |
|
1076 |
// easy if there is already a list item before with its own indentation - we just align |
|
1077 |
// if not: take the start of the list ( LPAREN, LBRACE, LBRACKET ) and either align or |
|
1078 |
// indent by list-indent |
|
1079 |
return skipToPreviousListItemOrListStart(); |
|
1080 |
default: |
|
1081 |
// inside whatever we don't know about: similar to the list case: |
|
1082 |
// if we are inside a continued expression, then either align with a previous line that |
|
1083 |
// has indentation or indent from the expression start line (either a scope introducer |
|
1084 |
// or the start of the expression). |
|
1085 |
return skipToPreviousListItemOrListStart(); |
|
1086 |
} |
|
1087 |
} |
|
1088 |
||
1089 |
/** |
|
1090 |
* Test whether an identifier encountered during scanning is part of |
|
1091 |
* a type declaration, by scanning backward and ignoring any identifiers, commas, |
|
1092 |
* and colons until we hit <code>class</code>, <code>struct</code>, <code>union</code>, |
|
1093 |
* or <code>enum</code>. If any braces, semicolons, or parentheses are encountered, |
|
1094 |
* this is not a type declaration. |
|
1095 |
* @return the reference offset of the start of the statement |
|
1096 |
*/ |
|
1097 |
private int matchTypeDeclaration() { |
|
1098 |
while (true) { |
|
1099 |
nextToken(); |
|
1100 |
if (fToken == Symbols.TokenIDENT |
|
1101 |
|| fToken == Symbols.TokenCOMMA |
|
1102 |
|| fToken == Symbols.TokenCOLON |
|
1103 |
|| fToken == Symbols.TokenPUBLIC |
|
1104 |
|| fToken == Symbols.TokenPROTECTED |
|
1105 |
|| fToken == Symbols.TokenPRIVATE) { |
|
1106 |
continue; |
|
1107 |
} |
|
1108 |
if (fToken == Symbols.TokenCLASS |
|
1109 |
|| fToken == Symbols.TokenSTRUCT |
|
1110 |
|| fToken == Symbols.TokenUNION |
|
1111 |
|| fToken == Symbols.TokenENUM) { |
|
1112 |
// inside a type declaration? Only so if not preceded by '(' or ',' as in |
|
1113 |
// a parameter list. To be safe, only accept ';' or EOF |
|
1114 |
int pos= fPosition; |
|
1115 |
nextToken(); |
|
1116 |
if (fToken == Symbols.TokenSEMICOLON || fToken == Symbols.TokenEOF) { |
|
1117 |
return pos; |
|
1118 |
} else { |
|
1119 |
return CHeuristicScanner.NOT_FOUND; |
|
1120 |
} |
|
1121 |
} else { |
|
1122 |
return CHeuristicScanner.NOT_FOUND; |
|
1123 |
} |
|
1124 |
} |
|
1125 |
} |
|
1126 |
||
1127 |
/** |
|
1128 |
* Test whether the colon at the current position marks a case statement |
|
1129 |
* |
|
1130 |
* @return <code>true</code> if this looks like a case statement |
|
1131 |
*/ |
|
1132 |
private boolean looksLikeCaseStatement() { |
|
1133 |
nextToken(); |
|
1134 |
switch (fToken) { |
|
1135 |
case Symbols.TokenCASE: |
|
1136 |
// char literal got skipped |
|
1137 |
return true; |
|
1138 |
case Symbols.TokenIDENT: |
|
1139 |
nextToken(); |
|
1140 |
while (skipQualifiers()) { |
|
1141 |
nextToken(); |
|
1142 |
} |
|
1143 |
if (fToken == Symbols.TokenCASE) { |
|
1144 |
return true; |
|
1145 |
} |
|
1146 |
break; |
|
1147 |
case Symbols.TokenOTHER: |
|
1148 |
nextToken(); |
|
1149 |
if (fToken == Symbols.TokenCASE) { |
|
1150 |
return true; |
|
1151 |
} |
|
1152 |
break; |
|
1153 |
case Symbols.TokenDEFAULT: |
|
1154 |
return true; |
|
1155 |
} |
|
1156 |
return false; |
|
1157 |
} |
|
1158 |
||
1159 |
/** |
|
1160 |
* Test whether the colon at the current position marks a type inheritance decl. |
|
1161 |
* |
|
1162 |
* @return <code>true</code> if this looks like a type inheritance decl. |
|
1163 |
*/ |
|
1164 |
private boolean looksLikeTypeInheritanceDecl() { |
|
1165 |
nextToken(); |
|
1166 |
switch (fToken) { |
|
1167 |
case Symbols.TokenIDENT: |
|
1168 |
nextToken(); |
|
1169 |
while (skipQualifiers()) { |
|
1170 |
nextToken(); |
|
1171 |
} |
|
1172 |
switch (fToken) { |
|
1173 |
case Symbols.TokenCLASS: |
|
1174 |
case Symbols.TokenSTRUCT: |
|
1175 |
case Symbols.TokenUNION: |
|
1176 |
return true; |
|
1177 |
} |
|
1178 |
break; |
|
1179 |
} |
|
1180 |
return false; |
|
1181 |
} |
|
1182 |
||
1183 |
/** |
|
1184 |
* Test whether the colon at the current position marks a constructor initializer list. |
|
1185 |
* |
|
1186 |
* @return <code>true</code> if this looks like a constructor initializer list. |
|
1187 |
*/ |
|
1188 |
private boolean looksLikeConstructorInitializer() { |
|
1189 |
nextToken(); |
|
1190 |
if (fToken != Symbols.TokenRPAREN) { |
|
1191 |
return false; |
|
1192 |
} |
|
1193 |
if (!skipScope()) { |
|
1194 |
return false; |
|
1195 |
} |
|
1196 |
nextToken(); |
|
1197 |
if (fToken == Symbols.TokenTHROW) { |
|
1198 |
nextToken(); |
|
1199 |
if (fToken != Symbols.TokenRPAREN) { |
|
1200 |
return false; |
|
1201 |
} |
|
1202 |
if (!skipScope()) { |
|
1203 |
return false; |
|
1204 |
} |
|
1205 |
nextToken(); |
|
1206 |
} |
|
1207 |
if (fToken != Symbols.TokenIDENT) { |
|
1208 |
return false; |
|
1209 |
} |
|
1210 |
nextToken(); |
|
1211 |
switch (fToken) { |
|
1212 |
case Symbols.TokenCOLON: |
|
1213 |
nextToken(); |
|
1214 |
switch (fToken) { |
|
1215 |
case Symbols.TokenCOLON: // A::A() : |
|
1216 |
case Symbols.TokenPUBLIC: // public: A() : |
|
1217 |
case Symbols.TokenPROTECTED: |
|
1218 |
case Symbols.TokenPRIVATE: |
|
1219 |
return true; |
|
1220 |
} |
|
1221 |
return false; |
|
1222 |
||
1223 |
case Symbols.TokenLBRACE: // class A { A() : |
|
1224 |
case Symbols.TokenRBRACE: |
|
1225 |
case Symbols.TokenSEMICOLON: |
|
1226 |
return true; |
|
1227 |
} |
|
1228 |
return false; |
|
1229 |
} |
|
1230 |
||
1231 |
/** |
|
1232 |
* Test whether the colon at the current position marks an access specifier. |
|
1233 |
* |
|
1234 |
* @return <code>true</code> if current position marks an access specifier |
|
1235 |
*/ |
|
1236 |
private boolean isAccessSpecifier() { |
|
1237 |
int pos= fPosition; |
|
1238 |
int token = fToken; |
|
1239 |
nextToken(); |
|
1240 |
switch (fToken) { |
|
1241 |
case Symbols.TokenPUBLIC: |
|
1242 |
case Symbols.TokenPROTECTED: |
|
1243 |
case Symbols.TokenPRIVATE: |
|
1244 |
return true; |
|
1245 |
} |
|
1246 |
fToken = token; |
|
1247 |
fPosition= pos; |
|
1248 |
return false; |
|
1249 |
} |
|
1250 |
||
1251 |
/** |
|
1252 |
* Skips to the start of a statement that ends at the current position. |
|
1253 |
* |
|
1254 |
* @param danglingElse whether to indent aligned with the last <code>if</code> |
|
1255 |
* @param isInBlock whether the current position is inside a block, which limits the search scope to |
|
1256 |
* the next scope introducer |
|
1257 |
* @return the reference offset of the start of the statement |
|
1258 |
*/ |
|
1259 |
private int skipToStatementStart(boolean danglingElse, boolean isInBlock) { |
|
1260 |
final int NOTHING= 0; |
|
1261 |
final int READ_PARENS= 1; |
|
1262 |
final int READ_IDENT= 2; |
|
1263 |
int mayBeMethodBody= NOTHING; |
|
1264 |
boolean isTypeBody= false; |
|
1265 |
int startLine = fLine; |
|
1266 |
while (true) { |
|
1267 |
int prevToken= fToken; |
|
1268 |
nextToken(); |
|
1269 |
||
1270 |
if (isInBlock) { |
|
1271 |
switch (fToken) { |
|
1272 |
// exit on all block introducers |
|
1273 |
case Symbols.TokenIF: |
|
1274 |
case Symbols.TokenELSE: |
|
1275 |
case Symbols.TokenCATCH: |
|
1276 |
case Symbols.TokenDO: |
|
1277 |
case Symbols.TokenWHILE: |
|
1278 |
case Symbols.TokenFOR: |
|
1279 |
case Symbols.TokenTRY: |
|
140 | 1280 |
fIndent += fPrefs.prefIndentBracesForBlocks ? 1 : 0; |
37 | 1281 |
return fPosition; |
1282 |
||
1283 |
case Symbols.TokenCLASS: |
|
1284 |
case Symbols.TokenENUM: |
|
1285 |
case Symbols.TokenSTRUCT: |
|
1286 |
case Symbols.TokenUNION: |
|
1287 |
isTypeBody= true; |
|
1288 |
break; |
|
1289 |
||
1290 |
case Symbols.TokenSWITCH: |
|
1291 |
fIndent= fPrefs.prefCaseIndent; |
|
1292 |
return fPosition; |
|
1293 |
} |
|
1294 |
} |
|
1295 |
||
1296 |
if (fToken == Symbols.TokenSEMICOLON && fLine == startLine) { |
|
1297 |
// Skip semicolons on the same line. Otherwise we may never reach beginning of a 'for' |
|
1298 |
// statement. |
|
1299 |
continue; |
|
1300 |
} |
|
1301 |
||
1302 |
switch (fToken) { |
|
1303 |
// scope introduction through: LPAREN, LBRACE, LBRACKET |
|
1304 |
// search stop on SEMICOLON, RBRACE, COLON, EOF |
|
1305 |
// -> the next token is the start of the statement (i.e. previousPos when backward scanning) |
|
1306 |
case Symbols.TokenLPAREN: |
|
1307 |
if (peekToken() == Symbols.TokenFOR) { |
|
1308 |
nextToken(); // Consume 'for' |
|
1309 |
fIndent = fPrefs.prefContinuationIndent; |
|
1310 |
return fPosition; |
|
1311 |
} |
|
1312 |
break; |
|
1313 |
||
1314 |
case Symbols.TokenLBRACE: |
|
1315 |
case Symbols.TokenSEMICOLON: |
|
1316 |
case Symbols.TokenEOF: |
|
1317 |
if (isInBlock) |
|
1318 |
fIndent= getBlockIndent(mayBeMethodBody == READ_IDENT, isTypeBody); |
|
1319 |
// else: fIndent set by previous calls |
|
1320 |
return fPreviousPos; |
|
1321 |
||
1322 |
case Symbols.TokenCOLON: |
|
1323 |
int pos= fPreviousPos; |
|
1324 |
if (!isConditional()) |
|
1325 |
return pos; |
|
1326 |
break; |
|
1327 |
||
1328 |
case Symbols.TokenRBRACE: |
|
1329 |
// RBRACE is a little tricky: it can be the end of an array definition, but |
|
1330 |
// usually it is the end of a previous block |
|
1331 |
pos= fPreviousPos; // store state |
|
1332 |
if (skipScope()) { |
|
1333 |
if (looksLikeArrayInitializerIntro()) { |
|
1334 |
continue; // it's an array |
|
1335 |
} |
|
1336 |
if (prevToken == Symbols.TokenSEMICOLON) { |
|
1337 |
// end of type def |
|
1338 |
continue; |
|
1339 |
} |
|
1340 |
} |
|
1341 |
if (isInBlock) |
|
1342 |
fIndent= getBlockIndent(mayBeMethodBody == READ_IDENT, isTypeBody); |
|
1343 |
return pos; // it's not - do as with all the above |
|
1344 |
||
1345 |
// scopes: skip them |
|
1346 |
case Symbols.TokenRPAREN: |
|
1347 |
if (isInBlock) |
|
1348 |
mayBeMethodBody= READ_PARENS; |
|
1349 |
// fall thru |
|
1350 |
pos= fPreviousPos; |
|
1351 |
if (skipScope()) |
|
1352 |
break; |
|
1353 |
else |
|
1354 |
return pos; |
|
1355 |
case Symbols.TokenRBRACKET: |
|
1356 |
pos= fPreviousPos; |
|
1357 |
if (skipScope()) |
|
1358 |
break; |
|
1359 |
else |
|
1360 |
return pos; |
|
1361 |
||
1362 |
// IF / ELSE: align the position after the conditional block with the if |
|
1363 |
// so we are ready for an else, except if danglingElse is false |
|
1364 |
// in order for this to work, we must skip an else to its if |
|
1365 |
case Symbols.TokenIF: |
|
1366 |
if (danglingElse) |
|
1367 |
return fPosition; |
|
1368 |
else |
|
1369 |
break; |
|
1370 |
case Symbols.TokenELSE: |
|
1371 |
// skip behind the next if, as we have that one covered |
|
1372 |
pos= fPosition; |
|
1373 |
if (skipNextIF()) |
|
1374 |
break; |
|
1375 |
else |
|
1376 |
return pos; |
|
1377 |
||
1378 |
case Symbols.TokenDO: |
|
1379 |
// align the WHILE position with its do |
|
1380 |
return fPosition; |
|
1381 |
||
1382 |
case Symbols.TokenWHILE: |
|
1383 |
// this one is tricky: while can be the start of a while loop |
|
1384 |
// or the end of a do - while |
|
1385 |
pos= fPosition; |
|
1386 |
if (hasMatchingDo()) { |
|
1387 |
// continue searching from the DO on |
|
1388 |
break; |
|
1389 |
} else { |
|
1390 |
// continue searching from the WHILE on |
|
1391 |
fPosition= pos; |
|
1392 |
break; |
|
1393 |
} |
|
1394 |
case Symbols.TokenIDENT: |
|
1395 |
if (mayBeMethodBody == READ_PARENS) |
|
1396 |
mayBeMethodBody= READ_IDENT; |
|
1397 |
break; |
|
1398 |
||
1399 |
default: |
|
1400 |
// keep searching |
|
1401 |
} |
|
1402 |
} |
|
1403 |
} |
|
1404 |
||
1405 |
private int getBlockIndent(boolean isMethodBody, boolean isTypeBody) { |
|
1406 |
if (isTypeBody) { |
|
1407 |
return fPrefs.prefTypeIndent + fPrefs.prefAccessSpecifierIndent; |
|
1408 |
} else if (isMethodBody) { |
|
1409 |
return fPrefs.prefMethodBodyIndent + (fPrefs.prefIndentBracesForMethods ? 1 : 0); |
|
1410 |
} else { |
|
1411 |
return fIndent; |
|
1412 |
} |
|
1413 |
} |
|
1414 |
||
1415 |
/** |
|
1416 |
* Returns <code>true</code> if the colon at the current position is part of a conditional |
|
1417 |
* (ternary) expression, <code>false</code> otherwise. |
|
1418 |
* |
|
1419 |
* @return <code>true</code> if the colon at the current position is part of a conditional |
|
1420 |
*/ |
|
1421 |
private boolean isConditional() { |
|
1422 |
while (true) { |
|
1423 |
int previous= fToken; |
|
1424 |
nextToken(); |
|
1425 |
switch (fToken) { |
|
1426 |
// search for case labels, which consist of (possibly qualified) identifiers or numbers |
|
1427 |
case Symbols.TokenIDENT: |
|
1428 |
if (previous == Symbols.TokenIDENT) { |
|
1429 |
return false; |
|
1430 |
} |
|
1431 |
// fall thru |
|
1432 |
continue; |
|
1433 |
case Symbols.TokenDOUBLECOLON: |
|
1434 |
case Symbols.TokenOTHER: |
|
1435 |
continue; |
|
1436 |
||
1437 |
case Symbols.TokenQUESTIONMARK: |
|
1438 |
return true; |
|
1439 |
||
1440 |
case Symbols.TokenSEMICOLON: |
|
1441 |
case Symbols.TokenLBRACE: |
|
1442 |
case Symbols.TokenRBRACE: |
|
1443 |
case Symbols.TokenCASE: |
|
1444 |
case Symbols.TokenDEFAULT: |
|
1445 |
case Symbols.TokenPUBLIC: |
|
1446 |
case Symbols.TokenPROTECTED: |
|
1447 |
case Symbols.TokenPRIVATE: |
|
1448 |
case Symbols.TokenCLASS: |
|
1449 |
case Symbols.TokenSTRUCT: |
|
1450 |
case Symbols.TokenUNION: |
|
1451 |
return false; |
|
1452 |
||
1453 |
default: |
|
1454 |
return true; |
|
1455 |
} |
|
1456 |
} |
|
1457 |
} |
|
1458 |
||
1459 |
/** |
|
1460 |
* Returns as a reference any previous <code>switch</code> labels (<code>case</code> |
|
1461 |
* or <code>default</code>) or the offset of the brace that scopes the switch |
|
1462 |
* statement. Sets <code>fIndent</code> to <code>prefCaseIndent</code> upon |
|
1463 |
* a match. |
|
1464 |
* |
|
1465 |
* @return the reference offset for a <code>switch</code> label |
|
1466 |
*/ |
|
1467 |
private int matchCaseAlignment() { |
|
1468 |
while (true) { |
|
1469 |
nextToken(); |
|
1470 |
switch (fToken) { |
|
1471 |
// invalid cases: another case label or an LBRACE must come before a case |
|
1472 |
// -> bail out with the current position |
|
1473 |
case Symbols.TokenLPAREN: |
|
1474 |
case Symbols.TokenLBRACKET: |
|
1475 |
case Symbols.TokenEOF: |
|
1476 |
return fPosition; |
|
1477 |
||
1478 |
case Symbols.TokenSWITCH: |
|
1479 |
// start of switch statement |
|
1480 |
fIndent= fPrefs.prefCaseIndent; |
|
1481 |
return fPosition; |
|
1482 |
||
1483 |
case Symbols.TokenCASE: |
|
1484 |
case Symbols.TokenDEFAULT: |
|
1485 |
// align with previous label |
|
1486 |
fIndent= 0; |
|
1487 |
return fPosition; |
|
1488 |
||
1489 |
// scopes: skip them |
|
1490 |
case Symbols.TokenRPAREN: |
|
1491 |
case Symbols.TokenRBRACKET: |
|
1492 |
case Symbols.TokenRBRACE: |
|
1493 |
skipScope(); |
|
1494 |
break; |
|
1495 |
||
1496 |
default: |
|
1497 |
// keep searching |
|
1498 |
continue; |
|
1499 |
} |
|
1500 |
} |
|
1501 |
} |
|
1502 |
||
1503 |
/** |
|
1504 |
* Returns as a reference any previous access specifiers (<code>public</code>, |
|
1505 |
* <code>protected</code> or <code>default</code>) or the offset of the brace that |
|
1506 |
* scopes the class body. |
|
1507 |
* Sets <code>fIndent</code> to <code>prefAccessSpecifierIndent</code> upon |
|
1508 |
* a match. |
|
1509 |
* |
|
1510 |
* @return the reference offset for an access specifier (public/protected/private) |
|
1511 |
*/ |
|
1512 |
private int matchAccessSpecifierAlignment() { |
|
1513 |
while (true) { |
|
1514 |
nextToken(); |
|
1515 |
switch (fToken) { |
|
1516 |
// invalid cases: another access specifier or an LBRACE must come before an access specifier |
|
1517 |
// -> bail out with the current position |
|
1518 |
case Symbols.TokenLPAREN: |
|
1519 |
case Symbols.TokenLBRACKET: |
|
1520 |
case Symbols.TokenEOF: |
|
1521 |
return fPosition; |
|
1522 |
||
1523 |
case Symbols.TokenLBRACE: |
|
1524 |
// opening brace of class body |
|
1525 |
int pos= fPosition; |
|
1526 |
int typeDeclPos= matchTypeDeclaration(); |
|
1527 |
fIndent= fPrefs.prefAccessSpecifierIndent; |
|
1528 |
fExtraSpaces = fPrefs.prefAccessSpecifierExtraSpaces; |
|
1529 |
if (typeDeclPos != CHeuristicScanner.NOT_FOUND) { |
|
1530 |
return typeDeclPos; |
|
1531 |
} |
|
1532 |
return pos; |
|
1533 |
case Symbols.TokenPUBLIC: |
|
1534 |
case Symbols.TokenPROTECTED: |
|
1535 |
case Symbols.TokenPRIVATE: |
|
1536 |
// align with previous access specifier |
|
1537 |
fIndent= 0; |
|
1538 |
return fPosition; |
|
1539 |
||
1540 |
// scopes: skip them |
|
1541 |
case Symbols.TokenRPAREN: |
|
1542 |
case Symbols.TokenRBRACKET: |
|
1543 |
case Symbols.TokenRBRACE: |
|
1544 |
skipScope(); |
|
1545 |
break; |
|
1546 |
||
1547 |
default: |
|
1548 |
// keep searching |
|
1549 |
continue; |
|
1550 |
} |
|
1551 |
} |
|
1552 |
} |
|
1553 |
||
1554 |
/** |
|
1555 |
* Returns the reference position for a list element. The algorithm |
|
1556 |
* tries to match any previous indentation on the same list. If there is none, |
|
1557 |
* the reference position returned is determined depending on the type of list: |
|
1558 |
* The indentation will either match the list scope introducer (e.g. for |
|
1559 |
* method declarations), so called deep indents, or simply increase the |
|
1560 |
* indentation by a number of standard indents. See also {@link #handleScopeIntroduction(int, boolean)}. |
|
1561 |
* @return the reference position for a list item: either a previous list item |
|
1562 |
* that has its own indentation, or the list introduction start. |
|
1563 |
*/ |
|
1564 |
private int skipToPreviousListItemOrListStart() { |
|
1565 |
int startLine= fLine; |
|
1566 |
int startPosition= fPosition; |
|
1567 |
boolean seenEqual = fToken == Symbols.TokenEQUAL; |
|
1568 |
boolean seenShiftLeft = fToken == Symbols.TokenSHIFTLEFT; |
|
1569 |
boolean seenRightParen = fToken == Symbols.TokenRPAREN; |
|
1570 |
while (true) { |
|
1571 |
nextToken(); |
|
1572 |
||
1573 |
// If any line item comes with its own indentation, adapt to it |
|
1574 |
if (fLine < startLine) { |
|
1575 |
try { |
|
1576 |
int lineOffset= fDocument.getLineOffset(startLine); |
|
1577 |
int bound= Math.min(fDocument.getLength(), startPosition + 1); |
|
1578 |
if ((fToken == Symbols.TokenSEMICOLON || fToken == Symbols.TokenRBRACE || |
|
1579 |
fToken == Symbols.TokenLBRACE && !looksLikeArrayInitializerIntro()) && |
|
1580 |
(seenEqual || seenShiftLeft || seenRightParen)) { |
|
1581 |
fIndent = fPrefs.prefContinuationIndent; |
|
1582 |
} else { |
|
1583 |
fAlign= fScanner.findNonWhitespaceForwardInAnyPartition(lineOffset, bound); |
|
1584 |
// If the reference line starts with a colon, skip the colon. |
|
1585 |
if (peekToken(fAlign) == Symbols.TokenCOLON) { |
|
1586 |
fAlign= fScanner.findNonWhitespaceForwardInAnyPartition(fAlign + 1, bound); |
|
1587 |
} |
|
1588 |
} |
|
1589 |
} catch (BadLocationException e) { |
|
1590 |
// Ignore and return just the position |
|
1591 |
} |
|
1592 |
return startPosition; |
|
1593 |
} |
|
1594 |
||
1595 |
switch (fToken) { |
|
1596 |
// scopes: skip them |
|
1597 |
case Symbols.TokenRPAREN: |
|
1598 |
seenRightParen = true; |
|
1599 |
//$FALL-THROUGH$ |
|
1600 |
case Symbols.TokenRBRACKET: |
|
1601 |
case Symbols.TokenRBRACE: |
|
1602 |
skipScope(); |
|
1603 |
break; |
|
1604 |
||
1605 |
// scope introduction: special treat who special is |
|
1606 |
case Symbols.TokenLPAREN: |
|
1607 |
case Symbols.TokenLBRACE: |
|
1608 |
case Symbols.TokenLBRACKET: |
|
1609 |
return handleScopeIntroduction(startPosition + 1, false); |
|
1610 |
||
1611 |
case Symbols.TokenSEMICOLON: |
|
1612 |
return fPosition; |
|
1613 |
||
1614 |
case Symbols.TokenQUESTIONMARK: |
|
1615 |
if (fPrefs.prefTernaryDeepAlign) { |
|
1616 |
setFirstElementAlignment(fPosition - 1, fPosition + 1); |
|
1617 |
} else { |
|
1618 |
fIndent= fPrefs.prefTernaryIndent; |
|
1619 |
} |
|
1620 |
return fPosition; |
|
1621 |
||
1622 |
case Symbols.TokenEQUAL: |
|
1623 |
seenEqual = true; |
|
1624 |
break; |
|
1625 |
||
1626 |
case Symbols.TokenSHIFTLEFT: |
|
1627 |
seenShiftLeft = true; |
|
1628 |
break; |
|
1629 |
||
1630 |
case Symbols.TokenEOF: |
|
1631 |
if (seenEqual || seenShiftLeft || seenRightParen) { |
|
1632 |
fIndent = fPrefs.prefContinuationIndent; |
|
1633 |
} |
|
1634 |
return 0; |
|
1635 |
} |
|
1636 |
} |
|
1637 |
} |
|
1638 |
||
1639 |
/** |
|
1640 |
* Skips a scope and positions the cursor (<code>fPosition</code>) on the |
|
1641 |
* token that opens the scope. Returns <code>true</code> if a matching peer |
|
1642 |
* could be found, <code>false</code> otherwise. The current token when calling |
|
1643 |
* must be one out of <code>Symbols.TokenRPAREN</code>, <code>Symbols.TokenRBRACE</code>, |
|
1644 |
* and <code>Symbols.TokenRBRACKET</code>. |
|
1645 |
* |
|
1646 |
* @return <code>true</code> if a matching peer was found, <code>false</code> otherwise |
|
1647 |
*/ |
|
1648 |
private boolean skipScope() { |
|
1649 |
switch (fToken) { |
|
1650 |
case Symbols.TokenRPAREN: |
|
1651 |
return skipScope(Symbols.TokenLPAREN, Symbols.TokenRPAREN); |
|
1652 |
case Symbols.TokenRBRACKET: |
|
1653 |
return skipScope(Symbols.TokenLBRACKET, Symbols.TokenRBRACKET); |
|
1654 |
case Symbols.TokenRBRACE: |
|
1655 |
return skipScope(Symbols.TokenLBRACE, Symbols.TokenRBRACE); |
|
1656 |
case Symbols.TokenGREATERTHAN: |
|
1657 |
if (!fPrefs.prefHasTemplates) |
|
1658 |
return false; |
|
1659 |
int storedPosition= fPosition; |
|
1660 |
int storedToken= fToken; |
|
1661 |
nextToken(); |
|
1662 |
switch (fToken) { |
|
1663 |
case Symbols.TokenIDENT: |
|
1664 |
if (skipScope(Symbols.TokenLESSTHAN, Symbols.TokenGREATERTHAN)) |
|
1665 |
return true; |
|
1666 |
break; |
|
1667 |
case Symbols.TokenQUESTIONMARK: |
|
1668 |
case Symbols.TokenGREATERTHAN: |
|
1669 |
if (skipScope(Symbols.TokenLESSTHAN, Symbols.TokenGREATERTHAN)) |
|
1670 |
return true; |
|
1671 |
break; |
|
1672 |
} |
|
1673 |
// <> are harder to detect - restore the position if we fail |
|
1674 |
fPosition= storedPosition; |
|
1675 |
fToken= storedToken; |
|
1676 |
return false; |
|
1677 |
||
1678 |
default: |
|
1679 |
// programming error |
|
1680 |
Assert.isTrue(false); |
|
1681 |
return false; |
|
1682 |
} |
|
1683 |
} |
|
1684 |
||
1685 |
/** |
|
1686 |
* Returns the contents of the current token. |
|
1687 |
* |
|
1688 |
* @return the contents of the current token |
|
1689 |
*/ |
|
1690 |
private CharSequence getTokenContent() { |
|
1691 |
return new DocumentCharacterIterator(fDocument, fPosition, fPreviousPos); |
|
1692 |
} |
|
1693 |
||
1694 |
/** |
|
1695 |
* Handles the introduction of a new scope. The current token must be one out |
|
1696 |
* of <code>Symbols.TokenLPAREN</code>, <code>Symbols.TokenLBRACE</code>, |
|
1697 |
* and <code>Symbols.TokenLBRACKET</code>. Returns as the reference position |
|
1698 |
* either the token introducing the scope or - if available - the first |
|
1699 |
* token after that. |
|
1700 |
* |
|
1701 |
* <p>Depending on the type of scope introduction, the indentation will align |
|
1702 |
* (deep indenting) with the reference position (<code>fAlign</code> will be |
|
1703 |
* set to the reference position) or <code>fIndent</code> will be set to |
|
1704 |
* the number of indentation units. |
|
1705 |
* </p> |
|
1706 |
* |
|
1707 |
* @param bound the bound for the search for the first token after the scope |
|
1708 |
* introduction. |
|
1709 |
* @param firstToken <code>true</code> if we are dealing with the first token after |
|
1710 |
* the opening parenthesis. |
|
1711 |
* @return the indent |
|
1712 |
*/ |
|
1713 |
private int handleScopeIntroduction(int bound, boolean firstToken) { |
|
1714 |
int pos= fPosition; // store |
|
1715 |
||
1716 |
switch (fToken) { |
|
1717 |
// scope introduction: special treat who special is |
|
1718 |
case Symbols.TokenLPAREN: |
|
1719 |
// special: method declaration deep indentation |
|
1720 |
if (looksLikeMethodDecl()) { |
|
1721 |
if (firstToken ? fPrefs.prefMethodDeclFirstParameterDeepIndent : fPrefs.prefMethodDeclDeepIndent) { |
|
1722 |
return setFirstElementAlignment(pos, bound); |
|
1723 |
} else { |
|
1724 |
fIndent= fPrefs.prefMethodDeclIndent; |
|
1725 |
return pos; |
|
1726 |
} |
|
1727 |
} else { |
|
1728 |
fPosition= pos; |
|
1729 |
if (looksLikeMethodCall()) { |
|
1730 |
if (firstToken ? fPrefs.prefMethodCallFirstParameterDeepIndent : fPrefs.prefMethodCallDeepIndent) { |
|
1731 |
return setFirstElementAlignment(pos, bound); |
|
1732 |
} else { |
|
1733 |
fIndent= fPrefs.prefMethodCallIndent; |
|
1734 |
return pos; |
|
1735 |
} |
|
1736 |
} else if (fPrefs.prefParenthesisDeepIndent) { |
|
1737 |
return setFirstElementAlignment(pos, bound); |
|
1738 |
} |
|
1739 |
} |
|
1740 |
||
1741 |
// normal: return the parenthesis as reference |
|
1742 |
fIndent= fPrefs.prefParenthesisIndent; |
|
1743 |
return pos; |
|
1744 |
||
1745 |
case Symbols.TokenLBRACE: |
|
1746 |
final boolean looksLikeArrayInitializerIntro= looksLikeArrayInitializerIntro(); |
|
1747 |
// special: array initializer |
|
1748 |
if (looksLikeArrayInitializerIntro) { |
|
1749 |
if (fPrefs.prefArrayDeepIndent) |
|
1750 |
return setFirstElementAlignment(pos, bound); |
|
1751 |
else |
|
1752 |
fIndent= fPrefs.prefArrayIndent; |
|
1753 |
} else if (isNamespace()) { |
|
1754 |
fIndent= fPrefs.prefNamespaceBodyIndent; |
|
1755 |
} else { |
|
1756 |
int typeDeclPos = matchTypeDeclaration(); |
|
1757 |
if (typeDeclPos == CHeuristicScanner.NOT_FOUND) { |
|
1758 |
fIndent= fPrefs.prefBlockIndent; |
|
1759 |
} else { |
|
1760 |
fIndent= fPrefs.prefAccessSpecifierIndent + fPrefs.prefTypeIndent; |
|
1761 |
} |
|
1762 |
} |
|
1763 |
||
1764 |
// normal: skip to the statement start before the scope introducer |
|
1765 |
// opening braces are often on differently ending indents than e.g. a method definition |
|
1766 |
if (!looksLikeArrayInitializerIntro) { |
|
1767 |
fPosition= pos; // restore |
|
1768 |
return skipToStatementStart(true, true); // set to true to match the first if |
|
1769 |
} else { |
|
1770 |
return pos; |
|
1771 |
} |
|
1772 |
||
1773 |
case Symbols.TokenLBRACKET: |
|
1774 |
// special: method declaration deep indentation |
|
1775 |
if (fPrefs.prefArrayDimensionsDeepIndent) { |
|
1776 |
return setFirstElementAlignment(pos, bound); |
|
1777 |
} |
|
1778 |
||
1779 |
// normal: return the bracket as reference |
|
1780 |
fIndent= fPrefs.prefBracketIndent; |
|
1781 |
return pos; // restore |
|
1782 |
||
1783 |
default: |
|
1784 |
// programming error |
|
1785 |
Assert.isTrue(false); |
|
1786 |
return -1; // dummy |
|
1787 |
} |
|
1788 |
} |
|
1789 |
||
1790 |
/** |
|
1791 |
* Sets the deep indent offset (<code>fAlign</code>) to either the offset |
|
1792 |
* right after <code>scopeIntroducerOffset</code> or - if available - the |
|
1793 |
* first C token after <code>scopeIntroducerOffset</code>, but before |
|
1794 |
* <code>bound</code>. |
|
1795 |
* |
|
1796 |
* @param scopeIntroducerOffset the offset of the scope introducer |
|
1797 |
* @param bound the bound for the search for another element |
|
1798 |
* @return the reference position |
|
1799 |
*/ |
|
1800 |
private int setFirstElementAlignment(int scopeIntroducerOffset, int bound) { |
|
1801 |
int firstPossible= scopeIntroducerOffset + 1; // align with the first position after the scope intro |
|
1802 |
fAlign= fScanner.findNonWhitespaceForwardInAnyPartition(firstPossible, bound); |
|
1803 |
if (fAlign == CHeuristicScanner.NOT_FOUND) { |
|
1804 |
fAlign= firstPossible; |
|
1805 |
} else { |
|
1806 |
try { |
|
1807 |
IRegion lineRegion = fDocument.getLineInformationOfOffset(scopeIntroducerOffset); |
|
1808 |
if (fAlign > lineRegion.getOffset() + lineRegion.getLength()) { |
|
1809 |
fAlign= firstPossible; |
|
1810 |
} |
|
1811 |
} catch (BadLocationException e) { |
|
1812 |
// Ignore. |
|
1813 |
} |
|
1814 |
} |
|
1815 |
return fAlign; |
|
1816 |
} |
|
1817 |
||
1818 |
/** |
|
1819 |
* Returns <code>true</code> if the next token received after calling |
|
1820 |
* <code>nextToken</code> is either an equal sign, an opening brace, |
|
1821 |
* a comma or an array designator ('[]'). |
|
1822 |
* |
|
1823 |
* @return <code>true</code> if the next elements look like the start of an array definition |
|
1824 |
*/ |
|
1825 |
private boolean looksLikeArrayInitializerIntro() { |
|
1826 |
int pos= fPosition; |
|
1827 |
nextToken(); |
|
1828 |
switch (fToken) { |
|
1829 |
case Symbols.TokenEQUAL: |
|
1830 |
return true; |
|
1831 |
case Symbols.TokenRBRACKET: |
|
1832 |
return skipBrackets(); |
|
1833 |
case Symbols.TokenLBRACE: |
|
1834 |
if (looksLikeArrayInitializerIntro()) { |
|
1835 |
fPosition= pos; |
|
1836 |
return true; |
|
1837 |
} |
|
1838 |
return false; |
|
1839 |
case Symbols.TokenCOMMA: |
|
1840 |
fPosition= pos; |
|
1841 |
return true; |
|
1842 |
} |
|
1843 |
return false; |
|
1844 |
} |
|
1845 |
||
1846 |
/** |
|
1847 |
* Returns <code>true</code> if the the current token is "namespace", or the current token |
|
1848 |
* is an identifier and the previous token is "namespace". |
|
1849 |
* |
|
1850 |
* @return <code>true</code> if the next elements look like the start of a namespace declaration. |
|
1851 |
*/ |
|
1852 |
private boolean isNamespace() { |
|
1853 |
int pos = fPosition; |
|
1854 |
if (fToken == Symbols.TokenNAMESPACE) { |
|
1855 |
return true; // Anonymous namespace |
|
1856 |
} else if (fToken == Symbols.TokenIDENT) { |
|
1857 |
nextToken(); // Get previous token |
|
1858 |
if (fToken == Symbols.TokenNAMESPACE) { |
|
1859 |
return true; // Named namespace |
|
1860 |
} |
|
1861 |
} |
|
1862 |
fPosition = pos; |
|
1863 |
return false; |
|
1864 |
} |
|
1865 |
||
1866 |
/** |
|
1867 |
* Skips over the next <code>if</code> keyword. The current token when calling |
|
1868 |
* this method must be an <code>else</code> keyword. Returns <code>true</code> |
|
1869 |
* if a matching <code>if</code> could be found, <code>false</code> otherwise. |
|
1870 |
* The cursor (<code>fPosition</code>) is set to the offset of the <code>if</code> |
|
1871 |
* token. |
|
1872 |
* |
|
1873 |
* @return <code>true</code> if a matching <code>if</code> token was found, <code>false</code> otherwise |
|
1874 |
*/ |
|
1875 |
private boolean skipNextIF() { |
|
1876 |
Assert.isTrue(fToken == Symbols.TokenELSE); |
|
1877 |
||
1878 |
while (true) { |
|
1879 |
nextToken(); |
|
1880 |
switch (fToken) { |
|
1881 |
// scopes: skip them |
|
1882 |
case Symbols.TokenRPAREN: |
|
1883 |
case Symbols.TokenRBRACKET: |
|
1884 |
case Symbols.TokenRBRACE: |
|
1885 |
skipScope(); |
|
1886 |
break; |
|
1887 |
||
1888 |
case Symbols.TokenIF: |
|
1889 |
// found it, return |
|
1890 |
return true; |
|
1891 |
case Symbols.TokenELSE: |
|
1892 |
// recursively skip else-if blocks |
|
1893 |
skipNextIF(); |
|
1894 |
break; |
|
1895 |
||
1896 |
// shortcut scope starts |
|
1897 |
case Symbols.TokenLPAREN: |
|
1898 |
case Symbols.TokenLBRACE: |
|
1899 |
case Symbols.TokenLBRACKET: |
|
1900 |
case Symbols.TokenEOF: |
|
1901 |
return false; |
|
1902 |
} |
|
1903 |
} |
|
1904 |
} |
|
1905 |
||
1906 |
/** |
|
1907 |
* while(condition); is ambiguous when parsed backwardly, as it is a valid |
|
1908 |
* statement by its own, so we have to check whether there is a matching |
|
1909 |
* do. A <code>do</code> can either be separated from the while by a |
|
1910 |
* block, or by a single statement, which limits our search distance. |
|
1911 |
* |
|
1912 |
* @return <code>true</code> if the <code>while</code> currently in |
|
1913 |
* <code>fToken</code> has a matching <code>do</code>. |
|
1914 |
*/ |
|
1915 |
private boolean hasMatchingDo() { |
|
1916 |
Assert.isTrue(fToken == Symbols.TokenWHILE); |
|
1917 |
nextToken(); |
|
1918 |
switch (fToken) { |
|
1919 |
case Symbols.TokenRBRACE: |
|
1920 |
skipScope(); // and fall thru |
|
1921 |
skipToStatementStart(false, false); |
|
1922 |
return fToken == Symbols.TokenDO; |
|
1923 |
||
1924 |
case Symbols.TokenSEMICOLON: |
|
1925 |
skipToStatementStart(false, false); |
|
1926 |
return fToken == Symbols.TokenDO; |
|
1927 |
} |
|
1928 |
return false; |
|
1929 |
} |
|
1930 |
||
1931 |
/** |
|
1932 |
* Skips pointer operators if the current token is a pointer operator. |
|
1933 |
* |
|
1934 |
* @return <code>true</code> if a <code>*</code> or <code>&</code> could be scanned, the |
|
1935 |
* current token is left at the operator. |
|
1936 |
*/ |
|
1937 |
private boolean skipPointerOperators() { |
|
1938 |
if (fToken == Symbols.TokenOTHER) { |
|
1939 |
CharSequence token= getTokenContent(); |
|
1940 |
if (token.length() == 1 && token.charAt(0) == '*' || token.charAt(0) == '&') { |
|
1941 |
return true; |
|
1942 |
} |
|
1943 |
} else if (fToken == Symbols.TokenCONST) { |
|
1944 |
return true; |
|
1945 |
} |
|
1946 |
return false; |
|
1947 |
} |
|
1948 |
||
1949 |
/** |
|
1950 |
* Skips brackets if the current token is a RBRACKET. There can be nothing |
|
1951 |
* but whitespace in between, this is only to be used for <code>[]</code> elements. |
|
1952 |
* |
|
1953 |
* @return <code>true</code> if a <code>[]</code> could be scanned, the |
|
1954 |
* current token is left at the LBRACKET. |
|
1955 |
*/ |
|
1956 |
private boolean skipBrackets() { |
|
1957 |
if (fToken == Symbols.TokenRBRACKET) { |
|
1958 |
nextToken(); |
|
1959 |
if (fToken == Symbols.TokenLBRACKET) { |
|
1960 |
return true; |
|
1961 |
} |
|
1962 |
} |
|
1963 |
return false; |
|
1964 |
} |
|
1965 |
||
1966 |
/** |
|
1967 |
* Skips scope qualifiers of identifiers. |
|
1968 |
* |
|
1969 |
* @return <code>true</code> if a qualifier was encountered, the last token |
|
1970 |
* will be an IDENT. |
|
1971 |
*/ |
|
1972 |
private boolean skipQualifiers() { |
|
1973 |
if (fToken == Symbols.TokenDOUBLECOLON) { |
|
1974 |
nextToken(); |
|
1975 |
if (fToken == Symbols.TokenIDENT) { |
|
1976 |
return true; |
|
1977 |
} |
|
1978 |
} |
|
1979 |
return false; |
|
1980 |
} |
|
1981 |
||
1982 |
/** |
|
1983 |
* Reads the next token in backward direction from the heuristic scanner |
|
1984 |
* and sets the fields <code>fToken, fPreviousPosition</code> and <code>fPosition</code> |
|
1985 |
* accordingly. |
|
1986 |
*/ |
|
1987 |
private void nextToken() { |
|
1988 |
nextToken(fPosition); |
|
1989 |
} |
|
1990 |
||
1991 |
/** |
|
1992 |
* Reads the next token in backward direction of <code>start</code> from |
|
1993 |
* the heuristic scanner and sets the fields <code>fToken, fPreviousPosition</code> |
|
1994 |
* and <code>fPosition</code> accordingly. |
|
1995 |
* |
|
1996 |
* @param start the start offset from which to scan backwards |
|
1997 |
*/ |
|
1998 |
private void nextToken(int start) { |
|
1999 |
fToken= fScanner.previousToken(start - 1, CHeuristicScanner.UNBOUND); |
|
2000 |
fPreviousPos= start; |
|
2001 |
fPosition= fScanner.getPosition() + 1; |
|
2002 |
try { |
|
2003 |
fLine= fDocument.getLineOfOffset(fPosition); |
|
2004 |
} catch (BadLocationException e) { |
|
2005 |
fLine= -1; |
|
2006 |
} |
|
2007 |
} |
|
2008 |
||
2009 |
/** |
|
2010 |
* Reads the next token in backward direction from the heuristic scanner |
|
2011 |
* and returns that token without changing the current position. |
|
2012 |
*/ |
|
2013 |
private int peekToken() { |
|
2014 |
return fScanner.previousToken(fPosition - 1, CHeuristicScanner.UNBOUND); |
|
2015 |
} |
|
2016 |
||
2017 |
/** |
|
2018 |
* Returns <code>true</code> if the current tokens look like a method |
|
2019 |
* declaration header (i.e. only the return type and method name). The |
|
2020 |
* heuristic calls <code>nextToken</code> and expects an identifier |
|
2021 |
* (method name) and an optional return type declaration. |
|
2022 |
* |
|
2023 |
* @return <code>true</code> if the current position looks like a method |
|
2024 |
* declaration header. |
|
2025 |
*/ |
|
2026 |
private boolean looksLikeMethodDecl() { |
|
2027 |
nextToken(); |
|
2028 |
switch (fToken) { |
|
2029 |
case Symbols.TokenIDENT: // method name |
|
2030 |
int pos= fPosition; |
|
2031 |
nextToken(); |
|
2032 |
// check destructor tilde |
|
2033 |
if (fToken == Symbols.TokenTILDE) { |
|
2034 |
return true; |
|
2035 |
} |
|
2036 |
if (skipQualifiers()) { |
|
2037 |
return true; |
|
2038 |
} |
|
2039 |
// optional brackets for array valued return types |
|
2040 |
while (skipBrackets()) { |
|
2041 |
nextToken(); |
|
2042 |
} |
|
2043 |
while (skipPointerOperators()) { |
|
2044 |
nextToken(); |
|
2045 |
} |
|
2046 |
switch (fToken) { |
|
2047 |
case Symbols.TokenIDENT: |
|
2048 |
return true; |
|
2049 |
case Symbols.TokenSEMICOLON: |
|
2050 |
case Symbols.TokenRBRACE: |
|
2051 |
fPosition= pos; |
|
2052 |
return true; |
|
2053 |
case Symbols.TokenLBRACE: |
|
2054 |
if (fScanner.looksLikeCompositeTypeDefinitionBackward(fPosition, CHeuristicScanner.UNBOUND)) { |
|
2055 |
fPosition= pos; |
|
2056 |
return true; |
|
2057 |
} |
|
2058 |
break; |
|
2059 |
case Symbols.TokenCOLON: |
|
2060 |
nextToken(); |
|
2061 |
switch (fToken) { |
|
2062 |
case Symbols.TokenPUBLIC: |
|
2063 |
case Symbols.TokenPROTECTED: |
|
2064 |
case Symbols.TokenPRIVATE: |
|
2065 |
fPosition= pos; |
|
2066 |
return true; |
|
2067 |
case Symbols.TokenRPAREN: |
|
2068 |
// constructor initializer |
|
2069 |
if (skipScope()) { |
|
2070 |
nextToken(); |
|
2071 |
// optional throw |
|
2072 |
if (fToken == Symbols.TokenTHROW) { |
|
2073 |
nextToken(); |
|
2074 |
if (fToken != Symbols.TokenRPAREN || !skipScope()) { |
|
2075 |
return false; |
|
2076 |
} |
|
2077 |
} |
|
2078 |
return looksLikeMethodDecl(); |
|
2079 |
} |
|
2080 |
break; |
|
2081 |
} |
|
2082 |
} |
|
2083 |
break; |
|
2084 |
case Symbols.TokenARROW: |
|
2085 |
case Symbols.TokenCOMMA: |
|
2086 |
case Symbols.TokenEQUAL: |
|
2087 |
case Symbols.TokenGREATERTHAN: |
|
2088 |
case Symbols.TokenLESSTHAN: |
|
2089 |
case Symbols.TokenMINUS: |
|
2090 |
case Symbols.TokenSHIFTRIGHT: |
|
2091 |
case Symbols.TokenSHIFTLEFT: |
|
2092 |
case Symbols.TokenDELETE: |
|
2093 |
case Symbols.TokenNEW: |
|
2094 |
nextToken(); |
|
2095 |
return fToken == Symbols.TokenOPERATOR; |
|
2096 |
case Symbols.TokenOTHER: |
|
2097 |
if (getTokenContent().length() == 1) { |
|
2098 |
nextToken(); |
|
2099 |
if (fToken == Symbols.TokenOPERATOR) |
|
2100 |
return true; |
|
2101 |
} |
|
2102 |
if (getTokenContent().length() == 1) { |
|
2103 |
nextToken(); |
|
2104 |
if (fToken == Symbols.TokenOPERATOR) |
|
2105 |
return true; |
|
2106 |
} |
|
2107 |
} |
|
2108 |
return false; |
|
2109 |
} |
|
2110 |
||
2111 |
/** |
|
2112 |
* Returns <code>true</code> if the current tokens look like an anonymous type declaration |
|
2113 |
* header (i.e. a type name (potentially qualified) and a new keyword). The heuristic calls |
|
2114 |
* <code>nextToken</code> and expects a possibly qualified identifier (type name) and a new |
|
2115 |
* keyword |
|
2116 |
* |
|
2117 |
* @return <code>true</code> if the current position looks like a anonymous type declaration |
|
2118 |
* header. |
|
2119 |
*/ |
|
2120 |
private boolean looksLikeAnonymousTypeDecl() { |
|
2121 |
nextToken(); |
|
2122 |
if (fToken == Symbols.TokenIDENT) { // type name |
|
2123 |
nextToken(); |
|
2124 |
while (fToken == Symbols.TokenOTHER) { // dot of qualification |
|
2125 |
nextToken(); |
|
2126 |
if (fToken != Symbols.TokenIDENT) // qualificating name |
|
2127 |
return false; |
|
2128 |
nextToken(); |
|
2129 |
} |
|
2130 |
return fToken == Symbols.TokenNEW; |
|
2131 |
} |
|
2132 |
return false; |
|
2133 |
} |
|
2134 |
||
2135 |
/** |
|
2136 |
* Returns <code>true</code> if the current tokens look like a method |
|
2137 |
* call header (i.e. an identifier as opposed to a keyword taking parenthesized |
|
2138 |
* parameters such as <code>if</code>). |
|
2139 |
* <p>The heuristic calls <code>nextToken</code> and expects an identifier |
|
2140 |
* (method name). |
|
2141 |
* |
|
2142 |
* @return <code>true</code> if the current position looks like a method call |
|
2143 |
* header. |
|
2144 |
*/ |
|
2145 |
private boolean looksLikeMethodCall() { |
|
2146 |
// TODO add awareness for constructor calls with templates: new complex<float>() |
|
2147 |
nextToken(); |
|
2148 |
return fToken == Symbols.TokenIDENT; // method name |
|
2149 |
} |
|
2150 |
||
2151 |
/** |
|
2152 |
* Scans tokens for the matching opening peer. The internal cursor |
|
2153 |
* (<code>fPosition</code>) is set to the offset of the opening peer if found. |
|
2154 |
* |
|
2155 |
* @param openToken the opening peer token |
|
2156 |
* @param closeToken the closing peer token |
|
2157 |
* @return <code>true</code> if a matching token was found, <code>false</code> |
|
2158 |
* otherwise |
|
2159 |
*/ |
|
2160 |
private boolean skipScope(int openToken, int closeToken) { |
|
2161 |
int depth= 1; |
|
2162 |
||
2163 |
while (true) { |
|
2164 |
nextToken(); |
|
2165 |
||
2166 |
if (fToken == closeToken) { |
|
2167 |
depth++; |
|
2168 |
} else if (fToken == openToken) { |
|
2169 |
depth--; |
|
2170 |
if (depth == 0) |
|
2171 |
return true; |
|
2172 |
} else if (fToken == Symbols.TokenEOF) { |
|
2173 |
return false; |
|
2174 |
} |
|
2175 |
} |
|
2176 |
} |
|
2177 |
} |