Class KotlinRewriter
- All Implemented Interfaces:
CopilotRewriter<KotlinComponentInfo>
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 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) Refused becauseadd-callis only ever sent by "add event listener", whose parameter is a Java lambda template (e -> {}) that is not Kotlin syntax.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, not just the last: leaving a second would let it take effect instead of the change just made.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 alignment) The alignment badge, in whichever vocabulary the project's theme uses.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) The gap badge: clear the spacing the component had, then write Lumo utility class names under Lumo and inline styles otherwise.voidsetPadding(KotlinComponentInfo component, String all, String top, String right, String bottom, String left) The padding badge.voidsetSizing(KotlinComponentInfo component, Map<String, String> changes) The sizing panel, which sends a CSS value per dimension ornullto clear it.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.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:
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
-
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
Refused becauseadd-callis only ever sent by "add event listener", whose parameter is a Java lambda template (e -> {}) that is not Kotlin syntax. Translating it to a trailing lambda is its own problem.- 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.- Parameters:
component- the component the call is made onfunction- the call's name, as the source spells itarguments- 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 onfunction- the call's name, as the source spells itargumentTexts- 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 onfunction- the call's name, as the source spells itremoving- the spellings to drop, matched as the source writes themadding- arguments to add; one already present is not added twice
-
replaceOrAddCall
Replaces the arguments of an existing call, or writes the call if there is none. UnlikereplaceFunctionCall(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:
replaceOrAddCallin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyfunction- the method to callparameters- the arguments
-
removeCalls
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:
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 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 ofstyle.setColor("blue").setFontSize("12px")one at a time leaves a baresayHello.stylebehind.- Parameters:
component- the component to restylechanges- dash-separated CSS properties to values, ornullto remove
-
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 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 restylechanges- property to value, withnullto clear one that 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
The sizing panel, which sends a CSS value per dimension ornullto clear it.Three outcomes, measured against
JavaStyleRewriter.setSizingrather than read off it: a value becomes an inline style; a cleared dimension takes away the component's ownsetWidth(…); and where nothing of the component's own states the size, an explicitsetWidth(null)overrides whatever does — a constructor, asVerticalLayoutsets its width, or asetWidthFull()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:
setSizingin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifychanges- the properties to set, by name
-
setAlignment
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 themecenter), andflexGrow/flexShrinkhave no Lumo class so both branches write them as inline styles —BadgePolicy.ALIGNMENT_PROPERTIES_WITHOUT_LUMO_CLASSES.- Specified by:
setAlignmentin interfaceCopilotRewriter<KotlinComponentInfo>- Parameters:
component- the component to modifyalignment- the alignment to apply
-
setGap
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:
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) The padding badge. WritessetPadding(false)when every value is cleared, because layouts such asVerticalLayoutswitch 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:
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
-