Class FileLocation

java.lang.Object
net.sourceforge.pmd.lang.document.FileLocation

public final class FileLocation extends Object
Represents the coordinates of a text region, used for reporting. This provides access to the line and column positions, as well as the text file. Instances can be obtained from a TextRegion with TextDocument::toLocation.

This should replace the text coordinates methods in Node, GenericToken, and RuleViolation at least (see Reportable).

  • Field Details

  • Method Details

    • getFileId

      public FileId getFileId()
      File name of this position.
    • getStartLine

      public int getStartLine()
      Inclusive, 1-based line number.
    • getEndLine

      public int getEndLine()
      Inclusive, 1-based line number.
    • getStartColumn

      public int getStartColumn()
      Inclusive, 1-based column number.
    • getEndColumn

      public int getEndColumn()
      Exclusive, 1-based column number.
    • getStartPos

      public TextPos2d getStartPos()
      Returns the start position.
    • getEndPos

      public TextPos2d getEndPos()
      Returns the end position.
    • toRange2d

      public TextRange2d toRange2d()
      Turn this into a range country.
    • getRegionInFile

      public @Nullable TextRegion getRegionInFile()
      Returns the region in the file, or null if this was not available.
    • startPosToString

      public String startPosToString()
      Formats the start position as e.g. "line 1, column 2".
    • startPosToStringWithFile

      public String startPosToStringWithFile()
      Formats the start position as e.g. "/path/to/file:1:2".
    • getLineCount

      public int getLineCount()
    • range

      public static FileLocation range(FileId fileName, TextRange2d range2d)
      Creates a new location for a range of text.
      Throws:
      IllegalArgumentException - If the file name is null
      IllegalArgumentException - If any of the line/col parameters are strictly less than 1
      IllegalArgumentException - If the line and column are not correctly ordered
      IllegalArgumentException - If the start offset or length are negative
    • caret

      public static FileLocation caret(FileId fileName, int line, int column)
      Returns a new location that starts and ends at the same position.
      Parameters:
      fileName - File name
      line - Line number
      column - Column number
      Returns:
      A new location
      Throws:
      IllegalArgumentException - See range(FileId, TextRange2d)
    • toString

      public String toString()
      Overrides:
      toString in class Object