Class KotlinLocalClassNames

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

public final class KotlinLocalClassNames extends Object
The theme editor's per-component class name, in Kotlin source.

The theme editor gives a component a class name of its own so that a CSS rule can target just that component, and it has to find that class name again later to show it, change it or take it away. It marks its own statement with a line comment so that a developer's addClassName("…") is never mistaken for one of its own:

 // <theme-editor-local-classname>
 name.addClassName("hello-world-view-text-field-1")
 
The marker text is passed in rather than declared here, so that this and the Java implementation cannot drift into writing one marker and looking for another.

The statement is found the way any other call on a component is — KotlinPropertyLocator's receiver rules, which know what a name means where it is written — so a class name set on a component shadowed by a local is not read as another component's.

  • Constructor Details

    • KotlinLocalClassNames

      public KotlinLocalClassNames(KotlinFileSourceProvider sources, Function<com.vaadin.flow.component.Component,List<ComponentTypeAndSourceLocation>> siblings, String marker)
      Parameters:
      sources - the parsed Kotlin sources for this operation
      siblings - the selected component's peers under its parent, for resolving which of several same-line creations it is
      marker - the comment text the theme editor marks its own statements with
  • Method Details

    • read

      public String read(ComponentTypeAndSourceLocation location) throws IOException
      Parameters:
      location - the selected component
      Returns:
      the class name the theme editor gave this component, or null when it has not given it one
      Throws:
      IOException - if the source cannot be read
    • set

      public void set(ComponentTypeAndSourceLocation location, String className) throws IOException
      Sets the class name, replacing the one already there.
      Parameters:
      location - the selected component
      className - the class name to set
      Throws:
      IOException - if the source cannot be read
    • remove

      public boolean remove(ComponentTypeAndSourceLocation location) throws IOException
      Takes the class name away, along with the marker that named it.
      Parameters:
      location - the selected component
      Returns:
      true if there was one to remove
      Throws:
      IOException - if the source cannot be read
    • isAccessible

      public boolean isAccessible(ComponentTypeAndSourceLocation location)
      Whether this component can be given a class name at all.

      It cannot when Copilot has nowhere to write the statement — an inline add(Button("x")) has no name to address and KotlinInsertionPoint refuses it. Asked before the theme editor offers the field, so a refusal here is an answer rather than an error.

      Parameters:
      location - the selected component
      Returns:
      true if a class name could be written
    • existingClassNames

      public List<String> existingClassNames(ComponentTypeAndSourceLocation location) throws IOException
      Every class name the file already states, so that a suggestion does not collide with one.

      All of them, not only the theme editor's own: a suggestion that matches a class name the developer wrote would style their component by accident.

      Parameters:
      location - the selected component
      Returns:
      the class names, in source order
      Throws:
      IOException - if the source cannot be read