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
    int
    computeColumnToAlignTo(org.openrewrite.Cursor cursor, int continuation)
    Computes the column position where an element should be aligned to.
    int
    computeTreeLength(org.openrewrite.Cursor cursor)
    Computes the total length of a tree element from the first character after its newline prefix to the end of the element, including any trailing semicolon if applicable.

    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

    • computeColumnToAlignTo

      public int computeColumnToAlignTo(org.openrewrite.Cursor cursor, int continuation)
      Computes the column position where an element should be aligned to.

      For elements that should align with a previous element (e.g., in method chains or parameter lists), this calculates the column position of that alignment point. For elements that don't align, it returns the parent's indentation plus the continuation indent.

      Parameters:
      cursor - the cursor pointing to the element whose alignment position should be computed
      continuation - the continuation indent to add when the element doesn't align with another element
      Returns:
      the column position (0-indexed) where the element should align to
    • computeTreeLength

      public int computeTreeLength(org.openrewrite.Cursor cursor)
      Computes the total length of a tree element from the first character after its newline prefix to the end of the element, including any trailing semicolon if applicable.

      This is useful for determining how much horizontal space an element occupies on its line, which is important for line wrapping decisions.

      Parameters:
      cursor - the cursor pointing to the element whose length should be computed
      Returns:
      the length in characters of the tree element