Class KotlinSource

java.lang.Object
com.vaadin.copilot.kotlinrewriter.KotlinSource
All Implemented Interfaces:
ParsedSource

public class KotlinSource extends Object implements ParsedSource
A Kotlin source file, parsed for reading and edited by splicing text at offsets.

The Java implementation mutates a JavaParser tree and prints it back. This one cannot: the Kotlin parser exists to read, and generating code through it would mean building PSI by hand. So the tree stays untouched and every edit is recorded in a TextEditBuffer as a character range plus replacement, which is also why a Kotlin file that Copilot edits comes out byte-identical except on the lines the edit actually touched.

The parsed tree is exposed only within this package. Callers outside it work through ParsedSource and the rewriter interfaces, so the parser stays replaceable — see KotlinPsi.

  • Constructor Details

    • KotlinSource

      public KotlinSource(File file, String source)
      Parses the source, eagerly: a KotlinSource that exists always has a tree, so no caller has to handle a half-built one.
      Parameters:
      file - the file this was read from, or null for source held only in memory
      source - the source text
  • Method Details

    • getFile

      public File getFile()
      Specified by:
      getFile in interface ParsedSource
      Returns:
      the file this source was read from, or null for source that has no file, such as a template being prepared in memory
    • getSource

      public String getSource()
      Specified by:
      getSource in interface ParsedSource
      Returns:
      the original, unmodified source
    • getResult

      public String getResult()
      Specified by:
      getResult in interface ParsedSource
      Returns:
      the source with all recorded changes applied
    • getLanguage

      public SourceLanguage getLanguage()
      Specified by:
      getLanguage in interface ParsedSource
      Returns:
      the language of this source
    • isChanged

      public boolean isChanged()
      Specified by:
      isChanged in interface ParsedSource
      Returns:
      true if anything has been changed
    • isWritten

      public boolean isWritten()
      Specified by:
      isWritten in interface ParsedSource
      Returns:
      true if the result has already been written to disk
    • setWritten

      public void setWritten(boolean written)
      Description copied from interface: ParsedSource
      Records that the result has been written to disk, so one operation touching a file twice does not write it twice.
      Specified by:
      setWritten in interface ParsedSource
      Parameters:
      written - true once written
    • getFirstModifiedRow

      public int getFirstModifiedRow()
      Description copied from interface: ParsedSource
      The first line that differs between the original source and the result, used to tell the IDE where to scroll.
      Specified by:
      getFirstModifiedRow in interface ParsedSource
      Returns:
      the 1-based line number, or -1 if nothing changed