Java Code Examples for org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner#NOT_FOUND

The following examples show how to use org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner#NOT_FOUND . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected final int guessMethodContextInformationPosition(ContentAssistInvocationContext context) {
	final int contextPosition= context.getInvocationOffset();

	IDocument document= context.getDocument();
	JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
	int bound= Math.max(-1, contextPosition - 2000);

	// try the innermost scope of parentheses that looks like a method call
	int pos= contextPosition - 1;
	do {
		int paren= scanner.findOpeningPeer(pos, bound, '(', ')');
		if (paren == JavaHeuristicScanner.NOT_FOUND)
			break;
		int token= scanner.previousToken(paren - 1, bound);
		// next token must be a method name (identifier) or the closing angle of a
		// constructor call of a parameterized type.
		if (token == Symbols.TokenIDENT || token == Symbols.TokenGREATERTHAN)
			return paren + 1;
		pos= paren - 1;
	} while (true);

	return contextPosition;
}
 
Example 2
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Skips the scope opened by <code>token</code>.
 *
 * @param scanner the scanner
 * @param start the start position
 * @param token the token
 * @return the position after the scope or <code>JavaHeuristicScanner.NOT_FOUND</code>
 */
private static int skipScope(JavaHeuristicScanner scanner, int start, int token) {
	int openToken= token;
	int closeToken;
	switch (token) {
		case Symbols.TokenLPAREN:
			closeToken= Symbols.TokenRPAREN;
			break;
		case Symbols.TokenLBRACKET:
			closeToken= Symbols.TokenRBRACKET;
			break;
		case Symbols.TokenLBRACE:
			closeToken= Symbols.TokenRBRACE;
			break;
		default:
			Assert.isTrue(false);
			return -1; // dummy
	}

	int depth= 1;
	int p= start;

	while (true) {
		int tok= scanner.nextToken(p, JavaHeuristicScanner.UNBOUND);
		p= scanner.getPosition();

		if (tok == openToken) {
			depth++;
		} else if (tok == closeToken) {
			depth--;
			if (depth == 0)
				return p + 1;
		} else if (tok == Symbols.TokenEOF) {
			return JavaHeuristicScanner.NOT_FOUND;
		}
	}
}
 
Example 3
Source File: JavaTypeCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected int guessContextInformationPosition(ContentAssistInvocationContext context) {
	final int contextPosition= context.getInvocationOffset();

	IDocument document= context.getDocument();
	JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
	int bound= Math.max(-1, contextPosition - 200);

	// try the innermost scope of angle brackets that looks like a generic type argument list
	try {
		int pos= contextPosition - 1;
		do {
			int angle= scanner.findOpeningPeer(pos, bound, '<', '>');
			if (angle == JavaHeuristicScanner.NOT_FOUND)
				break;
			int token= scanner.previousToken(angle - 1, bound);
			// next token must be a method name that is a generic type
			if (token == Symbols.TokenIDENT) {
				int off= scanner.getPosition() + 1;
				int end= angle;
				String ident= document.get(off, end - off).trim();
				if (JavaHeuristicScanner.isGenericStarter(ident))
					return angle + 1;
			}
			pos= angle - 1;
		} while (true);
	} catch (BadLocationException x) {
	}

	return super.guessContextInformationPosition(context);
}
 
Example 4
Source File: IndentUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes and returns the indentation for a javadoc line. The line
 * must be inside a javadoc comment.
 *
 * @param document the document
 * @param line the line in document
 * @param scanner the scanner
 * @param partition the comment partition
 * @return the indent, or <code>null</code> if not computable
 * @throws BadLocationException
 */
private static String computeJavadocIndent(IDocument document, int line, JavaHeuristicScanner scanner, ITypedRegion partition) throws BadLocationException {
	if (line == 0) // impossible - the first line is never inside a javadoc comment
		return null;

	// don't make any assumptions if the line does not start with \s*\* - it might be
	// commented out code, for which we don't want to change the indent
	final IRegion lineInfo= document.getLineInformation(line);
	final int lineStart= lineInfo.getOffset();
	final int lineLength= lineInfo.getLength();
	final int lineEnd= lineStart + lineLength;
	int nonWS= scanner.findNonWhitespaceForwardInAnyPartition(lineStart, lineEnd);
	if (nonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(nonWS) != '*') {
		if (nonWS == JavaHeuristicScanner.NOT_FOUND)
			return document.get(lineStart, lineLength);
		return document.get(lineStart, nonWS - lineStart);
	}

	// take the indent from the previous line and reuse
	IRegion previousLine= document.getLineInformation(line - 1);
	int previousLineStart= previousLine.getOffset();
	int previousLineLength= previousLine.getLength();
	int previousLineEnd= previousLineStart + previousLineLength;

	StringBuffer buf= new StringBuffer();
	int previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd);
	if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(previousLineNonWS) != '*') {
		// align with the comment start if the previous line is not an asterix line
		previousLine= document.getLineInformationOfOffset(partition.getOffset());
		previousLineStart= previousLine.getOffset();
		previousLineLength= previousLine.getLength();
		previousLineEnd= previousLineStart + previousLineLength;
		previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd);
		if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND)
			previousLineNonWS= previousLineEnd;

		// add the initial space
		// TODO this may be controlled by a formatter preference in the future
		buf.append(' ');
	}

	String indentation= document.get(previousLineStart, previousLineNonWS - previousLineStart);
	buf.insert(0, indentation);
	return buf.toString();
}
 
