Class SourcePositionService

java.lang.Object
org.openrewrite.java.service.SourcePositionService

@Incubating(since="8.63.0") public class SourcePositionService extends Object
Service for computing source code position metrics such as column alignment positions and tree element lengths.

This service is useful for formatting and layout calculations, particularly when determining how to align elements in chained method calls, multi-line variable declarations, and method parameters.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    columnsOf(org.openrewrite.Cursor cursor)
    Computes the position span of the element at the given cursor.
    columnsOf(org.openrewrite.Cursor cursor, org.openrewrite.java.tree.J child)
    Computes the position span of a J element.
    columnsOf(org.openrewrite.Cursor cursor, JContainer<? extends org.openrewrite.java.tree.J> container)
    Computes the position span of a container element.
    columnsOf(org.openrewrite.Cursor cursor, JRightPadded<org.openrewrite.java.tree.J> rightPadded)
    Computes the position span of a right-padded element.
    org.openrewrite.Cursor
    computeNewLinedCursorElement(org.openrewrite.Cursor cursor)
    Finds the ancestor element in the cursor path that has a newline in its prefix.
    positionOf(org.openrewrite.Cursor cursor)
    Computes the position span of the element at the given cursor.
    positionOf(org.openrewrite.Cursor cursor, org.openrewrite.java.tree.J child)
    Computes the position span of a J element.
    positionOf(org.openrewrite.Cursor cursor, JContainer<? extends org.openrewrite.java.tree.J> container)
    Computes the position span of a container element.
    positionOf(org.openrewrite.Cursor cursor, JRightPadded<org.openrewrite.java.tree.J> rightPadded)
    Computes the position span of a right-padded element.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SourcePositionService

      public SourcePositionService()
  • Method Details

    • positionOf

      public Span positionOf(org.openrewrite.Cursor cursor)
      Computes the position span of the element at the given cursor.
      See Also:
      • positionOfChild(Cursor, Object, Cursor)
    • positionOf

      public Span positionOf(org.openrewrite.Cursor cursor, JContainer<? extends org.openrewrite.java.tree.J> container)
      Computes the position span of a container element.
      See Also:
      • positionOfChild(Cursor, Object, Cursor)
    • positionOf

      public Span positionOf(org.openrewrite.Cursor cursor, JRightPadded<org.openrewrite.java.tree.J> rightPadded)
      Computes the position span of a right-padded element.
      See Also:
      • positionOfChild(Cursor, Object, Cursor)
    • positionOf

      public Span positionOf(org.openrewrite.Cursor cursor, org.openrewrite.java.tree.J child)
      Computes the position span of a J element.
      See Also:
      • positionOfChild(Cursor, Object, Cursor)
    • columnsOf

      public Span.ColSpan columnsOf(org.openrewrite.Cursor cursor)
      Computes the position span of the element at the given cursor.
      See Also:
      • positionOfChild(Cursor, Object, Cursor)
    • columnsOf

      public Span.ColSpan columnsOf(org.openrewrite.Cursor cursor, JContainer<? extends org.openrewrite.java.tree.J> container)
      Computes the position span of a container element.
      See Also:
      • positionOfChild(Cursor, Object, Cursor)
    • columnsOf

      public Span.ColSpan columnsOf(org.openrewrite.Cursor cursor, JRightPadded<org.openrewrite.java.tree.J> rightPadded)
      Computes the position span of a right-padded element.
      See Also:
      • positionOfChild(Cursor, Object, Cursor)
    • columnsOf

      public Span.ColSpan columnsOf(org.openrewrite.Cursor cursor, org.openrewrite.java.tree.J child)
      Computes the position span of a J element.
      See Also:
      • positionOfChild(Cursor, Object, Cursor)
    • computeNewLinedCursorElement

      public org.openrewrite.Cursor computeNewLinedCursorElement(org.openrewrite.Cursor cursor)
      Finds the ancestor element in the cursor path that has a newline in its prefix.

      For method chains, this navigates to the root of the chain. Then it walks up the cursor path until it finds an element with a newline prefix (or reaches the compilation unit). This is the element whose indentation forms the baseline for calculations.

      Parameters:
      cursor - the cursor to start searching from
      Returns:
      the cursor pointing to the element with a newline prefix