Java源码示例:com.github.javaparser.Position
示例1
protected static void addBlankImports(List<ImportDeclaration> imports, CompilationUnit nowCompilationUnit) {
String lastStartWith = null;
int size = imports.size();
for (int i = size - 1; i >= 0; i--) {
ImportDeclaration importDeclaration = imports.get(i);
String importName = importDeclaration.getName().toString();
int idx = importName.indexOf('.');
if (idx > 0) {
String nowStrartWith = importName.substring(0, idx + 1);
if (lastStartWith != null && !lastStartWith.equals(nowStrartWith)) {
Range range = new Range(Position.pos(0, 0), Position.pos(0, 0));
ImportDeclaration emptyDeclaration = new ImportDeclaration(range, new Name(), false, false);
imports.add(i + 1, emptyDeclaration);
lastStartWith = null;
} else {
lastStartWith = nowStrartWith;
}
}
}
}
示例2
@SuppressWarnings("try")
private static Location getFieldLocation(
SearchContext context, File targetFile, FieldDeclaration declaration) throws IOException {
try (TelemetryUtils.ScopedSpan scope =
TelemetryUtils.startScopedSpan("LocationSearcher.getFieldLocation")) {
TelemetryUtils.ScopedSpan.addAnnotation(
TelemetryUtils.annotationBuilder().put("targetFile", targetFile.getPath()).build("args"));
final List<VariableDeclarator> variables = declaration.getVariables();
for (final VariableDeclarator variable : variables) {
final SimpleName simpleName = variable.getName();
final String name = simpleName.getIdentifier();
final Optional<Position> begin = simpleName.getBegin();
if (name.equals(context.name) && begin.isPresent()) {
final Position position = begin.get();
return new Location(targetFile.getCanonicalPath(), position.line, position.column);
}
}
return null;
}
}
示例3
private Node getNodeAt(int line, int column, Node root, boolean allowSimple) {
// We want to know more about this type, don't resolve down to the lowest AST
// type... the parent has more data and is essentially just a wrapper around SimpleName.
if (!allowSimple && root instanceof SimpleName)
return null;
// Verify the node range can be accessed
if (!root.getBegin().isPresent() || !root.getEnd().isPresent())
return null;
// Check cursor is in bounds
// We won't instantly return null because the root range may be SMALLER than
// the range of children. This is really stupid IMO but thats how JavaParser is...
boolean bounds = true;
Position cursor = Position.pos(line, column);
if (cursor.isBefore(root.getBegin().get()) || cursor.isAfter(root.getEnd().get()))
bounds = false;
// Iterate over children, return non-null child
for (Node child : root.getChildNodes()) {
Node ret = getNodeAt(line, column, child, allowSimple);
if (ret != null)
return ret;
}
// If we're not in bounds and none of our children are THEN we assume this node is bad.
if (!bounds)
return null;
// In bounds so we're good!
return root;
}
示例4
private Range getParamsRange() {
List<Parameter> parameters = registry.get(oldStepValue.getStepText()).getParameters();
if (parameters.isEmpty()) {
int line = registry.get(oldStepValue.getStepText()).getSpan().begin.line + 1;
int column = registry.get(oldStepValue.getStepText()).getName().length();
return new Range(new Position(line, column), new Position(line, column));
}
Range firstParam = parameters.get(0).getRange().get();
Range lastParam = parameters.get(parameters.size() - 1).getRange().get();
return new Range(new Position(firstParam.begin.line, firstParam.begin.column - 1), new Position(lastParam.end.line, lastParam.end.column));
}
示例5
private Range getStepRange(Range range) {
return new Range(new Position(range.begin.line, range.begin.column - 1), new Position(range.end.line, range.end.column));
}
示例6
/**
* @param methodDeclaration
* @param lineNumber
* @return true if given method starts at given line, false otherwise
*/
public static boolean isMethodDeclarationAtLine(MethodDeclaration methodDeclaration, Integer lineNumber) {
Optional<Position> beginPositionOfName = methodDeclaration.getName().getBegin();
return (beginPositionOfName.isPresent() && lineNumber == beginPositionOfName.get().line);
}
示例7
/**
* @param fieldDeclaration
* @param lineNumber
* @return true if given field starts at given line, false otherwise
*/
public static boolean isFieldDeclarationAtLine(FieldDeclaration fieldDeclaration, Integer lineNumber) {
Optional<Position> beginPositionOfField = fieldDeclaration.getBegin();
return (beginPositionOfField.isPresent() && beginPositionOfField.get().line == lineNumber);
}