Class KotlinRewriter

java.lang.Object
com.vaadin.copilot.kotlinrewriter.KotlinRewriter
All Implemented Interfaces:
CopilotRewriter<KotlinComponentInfo>

public class KotlinRewriter extends Object implements CopilotRewriter<KotlinComponentInfo>
Rewrites Kotlin view source in response to visual editing.

The PSI is read-only, so edits are recorded as character-offset splices into a TextEditBuffer and applied at the end. An operation that removes and adds must decide both against the same unmodified tree.

Commands not listed in SourceLanguageCapabilities.KOTLIN_COMMANDS throw here rather than silently doing nothing; the two move together.

See docs/kotlin-rewriter.md for the rules this package follows and the known divergences from the Java path.

  • Constructor Details

    • KotlinRewriter

      public KotlinRewriter()
  • Method Details

    • replaceFunctionCall

      public ReplaceResult replaceFunctionCall(KotlinComponentInfo component, String function, Object value)
      Sets a property, replacing the value the source already states or adding a statement where it states none.

      Differs from Java twice, deliberately: an explicit set wins over a constructor argument, because the panel shows the last set and editing anything else looks like the edit was discarded; and the variable is never renamed, which belongs with the operation that generates names.

      Specified by:
      replaceFunctionCall in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      function - the setter name
      value - the new value, or null to remove the call
      Returns:
      a result that never carries a rename
    • removePropertySetter

      public void removePropertySetter(KotlinComponentInfo component, String setter)
      Description copied from interface: CopilotRewriter
      Removes a property setter call from the component.
      Specified by:
      removePropertySetter in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      setter - the setter name
    • getPropertyValue

      public Object getPropertyValue(KotlinComponentInfo component, String property)
      Description copied from interface: CopilotRewriter
      Reads a property's value from the source.
      Specified by:
      getPropertyValue in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to read
      property - the property name
      Returns:
      a constant value where the source holds one, otherwise a SourceExpression for something that has to be evaluated, or null if the property is not set
    • addCall

      public void addCall(KotlinComponentInfo component, String function, Object... parameters)
      Adds a call, which for this command always means an event listener.

      Always appends, never replaces: add-call is sent only by "add event listener", and a component may legitimately have several listeners for one event. The Java path does the same — addCall passes replace=false, so it skips the find-and-replace branch.

      The parameter arrives as a Java lambda template, e -> {}, which is not Kotlin syntax. It is translated to a trailing lambda rather than written verbatim; see

      invalid reference
      #trailingLambdaFor
      .
      Specified by:
      addCall in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      function - the method to call
      parameters - the arguments
    • addArguments

      public void addArguments(KotlinComponentInfo component, String function, List<String> arguments)
      Adds arguments to a call, creating it if the component has none.
      Parameters:
      component - the component the call is made on
      function - the call's name, as the source spells it
      arguments - source text, already rendered and importable; one already present is left alone, so a developer's own class names survive
    • removeArguments

      public void removeArguments(KotlinComponentInfo component, String function, Set<String> argumentTexts)
      Removes arguments from a call, and the call with them when nothing is left.
      Parameters:
      component - the component the call is made on
      function - the call's name, as the source spells it
      argumentTexts - the spellings to drop, matched as the source writes them
    • setArguments

      public void setArguments(KotlinComponentInfo component, String function, Set<String> removing, List<String> adding)
      Takes some arguments out of a call and puts others in, as one edit.

      One operation and not two: the read-only PSI never reflects a recorded edit, so a remove followed by an add reads the original arguments again and the buffer rejects the overlapping pair. Every call is planned before any edit is recorded, so a refusal leaves the source untouched. Removal applies to every call of that name; addition goes to the last, or writes a new call.

      Parameters:
      component - the component the call is made on
      function - the call's name, as the source spells it
      removing - the spellings to drop, matched as the source writes them
      adding - arguments to add; one already present is not added twice
    • replaceOrAddCall

      public void replaceOrAddCall(KotlinComponentInfo component, String function, Object... parameters)
      Replaces the arguments of an existing call, or writes the call if there is none. Unlike replaceFunctionCall(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.lang.String, java.lang.Object) this takes the function as the source spells it, which is what the badges need. The last call is the one replaced: it is the one in effect.
      Specified by:
      replaceOrAddCall in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      function - the method to call
      parameters - the arguments
    • removeCalls

      public void removeCalls(KotlinComponentInfo component, String function)
      Removes every call of this name, not just the last: leaving a second would let it take effect instead of the change just made.
      Specified by:
      removeCalls in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      function - the method name
    • setStyles

      public void setStyles(KotlinComponentInfo component, Map<String,String> changes)
      Applies a whole set of style changes in one pass.

      Not a convenience over repeated setStyle(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.lang.String, java.lang.String): whether a statement can be deleted outright depends on whether anything on it survives the whole set. Clearing both styles of style.setColor("blue").setFontSize("12px") one at a time leaves a bare sayHello.style behind.

      Parameters:
      component - the component to restyle
      changes - dash-separated CSS properties to values, or null to remove
    • setStylesClearingWhatIsThere

      public void setStylesClearingWhatIsThere(KotlinComponentInfo component, Map<String,String> changes)
      As setStyles(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.util.Map<java.lang.String, java.lang.String>), but a property that is not set is simply not there.

      For the badges, which clear every style they own without first asking which the source sets; refusing would make the first use of a badge an error. setStyles(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.util.Map<java.lang.String, java.lang.String>) keeps refusing, because the inspector sends one removal per rule it dropped and a false success would hide a disagreement.

      Parameters:
      component - the component to restyle
      changes - property to value, with null to clear one that is set
    • setStyle

      public void setStyle(KotlinComponentInfo component, String property, String value)
      Description copied from interface: CopilotRewriter
      Sets an inline style, or the equivalent utility class when the app uses Lumo.
      Specified by:
      setStyle in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      property - the CSS property
      value - the CSS value
    • setSizing

      public void setSizing(KotlinComponentInfo component, Map<String,String> changes)
      The sizing panel, which sends a CSS value per dimension or null to clear it.

      Three outcomes, measured against JavaStyleRewriter.setSizing rather than read off it: a value becomes an inline style; a cleared dimension takes away the component's own setWidth(…); and where nothing of the component's own states the size, an explicit setWidth(null) overrides whatever does — a constructor, as VerticalLayout sets its width, or a setWidthFull() this cannot delete.

      A cleared dimension runs every step rather than the first that matches, since a size can be stated as a call, a property assignment and a style at once. Java stops at the first; #9747 tracks that.

      Clearing a fill the parent applies refuses, because the edit needs a locator for a call whose receiver is the parent (#9702) — but only when it is the whole request, so a fill-to-fixed mode switch still writes its width.

      Specified by:
      setSizing in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      changes - the properties to set, by name
    • setAlignment

      public void setAlignment(KotlinComponentInfo component, AlignmentValues alignment)
      The alignment badge, in whichever vocabulary the project's theme uses.

      The odd one of the three: values arrive in different vocabularies per theme (Lumo sends items-center, base theme center), and flexGrow/flexShrink have no Lumo class so both branches write them as inline styles — BadgePolicy.ALIGNMENT_PROPERTIES_WITHOUT_LUMO_CLASSES.

      Specified by:
      setAlignment in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      alignment - the alignment to apply
    • setGap

      public void setGap(KotlinComponentInfo component, String all, String column, String row)
      The gap badge: clear the spacing the component had, then write Lumo utility class names under Lumo and inline styles otherwise. Which branch runs is a runtime decision (AppThemeUtils.isLumo()), which is why these three commands needed their own writers.

      Leaves a themeList.add("spacing") theme variant that Java strips — #9702, a chain-receiver locator this does not have yet.

      Specified by:
      setGap in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the layout to modify
      all - the gap on both axes, or null to set them separately
      column - the column gap
      row - the row gap
    • setPadding

      public void setPadding(KotlinComponentInfo component, String all, String top, String right, String bottom, String left)
      The padding badge. Writes setPadding(false) when every value is cleared, because layouts such as VerticalLayout switch padding on in their constructor and removing the classes alone would not say so.

      Carries the same theme-variant gap as setGap(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.lang.String, java.lang.String, java.lang.String) — #9702.

      Specified by:
      setPadding in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to modify
      all - the padding on all sides, or null to set them separately
      top - the top padding
      right - the right padding
      bottom - the bottom padding
      left - the left padding
    • delete

      public boolean delete(KotlinComponentInfo component)
      Description copied from interface: CopilotRewriter
      Deletes the component, along with the code that only existed to support it.
      Specified by:
      delete in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to delete
      Returns:
      true if the component was deleted
    • moveComponent

      public void moveComponent(KotlinComponentInfo component, KotlinComponentInfo container, KotlinComponentInfo reference, Where where, MoveTemplateOptions options)
      Description copied from interface: CopilotRewriter
      Moves a component into a container, relative to a reference component.
      Specified by:
      moveComponent in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to move
      container - the container to move it into
      reference - the sibling to position against, or null to append
      where - the position relative to the reference
      options - the attach method and add-or-replace behaviour
    • duplicate

      public void duplicate(KotlinComponentInfo component, List<KotlinComponentInfo> children)
      Description copied from interface: CopilotRewriter
      Duplicates a component together with the given children.
      Specified by:
      duplicate in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to duplicate
      children - the children to duplicate with it
    • addComponentUsingTemplate

      public List<KotlinComponentInfo> addComponentUsingTemplate(KotlinComponentInfo referenceComponent, Where where, List<CopilotComponent> template, AddTemplateOptions options)
      Writes the components a palette drop, a paste or a design import describes.

      Each becomes a declaration, the properties it carries, and an attach — val button = Button() where Java writes Button button = new Button();. The naming is shared with Java through TemplatePolicy so the same drop produces the same name in both languages; only the spelling of a declaration differs.

      Every statement for the whole template is built before the first is written, so a component this cannot express refuses with nothing half added — the rule the rest of this class keeps, and the one that matters most here because a drop is several statements rather than one.

      Specified by:
      addComponentUsingTemplate in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      referenceComponent - the component to position against
      where - the position relative to the reference
      template - the components to create
      options - how to declare and attach them
      Returns:
      an empty list: the components exist in the source after this, but resolving them back into KotlinComponentInfo needs a reparse this does not do. No caller reads the return value
    • mergeAndReplace

      public void mergeAndReplace(List<KotlinComponentInfo> components, CopilotComponent wrapperComponent)
      Description copied from interface: CopilotRewriter
      Wraps the given components in a new container.
      Specified by:
      mergeAndReplace in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      components - the components to wrap
      wrapperComponent - the container to wrap them in
    • extractComponent

      public String extractComponent(KotlinComponentInfo component, IdentityHashMap<KotlinComponentInfo,List<KotlinComponentInfo>> componentChildren, String extractedComponentClassNameSuggestion)
      Description copied from interface: CopilotRewriter
      Extracts a component and its children into a class of their own.
      Specified by:
      extractComponent in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to extract
      componentChildren - the children of each component, keyed by identity because these handles can be records whose equality changes as the source is rewritten
      extractedComponentClassNameSuggestion - the suggested class name
      Returns:
      the name of the class that was created
    • addConstructorParameterToClass

      public String addConstructorParameterToClass(KotlinComponentInfo referenceComponent, String parameterTypeName, String suggestedParameterName, boolean storeAsField)
      Description copied from interface: CopilotRewriter
      Adds a constructor parameter to the class the component is declared in, for injecting a service.
      Specified by:
      addConstructorParameterToClass in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      referenceComponent - a component in the target class
      parameterTypeName - the parameter's type
      suggestedParameterName - the suggested parameter name
      storeAsField - whether to also store the parameter in a field
      Returns:
      the parameter name that was used
    • addComment

      public void addComment(KotlinComponentInfo component, String comment, CommentType commentType)
      Description copied from interface: CopilotRewriter
      Adds a comment next to the component.
      Specified by:
      addComment in interface CopilotRewriter<KotlinComponentInfo>
      Parameters:
      component - the component to comment on
      comment - the comment text
      commentType - whether to write a line or block comment