Java Code Examples for org.eclipse.jdt.core.compiler.IScanner#getCurrentTokenStartPosition()
The following examples show how to use
org.eclipse.jdt.core.compiler.IScanner#getCurrentTokenStartPosition() .
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: TaskMarkerProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private int getSurroundingComment(IScanner scanner) { try { int start= fLocation.getOffset(); int end= start + fLocation.getLength(); int token= scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF) { if (TokenScanner.isComment(token)) { int currStart= scanner.getCurrentTokenStartPosition(); int currEnd= scanner.getCurrentTokenEndPosition() + 1; if (currStart <= start && end <= currEnd) { return token; } } token= scanner.getNextToken(); } } catch (InvalidInputException e) { // ignore } return ITerminalSymbols.TokenNameEOF; }
Example 2
Source File: SJavaTokenComparator.java From coming with MIT License | 5 votes |
/** * Creates a token comparator for the given string. * * @param text the text to be tokenized * @param textTokenComparatorFactory a factory to create text token comparators */ public SJavaTokenComparator(String text) { fText= text; int length= fText.length(); fStarts= new int[length]; fLengths= new int[length]; fTokens= new String[length]; fCount= 0; IScanner scanner= ToolFactory.createScanner(true, false, false, false); // returns comments & whitespace scanner.setSource(fText.toCharArray()); int endPos= 0; try { int tokenType; while ((tokenType= scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) { int start= scanner.getCurrentTokenStartPosition(); int end= scanner.getCurrentTokenEndPosition()+1; // Comments are treated as a single token (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=78063) if (!TokenScanner.isComment(tokenType)) { recordTokenRange(start, end - start); } endPos= end; } } catch (InvalidInputException ex) { // We couldn't parse part of the input. Fall through and make the rest a single token } // Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=13907 if (endPos < length) { recordTokenRange(endPos, length - endPos); } }
Example 3
Source File: NodeFinder.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * A visitor that maps a selection to a given ASTNode. The result node is * determined as follows: * <ul> * <li>first the visitor tries to find a node that is covered by <code>start</code> and * <code>length</code> where either <code>start</code> and <code>length</code> exactly * matches the node or where the text covered before and after the node only consists * of white spaces or comments.</li> * <li>if no such node exists than the node that encloses the range defined by * start and end is returned.</li> * <li>if the length is zero than also nodes are considered where the node's * start or end position matches <code>start</code>.</li> * <li>otherwise <code>null</code> is returned.</li> * </ul> * * @param root the root node from which the search starts * @param start the start offset * @param length the length * @param source the source of the compilation unit * * @return the result node * @throws JavaModelException if an error occurs in the Java model * * @since 3.0 */ public static ASTNode perform(ASTNode root, int start, int length, ITypeRoot source) throws JavaModelException { NodeFinder finder= new NodeFinder(start, length); root.accept(finder); ASTNode result= finder.getCoveredNode(); if (result == null) return null; Selection selection= Selection.createFromStartLength(start, length); if (selection.covers(result)) { IBuffer buffer= source.getBuffer(); if (buffer != null) { IScanner scanner= ToolFactory.createScanner(false, false, false, false); scanner.setSource(buffer.getText(start, length).toCharArray()); try { int token= scanner.getNextToken(); if (token != ITerminalSymbols.TokenNameEOF) { int tStart= scanner.getCurrentTokenStartPosition(); if (tStart == result.getStartPosition() - start) { scanner.resetTo(tStart + result.getLength(), length - 1); token= scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) return result; } } } catch (InvalidInputException e) { } } } return finder.getCoveringNode(); }
Example 4
Source File: GoToNextPreviousMemberAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static int firstOpeningBraceOffset(IInitializer iInitializer) throws JavaModelException { try { IScanner scanner= ToolFactory.createScanner(false, false, false, false); scanner.setSource(iInitializer.getSource().toCharArray()); int token= scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLBRACE) token= scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameLBRACE) return iInitializer.getSourceRange().getOffset() + scanner.getCurrentTokenStartPosition() + scanner.getRawTokenSource().length; return iInitializer.getSourceRange().getOffset(); } catch (InvalidInputException e) { return iInitializer.getSourceRange().getOffset(); } }
Example 5
Source File: JavaTokenComparator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Creates a token comparator for the given string. * * @param text the text to be tokenized */ public JavaTokenComparator(String text) { Assert.isLegal(text != null); fText= text; int length= fText.length(); fStarts= new int[length]; fLengths= new int[length]; fCount= 0; IScanner scanner= ToolFactory.createScanner(true, true, false, false); // returns comments & whitespace scanner.setSource(fText.toCharArray()); int endPos= 0; try { int tokenType; while ((tokenType= scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) { int start= scanner.getCurrentTokenStartPosition(); int end= scanner.getCurrentTokenEndPosition()+1; // Comments and strings should not be treated as a single token, see https://bugs.eclipse.org/78063 if (TokenScanner.isComment(tokenType) || tokenType == ITerminalSymbols.TokenNameStringLiteral) { // Line comments are often commented code, so lets treat them as code. See https://bugs.eclipse.org/216707 boolean parseAsJava= tokenType == ITerminalSymbols.TokenNameCOMMENT_LINE; int dl= parseAsJava ? getCommentStartTokenLength(tokenType) : 0; if (dl > 0) recordTokenRange(start, dl); parseSubrange(start + dl, text.substring(start + dl, end), parseAsJava); } else { recordTokenRange(start, end - start); } endPos= end; } } catch (InvalidInputException ex) { // We couldn't parse part of the input. Fall through and make the rest a single token } // Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=13907 if (endPos < length) { recordTokenRange(endPos, length - endPos); } }
Example 6
Source File: MethodOccurenceCollector.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException { if (match instanceof MethodReferenceMatch && ((MethodReferenceMatch) match).isSuperInvocation() && match.getAccuracy() == SearchMatch.A_INACCURATE) { return; // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=156491 } if (match.isImplicit()) { // see bug 94062 collectMatch(match); return; } int start= match.getOffset(); int length= match.getLength(); String matchText= unit.getBuffer().getText(start, length); //direct match: if (fName.equals(matchText)) { collectMatch(match); return; } // lambda expression if (match instanceof MethodDeclarationMatch && match.getElement() instanceof IMethod && ((IMethod) match.getElement()).isLambdaMethod()) { // don't touch the lambda return; } //Not a standard reference -- use scanner to find last identifier token before left parenthesis: IScanner scanner= getScanner(unit); scanner.setSource(matchText.toCharArray()); int simpleNameStart= -1; int simpleNameEnd= -1; try { int token = scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLPAREN) { // reference in code includes arguments in parentheses if (token == InternalTokenNameIdentifier) { simpleNameStart= scanner.getCurrentTokenStartPosition(); simpleNameEnd= scanner.getCurrentTokenEndPosition(); } token = scanner.getNextToken(); } } catch (InvalidInputException e){ //ignore } if (simpleNameStart != -1) { match.setOffset(start + simpleNameStart); match.setLength(simpleNameEnd + 1 - simpleNameStart); } collectMatch(match); }
Example 7
Source File: TypeOccurrenceCollector.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
public SearchMatch acceptSearchMatch2(ICompilationUnit unit, SearchMatch match) throws CoreException { int start= match.getOffset(); int length= match.getLength(); //unqualified: String matchText= unit.getBuffer().getText(start, length); if (fOldName.equals(matchText)) { return match; } //(partially) qualified: if (fOldQualifiedName.endsWith(matchText)) { //e.g. rename B and p.A.B ends with match A.B int simpleNameLenght= fOldName.length(); match.setOffset(start + length - simpleNameLenght); match.setLength(simpleNameLenght); return match; } //Not a standard reference -- use scanner to find last identifier token: IScanner scanner= getScanner(unit); scanner.setSource(matchText.toCharArray()); int simpleNameStart= -1; int simpleNameEnd= -1; try { int token = scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF) { if (token == ITerminalSymbols.TokenNameIdentifier) { // type reference can occur in module-info.java and collide with a restricted keyword. simpleNameStart= scanner.getCurrentTokenStartPosition(); simpleNameEnd= scanner.getCurrentTokenEndPosition(); } token = scanner.getNextToken(); } } catch (InvalidInputException e){ //ignore } if (simpleNameStart != -1) { match.setOffset(start + simpleNameStart); match.setLength(simpleNameEnd + 1 - simpleNameStart); } return match; }
Example 8
Source File: FoldingRangeHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IProgressMonitor monitor) { try { ISourceRange range = unit.getSourceRange(); if (!SourceRange.isAvailable(range)) { return; } String contents = unit.getSource(); if (StringUtils.isBlank(contents)) { return; } final int shift = range.getOffset(); IScanner scanner = getScanner(); scanner.setSource(contents.toCharArray()); scanner.resetTo(shift, shift + range.getLength()); int start = shift; int token = 0; Stack<Integer> regionStarts = new Stack<>(); while (token != ITerminalSymbols.TokenNameEOF) { start = scanner.getCurrentTokenStartPosition(); switch (token) { case ITerminalSymbols.TokenNameCOMMENT_JAVADOC: case ITerminalSymbols.TokenNameCOMMENT_BLOCK: int end = scanner.getCurrentTokenEndPosition(); FoldingRange commentFoldingRange = new FoldingRange(scanner.getLineNumber(start) - 1, scanner.getLineNumber(end) - 1); commentFoldingRange.setKind(FoldingRangeKind.Comment); foldingRanges.add(commentFoldingRange); break; case ITerminalSymbols.TokenNameCOMMENT_LINE: String currentSource = String.valueOf(scanner.getCurrentTokenSource()); if (REGION_START_PATTERN.matcher(currentSource).lookingAt()) { regionStarts.push(start); } else if (REGION_END_PATTERN.matcher(currentSource).lookingAt()) { if (regionStarts.size() > 0) { FoldingRange regionFolding = new FoldingRange(scanner.getLineNumber(regionStarts.pop()) - 1, scanner.getLineNumber(start) - 1); regionFolding.setKind(FoldingRangeKind.Region); foldingRanges.add(regionFolding); } } break; default: break; } token = getNextToken(scanner); } computeTypeRootRanges(foldingRanges, unit, scanner); } catch (CoreException e) { JavaLanguageServerPlugin.logException("Problem with folding range for " + unit.getPath().toPortableString(), e); monitor.setCanceled(true); } }
Example 9
Source File: MethodOccurenceCollector.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException { if (match instanceof MethodReferenceMatch && ((MethodReferenceMatch) match).isSuperInvocation() && match.getAccuracy() == SearchMatch.A_INACCURATE) { return; // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=156491 } if (match.isImplicit()) { // see bug 94062 collectMatch(match); return; } int start= match.getOffset(); int length= match.getLength(); String matchText= unit.getBuffer().getText(start, length); //direct match: if (fName.equals(matchText)) { collectMatch(match); return; } // lambda expression if (match instanceof MethodDeclarationMatch && match.getElement() instanceof IMethod && ((IMethod) match.getElement()).isLambdaMethod()) { // don't touch the lambda return; } //Not a standard reference -- use scanner to find last identifier token before left parenthesis: IScanner scanner= getScanner(unit); scanner.setSource(matchText.toCharArray()); int simpleNameStart= -1; int simpleNameEnd= -1; try { int token = scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLPAREN) { // reference in code includes arguments in parentheses if (token == ITerminalSymbols.TokenNameIdentifier) { simpleNameStart= scanner.getCurrentTokenStartPosition(); simpleNameEnd= scanner.getCurrentTokenEndPosition(); } token = scanner.getNextToken(); } } catch (InvalidInputException e){ //ignore } if (simpleNameStart != -1) { match.setOffset(start + simpleNameStart); match.setLength(simpleNameEnd + 1 - simpleNameStart); } collectMatch(match); }
Example 10
Source File: TypeOccurrenceCollector.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public SearchMatch acceptSearchMatch2(ICompilationUnit unit, SearchMatch match) throws CoreException { int start= match.getOffset(); int length= match.getLength(); //unqualified: String matchText= unit.getBuffer().getText(start, length); if (fOldName.equals(matchText)) { return match; } //(partially) qualified: if (fOldQualifiedName.endsWith(matchText)) { //e.g. rename B and p.A.B ends with match A.B int simpleNameLenght= fOldName.length(); match.setOffset(start + length - simpleNameLenght); match.setLength(simpleNameLenght); return match; } //Not a standard reference -- use scanner to find last identifier token: IScanner scanner= getScanner(unit); scanner.setSource(matchText.toCharArray()); int simpleNameStart= -1; int simpleNameEnd= -1; try { int token = scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF) { if (token == ITerminalSymbols.TokenNameIdentifier) { simpleNameStart= scanner.getCurrentTokenStartPosition(); simpleNameEnd= scanner.getCurrentTokenEndPosition(); } token = scanner.getNextToken(); } } catch (InvalidInputException e){ //ignore } if (simpleNameStart != -1) { match.setOffset(start + simpleNameStart); match.setLength(simpleNameEnd + 1 - simpleNameStart); } return match; }
Example 11
Source File: CommentAnalyzer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private boolean checkStart(IScanner scanner, int position) { return scanner.getCurrentTokenStartPosition() < position && position <= scanner.getCurrentTokenEndPosition(); }
Example 12
Source File: CommentAnalyzer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private boolean checkEnd(IScanner scanner, int position) { return scanner.getCurrentTokenStartPosition() <= position && position < scanner.getCurrentTokenEndPosition(); }
Example 13
Source File: NLSSearchResultRequestor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Finds the key defined by the given match. The assumption is that the key is the only argument * and it is a string literal i.e. quoted ("...") or a string constant i.e. 'static final * String' defined in the same class. * * @param keyPositionResult reference parameter: will be filled with the position of the found * key * @param enclosingElement enclosing java element * @return a string denoting the key, {@link #NO_KEY} if no key can be found and * <code>null</code> otherwise * @throws CoreException if a problem occurs while accessing the <code>enclosingElement</code> */ private String findKey(Position keyPositionResult, IJavaElement enclosingElement) throws CoreException { ICompilationUnit unit= (ICompilationUnit)enclosingElement.getAncestor(IJavaElement.COMPILATION_UNIT); if (unit == null) return null; String source= unit.getSource(); if (source == null) return null; IJavaProject javaProject= unit.getJavaProject(); IScanner scanner= null; if (javaProject != null) { String complianceLevel= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true); String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true); scanner= ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel); } else { scanner= ToolFactory.createScanner(false, false, false, false); } scanner.setSource(source.toCharArray()); scanner.resetTo(keyPositionResult.getOffset() + keyPositionResult.getLength(), source.length()); try { if (scanner.getNextToken() != ITerminalSymbols.TokenNameDOT) return null; if (scanner.getNextToken() != ITerminalSymbols.TokenNameIdentifier) return null; String src= new String(scanner.getCurrentTokenSource()); int tokenStart= scanner.getCurrentTokenStartPosition(); int tokenEnd= scanner.getCurrentTokenEndPosition(); if (scanner.getNextToken() == ITerminalSymbols.TokenNameLPAREN) { // Old school // next must be key string. Ignore methods which do not take a single String parameter (Bug 295040). int nextToken= scanner.getNextToken(); if (nextToken != ITerminalSymbols.TokenNameStringLiteral && nextToken != ITerminalSymbols.TokenNameIdentifier) return null; tokenStart= scanner.getCurrentTokenStartPosition(); tokenEnd= scanner.getCurrentTokenEndPosition(); int token; while ((token= scanner.getNextToken()) == ITerminalSymbols.TokenNameDOT) { if ((nextToken= scanner.getNextToken()) != ITerminalSymbols.TokenNameIdentifier) { return null; } tokenStart= scanner.getCurrentTokenStartPosition(); tokenEnd= scanner.getCurrentTokenEndPosition(); } if (token != ITerminalSymbols.TokenNameRPAREN) return null; if (nextToken == ITerminalSymbols.TokenNameStringLiteral) { keyPositionResult.setOffset(tokenStart + 1); keyPositionResult.setLength(tokenEnd - tokenStart - 1); return source.substring(tokenStart + 1, tokenEnd); } else if (nextToken == ITerminalSymbols.TokenNameIdentifier) { keyPositionResult.setOffset(tokenStart); keyPositionResult.setLength(tokenEnd - tokenStart + 1); IType parentClass= (IType)enclosingElement.getAncestor(IJavaElement.TYPE); IField[] fields= parentClass.getFields(); String identifier= source.substring(tokenStart, tokenEnd + 1); for (int i= 0; i < fields.length; i++) { if (fields[i].getElementName().equals(identifier)) { if (!Signature.getSignatureSimpleName(fields[i].getTypeSignature()).equals("String")) //$NON-NLS-1$ return null; Object obj= fields[i].getConstant(); return obj instanceof String ? ((String)obj).substring(1, ((String)obj).length() - 1) : NO_KEY; } } } return NO_KEY; } else { IJavaElement[] keys= unit.codeSelect(tokenStart, tokenEnd - tokenStart + 1); // an interface can't be a key if (keys.length == 1 && keys[0].getElementType() == IJavaElement.TYPE && ((IType) keys[0]).isInterface()) return null; keyPositionResult.setOffset(tokenStart); keyPositionResult.setLength(tokenEnd - tokenStart + 1); return src; } } catch (InvalidInputException e) { throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); } }
Example 14
Source File: DefaultJavaFoldingStructureProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private IRegion computeHeaderComment(FoldingStructureComputationContext ctx) throws JavaModelException { // search at most up to the first type ISourceRange range= ctx.getFirstType().getSourceRange(); if (range == null) return null; int start= 0; int end= range.getOffset(); /* code adapted from CommentFormattingStrategy: * scan the header content up to the first type. Once a comment is * found, accumulate any additional comments up to the stop condition. * The stop condition is reaching a package declaration, import container, * or the end of the input. */ IScanner scanner= ctx.getScanner(); scanner.resetTo(start, end); int headerStart= -1; int headerEnd= -1; try { boolean foundComment= false; int terminal= scanner.getNextToken(); while (terminal != ITerminalSymbols.TokenNameEOF && !(terminal == ITerminalSymbols.TokenNameclass || terminal == ITerminalSymbols.TokenNameinterface || terminal == ITerminalSymbols.TokenNameenum || (foundComment && (terminal == ITerminalSymbols.TokenNameimport || terminal == ITerminalSymbols.TokenNamepackage)))) { if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK || terminal == ITerminalSymbols.TokenNameCOMMENT_LINE) { if (!foundComment) headerStart= scanner.getCurrentTokenStartPosition(); headerEnd= scanner.getCurrentTokenEndPosition(); foundComment= true; } terminal= scanner.getNextToken(); } } catch (InvalidInputException ex) { return null; } if (headerEnd != -1) { return new Region(headerStart, headerEnd - headerStart); } return null; }
Example 15
Source File: Member.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public ISourceRange getJavadocRange() throws JavaModelException { ISourceRange range= getSourceRange(); if (range == null) return null; IBuffer buf= null; if (isBinary()) { buf = getClassFile().getBuffer(); } else { ICompilationUnit compilationUnit = getCompilationUnit(); if (!compilationUnit.isConsistent()) { return null; } buf = compilationUnit.getBuffer(); } final int start= range.getOffset(); final int length= range.getLength(); if (length > 0 && buf.getChar(start) == '/') { IScanner scanner= ToolFactory.createScanner(true, false, false, false); try { scanner.setSource(buf.getText(start, length).toCharArray()); int docOffset= -1; int docEnd= -1; int terminal= scanner.getNextToken(); loop: while (true) { switch(terminal) { case ITerminalSymbols.TokenNameCOMMENT_JAVADOC : docOffset= scanner.getCurrentTokenStartPosition(); docEnd= scanner.getCurrentTokenEndPosition() + 1; terminal= scanner.getNextToken(); break; case ITerminalSymbols.TokenNameCOMMENT_LINE : case ITerminalSymbols.TokenNameCOMMENT_BLOCK : terminal= scanner.getNextToken(); continue loop; default : break loop; } } if (docOffset != -1) { return new SourceRange(docOffset + start, docEnd - docOffset); } } catch (InvalidInputException ex) { // try if there is inherited Javadoc } catch (IndexOutOfBoundsException e) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=305001 } } return null; }