Package org.openrewrite.java.service
Class SourcePositionService
java.lang.Object
org.openrewrite.java.service.SourcePositionService
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 -
Method Summary
Modifier and TypeMethodDescriptionintcomputeColumnToAlignTo(org.openrewrite.Cursor cursor, int continuation) Computes the column position where an element should be aligned to.intcomputeTreeLength(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.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.
-
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 computedcontinuation- 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
-
positionOf
Computes the position span of the element at the given cursor.- See Also:
-
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:
-
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:
-
positionOf
Computes the position span of a J element.- See Also:
-