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

The following examples show how to use org.eclipse.jdt.core.dom.CompilationUnit#getPosition() . 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: OccurrencesSearchQuery.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private JavaElementLine getLineElement(CompilationUnit astRoot, OccurrenceLocation location, HashMap<Integer, JavaElementLine> lineToGroup) {
	int lineNumber= astRoot.getLineNumber(location.getOffset());
	if (lineNumber <= 0) {
		return null;
	}
	JavaElementLine lineElement= null;
	try {
		Integer key= new Integer(lineNumber);
		lineElement= lineToGroup.get(key);
		if (lineElement == null) {
			int lineStartOffset= astRoot.getPosition(lineNumber, 0);
			if (lineStartOffset >= 0) {
				lineElement= new JavaElementLine(astRoot.getTypeRoot(), lineNumber - 1, lineStartOffset);
				lineToGroup.put(key, lineElement);
			}
		}
	} catch (CoreException e) {
		//nothing
	}
	return lineElement;
}
 
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: LineInformation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static LineInformation create(final CompilationUnit astRoot) {
	return new LineInformation() {
		public int getLineOfOffset(int offset) {
			return astRoot.getLineNumber(offset) - 1;
		}
		public int getLineOffset(int line) {
			return astRoot.getPosition(line + 1, 0);
		}
	};
}
 
Example 4
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getPackageStatementEndPos(CompilationUnit root) {
	PackageDeclaration packDecl= root.getPackage();
	if (packDecl != null) {
		int afterPackageStatementPos= -1;
		int lineNumber= root.getLineNumber(packDecl.getStartPosition() + packDecl.getLength());
		if (lineNumber >= 0) {
			int lineAfterPackage= lineNumber + 1;
			afterPackageStatementPos= root.getPosition(lineAfterPackage, 0);
		}
		if (afterPackageStatementPos < 0) {
			this.flags|= F_NEEDS_LEADING_DELIM;
			return packDecl.getStartPosition() + packDecl.getLength();
		}
		int firstTypePos= getFirstTypeBeginPos(root);
		if (firstTypePos != -1 && firstTypePos <= afterPackageStatementPos) {
			this.flags|= F_NEEDS_TRAILING_DELIM;
			if (firstTypePos == afterPackageStatementPos) {
				this.flags|= F_NEEDS_LEADING_DELIM;
			}
			return firstTypePos;
		}
		this.flags|= F_NEEDS_LEADING_DELIM;
		return afterPackageStatementPos; // insert a line after after package statement
	}
	this.flags |= F_NEEDS_TRAILING_DELIM;
	return 0;
}
 
Example 5
Source File: SourceCopier.java    From juniversal with MIT License 5 votes vote down vote up
/**
 * Skips all whitespace, newlines, and comments from the source starting at startPosition and continuing backwards
 * until the beginning of the whitespace/newlines/comments. The returned position points to the beginning of the
 * whitespace/newlines/comments or the original position if there wasn't any whitespace/newlines/comments.
 *
 * @param startPosition starting position in source
 * @return position of first character in sequence of space and comments
 */
public int skipSpaceAndCommentsBackward(int startPosition) {
    int position = startPosition - 1;

    while (true) {
        int currChar = getSourceCharAtForBackward(position);

        if (currChar == -1)
            return position + 1;
        else if (currChar == ' ' || currChar == '\t' || currChar == '\r')
            --position;
        else if (currChar == '\n') {
            CompilationUnit compilationUnit = sourceFile.getCompilationUnit();
            int lineStartPosition = compilationUnit.getPosition(compilationUnit.getLineNumber(position), 0);
            int lineCommentStartPosition = getLineCommentStartPosition(lineStartPosition);

            if (lineCommentStartPosition != -1)
                position = lineCommentStartPosition - 1;
            else
                --position;
        } else if (currChar == '/' && getSourceCharAtForBackward(position - 1) == '*') {
            position -= 2;

            while (true) {
                currChar = getSourceCharAtForBackward(position);

                if (currChar == -1)
                    break;
                else if (currChar == '*' && getSourceCharAtForBackward(position - 1) == '/') {
                    position -= 2;
                    break;
                } else --position;
            }
        } else return position + 1;
    }
}