Class KotlinRewriter
java.lang.Object
com.vaadin.copilot.kotlinrewriter.KotlinRewriter
- All Implemented Interfaces:
CopilotRewriter<KotlinComponentInfo>
Rewrites Kotlin view source in response to visual editing.
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 TypeMethodDescriptionvoidaddCall(KotlinComponentInfo component, String function, Object... parameters) Adds a call on the component, even if the same call is already present.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.voidremoveCalls(KotlinComponentInfo component, String function) Removes every call to the given method 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) Adds a call on the component, replacing an existing call to the same method.voidsetAlignment(KotlinComponentInfo component, AlignmentValues alignmentValues) Sets the component's flexbox alignment properties.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.
-
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
Description copied from interface:CopilotRewriterAdds a call on the component, even if the same call is already present.- Specified by:
addCallin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyfunction- the method to callparameters- the arguments
-
replaceOrAddCall
Description copied from interface:CopilotRewriterAdds a call on the component, replacing an existing call to the same method.- Specified by:
replaceOrAddCallin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyfunction- the method to callparameters- the arguments
-
removeCalls
Description copied from interface:CopilotRewriterRemoves every call to the given method on the component.- Specified by:
removeCallsin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyfunction- the method name
-
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
-