Example 5
Source File: IndentAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes and returns the indentation for a javadoc line. The line
 * must be inside a javadoc comment.
 *
 * @param document the document
 * @param line the line in document
 * @param scanner the scanner
 * @param partition the javadoc partition
 * @return the indent, or <code>null</code> if not computable
 * @throws BadLocationException
 * @since 3.1
 */
private static String computeJavadocIndent(IDocument document, int line, JavaHeuristicScanner scanner, ITypedRegion partition) throws BadLocationException {
	if (line == 0) // impossible - the first line is never inside a javadoc comment
		return null;

	// don't make any assumptions if the line does not start with \s*\* - it might be
	// commented out code, for which we don't want to change the indent
	final IRegion lineInfo= document.getLineInformation(line);
	final int lineStart= lineInfo.getOffset();
	final int lineLength= lineInfo.getLength();
	final int lineEnd= lineStart + lineLength;
	int nonWS= scanner.findNonWhitespaceForwardInAnyPartition(lineStart, lineEnd);
	if (nonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(nonWS) != '*') {
		if (nonWS == JavaHeuristicScanner.NOT_FOUND)
			return document.get(lineStart, lineLength);
		return document.get(lineStart, nonWS - lineStart);
	}

	// take the indent from the previous line and reuse
	IRegion previousLine= document.getLineInformation(line - 1);
	int previousLineStart= previousLine.getOffset();
	int previousLineLength= previousLine.getLength();
	int previousLineEnd= previousLineStart + previousLineLength;

	StringBuffer buf= new StringBuffer();
	int previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd);
	if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(previousLineNonWS) != '*') {
		// align with the comment start if the previous line is not an asterisked line
		previousLine= document.getLineInformationOfOffset(partition.getOffset());
		previousLineStart= previousLine.getOffset();
		previousLineLength= previousLine.getLength();
		previousLineEnd= previousLineStart + previousLineLength;
		previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd);
		if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND)
			previousLineNonWS= previousLineEnd;

		// add the initial space
		// TODO this may be controlled by a formatter preference in the future
		buf.append(' ');
	}

	String indentation= document.get(previousLineStart, previousLineNonWS - previousLineStart);
	buf.insert(0, indentation);
	return buf.toString();
}
 
Example 6
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int getPeerPosition(IDocument document, DocumentCommand command) {
if (document.getLength() == 0)
	return 0;
  	/*
  	 * Search for scope closers in the pasted text and find their opening peers
  	 * in the document.
  	 */
  	Document pasted= new Document(command.text);
  	installJavaStuff(pasted);
  	int firstPeer= command.offset;

  	JavaHeuristicScanner pScanner= new JavaHeuristicScanner(pasted);
  	JavaHeuristicScanner dScanner= new JavaHeuristicScanner(document);

  	// add scope relevant after context to peer search
  	int afterToken= dScanner.nextToken(command.offset + command.length, JavaHeuristicScanner.UNBOUND);
  	try {
	switch (afterToken) {
	case Symbols.TokenRBRACE:
		pasted.replace(pasted.getLength(), 0, "}"); //$NON-NLS-1$
		break;
	case Symbols.TokenRPAREN:
		pasted.replace(pasted.getLength(), 0, ")"); //$NON-NLS-1$
		break;
	case Symbols.TokenRBRACKET:
		pasted.replace(pasted.getLength(), 0, "]"); //$NON-NLS-1$
		break;
	}
} catch (BadLocationException e) {
	// cannot happen
	Assert.isTrue(false);
}

  	int pPos= 0; // paste text position (increasing from 0)
  	int dPos= Math.max(0, command.offset - 1); // document position (decreasing from paste offset)
  	while (true) {
  		int token= pScanner.nextToken(pPos, JavaHeuristicScanner.UNBOUND);
 			pPos= pScanner.getPosition();
  		switch (token) {
  			case Symbols.TokenLBRACE:
  			case Symbols.TokenLBRACKET:
  			case Symbols.TokenLPAREN:
  				pPos= skipScope(pScanner, pPos, token);
  				if (pPos == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				break; // closed scope -> keep searching
  			case Symbols.TokenRBRACE:
  				int peer= dScanner.findOpeningPeer(dPos, '{', '}');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenRBRACKET:
  				peer= dScanner.findOpeningPeer(dPos, '[', ']');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenRPAREN:
  				peer= dScanner.findOpeningPeer(dPos, '(', ')');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenCASE:
  			case Symbols.TokenDEFAULT:
  				JavaIndenter indenter= new JavaIndenter(document, dScanner, fProject);
  				peer= indenter.findReferencePosition(dPos, false, false, false, true);
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching

  			case Symbols.TokenEOF:
  				return firstPeer;
  			default:
  				// keep searching
  		}
  	}
  }