Class KotlinEditor

java.lang.Object
com.vaadin.copilot.kotlinrewriter.KotlinEditor

public final class KotlinEditor extends Object
The rewrite commands, for a component declared in Kotlin.

One place for what a command does when the selected component is Kotlin, so the command handler holds a single dependency on this package rather than on the resolver, the rewriter and the component handle separately. That handler is close to Sonar's coupling limit and this change is what keeps it under: the Kotlin path is a self-contained subject, so it may as well be a class.

It is also the shape the remaining phases want. As more commands gain Kotlin support they gain a method here, rather than another branch and three more imports in a handler that already serves seventeen commands.

  • Constructor Details

    • KotlinEditor

      public KotlinEditor(KotlinFileSourceProvider sources, Function<com.vaadin.flow.component.Component,List<ComponentTypeAndSourceLocation>> siblings)
      Parameters:
      sources - the parsed Kotlin sources for this operation, so that several edits to one file accumulate into a single write
      siblings - the selected component's peers under its parent, used to work out which of several same-line creations it is
  • Method Details

    • handles

      public static boolean handles(ComponentTypeAndSourceLocation location)
      Whether this component's source is Kotlin, and so belongs to this class.

      Asked of the component's own file rather than of the project, because a project can hold both languages and often does.

      Parameters:
      location - the selected component's type and location
      Returns:
      true if the property commands should be routed here
    • setProperty

      public String setProperty(ComponentTypeAndSourceLocation location, String property, Object value) throws IOException
      Sets a property, or clears it when the value is null.

      The setter's name is resolved by the same reflection the Java path uses, because that is a question about the component's API rather than about the language. Everything after differs: the component is located by offset in a read-only parse tree and the edit is spliced into the text.

      Parameters:
      location - the selected component's type and location
      property - the JavaBean property name
      value - the new value, or null to remove the set
      Returns:
      the variable's new name if the edit renamed it, which it never does — see KotlinRewriter.replaceFunctionCall(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.lang.String, java.lang.Object)
      Throws:
      IOException - if the source cannot be read
    • readProperty

      public Object readProperty(ComponentTypeAndSourceLocation location, String property) throws IOException
      Reads what the source states for a property.
      Parameters:
      location - the selected component's type and location
      property - the JavaBean property name
      Returns:
      a String, Boolean or Number for a literal, a SourceExpression for anything that has to be evaluated, or null when the source does not set it
      Throws:
      IOException - if the source cannot be read
    • setStyles

      public void setStyles(ComponentTypeAndSourceLocation location, Map<String,String> styles) throws IOException
      Applies a set of inline style changes to one component.

      The whole set at once, rather than a call per rule, because the component is resolved and the file parsed once per call and the styles editor sends several rules together. Doing it per rule would parse the same file a dozen times for one save and, worse, would let the edits be computed against different parse trees.

      Parameters:
      location - the selected component's type and location
      styles - the changes, in the order they should be applied: a CSS property, dash-separated, mapped to its new value, or to null to remove it
      Throws:
      IOException - if the source cannot be read
    • setGap

      public void setGap(ComponentTypeAndSourceLocation location, String all, String column, String row) throws IOException
      Sets the gap, as the gap badge asks.
      Parameters:
      location - the selected component's type and location
      all - the gap on both axes, or null
      column - the column gap, or null
      row - the row gap, or null
      Throws:
      IOException - if the source cannot be read
    • setPadding

      public void setPadding(ComponentTypeAndSourceLocation location, String all, String top, String right, String bottom, String left) throws IOException
      Sets the padding, as the padding badge asks.
      Parameters:
      location - the selected component's type and location
      all - padding on every edge, or null
      top - the top edge, or null
      right - the right edge, or null
      bottom - the bottom edge, or null
      left - the left edge, or null
      Throws:
      IOException - if the source cannot be read
    • setAlignment

      public void setAlignment(ComponentTypeAndSourceLocation location, AlignmentValues alignment) throws IOException
      Sets the flexbox alignment, as the alignment badge asks.
      Parameters:
      location - the selected component's type and location
      alignment - the values to set, with null for each one left alone
      Throws:
      IOException - if the source cannot be read
    • setSizing

      public void setSizing(ComponentTypeAndSourceLocation location, Map<String,String> changes) throws IOException
      Sets the component's sizing, as the sizing panel asks.
      Parameters:
      location - the selected component's type and location
      changes - CSS property to value, camelCase as the panel sends them, with null to clear one
      Throws:
      IOException - if the source cannot be read
    • decline

      public static UnsupportedSourceLanguageException decline(String operation)
      Declines an operation this class does not cover.
      Parameters:
      operation - what was asked for, in words a developer will see
      Returns:
      the refusal to throw