|
Lines 22-27
Link Here
|
| 22 |
|
22 |
|
| 23 |
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; |
23 |
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; |
| 24 |
|
24 |
|
|
|
25 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
| 26 |
|
| 25 |
|
27 |
|
| 26 |
/** |
28 |
/** |
| 27 |
* Uses the {@link org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner} to |
29 |
* Uses the {@link org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner} to |
|
Lines 856-861
Link Here
|
| 856 |
fAlign= JavaHeuristicScanner.NOT_FOUND; |
858 |
fAlign= JavaHeuristicScanner.NOT_FOUND; |
| 857 |
fPosition= offset; |
859 |
fPosition= offset; |
| 858 |
|
860 |
|
|
|
861 |
int lineForStartingOffset= 0; |
| 862 |
try { |
| 863 |
lineForStartingOffset= fDocument.getLineOfOffset(offset); |
| 864 |
} catch (BadLocationException e) { |
| 865 |
JavaPlugin.log(e); |
| 866 |
} |
| 867 |
|
| 859 |
// forward cases |
868 |
// forward cases |
| 860 |
// an unindentation happens sometimes if the next token is special, namely on braces, parens and case labels |
869 |
// an unindentation happens sometimes if the next token is special, namely on braces, parens and case labels |
| 861 |
// align braces, but handle the case where we align with the method declaration start instead of |
870 |
// align braces, but handle the case where we align with the method declaration start instead of |
|
Lines 961-966
Link Here
|
| 961 |
|
970 |
|
| 962 |
case Symbols.TokenTRY: |
971 |
case Symbols.TokenTRY: |
| 963 |
return skipToStatementStart(danglingElse, false); |
972 |
return skipToStatementStart(danglingElse, false); |
|
|
973 |
|
| 974 |
case Symbols.TokenRBRACKET: |
| 975 |
fIndent= fPrefs.prefContinuationIndent; |
| 976 |
return fPosition; |
| 977 |
|
| 964 |
case Symbols.TokenRPAREN: |
978 |
case Symbols.TokenRPAREN: |
| 965 |
if (throwsClause) { |
979 |
if (throwsClause) { |
| 966 |
fIndent= fPrefs.prefContinuationIndent; |
980 |
fIndent= fPrefs.prefContinuationIndent; |
|
Lines 990-999
Link Here
|
| 990 |
fPosition= offset; |
1004 |
fPosition= offset; |
| 991 |
fLine= line; |
1005 |
fLine= line; |
| 992 |
|
1006 |
|
| 993 |
return skipToPreviousListItemOrListStart(); |
1007 |
return skipToPreviousListItemOrListStart(lineForStartingOffset); |
| 994 |
case Symbols.TokenRETURN: |
1008 |
case Symbols.TokenRETURN: |
| 995 |
fIndent= fPrefs.prefContinuationIndent; |
1009 |
fIndent= fPrefs.prefContinuationIndent; |
| 996 |
return fPosition; |
1010 |
return fPosition; |
|
|
1011 |
case Symbols.TokenDOUBLEQUOTE: |
| 1012 |
if (handleString(lineForStartingOffset)) |
| 1013 |
return fPosition; |
| 1014 |
return skipToPreviousListItemOrListStart(lineForStartingOffset); |
| 997 |
case Symbols.TokenCOMMA: |
1015 |
case Symbols.TokenCOMMA: |
| 998 |
// inside a list of some type |
1016 |
// inside a list of some type |
| 999 |
// easy if there is already a list item before with its own indentation - we just align |
1017 |
// easy if there is already a list item before with its own indentation - we just align |
|
Lines 1003-1009
Link Here
|
| 1003 |
// inside whatever we don't know about: similar to the list case: |
1021 |
// inside whatever we don't know about: similar to the list case: |
| 1004 |
// if we are inside a continued expression, then either align with a previous line that has indentation |
1022 |
// if we are inside a continued expression, then either align with a previous line that has indentation |
| 1005 |
// or indent from the expression start line (either a scope introducer or the start of the expr). |
1023 |
// or indent from the expression start line (either a scope introducer or the start of the expr). |
| 1006 |
return skipToPreviousListItemOrListStart(); |
1024 |
return skipToPreviousListItemOrListStart(lineForStartingOffset); |
| 1007 |
} |
1025 |
} |
| 1008 |
} |
1026 |
} |
| 1009 |
|
1027 |
|
|
Lines 1277-1293
Link Here
|
| 1277 |
} |
1295 |
} |
| 1278 |
|
1296 |
|
| 1279 |
/** |
1297 |
/** |
| 1280 |
* Returns the reference position for a list element. The algorithm |
1298 |
* Returns the reference position for a list element. The algorithm tries to match any previous |
| 1281 |
* tries to match any previous indentation on the same list. If there is none, |
1299 |
* indentation on the same list. If there is none, the reference position returned is determined |
| 1282 |
* the reference position returned is determined depending on the type of list: |
1300 |
* depending on the type of list: The indentation will either match the list scope introducer |
| 1283 |
* The indentation will either match the list scope introducer (e.g. for |
1301 |
* (e.g. for method declarations), so called deep indents, or simply increase the indentation by |
| 1284 |
* method declarations), so called deep indents, or simply increase the |
1302 |
* a number of standard indents. See also {@link #handleScopeIntroduction(int)}. |
| 1285 |
* indentation by a number of standard indents. See also {@link #handleScopeIntroduction(int)}. |
1303 |
* |
| 1286 |
* |
1304 |
* @param lineForStartingOffset The line for which we are trying to compute the indentation |
| 1287 |
* @return the reference position for a list item: either a previous list item |
1305 |
* @return the reference position for a list item: either a previous list item that has its own |
| 1288 |
* that has its own indentation, or the list introduction start. |
1306 |
* indentation, or the list introduction start. |
| 1289 |
*/ |
1307 |
*/ |
| 1290 |
private int skipToPreviousListItemOrListStart() { |
1308 |
private int skipToPreviousListItemOrListStart(int lineForStartingOffset) { |
| 1291 |
int startLine= fLine; |
1309 |
int startLine= fLine; |
| 1292 |
int startPosition= fPosition; |
1310 |
int startPosition= fPosition; |
| 1293 |
while (true) { |
1311 |
while (true) { |
|
Lines 1340-1345
Link Here
|
| 1340 |
return fPosition; |
1358 |
return fPosition; |
| 1341 |
case Symbols.TokenEQUAL: |
1359 |
case Symbols.TokenEQUAL: |
| 1342 |
return handleEqual(); |
1360 |
return handleEqual(); |
|
|
1361 |
case Symbols.TokenDOUBLEQUOTE: |
| 1362 |
if (handleString(lineForStartingOffset)) |
| 1363 |
return fPosition; |
| 1364 |
break; |
| 1365 |
|
| 1343 |
case Symbols.TokenEOF: |
1366 |
case Symbols.TokenEOF: |
| 1344 |
return 0; |
1367 |
return 0; |
| 1345 |
|
1368 |
|
|
Lines 1348-1353
Link Here
|
| 1348 |
} |
1371 |
} |
| 1349 |
|
1372 |
|
| 1350 |
/** |
1373 |
/** |
|
|
1374 |
* Called when a double quote is encountered. Handles skipping of strings so that we don't scan |
| 1375 |
* for tokens inside of it, and indents the string if there is continuation. |
| 1376 |
* |
| 1377 |
* @param lineForStartingOffset The line for which we are trying to compute the indentation |
| 1378 |
* @return <code>true</code> if the string was skipped and continuation indentation set. |
| 1379 |
* @since 3.7 |
| 1380 |
*/ |
| 1381 |
private boolean handleString(int lineForStartingOffset) { |
| 1382 |
int savedPosition= fPosition; |
| 1383 |
if (skipString()) { |
| 1384 |
int nonWSCharBackward= fScanner.findNonWhitespaceBackward(fPosition - 1, JavaHeuristicScanner.UNBOUND); |
| 1385 |
try { |
| 1386 |
if (nonWSCharBackward != JavaHeuristicScanner.NOT_FOUND && fDocument.getChar(nonWSCharBackward) != '+' && fDocument.getLineOfOffset(nonWSCharBackward) < fLine) { |
| 1387 |
int nonWSCharForward= fScanner.findNonWhitespaceForward(savedPosition + 1, JavaHeuristicScanner.UNBOUND); |
| 1388 |
if (nonWSCharForward != JavaHeuristicScanner.NOT_FOUND && fDocument.getChar(nonWSCharForward) == '+' && fLine < lineForStartingOffset) { |
| 1389 |
fIndent= fPrefs.prefContinuationIndent; |
| 1390 |
return true; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
} catch (BadLocationException e) { |
| 1394 |
JavaPlugin.log(e); |
| 1395 |
} |
| 1396 |
} |
| 1397 |
return false; |
| 1398 |
} |
| 1399 |
|
| 1400 |
/** |
| 1401 |
* Skip the string backwards in the current line when a double quote is encountered. |
| 1402 |
* |
| 1403 |
* @return <code>true</code> if the string was successfully |
| 1404 |
*/ |
| 1405 |
private boolean skipString() { |
| 1406 |
boolean stringSkipped= false; |
| 1407 |
try { |
| 1408 |
int lineOffset= fDocument.getLineOffset(fLine); |
| 1409 |
int positionOfDoubleQuote= fScanner.scanBackward(fPosition - 1, lineOffset, '"'); |
| 1410 |
while (positionOfDoubleQuote != JavaHeuristicScanner.NOT_FOUND) { |
| 1411 |
fPosition= positionOfDoubleQuote; |
| 1412 |
stringSkipped= true; |
| 1413 |
if (positionOfDoubleQuote != lineOffset && fDocument.getChar(positionOfDoubleQuote - 1) == '\\') { |
| 1414 |
positionOfDoubleQuote= fScanner.scanBackward(fPosition - 1, lineOffset, '"'); |
| 1415 |
} else { |
| 1416 |
break; |
| 1417 |
} |
| 1418 |
} |
| 1419 |
} catch (BadLocationException e) { |
| 1420 |
JavaPlugin.log(e); |
| 1421 |
} |
| 1422 |
return stringSkipped; |
| 1423 |
} |
| 1424 |
|
| 1425 |
/** |
| 1351 |
* Skips a scope and positions the cursor (<code>fPosition</code>) on the |
1426 |
* Skips a scope and positions the cursor (<code>fPosition</code>) on the |
| 1352 |
* token that opens the scope. Returns <code>true</code> if a matching peer |
1427 |
* token that opens the scope. Returns <code>true</code> if a matching peer |
| 1353 |
* could be found, <code>false</code> otherwise. The current token when calling |
1428 |
* could be found, <code>false</code> otherwise. The current token when calling |