1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.util.Arrays;
15 import org.eclipse.jface.text.Assert;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.ITypedRegion;
20 import org.eclipse.jface.text.Region;
21 import org.eclipse.jface.text.TextUtilities;
24 * Utility methods for heuristic based Java manipulations in an incomplete Java source file.
26 * <p>An instance holds some internal position in the document and is therefore not threadsafe.</p>
30 public class JavaHeuristicScanner implements Symbols {
32 * Returned by all methods when the requested position could not be found, or if a
33 * {@link BadLocationException} was thrown while scanning.
35 public static final int NOT_FOUND= -1;
38 * Special bound parameter that means either -1 (backward scanning) or
39 * <code>fDocument.getLength()</code> (forward scanning).
41 public static final int UNBOUND= -2;
44 /* character constants */
45 private static final char LBRACE= '{';
46 private static final char RBRACE= '}';
47 private static final char LPAREN= '(';
48 private static final char RPAREN= ')';
49 private static final char SEMICOLON= ';';
50 private static final char COLON= ':';
51 private static final char COMMA= ',';
52 private static final char LBRACKET= '[';
53 private static final char RBRACKET= ']';
54 private static final char QUESTIONMARK= '?';
55 private static final char EQUAL= '=';
58 * Specifies the stop condition, upon which the <code>scanXXX</code> methods will decide whether
59 * to keep scanning or not. This interface may implemented by clients.
61 public interface StopCondition {
63 * Instructs the scanner to return the current position.
65 * @param ch the char at the current position
66 * @param position the current position
67 * @param forward the iteration direction
68 * @return <code>true</code> if the stop condition is met.
70 boolean stop(char ch, int position, boolean forward);
74 * Stops upon a non-whitespace (as defined by {@link Character#isWhitespace(char)}) character.
76 private static class NonWhitespace implements StopCondition {
78 * @see org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
80 public boolean stop(char ch, int position, boolean forward) {
81 return !Character.isWhitespace(ch);
86 * Stops upon a non-whitespace character in the default partition.
90 private class NonWhitespaceDefaultPartition extends NonWhitespace {
92 * @see org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
94 public boolean stop(char ch, int position, boolean forward) {
95 return super.stop(ch, position, true) && isDefaultPartition(position);
100 * Stops upon a non-java identifier (as defined by {@link Character#isJavaIdentifierPart(char)}) character.
102 private static class NonJavaIdentifierPart implements StopCondition {
104 * @see org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
106 public boolean stop(char ch, int position, boolean forward) {
107 return !Character.isJavaIdentifierPart(ch);
112 * Stops upon a non-java identifier character in the default partition.
114 * @see NonJavaIdentifierPart
116 private class NonJavaIdentifierPartDefaultPartition extends NonJavaIdentifierPart {
118 * @see org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
120 public boolean stop(char ch, int position, boolean forward) {
121 return super.stop(ch, position, true) || !isDefaultPartition(position);
126 * Stops upon a character in the default partition that matches the given character list.
128 private class CharacterMatch implements StopCondition {
129 private final char[] fChars;
132 * Creates a new instance.
133 * @param ch the single character to match
135 public CharacterMatch(char ch) {
136 this(new char[] {ch});
140 * Creates a new instance.
141 * @param chars the chars to match.
143 public CharacterMatch(char[] chars) {
144 Assert.isNotNull(chars);
145 Assert.isTrue(chars.length > 0);
151 * @see org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char, int)
153 public boolean stop(char ch, int position, boolean forward) {
154 return Arrays.binarySearch(fChars, ch) >= 0 && isDefaultPartition(position);
159 * Acts like character match, but skips all scopes introduced by parenthesis, brackets, and
162 protected class SkippingScopeMatch extends CharacterMatch {
163 private char fOpening, fClosing;
164 private int fDepth= 0;
167 * Creates a new instance.
168 * @param ch the single character to match
170 public SkippingScopeMatch(char ch) {
175 * Creates a new instance.
176 * @param chars the chars to match.
178 public SkippingScopeMatch(char[] chars) {
183 * @see org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char, int)
185 public boolean stop(char ch, int position, boolean forward) {
187 if (fDepth == 0 && super.stop(ch, position, true))
189 else if (ch == fOpening)
191 else if (ch == fClosing) {
197 } else if (fDepth == 0) {
241 /** The document being scanned. */
242 private IDocument fDocument;
243 /** The partitioning being used for scanning. */
244 private String fPartitioning;
245 /** The partition to scan in. */
246 private String fPartition;
248 /* internal scan state */
250 /** the most recently read character. */
252 /** the most recently read position. */
255 /* preset stop conditions */
256 private final StopCondition fNonWSDefaultPart= new NonWhitespaceDefaultPartition();
257 private final static StopCondition fNonWS= new NonWhitespace();
258 private final StopCondition fNonIdent= new NonJavaIdentifierPartDefaultPartition();
261 * Creates a new instance.
263 * @param document the document to scan
264 * @param partitioning the partitioning to use for scanning
265 * @param partition the partition to scan in
267 public JavaHeuristicScanner(IDocument document, String partitioning, String partition) {
268 Assert.isNotNull(document);
269 Assert.isNotNull(partitioning);
270 Assert.isNotNull(partition);
272 fPartitioning= partitioning;
273 fPartition= partition;
277 * Calls <code>this(document, IJavaPartitions.JAVA_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE)</code>.
279 * @param document the document to scan.
281 public JavaHeuristicScanner(IDocument document) {
282 this(document, IPHPPartitions.PHP_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE);
286 * Returns the most recent internal scan position.
288 * @return the most recent internal scan position.
290 public int getPosition() {
295 * Returns the next token in forward direction, starting at <code>start</code>, and not extending
296 * further than <code>bound</code>. The return value is one of the constants defined in {@link Symbols}.
297 * After a call, {@link #getPosition()} will return the position just after the scanned token
298 * (i.e. the next position that will be scanned).
300 * @param start the first character position in the document to consider
301 * @param bound the first position not to consider any more
302 * @return a constant from {@link Symbols} describing the next token
304 public int nextToken(int start, int bound) {
305 int pos= scanForward(start, bound, fNonWSDefaultPart);
306 if (pos == NOT_FOUND)
317 return TokenLBRACKET;
319 return TokenRBRACKET;
325 return TokenSEMICOLON;
329 return TokenQUESTIONMARK;
335 if (Character.isJavaIdentifierPart(fChar)) {
336 // assume an ident or keyword
338 pos= scanForward(pos + 1, bound, fNonIdent);
339 if (pos == NOT_FOUND)
340 to= bound == UNBOUND ? fDocument.getLength() : bound;
344 String identOrKeyword;
346 identOrKeyword= fDocument.get(from, to - from);
347 } catch (BadLocationException e) {
351 return getToken(identOrKeyword);
355 // operators, number literals etc
361 * Returns the next token in backward direction, starting at <code>start</code>, and not extending
362 * further than <code>bound</code>. The return value is one of the constants defined in {@link Symbols}.
363 * After a call, {@link #getPosition()} will return the position just before the scanned token
364 * starts (i.e. the next position that will be scanned).
366 * @param start the first character position in the document to consider
367 * @param bound the first position not to consider any more
368 * @return a constant from {@link Symbols} describing the previous token
370 public int previousToken(int start, int bound) {
371 int pos= scanBackward(start, bound, fNonWSDefaultPart);
372 if (pos == NOT_FOUND)
383 return TokenLBRACKET;
385 return TokenRBRACKET;
391 return TokenSEMICOLON;
397 return TokenQUESTIONMARK;
403 if (Character.isJavaIdentifierPart(fChar)) {
404 // assume an ident or keyword
405 int from, to= pos + 1;
406 pos= scanBackward(pos - 1, bound, fNonIdent);
407 if (pos == NOT_FOUND)
408 from= bound == UNBOUND ? 0 : bound + 1;
412 String identOrKeyword;
414 identOrKeyword= fDocument.get(from, to - from);
415 } catch (BadLocationException e) {
419 return getToken(identOrKeyword);
423 // operators, number literals etc
430 * Returns one of the keyword constants or <code>TokenIDENT</code> for a scanned identifier.
432 * @param s a scanned identifier
433 * @return one of the constants defined in {@link Symbols}
435 private int getToken(String s) {
438 switch (s.length()) {
440 if ("if".equals(s)) //$NON-NLS-1$
442 if ("do".equals(s)) //$NON-NLS-1$
446 if ("for".equals(s)) //$NON-NLS-1$
448 if ("try".equals(s)) //$NON-NLS-1$
450 if ("new".equals(s)) //$NON-NLS-1$
454 if ("case".equals(s)) //$NON-NLS-1$
456 if ("else".equals(s)) //$NON-NLS-1$
458 if ("goto".equals(s)) //$NON-NLS-1$
462 if ("break".equals(s)) //$NON-NLS-1$
464 if ("catch".equals(s)) //$NON-NLS-1$
466 if ("while".equals(s)) //$NON-NLS-1$
470 if ("return".equals(s)) //$NON-NLS-1$
472 if ("static".equals(s)) //$NON-NLS-1$
474 if ("switch".equals(s)) //$NON-NLS-1$
478 if ("default".equals(s)) //$NON-NLS-1$
480 if ("finally".equals(s)) //$NON-NLS-1$
484 if ("synchronized".equals(s)) //$NON-NLS-1$
485 return TokenSYNCHRONIZED;
492 * Returns the position of the closing peer character (forward search). Any scopes introduced by opening peers
493 * are skipped. All peers accounted for must reside in the default partition.
495 * <p>Note that <code>start</code> must not point to the opening peer, but to the first
496 * character being searched.</p>
498 * @param start the start position
499 * @param openingPeer the opening peer character (e.g. '{')
500 * @param closingPeer the closing peer character (e.g. '}')
501 * @return the matching peer character position, or <code>NOT_FOUND</code>
503 public int findClosingPeer(int start, final char openingPeer, final char closingPeer) {
504 Assert.isNotNull(fDocument);
505 Assert.isTrue(start >= 0);
511 start= scanForward(start + 1, UNBOUND, new CharacterMatch(new char[] {openingPeer, closingPeer}));
512 if (start == NOT_FOUND)
515 if (fDocument.getChar(start) == openingPeer)
524 } catch (BadLocationException e) {
530 * Returns the position of the opening peer character (backward search). Any scopes introduced by closing peers
531 * are skipped. All peers accounted for must reside in the default partition.
533 * <p>Note that <code>start</code> must not point to the closing peer, but to the first
534 * character being searched.</p>
536 * @param start the start position
537 * @param openingPeer the opening peer character (e.g. '{')
538 * @param closingPeer the closing peer character (e.g. '}')
539 * @return the matching peer character position, or <code>NOT_FOUND</code>
541 public int findOpeningPeer(int start, char openingPeer, char closingPeer) {
542 Assert.isTrue(start < fDocument.getLength());
548 start= scanBackward(start - 1, UNBOUND, new CharacterMatch(new char[] {openingPeer, closingPeer}));
549 if (start == NOT_FOUND)
552 if (fDocument.getChar(start) == closingPeer)
561 } catch (BadLocationException e) {
567 * Computes the surrounding block around <code>offset</code>. The search is started at the
568 * beginning of <code>offset</code>, i.e. an opening brace at <code>offset</code> will not be
569 * part of the surrounding block, but a closing brace will.
571 * @param offset the offset for which the surrounding block is computed
572 * @return a region describing the surrounding block, or <code>null</code> if none can be found
574 public IRegion findSurroundingBlock(int offset) {
575 if (offset < 1 || offset >= fDocument.getLength())
578 int begin= findOpeningPeer(offset - 1, LBRACE, RBRACE);
579 int end= findClosingPeer(offset, LBRACE, RBRACE);
580 if (begin == NOT_FOUND || end == NOT_FOUND)
582 return new Region(begin, end + 1 - begin);
586 * Finds the smallest position in <code>fDocument</code> such that the position is >= <code>position</code>
587 * and < <code>bound</code> and <code>Character.isWhitespace(fDocument.getChar(pos))</code> evaluates to <code>false</code>
588 * and the position is in the default partition.
590 * @param position the first character position in <code>fDocument</code> to be considered
591 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> > <code>position</code>, or <code>UNBOUND</code>
592 * @return the smallest position of a non-whitespace character in [<code>position</code>, <code>bound</code>) that resides in a Java partition, or <code>NOT_FOUND</code> if none can be found
594 public int findNonWhitespaceForward(int position, int bound) {
595 return scanForward(position, bound, fNonWSDefaultPart);
599 * Finds the smallest position in <code>fDocument</code> such that the position is >= <code>position</code>
600 * and < <code>bound</code> and <code>Character.isWhitespace(fDocument.getChar(pos))</code> evaluates to <code>false</code>.
602 * @param position the first character position in <code>fDocument</code> to be considered
603 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> > <code>position</code>, or <code>UNBOUND</code>
604 * @return the smallest position of a non-whitespace character in [<code>position</code>, <code>bound</code>), or <code>NOT_FOUND</code> if none can be found
606 public int findNonWhitespaceForwardInAnyPartition(int position, int bound) {
607 return scanForward(position, bound, fNonWS);
611 * Finds the highest position in <code>fDocument</code> such that the position is <= <code>position</code>
612 * and > <code>bound</code> and <code>Character.isWhitespace(fDocument.getChar(pos))</code> evaluates to <code>false</code>
613 * and the position is in the default partition.
615 * @param position the first character position in <code>fDocument</code> to be considered
616 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> < <code>position</code>, or <code>UNBOUND</code>
617 * @return the highest position of a non-whitespace character in (<code>bound</code>, <code>position</code>] that resides in a Java partition, or <code>NOT_FOUND</code> if none can be found
619 public int findNonWhitespaceBackward(int position, int bound) {
620 return scanBackward(position, bound, fNonWSDefaultPart);
624 * Finds the lowest position <code>p</code> in <code>fDocument</code> such that <code>start</code> <= p <
625 * <code>bound</code> and <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to <code>true</code>.
627 * @param start the first character position in <code>fDocument</code> to be considered
628 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> > <code>start</code>, or <code>UNBOUND</code>
629 * @param condition the <code>StopCondition</code> to check
630 * @return the lowest position in [<code>start</code>, <code>bound</code>) for which <code>condition</code> holds, or <code>NOT_FOUND</code> if none can be found
632 public int scanForward(int start, int bound, StopCondition condition) {
633 Assert.isTrue(start >= 0);
635 if (bound == UNBOUND)
636 bound= fDocument.getLength();
638 Assert.isTrue(bound <= fDocument.getLength());
642 while (fPos < bound) {
644 fChar= fDocument.getChar(fPos);
645 if (condition.stop(fChar, fPos, true))
650 } catch (BadLocationException e) {
657 * Finds the lowest position in <code>fDocument</code> such that the position is >= <code>position</code>
658 * and < <code>bound</code> and <code>fDocument.getChar(position) == ch</code> evaluates to <code>true</code>
659 * and the position is in the default partition.
661 * @param position the first character position in <code>fDocument</code> to be considered
662 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> > <code>position</code>, or <code>UNBOUND</code>
663 * @param ch the <code>char</code> to search for
664 * @return the lowest position of <code>ch</code> in (<code>bound</code>, <code>position</code>] that resides in a Java partition, or <code>NOT_FOUND</code> if none can be found
666 public int scanForward(int position, int bound, char ch) {
667 return scanForward(position, bound, new CharacterMatch(ch));
671 * Finds the lowest position in <code>fDocument</code> such that the position is >= <code>position</code>
672 * and < <code>bound</code> and <code>fDocument.getChar(position) == ch</code> evaluates to <code>true</code> for at least one
673 * ch in <code>chars</code> and the position is in the default partition.
675 * @param position the first character position in <code>fDocument</code> to be considered
676 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> > <code>position</code>, or <code>UNBOUND</code>
677 * @param chars an array of <code>char</code> to search for
678 * @return the lowest position of a non-whitespace character in [<code>position</code>, <code>bound</code>) that resides in a Java partition, or <code>NOT_FOUND</code> if none can be found
680 public int scanForward(int position, int bound, char[] chars) {
681 return scanForward(position, bound, new CharacterMatch(chars));
685 * Finds the highest position <code>p</code> in <code>fDocument</code> such that <code>bound</code> < <code>p</code> <= <code>start</code>
686 * and <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to <code>true</code>.
688 * @param start the first character position in <code>fDocument</code> to be considered
689 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> < <code>start</code>, or <code>UNBOUND</code>
690 * @param condition the <code>StopCondition</code> to check
691 * @return the highest position in (<code>bound</code>, <code>start</code> for which <code>condition</code> holds, or <code>NOT_FOUND</code> if none can be found
693 public int scanBackward(int start, int bound, StopCondition condition) {
694 if (bound == UNBOUND)
697 Assert.isTrue(bound >= -1);
698 Assert.isTrue(start < fDocument.getLength() );
702 while (fPos > bound) {
704 fChar= fDocument.getChar(fPos);
705 if (condition.stop(fChar, fPos, false))
710 } catch (BadLocationException e) {
716 * Finds the highest position in <code>fDocument</code> such that the position is <= <code>position</code>
717 * and > <code>bound</code> and <code>fDocument.getChar(position) == ch</code> evaluates to <code>true</code> for at least one
718 * ch in <code>chars</code> and the position is in the default partition.
720 * @param position the first character position in <code>fDocument</code> to be considered
721 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> < <code>position</code>, or <code>UNBOUND</code>
722 * @param ch the <code>char</code> to search for
723 * @return the highest position of one element in <code>chars</code> in (<code>bound</code>, <code>position</code>] that resides in a Java partition, or <code>NOT_FOUND</code> if none can be found
725 public int scanBackward(int position, int bound, char ch) {
726 return scanBackward(position, bound, new CharacterMatch(ch));
730 * Finds the highest position in <code>fDocument</code> such that the position is <= <code>position</code>
731 * and > <code>bound</code> and <code>fDocument.getChar(position) == ch</code> evaluates to <code>true</code> for at least one
732 * ch in <code>chars</code> and the position is in the default partition.
734 * @param position the first character position in <code>fDocument</code> to be considered
735 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> < <code>position</code>, or <code>UNBOUND</code>
736 * @param chars an array of <code>char</code> to search for
737 * @return the highest position of one element in <code>chars</code> in (<code>bound</code>, <code>position</code>] that resides in a Java partition, or <code>NOT_FOUND</code> if none can be found
739 public int scanBackward(int position, int bound, char[] chars) {
740 return scanBackward(position, bound, new CharacterMatch(chars));
744 * Checks whether <code>position</code> resides in a default (Java) partition of <code>fDocument</code>.
746 * @param position the position to be checked
747 * @return <code>true</code> if <code>position</code> is in the default partition of <code>fDocument</code>, <code>false</code> otherwise
749 public boolean isDefaultPartition(int position) {
750 Assert.isTrue(position >= 0);
751 Assert.isTrue(position <= fDocument.getLength());
754 ITypedRegion region= TextUtilities.getPartition(fDocument, fPartitioning, position, false);
755 return region.getType().equals(fPartition);
757 } catch (BadLocationException e) {
764 * Checks if the line seems to be an open condition not followed by a block (i.e. an if, while,
765 * or for statement with just one following statement, see example below).
772 * <p>Algorithm: if the last non-WS, non-Comment code on the line is an if (condition), while (condition),
773 * for( expression), do, else, and there is no statement after that </p>
775 * @param position the insert position of the new character
776 * @param bound the lowest position to consider
777 * @return <code>true</code> if the code is a conditional statement or loop without a block, <code>false</code> otherwise
779 public boolean isBracelessBlockStart(int position, int bound) {
783 switch (previousToken(position, bound)) {
788 position= findOpeningPeer(fPos, LPAREN, RPAREN);
790 switch (previousToken(position - 1, bound)) {