com.github.javaparser.Position Java Examples

The following examples show how to use com.github.javaparser.Position. 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: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
            }
        }
    }
}
 
Example #2
Source File: LocationSearcher.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
@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;
  }
}
 
Example #3
Source File: RegionMapper.java    From JRemapper with MIT License 5 votes vote down vote up
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;
}
 
Example #4
Source File: JavaRefactoring.java    From gauge-java with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #5
Source File: JavaRefactoring.java    From gauge-java with Apache License 2.0 4 votes vote down vote up
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));
}
 
Example #6
Source File: RefactoringHelper.java    From Refactoring-Bot with MIT License 2 votes vote down vote up
/**
 * @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);
}
 
Example #7
Source File: RefactoringHelper.java    From Refactoring-Bot with MIT License 2 votes vote down vote up
/**
 * @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);
}