Class KotlinRewriter
- All Implemented Interfaces:
CopilotRewriter<KotlinComponentInfo>
Only the property operations are implemented: set-component-property
and the read that can-be-edited needs. Every other operation throws,
and SourceLanguageCapabilities says so, so the frontend disables
those actions rather than offering something that fails. Operations move in
here and into that capability set together.
Edits are spliced at character offsets rather than printed from a mutated
tree: the tree is read-only by design (see KotlinSource), and the
practical consequence is the one that matters to a developer — a file Copilot
edits comes back byte-identical except on the lines the edit needed.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddArguments(KotlinComponentInfo component, String function, List<String> arguments) Adds arguments to a call, creating it if the component has none.voidaddCall(KotlinComponentInfo component, String function, Object... parameters) Still refused, and not because writing a call is hard.voidaddComment(KotlinComponentInfo component, String comment, CommentType commentType) Adds a comment next to the component.addComponentUsingTemplate(KotlinComponentInfo 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.addConstructorParameterToClass(KotlinComponentInfo referenceComponent, String parameterTypeName, String suggestedParameterName, boolean storeAsField) Adds a constructor parameter to the class the component is declared in, for injecting a service.booleandelete(KotlinComponentInfo component) Deletes the component, along with the code that only existed to support it.voidduplicate(KotlinComponentInfo component, List<KotlinComponentInfo> children) Duplicates a component together with the given children.extractComponent(KotlinComponentInfo component, IdentityHashMap<KotlinComponentInfo, List<KotlinComponentInfo>> componentChildren, String extractedComponentClassNameSuggestion) Extracts a component and its children into a class of their own.getPropertyValue(KotlinComponentInfo component, String property) Reads a property's value from the source.voidmergeAndReplace(List<KotlinComponentInfo> components, CopilotComponent wrapperComponent) Wraps the given components in a new container.voidmoveComponent(KotlinComponentInfo component, KotlinComponentInfo container, KotlinComponentInfo reference, Where where, MoveTemplateOptions options) Moves a component into a container, relative to a reference component.voidremoveArguments(KotlinComponentInfo component, String function, Set<String> argumentTexts) Removes arguments from a call, and the call with them when nothing is left.voidremoveCalls(KotlinComponentInfo component, String function) Removes every call of this name made on the component.voidremovePropertySetter(KotlinComponentInfo component, String setter) Removes a property setter call from the component.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.voidreplaceOrAddCall(KotlinComponentInfo component, String function, Object... parameters) Replaces the arguments of an existing call, or writes the call if there is none.voidsetAlignment(KotlinComponentInfo component, AlignmentValues alignmentValues) Sets the component's flexbox alignment properties.voidsetArguments(KotlinComponentInfo component, String function, Set<String> removing, List<String> adding) Takes some arguments out of a call and puts others in, as one edit.voidsetGap(KotlinComponentInfo component, String all, String column, String row) Sets the gap between a layout's children.voidsetPadding(KotlinComponentInfo component, String all, String top, String right, String bottom, String left) Sets a component's padding.voidsetSizing(KotlinComponentInfo component, Map<String, String> changes) Sets width and height related properties.voidsetStyle(KotlinComponentInfo component, String property, String value) Sets an inline style, or the equivalent utility class when the app uses Lumo.voidsetStyles(KotlinComponentInfo component, Map<String, String> changes) Applies a whole set of style changes in one pass.voidsetStylesClearingWhatIsThere(KotlinComponentInfo component, Map<String, String> changes) AssetStyles(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.
-
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.Two behaviours here differ from the Java implementation, both deliberately:
- An explicit set is preferred over a constructor argument when the source
has both. Java replaces the constructor argument first, which for
Button("A")followed bybutton.text = "B"edits the argument and leaves the assignment still winning at runtime — so the panel appears to discard the edit. Editing whatever the panel is showing is the only self-consistent choice, and the panel shows the last set. - The variable is never renamed. Java regenerates an auto-generated name from the new text, which means renaming every reference to it; that belongs with the operation that creates auto-generated names in the first place.
- Specified by:
replaceFunctionCallin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyfunction- the setter namevalue- the new value, ornullto remove the call- Returns:
- a result that never carries a rename, per the note above
- An explicit set is preferred over a constructor argument when the source
has both. Java replaces the constructor argument first, which for
-
removePropertySetter
Description copied from interface:CopilotRewriterRemoves a property setter call from the component.- Specified by:
removePropertySetterin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifysetter- the setter name
-
getPropertyValue
Description copied from interface:CopilotRewriterReads a property's value from the source.- Specified by:
getPropertyValuein interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to readproperty- the property name- Returns:
- a constant value where the source holds one, otherwise a
SourceExpressionfor something that has to be evaluated, ornullif the property is not set
-
addCall
Still refused, and not because writing a call is hard.The only thing that sends
add-callis "add event listener", and the parameter it sends is a Java lambda —ComponentEventCollectorhands the frontend the templatee -> {}— which is not Kotlin syntax. Writing it verbatim would put code in the file that does not compile; the Kotlin idiom is a trailing lambda,sayHello.addClickListener { }. That is a translation problem rather than a value-writing one, so it belongs with its own tests rather than inside the styles work.- Specified by:
addCallin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyfunction- the method to callparameters- the arguments
-
addArguments
Adds arguments to a call, creating it if the component has none.For the Lumo paths, which express a badge as class names:
name.addClassNames(LumoUtility.Gap.MEDIUM). Arguments already present are left as they are, so a developer's own class names survive.- Parameters:
component- the component to add them tofunction- the call's name, as the source spells itarguments- the arguments as source text, already rendered and importable
-
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.The counterpart of adding: a badge takes away the class names it owns before writing the new ones, and a
addClassNames()with no arguments left is not something to leave behind.- Parameters:
component- the componentfunction- the call's nameargumentTexts- the argument spellings to drop, matched as written
-
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.The operation the badges actually perform: a badge clears the utility class it owns and writes the new one, and doing that as
removeArgumentsfollowed byaddArgumentscannot work — the PSI is read-only and never reflects an edit already recorded, so the second call reads the original arguments and records a second replacement of the same range.TextEditBufferthen rejects the pair as overlapping, and had it not, the second edit would have put the removed argument back. Raised in review of #9653, where the two were separate primitives and the documented workflow combined them.Arguments are planned for every matching call 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 one, or writes a new call when there is none.
- Parameters:
component- the component the call is made onfunction- the call's name, as the source spells itremoving- argument spellings to drop, matched as writtenadding- arguments to add, as source text; one already present is left where it is rather than repeated
-
replaceOrAddCall
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), which is property-shaped and resolves a JavaBean name, this takes the function as the source spells it. The badge commands need that:setSpacing(false)is a call to make, not a property to set, and the component may not even have aspacingproperty of the right type.The last existing call is the one replaced, for the reason reading a property takes the last declaration: it is the one in effect.
- Specified by:
replaceOrAddCallin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyfunction- the method to callparameters- the arguments
-
removeCalls
Removes every call of this name made on the component.Every one rather than the last: the callers are removals, and Java's gap path deletes any
setSpacingstatement it finds. Leaving a second in place would let it take effect instead of the change just made.- Specified by:
removeCallsin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyfunction- the method name
-
setStyles
Applies a whole set of style changes in one pass.Not a convenience over calling
setStyle(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.lang.String, java.lang.String)repeatedly: the removals have to be decided together. Each is an offset splice, and whether a statement should be deleted outright depends on whether anything on it survives the set — clearing both styles ofstyle.setColor("blue").setFontSize("12px")one at a time would splice out each call and leave a baresayHello.stylebehind.- Parameters:
component- the component to restylechanges- CSS properties, dash-separated, mapped to their new values, or tonullto remove them
-
setStylesClearingWhatIsThere
AssetStyles(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 callers that clear a group before writing it. The badge commands start by taking away every style they own — alignment clears seven properties, gap and padding everything matching their prefixes — and they do that without first asking which of them the source happens to set. Refusing would make the first use of a badge on an unstyled component an error.
setStyles(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.util.Map<java.lang.String, java.lang.String>)keeps refusing, because for the inspector it is the right answer: it sends a removal per rule it dropped, and reporting success for a style that was never there would hide a disagreement about which styles the component has.- Parameters:
component- the component to restylechanges- property to value, withnullto clear it if it is set
-
setStyle
Description copied from interface:CopilotRewriterSets an inline style, or the equivalent utility class when the app uses Lumo.- Specified by:
setStylein interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyproperty- the CSS propertyvalue- the CSS value
-
setSizing
Description copied from interface:CopilotRewriterSets width and height related properties.- Specified by:
setSizingin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifychanges- the properties to set, by name
-
setAlignment
Description copied from interface:CopilotRewriterSets the component's flexbox alignment properties.- Specified by:
setAlignmentin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyalignmentValues- the alignment to apply
-
setGap
Description copied from interface:CopilotRewriterSets the gap between a layout's children.- Specified by:
setGapin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the layout to modifyall- the gap on both axes, ornullto set them separatelycolumn- the column gaprow- the row gap
-
setPadding
public void setPadding(KotlinComponentInfo component, String all, String top, String right, String bottom, String left) Description copied from interface:CopilotRewriterSets a component's padding.- Specified by:
setPaddingin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyall- the padding on all sides, ornullto set them separatelytop- the top paddingright- the right paddingbottom- the bottom paddingleft- the left padding
-
delete
Description copied from interface:CopilotRewriterDeletes the component, along with the code that only existed to support it.- Specified by:
deletein interfaceCopilotRewriter<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:CopilotRewriterMoves a component into a container, relative to a reference component.- Specified by:
moveComponentin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to movecontainer- the container to move it intoreference- the sibling to position against, ornullto appendwhere- the position relative to the referenceoptions- the attach method and add-or-replace behaviour
-
duplicate
Description copied from interface:CopilotRewriterDuplicates a component together with the given children.- Specified by:
duplicatein interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to duplicatechildren- the children to duplicate with it
-
addComponentUsingTemplate
public List<KotlinComponentInfo> addComponentUsingTemplate(KotlinComponentInfo referenceComponent, Where where, List<CopilotComponent> template, AddTemplateOptions options) Description copied from interface:CopilotRewriterAdds components described by a template, as sent when dropping from the palette, pasting, or importing a design.- Specified by:
addComponentUsingTemplatein interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
referenceComponent- the component to position againstwhere- the position relative to the referencetemplate- the components to createoptions- how to declare and attach them- Returns:
- the components that were created
-
mergeAndReplace
public void mergeAndReplace(List<KotlinComponentInfo> components, CopilotComponent wrapperComponent) Description copied from interface:CopilotRewriterWraps the given components in a new container.- Specified by:
mergeAndReplacein interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
components- the components to wrapwrapperComponent- the container to wrap them in
-
extractComponent
public String extractComponent(KotlinComponentInfo component, IdentityHashMap<KotlinComponentInfo, List<KotlinComponentInfo>> componentChildren, String extractedComponentClassNameSuggestion) Description copied from interface:CopilotRewriterExtracts a component and its children into a class of their own.- Specified by:
extractComponentin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to extractcomponentChildren- the children of each component, keyed by identity because these handles can be records whose equality changes as the source is rewrittenextractedComponentClassNameSuggestion- 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:CopilotRewriterAdds a constructor parameter to the class the component is declared in, for injecting a service.- Specified by:
addConstructorParameterToClassin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
referenceComponent- a component in the target classparameterTypeName- the parameter's typesuggestedParameterName- the suggested parameter namestoreAsField- whether to also store the parameter in a field- Returns:
- the parameter name that was used
-
addComment
Description copied from interface:CopilotRewriterAdds a comment next to the component.- Specified by:
addCommentin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to comment oncomment- the comment textcommentType- whether to write a line or block comment
-