Interface CopilotRewriter<C extends ComponentRef>

Type Parameters:
C - the component handle this rewriter resolves components to
All Known Implementing Classes:
JavaRewriter

public interface CopilotRewriter<C extends ComponentRef>
The source-rewriting operations Copilot performs in response to visual editing, independent of the language being rewritten.

Implemented by the Java rewriter and, once it exists, the Kotlin one. Only operations whose contract is genuinely language-independent appear here. Anything that trades in parse-tree nodes — attaching a component to a given insertion point, importing a Lit template, the result of a duplicate — stays on the concrete rewriter, because expressing it here would mean inventing a neutral node model that the Kotlin side does not want: it works in text offsets, not nodes.

The component handle is a type parameter rather than ComponentRef itself, so an implementation can keep taking its own richer handle without casting on every call. Java has no covariant parameter types, so the alternative would have been to change every signature in the Java rewriter and cast internally.

  • Method Details

    • replaceFunctionCall

      ReplaceResult replaceFunctionCall(C component, String function, Object value)
      Replaces a constructor argument, when the value maps to one, or the corresponding setter call otherwise.
      Parameters:
      component - the component to modify
      function - the setter name
      value - the new value, or null to remove the call
      Returns:
      the new variable name if the change renamed the variable
    • addCall

      void addCall(C component, String function, Object... parameters)
      Adds a call on the component, even if the same call is already present.
      Parameters:
      component - the component to modify
      function - the method to call
      parameters - the arguments
    • replaceOrAddCall

      void replaceOrAddCall(C component, String function, Object... parameters)
      Adds a call on the component, replacing an existing call to the same method.
      Parameters:
      component - the component to modify
      function - the method to call
      parameters - the arguments
    • removePropertySetter

      void removePropertySetter(C component, String setter)
      Removes a property setter call from the component.
      Parameters:
      component - the component to modify
      setter - the setter name
    • removeCalls

      void removeCalls(C component, String function)
      Removes every call to the given method on the component.
      Parameters:
      component - the component to modify
      function - the method name
    • getPropertyValue

      Object getPropertyValue(C component, String property)
      Reads a property's value from the source.
      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
    • setStyle

      void setStyle(C component, String property, String value)
      Sets an inline style, or the equivalent utility class when the app uses Lumo.
      Parameters:
      component - the component to modify
      property - the CSS property
      value - the CSS value
    • setSizing

      void setSizing(C component, Map<String,String> changes)
      Sets width and height related properties.
      Parameters:
      component - the component to modify
      changes - the properties to set, by name
    • setAlignment

      void setAlignment(C component, AlignmentValues alignmentValues)
      Sets the component's flexbox alignment properties.
      Parameters:
      component - the component to modify
      alignmentValues - the alignment to apply
    • setGap

      void setGap(C component, String all, String column, String row)
      Sets the gap between a layout's children.
      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

      void setPadding(C component, String all, String top, String right, String bottom, String left)
      Sets a component's padding.
      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

      boolean delete(C component)
      Deletes the component, along with the code that only existed to support it.
      Parameters:
      component - the component to delete
      Returns:
      true if the component was deleted
    • moveComponent

      void moveComponent(C component, C container, C reference, Where where, MoveTemplateOptions options)
      Moves a component into a container, relative to a reference component.
      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

      void duplicate(C component, List<C> children)
      Duplicates a component together with the given children.
      Parameters:
      component - the component to duplicate
      children - the children to duplicate with it
    • addComponentUsingTemplate

      List<C> addComponentUsingTemplate(C referenceComponent, Where where, List<CopilotComponent> template, AddTemplateOptions options)
      Adds components described by a template, as sent when dropping from the palette, pasting, or importing a design.
      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:
      the components that were created
    • mergeAndReplace

      void mergeAndReplace(List<C> components, CopilotComponent wrapperComponent)
      Wraps the given components in a new container.
      Parameters:
      components - the components to wrap
      wrapperComponent - the container to wrap them in
    • extractComponent

      String extractComponent(C component, IdentityHashMap<C,List<C>> componentChildren, String extractedComponentClassNameSuggestion)
      Extracts a component and its children into a class of their own.
      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

      String addConstructorParameterToClass(C referenceComponent, String parameterTypeName, String suggestedParameterName, boolean storeAsField)
      Adds a constructor parameter to the class the component is declared in, for injecting a service.
      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

      void addComment(C component, String comment, CommentType commentType)
      Adds a comment next to the component.
      Parameters:
      component - the component to comment on
      comment - the comment text
      commentType - whether to write a line or block comment