Java Code Examples for org.eclipse.jdt.core.dom.CompilationUnit#getExtendedLength()

The following examples show how to use org.eclipse.jdt.core.dom.CompilationUnit#getExtendedLength() . 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: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the source of the given node from the location where it was parsed.
 * @param node the node to get the source from
 * @param extendedRange if set, the extended ranges of the nodes should ne used
 * @param removeIndent if set, the indentation is removed.
 * @return return the source for the given node or null if accessing the source failed.
 */
public static String getNodeSource(ASTNode node, boolean extendedRange, boolean removeIndent) {
	ASTNode root= node.getRoot();
	if (root instanceof CompilationUnit) {
		CompilationUnit astRoot= (CompilationUnit) root;
		ITypeRoot typeRoot= astRoot.getTypeRoot();
		try {
			if (typeRoot != null && typeRoot.getBuffer() != null) {
				IBuffer buffer= typeRoot.getBuffer();
				int offset= extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition();
				int length= extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
				String str= buffer.getText(offset, length);
				if (removeIndent) {
					IJavaProject project= typeRoot.getJavaProject();
					int indent= StubUtility.getIndentUsed(buffer, node.getStartPosition(), project);
					str= Strings.changeIndent(str, indent, project, new String(), typeRoot.findRecommendedLineSeparator());
				}
				return str;
			}
		} catch (JavaModelException e) {
			// ignore
		}
	}
	return null;
}
 
Example 2
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IRegion evaluateReplaceRange(CompilationUnit root) {
	List imports= root.imports();
	if (!imports.isEmpty()) {
		ImportDeclaration first= (ImportDeclaration) imports.get(0);
		ImportDeclaration last= (ImportDeclaration) imports.get(imports.size() - 1);

		int startPos= first.getStartPosition(); // no extended range for first: bug 121428
		int endPos= root.getExtendedStartPosition(last) + root.getExtendedLength(last);
		int endLine= root.getLineNumber(endPos);
		if (endLine > 0) {
			int nextLinePos= root.getPosition(endLine + 1, 0);
			if (nextLinePos >= 0) {
				int firstTypePos= getFirstTypeBeginPos(root);
				if (firstTypePos != -1 && firstTypePos < nextLinePos) {
					endPos= firstTypePos;
				} else {
					endPos= nextLinePos;
				}
			}
		}
		return new Region(startPos, endPos - startPos);
	} else {
		int start= getPackageStatementEndPos(root);
		return new Region(start, 0);
	}
}
 
Example 3
Source File: TargetSourceRangeComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns the target source range of the given node. Unlike
 * {@link ASTNode#getStartPosition()} and {@link ASTNode#getLength()},
 * the extended source range may include comments and whitespace
 * immediately before or after the normal source range for the node.
 * <p>
 * The returned source ranges must satisfy the following conditions:
 * <dl>
 * <li>no two source ranges in an AST may be overlapping</li>
 * <li>a source range of a parent node must fully cover the source ranges of its children</li>
 * 	</dl>
 * 	</p>
 * <p>
 * The default implementation uses
 * {@link CompilationUnit#getExtendedStartPosition(ASTNode)}
 * and {@link CompilationUnit#getExtendedLength(ASTNode)}
 * to compute the target source range. Clients may override or
 * extend this method to expand or contract the source range of the
 * given node. The resulting source range must cover at least the
 * original source range of the node.
 * </p>
 *
 * @param node the node with a known source range in the compilation unit
 * being rewritten
 * @return the exact source range in the compilation unit being rewritten
 * that should be replaced (or deleted)
 */
public SourceRange computeSourceRange(ASTNode node) {
	ASTNode root= node.getRoot();
	if (root instanceof CompilationUnit) {
		CompilationUnit cu= (CompilationUnit) root;
		return new SourceRange(cu.getExtendedStartPosition(node), cu.getExtendedLength(node));
	}
	return new SourceRange(node.getStartPosition(), node.getLength());
}