-
Notifications
You must be signed in to change notification settings - Fork 82
/
IndentUtil.java
484 lines (421 loc) · 18.1 KB
/
IndentUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*******************************************************************************
* Copyright (c) 2005, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.javaeditor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.source.ILineRange;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
import org.eclipse.jdt.ui.text.IJavaPartitions;
import org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner;
import org.eclipse.jdt.internal.ui.text.JavaIndenter;
/**
* Utility that indents a number of lines in a document.
*
* @since 3.1
*/
public final class IndentUtil {
private static final String SLASHES= "//"; //$NON-NLS-1$
/**
* The result of an indentation operation. The result may be passed to
* subsequent calls to
* {@link IndentUtil#indentLines(IDocument, ILineRange, IJavaProject, IndentResult) indentLines}
* to obtain consistent results with respect to the indentation of
* line-comments.
*/
public static final class IndentResult {
private IndentResult(boolean[] commentLines) {
commentLinesAtColumnZero= commentLines;
}
private boolean[] commentLinesAtColumnZero;
private boolean hasChanged;
private int leftmostLine= -1;
/**
* Returns <code>true</code> if the indentation operation changed the
* document, <code>false</code> if not.
* @return <code>true</code> if the document was changed
*/
public boolean hasChanged() {
return hasChanged;
}
}
private IndentUtil() {
// do not instantiate
}
/**
* Indents the line range specified by <code>lines</code> in
* <code>document</code>. The passed Java project may be
* <code>null</code>, it is used solely to obtain formatter preferences.
*
* @param document the document to be changed
* @param lines the line range to be indented
* @param project the Java project to get the formatter preferences from, or
* <code>null</code> if global preferences should be used
* @param result the result from a previous call to <code>indentLines</code>,
* in order to maintain comment line properties, or <code>null</code>.
* Note that the passed result may be changed by the call.
* @return an indent result that may be queried for changes and can be
* reused in subsequent indentation operations
* @throws BadLocationException if <code>lines</code> is not a valid line
* range on <code>document</code>
*/
public static IndentResult indentLines(IDocument document, ILineRange lines, IJavaProject project, IndentResult result) throws BadLocationException {
int numberOfLines= lines.getNumberOfLines();
if (numberOfLines < 1)
return new IndentResult(null);
result= reuseOrCreateToken(result, numberOfLines);
JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
JavaIndenter indenter= new JavaIndenter(document, scanner, project);
boolean changed= false;
for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++) {
changed |= indentLine(document, line, indenter, scanner, result.commentLinesAtColumnZero, i++);
}
result.hasChanged= changed;
return result;
}
/**
* Shifts the line range specified by <code>lines</code> in
* <code>document</code>. The amount that the lines get shifted
* are determined by the first line in the range, all subsequent
* lines are adjusted accordingly. The passed Java project may be
* <code>null</code>, it is used solely to obtain formatter
* preferences.
*
* @param document the document to be changed
* @param lines the line range to be shifted
* @param project the Java project to get the formatter preferences
* from, or <code>null</code> if global preferences should
* be used
* @param result the result from a previous call to
* <code>shiftLines</code>, in order to maintain comment
* line properties, or <code>null</code>. Note that the
* passed result may be changed by the call.
* @return an indent result that may be queried for changes and can
* be reused in subsequent indentation operations
* @throws BadLocationException if <code>lines</code> is not a
* valid line range on <code>document</code>
*/
public static IndentResult shiftLines(IDocument document, ILineRange lines, IJavaProject project, IndentResult result) throws BadLocationException {
int numberOfLines= lines.getNumberOfLines();
if (numberOfLines < 1)
return new IndentResult(null);
result= reuseOrCreateToken(result, numberOfLines);
result.hasChanged= false;
JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
JavaIndenter indenter= new JavaIndenter(document, scanner, project);
String current= getCurrentIndent(document, lines.getStartLine());
StringBuffer correct= indenter.computeIndentation(document.getLineOffset(lines.getStartLine()));
if (correct == null)
return result; // bail out
int tabSize= CodeFormatterUtil.getTabWidth(project);
StringBuilder addition= new StringBuilder();
int difference= subtractIndent(correct, current, addition, tabSize);
if (difference == 0)
return result;
if (result.leftmostLine == -1)
result.leftmostLine= getLeftMostLine(document, lines, tabSize);
int maxReduction= computeVisualLength(getCurrentIndent(document, result.leftmostLine + lines.getStartLine()), tabSize);
if (difference > 0) {
for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++)
addIndent(document, line, addition, result.commentLinesAtColumnZero, i++);
} else {
int reduction= Math.min(-difference, maxReduction);
for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++)
cutIndent(document, line, reduction, tabSize, result.commentLinesAtColumnZero, i++);
}
result.hasChanged= true;
return result;
}
/**
* Indents line <code>line</code> in <code>document</code> with <code>indent</code>.
* Leaves leading comment signs alone.
*
* @param document the document
* @param line the line
* @param indent the indentation to insert
* @throws BadLocationException on concurrent document modification
*/
private static void addIndent(IDocument document, int line, CharSequence indent, boolean[] commentlines, int relative) throws BadLocationException {
IRegion region= document.getLineInformation(line);
int insert= region.getOffset();
int endOffset= region.getOffset() + region.getLength();
// go behind line comments
if (!commentlines[relative]) {
while (insert < endOffset - 2 && SLASHES.equals(document.get(insert, 2)))
insert += 2;
}
// insert indent
document.replace(insert, 0, indent.toString());
}
/**
* Cuts the visual equivalent of <code>toDelete</code> characters out of the
* indentation of line <code>line</code> in <code>document</code>. Leaves
* leading comment signs alone.
*
* @param document the document
* @param line the line
* @param toDelete the number of space equivalents to delete.
* @throws BadLocationException on concurrent document modification
*/
private static void cutIndent(IDocument document, int line, int toDelete, int tabSize, boolean[] commentLines, int relative) throws BadLocationException {
IRegion region= document.getLineInformation(line);
int from= region.getOffset();
int endOffset= region.getOffset() + region.getLength();
// go behind line comments
while (from < endOffset - 2 && SLASHES.equals(document.get(from, 2)))
from += 2;
int to= from;
while (toDelete > 0 && to < endOffset) {
char ch= document.getChar(to);
if (!Character.isWhitespace(ch))
break;
toDelete -= computeVisualLength(ch, tabSize);
if (toDelete >= 0)
to++;
else
break;
}
if (endOffset > to + 1 && SLASHES.equals(document.get(to, 2)))
commentLines[relative]= true;
document.replace(from, to - from, null);
}
/**
* Computes the difference of two indentations and returns the difference in
* length of current and correct. If the return value is positive, <code>addition</code>
* is initialized with a substring of that length of <code>correct</code>.
*
* @param correct the correct indentation
* @param current the current indentation (migth contain non-whitespace)
* @param difference a string buffer - if the return value is positive, it will be cleared and set to the substring of <code>current</code> of that length
* @return the difference in lenght of <code>correct</code> and <code>current</code>
*/
private static int subtractIndent(CharSequence correct, CharSequence current, StringBuilder difference, int tabSize) {
int c1= computeVisualLength(correct, tabSize);
int c2= computeVisualLength(current, tabSize);
int diff= c1 - c2;
if (diff <= 0)
return diff;
difference.setLength(0);
int len= 0, i= 0;
while (len < diff) {
char c= correct.charAt(i++);
difference.append(c);
len += computeVisualLength(c, tabSize);
}
return diff;
}
private static int computeVisualLength(char ch, int tabSize) {
if (ch == '\t')
return tabSize;
else
return 1;
}
/**
* Returns the visual length of a given <code>CharSequence</code> taking into
* account the visual tabulator length.
*
* @param seq the string to measure
* @return the visual length of <code>seq</code>
*/
private static int computeVisualLength(CharSequence seq, int tablen) {
int size= 0;
for (int i= 0; i < seq.length(); i++) {
char ch= seq.charAt(i);
if (ch == '\t') {
if (tablen != 0)
size += tablen - size % tablen;
// else: size stays the same
} else {
size++;
}
}
return size;
}
/**
* Returns the indentation of the line <code>line</code> in <code>document</code>.
* The returned string may contain pairs of leading slashes that are considered
* part of the indentation. The space before the asterix in a javadoc-like
* comment is not considered part of the indentation.
*
* @param document the document
* @param line the line
* @return the indentation of <code>line</code> in <code>document</code>
* @throws BadLocationException if the document is changed concurrently
*/
private static String getCurrentIndent(IDocument document, int line) throws BadLocationException {
IRegion region= document.getLineInformation(line);
int from= region.getOffset();
int endOffset= region.getOffset() + region.getLength();
// go behind line comments
int to= from;
while (to < endOffset - 2 && SLASHES.equals(document.get(to, 2)))
to += 2;
while (to < endOffset) {
char ch= document.getChar(to);
if (!Character.isWhitespace(ch))
break;
to++;
}
// don't count the space before javadoc like, asterix-style comment lines
if (to > from && to < endOffset - 1 && " *".equals(document.get(to - 1, 2))) { //$NON-NLS-1$
String type= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, to, true);
if (IJavaPartitions.JAVA_DOC.equals(type) || IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(type))
to--;
}
return document.get(from, to - from);
}
private static int getLeftMostLine(IDocument document, ILineRange lines, int tabSize) throws BadLocationException {
int numberOfLines= lines.getNumberOfLines();
int first= lines.getStartLine();
int minLine= -1;
int minIndent= Integer.MAX_VALUE;
for (int line= 0; line < numberOfLines; line++) {
int length= computeVisualLength(getCurrentIndent(document, line + first), tabSize);
if (length < minIndent) {
minIndent= length;
minLine= line;
}
}
return minLine;
}
private static IndentResult reuseOrCreateToken(IndentResult token, int numberOfLines) {
if (token == null)
token= new IndentResult(new boolean[numberOfLines]);
else if (token.commentLinesAtColumnZero == null)
token.commentLinesAtColumnZero= new boolean[numberOfLines];
else if (token.commentLinesAtColumnZero.length != numberOfLines) {
boolean[] commentBooleans= new boolean[numberOfLines];
System.arraycopy(token.commentLinesAtColumnZero, 0, commentBooleans, 0, Math.min(numberOfLines, token.commentLinesAtColumnZero.length));
token.commentLinesAtColumnZero= commentBooleans;
}
return token;
}
/**
* Indents a single line using the java heuristic scanner. Javadoc and multi
* line comments are indented as specified by the
* <code>JavaDocAutoIndentStrategy</code>.
*
* @param document the document
* @param line the line to be indented
* @param indenter the java indenter
* @param scanner the heuristic scanner
* @param commentLines the indent token comment booleans
* @param lineIndex the zero-based line index
* @return <code>true</code> if the document was modified,
* <code>false</code> if not
* @throws BadLocationException if the document got changed concurrently
*/
private static boolean indentLine(IDocument document, int line, JavaIndenter indenter, JavaHeuristicScanner scanner, boolean[] commentLines, int lineIndex) throws BadLocationException {
IRegion currentLine= document.getLineInformation(line);
final int offset= currentLine.getOffset();
int wsStart= offset; // where we start searching for non-WS; after the "//" in single line comments
String indent= null;
if (offset < document.getLength()) {
ITypedRegion partition= TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, offset, true);
ITypedRegion startingPartition= TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, offset, false);
String type= partition.getType();
if (IJavaPartitions.JAVA_DOC.equals(type) || IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(type)) {
indent= computeJavadocIndent(document, line, scanner, startingPartition);
} else if (!commentLines[lineIndex] && startingPartition.getOffset() == offset && IJavaPartitions.JAVA_SINGLE_LINE_COMMENT.equals(startingPartition.getType())) {
return false;
}
}
// standard java indentation
if (indent == null) {
StringBuffer computed= indenter.computeIndentation(offset);
if (computed != null)
indent= computed.toString();
else
indent= ""; //$NON-NLS-1$
}
// change document:
// get current white space
int lineLength= currentLine.getLength();
int end= scanner.findNonWhitespaceForwardInAnyPartition(wsStart, offset + lineLength);
if (end == JavaHeuristicScanner.NOT_FOUND)
end= offset + lineLength;
int length= end - offset;
String currentIndent= document.get(offset, length);
// memorize the fact that a line is a single line comment (but not at column 0) and should be treated like code
// as opposed to commented out code, which should keep its slashes at column 0
if (length > 0) {
ITypedRegion partition= TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, end, false);
if (IJavaPartitions.JAVA_MULTI_LINE_STRING.equals(partition.getType())) {
// don't play with indentation of a Text Block
return false;
}
if (partition.getOffset() == end && IJavaPartitions.JAVA_SINGLE_LINE_COMMENT.equals(partition.getType())) {
commentLines[lineIndex]= true;
}
}
// only change the document if it is a real change
if (!indent.equals(currentIndent)) {
document.replace(offset, length, indent);
return true;
}
return false;
}
/**
* Computes and returns the indentation for a javadoc line. The line
* must be inside a javadoc comment.
*
* @param document the document
* @param line the line in document
* @param scanner the scanner
* @param partition the comment partition
* @return the indent, or <code>null</code> if not computable
*/
private static String computeJavadocIndent(IDocument document, int line, JavaHeuristicScanner scanner, ITypedRegion partition) throws BadLocationException {
if (line == 0) // impossible - the first line is never inside a javadoc comment
return null;
// don't make any assumptions if the line does not start with \s*\* - it might be
// commented out code, for which we don't want to change the indent
final IRegion lineInfo= document.getLineInformation(line);
final int lineStart= lineInfo.getOffset();
final int lineLength= lineInfo.getLength();
final int lineEnd= lineStart + lineLength;
int nonWS= scanner.findNonWhitespaceForwardInAnyPartition(lineStart, lineEnd);
if (nonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(nonWS) != '*') {
if (nonWS == JavaHeuristicScanner.NOT_FOUND)
return document.get(lineStart, lineLength);
return document.get(lineStart, nonWS - lineStart);
}
// take the indent from the previous line and reuse
IRegion previousLine= document.getLineInformation(line - 1);
int previousLineStart= previousLine.getOffset();
int previousLineLength= previousLine.getLength();
int previousLineEnd= previousLineStart + previousLineLength;
StringBuilder buf= new StringBuilder();
int previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd);
if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(previousLineNonWS) != '*') {
// align with the comment start if the previous line is not an asterix line
previousLine= document.getLineInformationOfOffset(partition.getOffset());
previousLineStart= previousLine.getOffset();
previousLineLength= previousLine.getLength();
previousLineEnd= previousLineStart + previousLineLength;
previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd);
if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND)
previousLineNonWS= previousLineEnd;
// add the initial space
// TODO this may be controlled by a formatter preference in the future
buf.append(' ');
}
String indentation= document.get(previousLineStart, previousLineNonWS - previousLineStart);
buf.insert(0, indentation);
return buf.toString();
}
}