Java Code Examples for org.eclipse.jdt.core.compiler.IScanner#getCurrentTokenEndPosition()
The following examples show how to use
org.eclipse.jdt.core.compiler.IScanner#getCurrentTokenEndPosition() .
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: MarkerUtil.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
/** * @param source * must be not null * @param range * can be null * @return may return null, otherwise an initialized scanner which may * answer which source offset index belongs to which source line * @throws JavaModelException */ private static IScanner initScanner(IType source, ISourceRange range) throws JavaModelException { if (range == null) { return null; } char[] charContent = getContent(source); if (charContent == null) { return null; } IScanner scanner = ToolFactory.createScanner(false, false, false, true); scanner.setSource(charContent); int offset = range.getOffset(); try { while (scanner.getNextToken() != ITerminalSymbols.TokenNameEOF) { // do nothing, just wait for the end of stream if (offset <= scanner.getCurrentTokenEndPosition()) { break; } } } catch (InvalidInputException e) { FindbugsPlugin.getDefault().logException(e, "Could not init scanner for type: " + source); } return scanner; }
Example 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
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; }