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 net.sourceforge.phpdt.internal.compiler.parser.Scanner;
17 import org.eclipse.jface.text.Assert;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.ITypedRegion;
22 import org.eclipse.jface.text.Region;
23 import org.eclipse.jface.text.TextUtilities;
26 * Utility methods for heuristic based Java manipulations in an incomplete Java
30 * An instance holds some internal position in the document and is therefore not
36 public class JavaHeuristicScanner implements Symbols {
38 * Returned by all methods when the requested position could not be found,
39 * or if a {@link BadLocationException} was thrown while scanning.
41 public static final int NOT_FOUND = -1;
44 * Special bound parameter that means either -1 (backward scanning) or
45 * <code>fDocument.getLength()</code> (forward scanning).
47 public static final int UNBOUND = -2;
49 /* character constants */
50 private static final char LBRACE = '{';
52 private static final char RBRACE = '}';
54 private static final char LPAREN = '(';
56 private static final char RPAREN = ')';
58 private static final char SEMICOLON = ';';
60 private static final char COLON = ':';
62 private static final char COMMA = ',';
64 private static final char LBRACKET = '[';
66 private static final char RBRACKET = ']';
68 private static final char QUESTIONMARK = '?';
70 private static final char EQUAL = '=';
73 * Specifies the stop condition, upon which the <code>scanXXX</code>
74 * methods will decide whether to keep scanning or not. This interface may
75 * implemented by clients.
77 public interface StopCondition {
79 * Instructs the scanner to return the current position.
82 * the char at the current position
84 * the current position
86 * the iteration direction
87 * @return <code>true</code> if the stop condition is met.
89 boolean stop(char ch, int position, boolean forward);
93 * Stops upon a non-whitespace (as defined by
94 * {@link Character#isWhitespace(char)}) character.
96 private static class NonWhitespace implements StopCondition {
98 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
100 public boolean stop(char ch, int position, boolean forward) {
101 return !Character.isWhitespace(ch);
106 * Stops upon a non-whitespace character in the default partition.
110 private class NonWhitespaceDefaultPartition extends NonWhitespace {
112 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
114 public boolean stop(char ch, int position, boolean forward) {
115 return super.stop(ch, position, true)
116 && isDefaultPartition(position);
121 * Stops upon a non-java identifier (as defined by
122 * {@link Scanner#isPHPIdentifierPart(char)}) character.
124 private static class NonJavaIdentifierPart implements StopCondition {
126 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
128 public boolean stop(char ch, int position, boolean forward) {
129 return !Scanner.isPHPIdentifierPart(ch);
134 * Stops upon a non-java identifier character in the default partition.
136 * @see NonJavaIdentifierPart
138 private class NonJavaIdentifierPartDefaultPartition extends
139 NonJavaIdentifierPart {
141 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
143 public boolean stop(char ch, int position, boolean forward) {
144 return super.stop(ch, position, true)
145 || !isDefaultPartition(position);
150 * Stops upon a character in the default partition that matches the given
153 private class CharacterMatch implements StopCondition {
154 private final char[] fChars;
157 * Creates a new instance.
160 * the single character to match
162 public CharacterMatch(char ch) {
163 this(new char[] { ch });
167 * Creates a new instance.
170 * the chars to match.
172 public CharacterMatch(char[] chars) {
173 Assert.isNotNull(chars);
174 Assert.isTrue(chars.length > 0);
180 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char,
183 public boolean stop(char ch, int position, boolean forward) {
184 return Arrays.binarySearch(fChars, ch) >= 0
185 && isDefaultPartition(position);
190 * Acts like character match, but skips all scopes introduced by
191 * parenthesis, brackets, and braces.
193 protected class SkippingScopeMatch extends CharacterMatch {
194 private char fOpening, fClosing;
196 private int fDepth = 0;
199 * Creates a new instance.
202 * the single character to match
204 public SkippingScopeMatch(char ch) {
209 * Creates a new instance.
212 * the chars to match.
214 public SkippingScopeMatch(char[] chars) {
219 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char,
222 public boolean stop(char ch, int position, boolean forward) {
224 if (fDepth == 0 && super.stop(ch, position, true))
226 else if (ch == fOpening)
228 else if (ch == fClosing) {
234 } else if (fDepth == 0) {
278 /** The document being scanned. */
279 private IDocument fDocument;
281 /** The partitioning being used for scanning. */
282 private String fPartitioning;
284 /** The partition to scan in. */
285 private String fPartition;
287 /* internal scan state */
289 /** the most recently read character. */
292 /** the most recently read position. */
295 /* preset stop conditions */
296 private final StopCondition fNonWSDefaultPart = new NonWhitespaceDefaultPartition();
298 private final static StopCondition fNonWS = new NonWhitespace();
300 private final StopCondition fNonIdent = new NonJavaIdentifierPartDefaultPartition();
303 * Creates a new instance.
306 * the document to scan
307 * @param partitioning
308 * the partitioning to use for scanning
310 * the partition to scan in
312 public JavaHeuristicScanner(IDocument document, String partitioning,
314 Assert.isNotNull(document);
315 Assert.isNotNull(partitioning);
316 Assert.isNotNull(partition);
317 fDocument = document;
318 fPartitioning = partitioning;
319 fPartition = partition;
324 * <code>this(document, IJavaPartitions.JAVA_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE)</code>.
327 * the document to scan.
329 public JavaHeuristicScanner(IDocument document) {
330 this(document, IPHPPartitions.PHP_PARTITIONING,
331 IDocument.DEFAULT_CONTENT_TYPE);
335 * Returns the most recent internal scan position.
337 * @return the most recent internal scan position.
339 public int getPosition() {
344 * Returns the next token in forward direction, starting at
345 * <code>start</code>, and not extending further than <code>bound</code>.
346 * The return value is one of the constants defined in {@link Symbols}.
347 * After a call, {@link #getPosition()} will return the position just after
348 * the scanned token (i.e. the next position that will be scanned).
351 * the first character position in the document to consider
353 * the first position not to consider any more
354 * @return a constant from {@link Symbols} describing the next token
356 public int nextToken(int start, int bound) {
357 int pos = scanForward(start, bound, fNonWSDefaultPart);
358 if (pos == NOT_FOUND)
369 return TokenLBRACKET;
371 return TokenRBRACKET;
377 return TokenSEMICOLON;
381 return TokenQUESTIONMARK;
387 if (Scanner.isPHPIdentifierPart(fChar)) {
388 // assume an ident or keyword
390 pos = scanForward(pos + 1, bound, fNonIdent);
391 if (pos == NOT_FOUND)
392 to = bound == UNBOUND ? fDocument.getLength() : bound;
396 String identOrKeyword;
398 identOrKeyword = fDocument.get(from, to - from);
399 } catch (BadLocationException e) {
403 return getToken(identOrKeyword);
406 // operators, number literals etc
412 * Returns the next token in backward direction, starting at
413 * <code>start</code>, and not extending further than <code>bound</code>.
414 * The return value is one of the constants defined in {@link Symbols}.
415 * After a call, {@link #getPosition()} will return the position just before
416 * the scanned token starts (i.e. the next position that will be scanned).
419 * the first character position in the document to consider
421 * the first position not to consider any more
422 * @return a constant from {@link Symbols} describing the previous token
424 public int previousToken(int start, int bound) {
425 int pos = scanBackward(start, bound, fNonWSDefaultPart);
426 if (pos == NOT_FOUND)
437 return TokenLBRACKET;
439 return TokenRBRACKET;
445 return TokenSEMICOLON;
451 return TokenQUESTIONMARK;
457 if (Scanner.isPHPIdentifierPart(fChar)) {
458 // assume an ident or keyword
459 int from, to = pos + 1;
460 pos = scanBackward(pos - 1, bound, fNonIdent);
461 if (pos == NOT_FOUND)
462 from = bound == UNBOUND ? 0 : bound + 1;
466 String identOrKeyword;
468 identOrKeyword = fDocument.get(from, to - from);
469 } catch (BadLocationException e) {
473 return getToken(identOrKeyword);
476 // operators, number literals etc
483 * Returns one of the keyword constants or <code>TokenIDENT</code> for a
484 * scanned identifier.
487 * a scanned identifier
488 * @return one of the constants defined in {@link Symbols}
490 private int getToken(String s) {
493 switch (s.length()) {
495 if ("if".equals(s)) //$NON-NLS-1$
497 if ("do".equals(s)) //$NON-NLS-1$
501 if ("for".equals(s)) //$NON-NLS-1$
503 if ("try".equals(s)) //$NON-NLS-1$
505 if ("new".equals(s)) //$NON-NLS-1$
509 if ("case".equals(s)) //$NON-NLS-1$
511 if ("else".equals(s)) //$NON-NLS-1$
513 if ("goto".equals(s)) //$NON-NLS-1$
517 if ("break".equals(s)) //$NON-NLS-1$
519 if ("catch".equals(s)) //$NON-NLS-1$
521 if ("while".equals(s)) //$NON-NLS-1$
525 if ("return".equals(s)) //$NON-NLS-1$
527 if ("static".equals(s)) //$NON-NLS-1$
529 if ("switch".equals(s)) //$NON-NLS-1$
533 if ("default".equals(s)) //$NON-NLS-1$
535 if ("finally".equals(s)) //$NON-NLS-1$
539 if ("synchronized".equals(s)) //$NON-NLS-1$
540 return TokenSYNCHRONIZED;
547 * Returns the position of the closing peer character (forward search). Any
548 * scopes introduced by opening peers are skipped. All peers accounted for
549 * must reside in the default partition.
552 * Note that <code>start</code> must not point to the opening peer, but to
553 * the first character being searched.
559 * the opening peer character (e.g. '{')
561 * the closing peer character (e.g. '}')
562 * @return the matching peer character position, or <code>NOT_FOUND</code>
564 public int findClosingPeer(int start, final char openingPeer,
565 final char closingPeer) {
566 Assert.isNotNull(fDocument);
567 Assert.isTrue(start >= 0);
573 start = scanForward(start + 1, UNBOUND, new CharacterMatch(
574 new char[] { openingPeer, closingPeer }));
575 if (start == NOT_FOUND)
578 if (fDocument.getChar(start) == openingPeer)
587 } catch (BadLocationException e) {
593 * Returns the position of the opening peer character (backward search). Any
594 * scopes introduced by closing peers are skipped. All peers accounted for
595 * must reside in the default partition.
598 * Note that <code>start</code> must not point to the closing peer, but to
599 * the first character being searched.
605 * the opening peer character (e.g. '{')
607 * the closing peer character (e.g. '}')
608 * @return the matching peer character position, or <code>NOT_FOUND</code>
610 public int findOpeningPeer(int start, char openingPeer, char closingPeer) {
611 Assert.isTrue(start < fDocument.getLength());
617 start = scanBackward(start - 1, UNBOUND, new CharacterMatch(
618 new char[] { openingPeer, closingPeer }));
619 if (start == NOT_FOUND)
622 if (fDocument.getChar(start) == closingPeer)
631 } catch (BadLocationException e) {
637 * Computes the surrounding block around <code>offset</code>. The search
638 * is started at the beginning of <code>offset</code>, i.e. an opening
639 * brace at <code>offset</code> will not be part of the surrounding block,
640 * but a closing brace will.
643 * the offset for which the surrounding block is computed
644 * @return a region describing the surrounding block, or <code>null</code>
645 * if none can be found
647 public IRegion findSurroundingBlock(int offset) {
648 if (offset < 1 || offset >= fDocument.getLength())
651 int begin = findOpeningPeer(offset - 1, LBRACE, RBRACE);
652 int end = findClosingPeer(offset, LBRACE, RBRACE);
653 if (begin == NOT_FOUND || end == NOT_FOUND)
655 return new Region(begin, end + 1 - begin);
659 * Finds the smallest position in <code>fDocument</code> such that the
660 * position is >= <code>position</code> and < <code>bound</code>
661 * and <code>Character.isWhitespace(fDocument.getChar(pos))</code>
662 * evaluates to <code>false</code> and the position is in the default
666 * the first character position in <code>fDocument</code> to be
669 * the first position in <code>fDocument</code> to not consider
670 * any more, with <code>bound</code> > <code>position</code>,
671 * or <code>UNBOUND</code>
672 * @return the smallest position of a non-whitespace character in [<code>position</code>,
673 * <code>bound</code>) that resides in a Java partition, or
674 * <code>NOT_FOUND</code> if none can be found
676 public int findNonWhitespaceForward(int position, int bound) {
677 return scanForward(position, bound, fNonWSDefaultPart);
681 * Finds the smallest position in <code>fDocument</code> such that the
682 * position is >= <code>position</code> and < <code>bound</code>
683 * and <code>Character.isWhitespace(fDocument.getChar(pos))</code>
684 * evaluates to <code>false</code>.
687 * the first character position in <code>fDocument</code> to be
690 * the first position in <code>fDocument</code> to not consider
691 * any more, with <code>bound</code> > <code>position</code>,
692 * or <code>UNBOUND</code>
693 * @return the smallest position of a non-whitespace character in [<code>position</code>,
694 * <code>bound</code>), or <code>NOT_FOUND</code> if none can
697 public int findNonWhitespaceForwardInAnyPartition(int position, int bound) {
698 return scanForward(position, bound, fNonWS);
702 * Finds the highest position in <code>fDocument</code> such that the
703 * position is <= <code>position</code> and > <code>bound</code>
704 * and <code>Character.isWhitespace(fDocument.getChar(pos))</code>
705 * evaluates to <code>false</code> and the position is in the default
709 * the first character position in <code>fDocument</code> to be
712 * the first position in <code>fDocument</code> to not consider
713 * any more, with <code>bound</code> < <code>position</code>,
714 * or <code>UNBOUND</code>
715 * @return the highest position of a non-whitespace character in (<code>bound</code>,
716 * <code>position</code>] that resides in a Java partition, or
717 * <code>NOT_FOUND</code> if none can be found
719 public int findNonWhitespaceBackward(int position, int bound) {
720 return scanBackward(position, bound, fNonWSDefaultPart);
724 * Finds the lowest position <code>p</code> in <code>fDocument</code>
725 * such that <code>start</code> <= p < <code>bound</code> and
726 * <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to
730 * the first character position in <code>fDocument</code> to be
733 * the first position in <code>fDocument</code> to not consider
734 * any more, with <code>bound</code> > <code>start</code>,
735 * or <code>UNBOUND</code>
737 * the <code>StopCondition</code> to check
738 * @return the lowest position in [<code>start</code>,
739 * <code>bound</code>) for which <code>condition</code> holds,
740 * or <code>NOT_FOUND</code> if none can be found
742 public int scanForward(int start, int bound, StopCondition condition) {
743 Assert.isTrue(start >= 0);
745 if (bound == UNBOUND)
746 bound = fDocument.getLength();
748 Assert.isTrue(bound <= fDocument.getLength());
752 while (fPos < bound) {
754 fChar = fDocument.getChar(fPos);
755 if (condition.stop(fChar, fPos, true))
760 } catch (BadLocationException e) {
766 * Finds the lowest position in <code>fDocument</code> such that the
767 * position is >= <code>position</code> and < <code>bound</code>
768 * and <code>fDocument.getChar(position) == ch</code> evaluates to
769 * <code>true</code> and the position is in the default partition.
772 * the first character position in <code>fDocument</code> to be
775 * the first position in <code>fDocument</code> to not consider
776 * any more, with <code>bound</code> > <code>position</code>,
777 * or <code>UNBOUND</code>
779 * the <code>char</code> to search for
780 * @return the lowest position of <code>ch</code> in (<code>bound</code>,
781 * <code>position</code>] that resides in a Java partition, or
782 * <code>NOT_FOUND</code> if none can be found
784 public int scanForward(int position, int bound, char ch) {
785 return scanForward(position, bound, new CharacterMatch(ch));
789 * Finds the lowest position in <code>fDocument</code> such that the
790 * position is >= <code>position</code> and < <code>bound</code>
791 * and <code>fDocument.getChar(position) == ch</code> evaluates to
792 * <code>true</code> for at least one ch in <code>chars</code> and the
793 * position is in the default partition.
796 * the first character position in <code>fDocument</code> to be
799 * the first position in <code>fDocument</code> to not consider
800 * any more, with <code>bound</code> > <code>position</code>,
801 * or <code>UNBOUND</code>
803 * an array of <code>char</code> to search for
804 * @return the lowest position of a non-whitespace character in [<code>position</code>,
805 * <code>bound</code>) that resides in a Java partition, or
806 * <code>NOT_FOUND</code> if none can be found
808 public int scanForward(int position, int bound, char[] chars) {
809 return scanForward(position, bound, new CharacterMatch(chars));
813 * Finds the highest position <code>p</code> in <code>fDocument</code>
814 * such that <code>bound</code> < <code>p</code> <=
815 * <code>start</code> and
816 * <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to
820 * the first character position in <code>fDocument</code> to be
823 * the first position in <code>fDocument</code> to not consider
824 * any more, with <code>bound</code> < <code>start</code>,
825 * or <code>UNBOUND</code>
827 * the <code>StopCondition</code> to check
828 * @return the highest position in (<code>bound</code>,
829 * <code>start</code> for which <code>condition</code> holds, or
830 * <code>NOT_FOUND</code> if none can be found
832 public int scanBackward(int start, int bound, StopCondition condition) {
833 if (bound == UNBOUND)
836 Assert.isTrue(bound >= -1);
837 Assert.isTrue(start < fDocument.getLength());
841 while (fPos > bound) {
843 fChar = fDocument.getChar(fPos);
844 if (condition.stop(fChar, fPos, false))
849 } catch (BadLocationException e) {
855 * Finds the highest position in <code>fDocument</code> such that the
856 * position is <= <code>position</code> and > <code>bound</code>
857 * and <code>fDocument.getChar(position) == ch</code> evaluates to
858 * <code>true</code> for at least one ch in <code>chars</code> and the
859 * position is in the default partition.
862 * the first character position in <code>fDocument</code> to be
865 * the first position in <code>fDocument</code> to not consider
866 * any more, with <code>bound</code> < <code>position</code>,
867 * or <code>UNBOUND</code>
869 * the <code>char</code> to search for
870 * @return the highest position of one element in <code>chars</code> in (<code>bound</code>,
871 * <code>position</code>] that resides in a Java partition, or
872 * <code>NOT_FOUND</code> if none can be found
874 public int scanBackward(int position, int bound, char ch) {
875 return scanBackward(position, bound, new CharacterMatch(ch));
879 * Finds the highest position in <code>fDocument</code> such that the
880 * position is <= <code>position</code> and > <code>bound</code>
881 * and <code>fDocument.getChar(position) == ch</code> evaluates to
882 * <code>true</code> for at least one ch in <code>chars</code> and the
883 * position is in the default partition.
886 * the first character position in <code>fDocument</code> to be
889 * the first position in <code>fDocument</code> to not consider
890 * any more, with <code>bound</code> < <code>position</code>,
891 * or <code>UNBOUND</code>
893 * an array of <code>char</code> to search for
894 * @return the highest position of one element in <code>chars</code> in (<code>bound</code>,
895 * <code>position</code>] that resides in a Java partition, or
896 * <code>NOT_FOUND</code> if none can be found
898 public int scanBackward(int position, int bound, char[] chars) {
899 return scanBackward(position, bound, new CharacterMatch(chars));
903 * Checks whether <code>position</code> resides in a default (Java)
904 * partition of <code>fDocument</code>.
907 * the position to be checked
908 * @return <code>true</code> if <code>position</code> is in the default
909 * partition of <code>fDocument</code>, <code>false</code>
912 public boolean isDefaultPartition(int position) {
913 Assert.isTrue(position >= 0);
914 Assert.isTrue(position <= fDocument.getLength());
917 ITypedRegion region = TextUtilities.getPartition(fDocument,
918 fPartitioning, position, false);
919 return region.getType().equals(fPartition);
921 } catch (BadLocationException e) {
928 * Checks if the line seems to be an open condition not followed by a block
929 * (i.e. an if, while, or for statement with just one following statement,
930 * see example below).
938 * Algorithm: if the last non-WS, non-Comment code on the line is an if
939 * (condition), while (condition), for( expression), do, else, and there is
940 * no statement after that
944 * the insert position of the new character
946 * the lowest position to consider
947 * @return <code>true</code> if the code is a conditional statement or
948 * loop without a block, <code>false</code> otherwise
950 public boolean isBracelessBlockStart(int position, int bound) {
954 switch (previousToken(position, bound)) {
959 position = findOpeningPeer(fPos, LPAREN, RPAREN);
961 switch (previousToken(position - 1, bound)) {