下面列出了org.eclipse.jdt.core.dom.CompilationUnit#getPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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);
}
}
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);
}
};
}
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;
}
/**
* 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;
}
}