List of usage examples for org.eclipse.jdt.core.dom ASTParser setSourceRange
public void setSourceRange(int offset, int length)
From source file:com.bsiag.eclipse.jdt.java.formatter.DefaultCodeFormatter.java
License:Open Source License
private TextEdit formatComments(String source, int kind, IRegion[] regions) { MultiTextEdit result = new MultiTextEdit(); if (!init(source)) return result; CommentsPreparator commentsPreparator = new CommentsPreparator(this.tokenManager, this.workingOptions, this.sourceLevel); CommentWrapExecutor commentWrapper = new CommentWrapExecutor(this.tokenManager, this.workingOptions); switch (kind) { case K_JAVA_DOC: ASTParser parser = ASTParser.newParser(AST.JLS8); for (Token token : this.tokens) { if (token.tokenType == TokenNameCOMMENT_JAVADOC) { parser.setSourceRange(token.originalStart, token.countChars()); CompilationUnit cu = (CompilationUnit) parseSourceCode(parser, ASTParser.K_COMPILATION_UNIT, true);//from ww w .j av a 2 s . co m Javadoc javadoc = (Javadoc) cu.getCommentList().get(0); javadoc.accept(commentsPreparator); int startPosition = this.tokenManager.findSourcePositionInLine(token.originalStart); commentWrapper.wrapMultiLineComment(token, startPosition, false, false); } } break; case K_MULTI_LINE_COMMENT: for (int i = 0; i < this.tokens.size(); i++) { Token token = this.tokens.get(i); if (token.tokenType == TokenNameCOMMENT_BLOCK) { commentsPreparator.handleBlockComment(i); int startPosition = this.tokenManager.findSourcePositionInLine(token.originalStart); commentWrapper.wrapMultiLineComment(token, startPosition, false, false); } } break; case K_SINGLE_LINE_COMMENT: for (int i = 0; i < this.tokens.size(); i++) { Token token = this.tokens.get(i); if (token.tokenType == TokenNameCOMMENT_LINE) { commentsPreparator.handleLineComment(i); if (i >= this.tokens.size() || this.tokens.get(i) != token) { // current token has been removed and merged with previous one i--; token = this.tokens.get(i); } int startPosition = this.tokenManager.findSourcePositionInLine(token.originalStart); commentWrapper.wrapLineComment(token, startPosition); } } break; default: throw new AssertionError(String.valueOf(kind)); } this.tokenManager.applyFormatOff(); TextEditsBuilder resultBuilder = new TextEditsBuilder(source, regions, this.tokenManager, this.workingOptions); resultBuilder.setAlignChar(DefaultCodeFormatterOptions.SPACE); for (Token token : this.tokens) { List<Token> structure = token.getInternalStructure(); if (structure != null && !structure.isEmpty()) resultBuilder.processComment(token); } for (TextEdit edit : resultBuilder.getEdits()) { result.addChild(edit); } return result; }
From source file:org.eclipse.pde.api.tools.internal.builder.AbstractProblemDetector.java
License:Open Source License
/** * Returns the enclosing {@link IMethod} for the given type or * <code>null</code> if it cannot be computed * //from ww w . j av a2 s . co m * @param type * @param jtype * @param reference * @param document * @return the {@link IMethod} enclosing the given type or <code>null</code> * @throws CoreException */ protected IMethod getEnclosingMethod(final IType jtype, IReference reference, IDocument document) throws CoreException { IApiMember member = reference.getMember(); if ((member.getType() == IApiElement.TYPE)) { ApiType type = (ApiType) member; IApiMethod apimethod = type.getEnclosingMethod(); if (apimethod != null) { String signature = Signatures.processMethodSignature(apimethod); String methodname = Signatures.getMethodName(apimethod); IMethod method = jtype.getMethod(methodname, Signature.getParameterTypes(signature)); if (method.exists()) { return method; } } else { // try to look it up IMethod method = null; if (reference.getLineNumber() > -1) { try { int offset = document.getLineOffset(reference.getLineNumber()); method = quickLookup(jtype, document, reference, offset); } catch (BadLocationException ble) { } } if (method == null) { // look it up the hard way ISourceRange range = jtype.getCompilationUnit().getSourceRange(); ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setSource(jtype.getCompilationUnit()); parser.setSourceRange(range.getOffset(), range.getLength()); parser.setResolveBindings(true); ASTNode ptype = parser.createAST(null); MethodFinder finder = new MethodFinder(type, jtype); ptype.accept(finder); method = finder.method; } if (method != null && method.exists()) { ApiType etype = (ApiType) type.getEnclosingType(); IApiMethod[] methods = etype.getMethods(); String msig = null; for (int i = 0; i < methods.length; i++) { msig = methods[i].getSignature(); if (Signatures.getMethodName(methods[i]).equals(method.getElementName()) && Signatures.matchesSignatures(msig.replace('/', '.'), method.getSignature())) { type.setEnclosingMethodInfo(methods[i].getName(), msig); } } return method; } } } return null; }
From source file:org.eclipse.pde.api.tools.internal.builder.IllegalExtendsProblemDetector.java
License:Open Source License
/** * Returns the enclosing {@link IMethod} for the given type or <code>null</code> * if it cannot be computed/*from w w w.j av a 2 s .c om*/ * @param type * @param jtype * @param reference * @param document * @return the {@link IMethod} enclosing the given type or <code>null</code> * @throws CoreException */ private IMethod getEnclosingMethod(final IType jtype, IReference reference, IDocument document) throws CoreException { ApiType type = (ApiType) reference.getMember(); IApiMethod apimethod = type.getEnclosingMethod(); if (apimethod != null) { String signature = Signatures.processMethodSignature(apimethod); String methodname = Signatures.getMethodName(apimethod); IMethod method = jtype.getMethod(methodname, Signature.getParameterTypes(signature)); if (method.exists()) { return method; } } else { //try to look it up IMethod method = null; if (reference.getLineNumber() > -1) { try { int offset = document.getLineOffset(reference.getLineNumber()); method = quickLookup(jtype, document, reference, offset); } catch (BadLocationException ble) { } } if (method == null) { //look it up the hard way ISourceRange range = jtype.getCompilationUnit().getSourceRange(); ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setSource(jtype.getCompilationUnit()); parser.setSourceRange(range.getOffset(), range.getLength()); parser.setResolveBindings(true); ASTNode ptype = parser.createAST(null); MethodFinder finder = new MethodFinder(type, jtype); ptype.accept(finder); method = finder.method; } if (method != null && method.exists()) { ApiType etype = (ApiType) type.getEnclosingType(); IApiMethod[] methods = etype.getMethods(); String msig = null; for (int i = 0; i < methods.length; i++) { msig = methods[i].getSignature(); if (Signatures.getMethodName(methods[i]).equals(method.getElementName()) && Signatures.matchesSignatures(msig.replace('/', '.'), method.getSignature())) { type.setEnclosingMethodInfo(methods[i].getName(), msig); } } return method; } } return null; }
From source file:org.key_project.util.jdt.JDTUtil.java
License:Open Source License
/** * Parses the given {@link ICompilationUnit} in the specified range into an AST. * @param compilationUnit The {@link ICompilationUnit} to parse. * @param offset The start index in the text to parse. * @param length The length of the text to parse. * @return The {@link ASTNode} which is the root of the AST. *//*from w w w . j av a 2 s.c o m*/ public static ASTNode parse(ICompilationUnit compilationUnit, int offset, int length) { ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); // Hopefully always the newest AST level (e.g. AST.JLS4) parser.setSource(compilationUnit); parser.setSourceRange(offset, length); return parser.createAST(null); }