Lines 10-15
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.internal.formatter; |
11 |
package org.eclipse.jdt.internal.formatter; |
12 |
|
12 |
|
|
|
13 |
import java.util.Arrays; |
14 |
import java.util.Comparator; |
13 |
import java.util.HashMap; |
15 |
import java.util.HashMap; |
14 |
import java.util.Map; |
16 |
import java.util.Map; |
15 |
|
17 |
|
Lines 32-38
Link Here
|
32 |
import org.eclipse.jdt.internal.formatter.comment.MultiCommentRegion; |
34 |
import org.eclipse.jdt.internal.formatter.comment.MultiCommentRegion; |
33 |
import org.eclipse.jface.text.Document; |
35 |
import org.eclipse.jface.text.Document; |
34 |
import org.eclipse.jface.text.IDocument; |
36 |
import org.eclipse.jface.text.IDocument; |
|
|
37 |
import org.eclipse.jface.text.IRegion; |
35 |
import org.eclipse.jface.text.Position; |
38 |
import org.eclipse.jface.text.Position; |
|
|
39 |
import org.eclipse.jface.text.Region; |
36 |
import org.eclipse.text.edits.MultiTextEdit; |
40 |
import org.eclipse.text.edits.MultiTextEdit; |
37 |
import org.eclipse.text.edits.TextEdit; |
41 |
import org.eclipse.text.edits.TextEdit; |
38 |
|
42 |
|
Lines 144-180
Link Here
|
144 |
int indentationLevel, |
148 |
int indentationLevel, |
145 |
String lineSeparator) { |
149 |
String lineSeparator) { |
146 |
|
150 |
|
|
|
151 |
return format(kind, source, new IRegion[] {new Region(offset, length)}, indentationLevel, lineSeparator); |
152 |
} |
153 |
|
154 |
/** |
155 |
* {@inheritDoc} |
156 |
*/ |
157 |
public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { |
158 |
int regionsLength = regions == null ? 0 : regions.length; |
159 |
if (regionsLength == 0) { |
160 |
throw new IllegalArgumentException(); |
161 |
} |
162 |
|
163 |
if (regionsLength > 1) { |
164 |
Arrays.sort(regions, new Comparator() { |
165 |
public int compare(Object o1, Object o2) { |
166 |
IRegion r1= (IRegion)o1; |
167 |
IRegion r2= (IRegion)o2; |
168 |
if (r1.getOffset() == r2.getOffset()) |
169 |
return 0; |
170 |
|
171 |
if (r1.getOffset() < r2.getOffset()) |
172 |
return -1; |
173 |
|
174 |
return 1; |
175 |
} |
176 |
}); |
177 |
} |
178 |
|
179 |
int offset= regions[0].getOffset(); |
180 |
IRegion lastRegions= regions[regionsLength - 1]; |
181 |
int end= lastRegions.getOffset() + lastRegions.getLength() - 1; |
182 |
int length = end - offset + 1; |
183 |
|
147 |
if (offset < 0 || length < 0 || length > source.length()) { |
184 |
if (offset < 0 || length < 0 || length > source.length()) { |
148 |
throw new IllegalArgumentException(); |
185 |
throw new IllegalArgumentException(); |
149 |
} |
186 |
} |
|
|
187 |
|
188 |
IRegion last= regions[0]; |
189 |
for (int i= 1; i < regionsLength; i++) { |
190 |
IRegion current= regions[i]; |
191 |
if (last.getLength() + last.getOffset() - 1 > current.getOffset()) |
192 |
throw new IllegalArgumentException(); |
193 |
} |
194 |
|
150 |
this.codeSnippetParsingUtil = new CodeSnippetParsingUtil(); |
195 |
this.codeSnippetParsingUtil = new CodeSnippetParsingUtil(); |
151 |
switch(kind) { |
196 |
switch(kind) { |
152 |
case K_CLASS_BODY_DECLARATIONS : |
197 |
case K_CLASS_BODY_DECLARATIONS : |
153 |
return formatClassBodyDeclarations(source, indentationLevel, lineSeparator, offset, length); |
198 |
return formatClassBodyDeclarations(source, indentationLevel, lineSeparator, regions); |
154 |
case K_COMPILATION_UNIT : |
199 |
case K_COMPILATION_UNIT : |
155 |
return formatCompilationUnit(source, indentationLevel, lineSeparator, offset, length); |
200 |
return formatCompilationUnit(source, indentationLevel, lineSeparator, regions); |
156 |
case K_EXPRESSION : |
201 |
case K_EXPRESSION : |
157 |
return formatExpression(source, indentationLevel, lineSeparator, offset, length); |
202 |
return formatExpression(source, indentationLevel, lineSeparator, regions); |
158 |
case K_STATEMENTS : |
203 |
case K_STATEMENTS : |
159 |
return formatStatements(source, indentationLevel, lineSeparator, offset, length); |
204 |
return formatStatements(source, indentationLevel, lineSeparator, regions); |
160 |
case K_UNKNOWN : |
205 |
case K_UNKNOWN : |
161 |
return probeFormatting(source, indentationLevel, lineSeparator, offset, length); |
206 |
return probeFormatting(source, indentationLevel, lineSeparator, regions); |
162 |
case K_JAVA_DOC : |
207 |
case K_JAVA_DOC : |
163 |
case K_MULTI_LINE_COMMENT : |
208 |
case K_MULTI_LINE_COMMENT : |
164 |
case K_SINGLE_LINE_COMMENT : |
209 |
case K_SINGLE_LINE_COMMENT : |
165 |
return formatComment(kind, source, indentationLevel, lineSeparator, offset, length); |
210 |
return formatComment(kind, source, indentationLevel, lineSeparator, regions); |
166 |
} |
211 |
} |
167 |
return null; |
212 |
return null; |
168 |
} |
213 |
} |
169 |
|
214 |
|
170 |
private TextEdit formatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, int offset, int length) { |
215 |
private TextEdit formatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, IRegion[] regions) { |
171 |
ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true); |
216 |
ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true); |
172 |
|
217 |
|
173 |
if (bodyDeclarations == null) { |
218 |
if (bodyDeclarations == null) { |
174 |
// a problem occured while parsing the source |
219 |
// a problem occured while parsing the source |
175 |
return null; |
220 |
return null; |
176 |
} |
221 |
} |
177 |
return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, offset, length); |
222 |
return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions); |
178 |
} |
223 |
} |
179 |
|
224 |
|
180 |
/** |
225 |
/** |
Lines 184-195
Link Here
|
184 |
* @param source the given source |
229 |
* @param source the given source |
185 |
* @param indentationLevel the given indentation level |
230 |
* @param indentationLevel the given indentation level |
186 |
* @param lineSeparator the given line separator |
231 |
* @param lineSeparator the given line separator |
187 |
* @param offset the given offset |
232 |
* @param regions the given regions |
188 |
* @param length the given length |
|
|
189 |
* @return the resulting text edit |
233 |
* @return the resulting text edit |
190 |
* @deprecated |
234 |
* @deprecated |
191 |
*/ |
235 |
*/ |
192 |
private TextEdit formatComment(int kind, String source, int indentationLevel, String lineSeparator, int offset, int length) { |
236 |
private TextEdit formatComment(int kind, String source, int indentationLevel, String lineSeparator, IRegion[] regions) { |
193 |
Object oldOption = this.options.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT); |
237 |
Object oldOption = this.options.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT); |
194 |
boolean isFormattingComments = false; |
238 |
boolean isFormattingComments = false; |
195 |
if (oldOption == null) { |
239 |
if (oldOption == null) { |
Lines 213-219
Link Here
|
213 |
this.preferences.line_separator = Util.LINE_SEPARATOR; |
257 |
this.preferences.line_separator = Util.LINE_SEPARATOR; |
214 |
} |
258 |
} |
215 |
this.preferences.initial_indentation_level = indentationLevel; |
259 |
this.preferences.initial_indentation_level = indentationLevel; |
216 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, offset, length, null); |
260 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, null); |
|
|
261 |
|
262 |
int offset= regions[0].getOffset(); |
263 |
IRegion lastRegions= regions[regions.length - 1]; |
264 |
int end= lastRegions.getOffset() + lastRegions.getLength() - 1; |
265 |
int length= end - offset + 1; |
266 |
|
217 |
final CommentRegion region = createRegion(kind, new Document(source), new Position(offset, length), this.newCodeFormatter); |
267 |
final CommentRegion region = createRegion(kind, new Document(source), new Position(offset, length), this.newCodeFormatter); |
218 |
if (region != null) { |
268 |
if (region != null) { |
219 |
return this.newCodeFormatter.format(source, region); |
269 |
return this.newCodeFormatter.format(source, region); |
Lines 222-228
Link Here
|
222 |
return new MultiTextEdit(); |
272 |
return new MultiTextEdit(); |
223 |
} |
273 |
} |
224 |
|
274 |
|
225 |
private TextEdit formatCompilationUnit(String source, int indentationLevel, String lineSeparator, int offset, int length) { |
275 |
private TextEdit formatCompilationUnit(String source, int indentationLevel, String lineSeparator, IRegion[] regions) { |
226 |
CompilationUnitDeclaration compilationUnitDeclaration = this.codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true); |
276 |
CompilationUnitDeclaration compilationUnitDeclaration = this.codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true); |
227 |
|
277 |
|
228 |
if (lineSeparator != null) { |
278 |
if (lineSeparator != null) { |
Lines 232-260
Link Here
|
232 |
} |
282 |
} |
233 |
this.preferences.initial_indentation_level = indentationLevel; |
283 |
this.preferences.initial_indentation_level = indentationLevel; |
234 |
|
284 |
|
235 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, offset, length, this.codeSnippetParsingUtil); |
285 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil); |
236 |
|
286 |
|
237 |
return this.newCodeFormatter.format(source, compilationUnitDeclaration); |
287 |
return this.newCodeFormatter.format(source, compilationUnitDeclaration); |
238 |
} |
288 |
} |
239 |
|
289 |
|
240 |
private TextEdit formatExpression(String source, int indentationLevel, String lineSeparator, int offset, int length) { |
290 |
private TextEdit formatExpression(String source, int indentationLevel, String lineSeparator, IRegion[] regions) { |
241 |
Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true); |
291 |
Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true); |
242 |
|
292 |
|
243 |
if (expression == null) { |
293 |
if (expression == null) { |
244 |
// a problem occured while parsing the source |
294 |
// a problem occured while parsing the source |
245 |
return null; |
295 |
return null; |
246 |
} |
296 |
} |
247 |
return internalFormatExpression(source, indentationLevel, lineSeparator, expression, offset, length); |
297 |
return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions); |
248 |
} |
298 |
} |
249 |
|
299 |
|
250 |
private TextEdit formatStatements(String source, int indentationLevel, String lineSeparator, int offset, int length) { |
300 |
private TextEdit formatStatements(String source, int indentationLevel, String lineSeparator, IRegion[] regions) { |
251 |
ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false); |
301 |
ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false); |
252 |
|
302 |
|
253 |
if (constructorDeclaration.statements == null) { |
303 |
if (constructorDeclaration.statements == null) { |
254 |
// a problem occured while parsing the source |
304 |
// a problem occured while parsing the source |
255 |
return null; |
305 |
return null; |
256 |
} |
306 |
} |
257 |
return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, offset, length); |
307 |
return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions); |
258 |
} |
308 |
} |
259 |
|
309 |
|
260 |
public String getDebugOutput() { |
310 |
public String getDebugOutput() { |
Lines 331-337
Link Here
|
331 |
return this.defaultCompilerOptions; |
381 |
return this.defaultCompilerOptions; |
332 |
} |
382 |
} |
333 |
|
383 |
|
334 |
private TextEdit internalFormatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, ASTNode[] bodyDeclarations, int offset, int length) { |
384 |
private TextEdit internalFormatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, ASTNode[] bodyDeclarations, IRegion[] regions) { |
335 |
if (lineSeparator != null) { |
385 |
if (lineSeparator != null) { |
336 |
this.preferences.line_separator = lineSeparator; |
386 |
this.preferences.line_separator = lineSeparator; |
337 |
} else { |
387 |
} else { |
Lines 339-349
Link Here
|
339 |
} |
389 |
} |
340 |
this.preferences.initial_indentation_level = indentationLevel; |
390 |
this.preferences.initial_indentation_level = indentationLevel; |
341 |
|
391 |
|
342 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, offset, length, this.codeSnippetParsingUtil); |
392 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil); |
343 |
return this.newCodeFormatter.format(source, bodyDeclarations); |
393 |
return this.newCodeFormatter.format(source, bodyDeclarations); |
344 |
} |
394 |
} |
345 |
|
395 |
|
346 |
private TextEdit internalFormatExpression(String source, int indentationLevel, String lineSeparator, Expression expression, int offset, int length) { |
396 |
private TextEdit internalFormatExpression(String source, int indentationLevel, String lineSeparator, Expression expression, IRegion[] regions) { |
347 |
if (lineSeparator != null) { |
397 |
if (lineSeparator != null) { |
348 |
this.preferences.line_separator = lineSeparator; |
398 |
this.preferences.line_separator = lineSeparator; |
349 |
} else { |
399 |
} else { |
Lines 351-363
Link Here
|
351 |
} |
401 |
} |
352 |
this.preferences.initial_indentation_level = indentationLevel; |
402 |
this.preferences.initial_indentation_level = indentationLevel; |
353 |
|
403 |
|
354 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, offset, length, this.codeSnippetParsingUtil); |
404 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil); |
355 |
|
405 |
|
356 |
TextEdit textEdit = this.newCodeFormatter.format(source, expression); |
406 |
TextEdit textEdit = this.newCodeFormatter.format(source, expression); |
357 |
return textEdit; |
407 |
return textEdit; |
358 |
} |
408 |
} |
359 |
|
409 |
|
360 |
private TextEdit internalFormatStatements(String source, int indentationLevel, String lineSeparator, ConstructorDeclaration constructorDeclaration, int offset, int length) { |
410 |
private TextEdit internalFormatStatements(String source, int indentationLevel, String lineSeparator, ConstructorDeclaration constructorDeclaration, IRegion[] regions) { |
361 |
if (lineSeparator != null) { |
411 |
if (lineSeparator != null) { |
362 |
this.preferences.line_separator = lineSeparator; |
412 |
this.preferences.line_separator = lineSeparator; |
363 |
} else { |
413 |
} else { |
Lines 365-397
Link Here
|
365 |
} |
415 |
} |
366 |
this.preferences.initial_indentation_level = indentationLevel; |
416 |
this.preferences.initial_indentation_level = indentationLevel; |
367 |
|
417 |
|
368 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, offset, length, this.codeSnippetParsingUtil); |
418 |
this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil); |
369 |
|
419 |
|
370 |
return this.newCodeFormatter.format(source, constructorDeclaration); |
420 |
return this.newCodeFormatter.format(source, constructorDeclaration); |
371 |
} |
421 |
} |
372 |
|
422 |
|
373 |
private TextEdit probeFormatting(String source, int indentationLevel, String lineSeparator, int offset, int length) { |
423 |
private TextEdit probeFormatting(String source, int indentationLevel, String lineSeparator, IRegion[] regions) { |
374 |
if (ProbingScanner == null) { |
424 |
if (ProbingScanner == null) { |
375 |
// scanner use to check if the kind could be K_JAVA_DOC, K_MULTI_LINE_COMMENT or K_SINGLE_LINE_COMMENT |
425 |
// scanner use to check if the kind could be K_JAVA_DOC, K_MULTI_LINE_COMMENT or K_SINGLE_LINE_COMMENT |
376 |
ProbingScanner = new Scanner(true, true, false/*nls*/, ClassFileConstants.JDK1_3, ClassFileConstants.JDK1_3, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
426 |
ProbingScanner = new Scanner(true, true, false/*nls*/, ClassFileConstants.JDK1_3, ClassFileConstants.JDK1_3, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
377 |
} |
427 |
} |
378 |
ProbingScanner.setSource(source.toCharArray()); |
428 |
ProbingScanner.setSource(source.toCharArray()); |
|
|
429 |
|
430 |
int offset= regions[0].getOffset(); |
431 |
IRegion lastRegions= regions[regions.length - 1]; |
432 |
int end= lastRegions.getOffset() + lastRegions.getLength() - 1; |
433 |
int length= end - offset + 1; |
434 |
|
379 |
ProbingScanner.resetTo(offset, offset + length); |
435 |
ProbingScanner.resetTo(offset, offset + length); |
380 |
try { |
436 |
try { |
381 |
switch(ProbingScanner.getNextToken()) { |
437 |
switch(ProbingScanner.getNextToken()) { |
382 |
case ITerminalSymbols.TokenNameCOMMENT_BLOCK : |
438 |
case ITerminalSymbols.TokenNameCOMMENT_BLOCK : |
383 |
if (ProbingScanner.getCurrentTokenEndPosition() == offset + length - 1) { |
439 |
if (ProbingScanner.getCurrentTokenEndPosition() == offset + length - 1) { |
384 |
return formatComment(K_MULTI_LINE_COMMENT, source, indentationLevel, lineSeparator, offset, length); |
440 |
return formatComment(K_MULTI_LINE_COMMENT, source, indentationLevel, lineSeparator, regions); |
385 |
} |
441 |
} |
386 |
break; |
442 |
break; |
387 |
case ITerminalSymbols.TokenNameCOMMENT_LINE : |
443 |
case ITerminalSymbols.TokenNameCOMMENT_LINE : |
388 |
if (ProbingScanner.getCurrentTokenEndPosition() == offset + length - 1) { |
444 |
if (ProbingScanner.getCurrentTokenEndPosition() == offset + length - 1) { |
389 |
return formatComment(K_SINGLE_LINE_COMMENT, source, indentationLevel, lineSeparator, offset, length); |
445 |
return formatComment(K_SINGLE_LINE_COMMENT, source, indentationLevel, lineSeparator, regions); |
390 |
} |
446 |
} |
391 |
break; |
447 |
break; |
392 |
case ITerminalSymbols.TokenNameCOMMENT_JAVADOC : |
448 |
case ITerminalSymbols.TokenNameCOMMENT_JAVADOC : |
393 |
if (ProbingScanner.getCurrentTokenEndPosition() == offset + length - 1) { |
449 |
if (ProbingScanner.getCurrentTokenEndPosition() == offset + length - 1) { |
394 |
return formatComment(K_JAVA_DOC, source, indentationLevel, lineSeparator, offset, length); |
450 |
return formatComment(K_JAVA_DOC, source, indentationLevel, lineSeparator, regions); |
395 |
} |
451 |
} |
396 |
} |
452 |
} |
397 |
} catch (InvalidInputException e) { |
453 |
} catch (InvalidInputException e) { |
Lines 402-423
Link Here
|
402 |
// probe for expression |
458 |
// probe for expression |
403 |
Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true); |
459 |
Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true); |
404 |
if (expression != null) { |
460 |
if (expression != null) { |
405 |
return internalFormatExpression(source, indentationLevel, lineSeparator, expression, offset, length); |
461 |
return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions); |
406 |
} |
462 |
} |
407 |
|
463 |
|
408 |
// probe for body declarations (fields, methods, constructors) |
464 |
// probe for body declarations (fields, methods, constructors) |
409 |
ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true); |
465 |
ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true); |
410 |
if (bodyDeclarations != null) { |
466 |
if (bodyDeclarations != null) { |
411 |
return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, offset, length); |
467 |
return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions); |
412 |
} |
468 |
} |
413 |
|
469 |
|
414 |
// probe for statements |
470 |
// probe for statements |
415 |
ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false); |
471 |
ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false); |
416 |
if (constructorDeclaration.statements != null) { |
472 |
if (constructorDeclaration.statements != null) { |
417 |
return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, offset, length); |
473 |
return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions); |
418 |
} |
474 |
} |
419 |
|
475 |
|
420 |
// this has to be a compilation unit |
476 |
// this has to be a compilation unit |
421 |
return formatCompilationUnit(source, indentationLevel, lineSeparator, offset, length); |
477 |
return formatCompilationUnit(source, indentationLevel, lineSeparator, regions); |
422 |
} |
478 |
} |
|
|
479 |
|
423 |
} |
480 |
} |