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 source file.
28 * <p>An instance holds some internal position in the document and is therefore not threadsafe.</p>
32 public class JavaHeuristicScanner implements Symbols {
34 * Returned by all methods when the requested position could not be found, or if a
35 * {@link BadLocationException} was thrown while scanning.
37 public static final int NOT_FOUND= -1;
40 * Special bound parameter that means either -1 (backward scanning) or
41 * <code>fDocument.getLength()</code> (forward scanning).
43 public static final int UNBOUND= -2;
46 /* character constants */
47 private static final char LBRACE= '{';
48 private static final char RBRACE= '}';
49 private static final char LPAREN= '(';
50 private static final char RPAREN= ')';
51 private static final char SEMICOLON= ';';
52 private static final char COLON= ':';
53 private static final char COMMA= ',';
54 private static final char LBRACKET= '[';
55 private static final char RBRACKET= ']';
56 private static final char QUESTIONMARK= '?';
57 private static final char EQUAL= '=';
60 * Specifies the stop condition, upon which the <code>scanXXX</code> methods will decide whether
61 * to keep scanning or not. This interface may implemented by clients.
63 public interface StopCondition {
65 * Instructs the scanner to return the current position.
67 * @param ch the char at the current position
68 * @param position the current position
69 * @param forward the iteration direction
70 * @return <code>true</code> if the stop condition is met.
72 boolean stop(char ch, int position, boolean forward);
76 * Stops upon a non-whitespace (as defined by {@link Character#isWhitespace(char)}) character.
78 private static class NonWhitespace implements StopCondition {
80 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
82 public boolean stop(char ch, int position, boolean forward) {
83 return !Character.isWhitespace(ch);
88 * Stops upon a non-whitespace character in the default partition.
92 private class NonWhitespaceDefaultPartition extends NonWhitespace {
94 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
96 public boolean stop(char ch, int position, boolean forward) {
97 return super.stop(ch, position, true) && isDefaultPartition(position);
102 * Stops upon a non-java identifier (as defined by {@link Scanner#isPHPIdentifierPart(char)}) character.
104 private static class NonJavaIdentifierPart implements StopCondition {
106 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
108 public boolean stop(char ch, int position, boolean forward) {
109 return !Scanner.isPHPIdentifierPart(ch);
114 * Stops upon a non-java identifier character in the default partition.
116 * @see NonJavaIdentifierPart
118 private class NonJavaIdentifierPartDefaultPartition extends NonJavaIdentifierPart {
120 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char)
122 public boolean stop(char ch, int position, boolean forward) {
123 return super.stop(ch, position, true) || !isDefaultPartition(position);
128 * Stops upon a character in the default partition that matches the given character list.
130 private class CharacterMatch implements StopCondition {
131 private final char[] fChars;
134 * Creates a new instance.
135 * @param ch the single character to match
137 public CharacterMatch(char ch) {
138 this(new char[] {ch});
142 * Creates a new instance.
143 * @param chars the chars to match.
145 public CharacterMatch(char[] chars) {
146 Assert.isNotNull(chars);
147 Assert.isTrue(chars.length > 0);
153 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char, int)
155 public boolean stop(char ch, int position, boolean forward) {
156 return Arrays.binarySearch(fChars, ch) >= 0 && isDefaultPartition(position);
161 * Acts like character match, but skips all scopes introduced by parenthesis, brackets, and
164 protected class SkippingScopeMatch extends CharacterMatch {
165 private char fOpening, fClosing;
166 private int fDepth= 0;
169 * Creates a new instance.
170 * @param ch the single character to match
172 public SkippingScopeMatch(char ch) {
177 * Creates a new instance.
178 * @param chars the chars to match.
180 public SkippingScopeMatch(char[] chars) {
185 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner.StopCondition#stop(char, int)
187 public boolean stop(char ch, int position, boolean forward) {
189 if (fDepth == 0 && super.stop(ch, position, true))
191 else if (ch == fOpening)
193 else if (ch == fClosing) {
199 } else if (fDepth == 0) {
243 /** The document being scanned. */
244 private IDocument fDocument;
245 /** The partitioning being used for scanning. */
246 private String fPartitioning;
247 /** The partition to scan in. */
248 private String fPartition;
250 /* internal scan state */
252 /** the most recently read character. */
254 /** the most recently read position. */
257 /* preset stop conditions */
258 private final StopCondition fNonWSDefaultPart= new NonWhitespaceDefaultPartition();
259 private final static StopCondition fNonWS= new NonWhitespace();
260 private final StopCondition fNonIdent= new NonJavaIdentifierPartDefaultPartition();
263 * Creates a new instance.
265 * @param document the document to scan
266 * @param partitioning the partitioning to use for scanning
267 * @param partition the partition to scan in
269 public JavaHeuristicScanner(IDocument document, String partitioning, String partition) {
270 Assert.isNotNull(document);
271 Assert.isNotNull(partitioning);
272 Assert.isNotNull(partition);
274 fPartitioning= partitioning;
275 fPartition= partition;
279 * Calls <code>this(document, IJavaPartitions.JAVA_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE)</code>.
281 * @param document the document to scan.
283 public JavaHeuristicScanner(IDocument document) {
284 this(document, IPHPPartitions.PHP_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE);
288 * Returns the most recent internal scan position.
290 * @return the most recent internal scan position.
292 public int getPosition() {
297 * Returns the next token in forward direction, starting at <code>start</code>, and not extending
298 * further than <code>bound</code>. The return value is one of the constants defined in {@link Symbols}.
299 * After a call, {@link #getPosition()} will return the position just after the scanned token
300 * (i.e. the next position that will be scanned).
302 * @param start the first character position in the document to consider
303 * @param bound the first position not to consider any more
304 * @return a constant from {@link Symbols} describing the next token
306 public int nextToken(int start, int bound) {
307 int pos= scanForward(start, bound, fNonWSDefaultPart);
308 if (pos == NOT_FOUND)
319 return TokenLBRACKET;
321 return TokenRBRACKET;
327 return TokenSEMICOLON;
331 return TokenQUESTIONMARK;
337 if (Scanner.isPHPIdentifierPart(fChar)) {
338 // assume an ident or keyword
340 pos= scanForward(pos + 1, bound, fNonIdent);
341 if (pos == NOT_FOUND)
342 to= bound == UNBOUND ? fDocument.getLength() : bound;
346 String identOrKeyword;
348 identOrKeyword= fDocument.get(from, to - from);
349 } catch (BadLocationException e) {
353 return getToken(identOrKeyword);
357 // operators, number literals etc
363 * Returns the next token in backward direction, starting at <code>start</code>, and not extending
364 * further than <code>bound</code>. The return value is one of the constants defined in {@link Symbols}.
365 * After a call, {@link #getPosition()} will return the position just before the scanned token
366 * starts (i.e. the next position that will be scanned).
368 * @param start the first character position in the document to consider
369 * @param bound the first position not to consider any more
370 * @return a constant from {@link Symbols} describing the previous token
372 public int previousToken(int start, int bound) {
373 int pos= scanBackward(start, bound, fNonWSDefaultPart);
374 if (pos == NOT_FOUND)
385 return TokenLBRACKET;
387 return TokenRBRACKET;
393 return TokenSEMICOLON;
399 return TokenQUESTIONMARK;
405 if (Scanner.isPHPIdentifierPart(fChar)) {
406 // assume an ident or keyword
407 int from, to= pos + 1;
408 pos= scanBackward(pos - 1, bound, fNonIdent);
409 if (pos == NOT_FOUND)
410 from= bound == UNBOUND ? 0 : bound + 1;
414 String identOrKeyword;
416 identOrKeyword= fDocument.get(from, to - from);
417 } catch (BadLocationException e) {
421 return getToken(identOrKeyword);
425 // operators, number literals etc
432 * Returns one of the keyword constants or <code>TokenIDENT</code> for a scanned identifier.
434 * @param s a scanned identifier
435 * @return one of the constants defined in {@link Symbols}
437 private int getToken(String s) {
440 switch (s.length()) {
442 if ("if".equals(s)) //$NON-NLS-1$
444 if ("do".equals(s)) //$NON-NLS-1$
448 if ("for".equals(s)) //$NON-NLS-1$
450 if ("try".equals(s)) //$NON-NLS-1$
452 if ("new".equals(s)) //$NON-NLS-1$
456 if ("case".equals(s)) //$NON-NLS-1$
458 if ("else".equals(s)) //$NON-NLS-1$
460 if ("goto".equals(s)) //$NON-NLS-1$
464 if ("break".equals(s)) //$NON-NLS-1$
466 if ("catch".equals(s)) //$NON-NLS-1$
468 if ("while".equals(s)) //$NON-NLS-1$
472 if ("return".equals(s)) //$NON-NLS-1$
474 if ("static".equals(s)) //$NON-NLS-1$
476 if ("switch".equals(s)) //$NON-NLS-1$
480 if ("default".equals(s)) //$NON-NLS-1$
482 if ("finally".equals(s)) //$NON-NLS-1$
486 if ("synchronized".equals(s)) //$NON-NLS-1$
487 return TokenSYNCHRONIZED;
494 * Returns the position of the closing peer character (forward search). Any scopes introduced by opening peers
495 * are skipped. All peers accounted for must reside in the default partition.
497 * <p>Note that <code>start</code> must not point to the opening peer, but to the first
498 * character being searched.</p>
500 * @param start the start position
501 * @param openingPeer the opening peer character (e.g. '{')
502 * @param closingPeer the closing peer character (e.g. '}')
503 * @return the matching peer character position, or <code>NOT_FOUND</code>
505 public int findClosingPeer(int start, final char openingPeer, final char closingPeer) {
506 Assert.isNotNull(fDocument);
507 Assert.isTrue(start >= 0);
513 start= scanForward(start + 1, UNBOUND, new CharacterMatch(new char[] {openingPeer, closingPeer}));
514 if (start == NOT_FOUND)
517 if (fDocument.getChar(start) == openingPeer)
526 } catch (BadLocationException e) {
532 * Returns the position of the opening peer character (backward search). Any scopes introduced by closing peers
533 * are skipped. All peers accounted for must reside in the default partition.
535 * <p>Note that <code>start</code> must not point to the closing peer, but to the first
536 * character being searched.</p>
538 * @param start the start position
539 * @param openingPeer the opening peer character (e.g. '{')
540 * @param closingPeer the closing peer character (e.g. '}')
541 * @return the matching peer character position, or <code>NOT_FOUND</code>
543 public int findOpeningPeer(int start, char openingPeer, char closingPeer) {
544 Assert.isTrue(start < fDocument.getLength());
550 start= scanBackward(start - 1, UNBOUND, new CharacterMatch(new char[] {openingPeer, closingPeer}));
551 if (start == NOT_FOUND)
554 if (fDocument.getChar(start) == closingPeer)
563 } catch (BadLocationException e) {
569 * Computes the surrounding block around <code>offset</code>. The search is started at the
570 * beginning of <code>offset</code>, i.e. an opening brace at <code>offset</code> will not be
571 * part of the surrounding block, but a closing brace will.
573 * @param offset the offset for which the surrounding block is computed
574 * @return a region describing the surrounding block, or <code>null</code> if none can be found
576 public IRegion findSurroundingBlock(int offset) {
577 if (offset < 1 || offset >= fDocument.getLength())
580 int begin= findOpeningPeer(offset - 1, LBRACE, RBRACE);
581 int end= findClosingPeer(offset, LBRACE, RBRACE);
582 if (begin == NOT_FOUND || end == NOT_FOUND)
584 return new Region(begin, end + 1 - begin);
588 * Finds the smallest position in <code>fDocument</code> such that the position is >= <code>position</code>
589 * and < <code>bound</code> and <code>Character.isWhitespace(fDocument.getChar(pos))</code> evaluates to <code>false</code>
590 * and the position is in the default partition.
592 * @param position the first character position in <code>fDocument</code> to be considered
593 * @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>
594 * @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
596 public int findNonWhitespaceForward(int position, int bound) {
597 return scanForward(position, bound, fNonWSDefaultPart);
601 * Finds the smallest position in <code>fDocument</code> such that the position is >= <code>position</code>
602 * and < <code>bound</code> and <code>Character.isWhitespace(fDocument.getChar(pos))</code> evaluates to <code>false</code>.
604 * @param position the first character position in <code>fDocument</code> to be considered
605 * @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>
606 * @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
608 public int findNonWhitespaceForwardInAnyPartition(int position, int bound) {
609 return scanForward(position, bound, fNonWS);
613 * Finds the highest position in <code>fDocument</code> such that the position is <= <code>position</code>
614 * and > <code>bound</code> and <code>Character.isWhitespace(fDocument.getChar(pos))</code> evaluates to <code>false</code>
615 * and the position is in the default partition.
617 * @param position the first character position in <code>fDocument</code> to be considered
618 * @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>
619 * @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
621 public int findNonWhitespaceBackward(int position, int bound) {
622 return scanBackward(position, bound, fNonWSDefaultPart);
626 * Finds the lowest position <code>p</code> in <code>fDocument</code> such that <code>start</code> <= p <
627 * <code>bound</code> and <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to <code>true</code>.
629 * @param start the first character position in <code>fDocument</code> to be considered
630 * @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>
631 * @param condition the <code>StopCondition</code> to check
632 * @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
634 public int scanForward(int start, int bound, StopCondition condition) {
635 Assert.isTrue(start >= 0);
637 if (bound == UNBOUND)
638 bound= fDocument.getLength();
640 Assert.isTrue(bound <= fDocument.getLength());
644 while (fPos < bound) {
646 fChar= fDocument.getChar(fPos);
647 if (condition.stop(fChar, fPos, true))
652 } catch (BadLocationException e) {
659 * Finds the lowest position in <code>fDocument</code> such that the position is >= <code>position</code>
660 * and < <code>bound</code> and <code>fDocument.getChar(position) == ch</code> evaluates to <code>true</code>
661 * and the position is in the default partition.
663 * @param position the first character position in <code>fDocument</code> to be considered
664 * @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>
665 * @param ch the <code>char</code> to search for
666 * @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
668 public int scanForward(int position, int bound, char ch) {
669 return scanForward(position, bound, new CharacterMatch(ch));
673 * Finds the lowest position in <code>fDocument</code> such that the position is >= <code>position</code>
674 * and < <code>bound</code> and <code>fDocument.getChar(position) == ch</code> evaluates to <code>true</code> for at least one
675 * ch in <code>chars</code> and the position is in the default partition.
677 * @param position the first character position in <code>fDocument</code> to be considered
678 * @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>
679 * @param chars an array of <code>char</code> to search for
680 * @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
682 public int scanForward(int position, int bound, char[] chars) {
683 return scanForward(position, bound, new CharacterMatch(chars));
687 * Finds the highest position <code>p</code> in <code>fDocument</code> such that <code>bound</code> < <code>p</code> <= <code>start</code>
688 * and <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to <code>true</code>.
690 * @param start the first character position in <code>fDocument</code> to be considered
691 * @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>
692 * @param condition the <code>StopCondition</code> to check
693 * @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
695 public int scanBackward(int start, int bound, StopCondition condition) {
696 if (bound == UNBOUND)
699 Assert.isTrue(bound >= -1);
700 Assert.isTrue(start < fDocument.getLength() );
704 while (fPos > bound) {
706 fChar= fDocument.getChar(fPos);
707 if (condition.stop(fChar, fPos, false))
712 } catch (BadLocationException e) {
718 * Finds the highest position in <code>fDocument</code> such that the position is <= <code>position</code>
719 * and > <code>bound</code> and <code>fDocument.getChar(position) == ch</code> evaluates to <code>true</code> for at least one
720 * ch in <code>chars</code> and the position is in the default partition.
722 * @param position the first character position in <code>fDocument</code> to be considered
723 * @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>
724 * @param ch the <code>char</code> to search for
725 * @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
727 public int scanBackward(int position, int bound, char ch) {
728 return scanBackward(position, bound, new CharacterMatch(ch));
732 * Finds the highest position in <code>fDocument</code> such that the position is <= <code>position</code>
733 * and > <code>bound</code> and <code>fDocument.getChar(position) == ch</code> evaluates to <code>true</code> for at least one
734 * ch in <code>chars</code> and the position is in the default partition.
736 * @param position the first character position in <code>fDocument</code> to be considered
737 * @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>
738 * @param chars an array of <code>char</code> to search for
739 * @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
741 public int scanBackward(int position, int bound, char[] chars) {
742 return scanBackward(position, bound, new CharacterMatch(chars));
746 * Checks whether <code>position</code> resides in a default (Java) partition of <code>fDocument</code>.
748 * @param position the position to be checked
749 * @return <code>true</code> if <code>position</code> is in the default partition of <code>fDocument</code>, <code>false</code> otherwise
751 public boolean isDefaultPartition(int position) {
752 Assert.isTrue(position >= 0);
753 Assert.isTrue(position <= fDocument.getLength());
756 ITypedRegion region= TextUtilities.getPartition(fDocument, fPartitioning, position, false);
757 return region.getType().equals(fPartition);
759 } catch (BadLocationException e) {
766 * Checks if the line seems to be an open condition not followed by a block (i.e. an if, while,
767 * or for statement with just one following statement, see example below).
774 * <p>Algorithm: if the last non-WS, non-Comment code on the line is an if (condition), while (condition),
775 * for( expression), do, else, and there is no statement after that </p>
777 * @param position the insert position of the new character
778 * @param bound the lowest position to consider
779 * @return <code>true</code> if the code is a conditional statement or loop without a block, <code>false</code> otherwise
781 public boolean isBracelessBlockStart(int position, int bound) {
785 switch (previousToken(position, bound)) {
790 position= findOpeningPeer(fPos, LPAREN, RPAREN);
792 switch (previousToken(position - 1, bound)) {