KtPsiMutationService¶
interface KtPsiMutationService
Service responsible for Kotlin PSI mutation operations whose implementation is provided by the Kotlin plugin environment.
Avoid using this service directly if possible: instead, use the helper extension functions from org.jetbrains.kotlin.idea.base.psi.KotlinPsiModificationUtils.
When the service is not registered (e.g., when the Kotlin PSI is used outside the IntelliJ Kotlin plugin), Kotlin PSI overrides of platform mutation methods, such as PsiElement.delete, PsiElement.replace, com.intellij.psi.PsiNamedElement.setName and PsiLanguageInjectionHost.updateText, fall back to plain operations without Kotlin-specific adjustments like removing dangling separators or adding parentheses. However, Kotlin-specific mutation methods still require the service.
Types¶
Companion¶
object Companion
Functions¶
addAnnotation¶
abstract fun addAnnotation(owner: KtModifierListOwner, annotationEntry: KtAnnotationEntry): KtAnnotationEntry
Adds annotationEntry to owner.
abstract fun addAnnotation(constructor: KtPrimaryConstructor, annotationEntry: KtAnnotationEntry): KtAnnotationEntry
Adds annotationEntry to constructor using primary-constructor-specific behavior.
addEnumEntrySemicolon¶
abstract fun addEnumEntrySemicolon(enumEntry: KtEnumEntry): PsiElement
Adds a semicolon to enumEntry, reusing an existing sibling semicolon when possible.
addMemberDeclaration¶
abstract fun <T : KtDeclaration> addMemberDeclaration(classOrObject: KtClassOrObject, declaration: T): T
Adds declaration to classOrObject, creating a body when needed.
addMemberDeclarationAfter¶
abstract fun <T : KtDeclaration> addMemberDeclarationAfter(
classOrObject: KtClassOrObject,
declaration: T,
anchor: PsiElement?
): T
Adds declaration after anchor in classOrObject, or appends it when anchor is null.
addMemberDeclarationBefore¶
abstract fun <T : KtDeclaration> addMemberDeclarationBefore(
classOrObject: KtClassOrObject,
declaration: T,
anchor: PsiElement?
): T
Adds declaration before anchor in classOrObject, or prepends it when anchor is null.
addModifierKeyword¶
abstract fun addModifierKeyword(owner: KtModifierListOwner, modifier: KtModifierKeywordToken)
abstract fun addModifierKeyword(constructor: KtPrimaryConstructor, modifier: KtModifierKeywordToken)
Adds modifier to constructor using primary-constructor-specific behavior.
addSuperType¶
abstract fun addSuperType(declaration: KtClassOrObject, superTypeListEntry: KtSuperTypeListEntry): KtSuperTypeListEntry
Adds superTypeListEntry to declaration.
abstract fun addSuperType(superTypeList: KtSuperTypeList, superTypeListEntry: KtSuperTypeListEntry): KtSuperTypeListEntry
Adds superTypeListEntry to superTypeList.
appendParameter¶
abstract fun appendParameter(parameterList: KtParameterList, parameter: KtParameter): KtParameter
Adds parameter to parameterList.
appendTypeArgument¶
abstract fun appendTypeArgument(typeArgumentList: KtTypeArgumentList, typeArgument: KtTypeProjection): KtTypeProjection
Adds typeArgument to typeArgumentList.
abstract fun appendTypeArgument(callExpression: KtCallExpression, typeArgument: KtTypeProjection)
Adds typeArgument to callExpression, creating the type argument list if needed.
appendTypeParameter¶
abstract fun appendTypeParameter(typeParameterList: KtTypeParameterList, typeParameter: KtTypeParameter): KtTypeParameter
Adds typeParameter to typeParameterList.
appendValueArgument¶
abstract fun appendValueArgument(argumentList: KtValueArgumentList, argument: KtValueArgument): KtValueArgument
Adds argument to argumentList.
astReplace¶
abstract fun astReplace(element: PsiElement, newElement: PsiElement)
Replaces element with newElement on the AST level.
convertImplicitDelegationCallToExplicit¶
abstract fun convertImplicitDelegationCallToExplicit(constructor: KtSecondaryConstructor, isThis: Boolean): KtConstructorDelegationCall
Replaces the implicit delegation call in constructor with an explicit this() or super() call.
deleteBlockExpression¶
abstract fun deleteBlockExpression(blockExpression: KtBlockExpression)
Performs smart deletion of blockExpression.
deleteClassOrObject¶
abstract fun deleteClassOrObject(declaration: KtClassOrObject)
Performs smart deletion of declaration.
deleteElement¶
abstract fun deleteElement(element: KtElement)
Performs smart deletion of element.
deleteEnumEntry¶
abstract fun deleteEnumEntry(enumEntry: KtEnumEntry)
Performs smart deletion of enumEntry.
deleteParameter¶
abstract fun deleteParameter(parameterList: KtParameterList, parameter: KtParameter)
Removes parameter from parameterList.
abstract fun deleteParameter(parameterList: KtParameterList, index: Int)
Removes a parameter at index from parameterList.
deleteSuperTypeList¶
abstract fun deleteSuperTypeList(superTypeList: KtSuperTypeList)
Deletes superTypeList, removing the preceding colon when needed.
deleteValueArgument¶
abstract fun deleteValueArgument(argumentList: KtValueArgumentList, argument: KtValueArgument)
Removes argument from argumentList.
abstract fun deleteValueArgument(argumentList: KtValueArgumentList, index: Int)
Removes an argument at index from argumentList.
getOrCreateCallValueArgumentList¶
abstract fun getOrCreateCallValueArgumentList(callExpression: KtCallExpression): KtValueArgumentList
Returns the existing value argument list for callExpression, or creates an empty one.
getOrCreateClassBody¶
abstract fun getOrCreateClassBody(classOrObject: KtClassOrObject): KtClassBody
Returns the existing body for classOrObject, or creates one if missing.
getOrCreateFunctionLiteralParameterList¶
abstract fun getOrCreateFunctionLiteralParameterList(functionLiteral: KtFunctionLiteral): KtParameterList
Returns the existing value parameter list for functionLiteral, or creates an empty one together with the arrow token.
getOrCreatePrimaryConstructor¶
abstract fun getOrCreatePrimaryConstructor(klass: KtClass): KtPrimaryConstructor
Returns the existing primary constructor for klass, or creates one if missing.
getOrCreatePrimaryConstructorParameterList¶
abstract fun getOrCreatePrimaryConstructorParameterList(klass: KtClass): KtParameterList
Returns the existing primary constructor parameter list for klass, or creates one if missing.
insertParameterAfter¶
abstract fun insertParameterAfter(
parameterList: KtParameterList,
parameter: KtParameter,
anchor: KtParameter?
): KtParameter
Adds parameter to parameterList after anchor.
insertParameterBefore¶
abstract fun insertParameterBefore(
parameterList: KtParameterList,
parameter: KtParameter,
anchor: KtParameter?
): KtParameter
Adds parameter to parameterList before anchor.
insertValueArgumentAfter¶
abstract fun insertValueArgumentAfter(
argumentList: KtValueArgumentList,
argument: KtValueArgument,
anchor: KtValueArgument?
): KtValueArgument
Adds argument to argumentList after anchor.
insertValueArgumentBefore¶
abstract fun insertValueArgumentBefore(
argumentList: KtValueArgumentList,
argument: KtValueArgument,
anchor: KtValueArgument?
): KtValueArgument
Adds argument to argumentList before anchor.
removeAnnotationEntry¶
abstract fun removeAnnotationEntry(annotation: KtAnnotation, entry: KtAnnotationEntry)
Removes entry from annotation, deleting the annotation when it becomes empty.
removeModifierKeyword¶
abstract fun removeModifierKeyword(owner: KtModifierListOwner, modifier: KtModifierKeywordToken)
abstract fun removeModifierKeyword(constructor: KtPrimaryConstructor, modifier: KtModifierKeywordToken)
Removes modifier from constructor using primary-constructor-specific behavior.
removeQualifier¶
abstract fun removeQualifier(userType: KtUserType)
Removes the qualifier of userType, keeping the referenced name intact.
removeRedundantConstructorKeyword¶
abstract fun removeRedundantConstructorKeyword(constructor: KtPrimaryConstructor)
Removes the redundant constructor keyword and the following whitespace from constructor.
removeSuperType¶
abstract fun removeSuperType(declaration: KtClassOrObject, superTypeListEntry: KtSuperTypeListEntry)
Removes superTypeListEntry from declaration.
abstract fun removeSuperType(superTypeList: KtSuperTypeList, superTypeListEntry: KtSuperTypeListEntry)
Removes superTypeListEntry from superTypeList.
replaceExpression¶
abstract fun replaceExpression(
expression: KtExpression,
newElement: PsiElement,
reformat: Boolean,
rawReplaceHandler: (PsiElement) -> PsiElement
): PsiElement
Replaces expression with newElement, adding parentheses or string-template braces when needed.
replaceFileAnnotationList¶
abstract fun replaceFileAnnotationList(file: KtFile, annotationList: KtFileAnnotationList): KtFileAnnotationList
Replaces file's file annotation list with annotationList, or adds it when missing.
replaceModifierList¶
abstract fun replaceModifierList(owner: KtModifierListOwner, modifierList: KtModifierList?): KtModifierList?
Replaces the existing modifier list on owner with modifierList, adds it if missing, or removes it when modifierList is null.
setCallableReceiverTypeReference¶
abstract fun setCallableReceiverTypeReference(declaration: KtCallableDeclaration, typeRef: KtTypeReference?): KtTypeReference?
Replaces the receiver type on declaration with typeRef, adds it if missing, or removes it when typeRef is null.
setCallableTypeReference¶
abstract fun setCallableTypeReference(
declaration: KtCallableDeclaration,
addAfter: PsiElement?,
typeRef: KtTypeReference?
): KtTypeReference?
Replaces the explicit return type on declaration with typeRef, adds it if missing, or removes it when typeRef is null.
setCommonFileName¶
abstract fun setCommonFileName(file: KtCommonFile, name: String): PsiElement
Renames file, reparsing it when the rename changes whether it should be treated as a script.
setDestructuringDeclarationEntryTypeReference¶
abstract fun setDestructuringDeclarationEntryTypeReference(entry: KtDestructuringDeclarationEntry, typeRef: KtTypeReference?): KtTypeReference?
setDoubleColonReceiverExpression¶
abstract fun setDoubleColonReceiverExpression(expression: KtDoubleColonExpression, newReceiverExpression: KtExpression)
Replaces the receiver expression on expression with newReceiverExpression, or adds it if missing.
setFunctionTypeReceiverTypeReference¶
abstract fun setFunctionTypeReceiverTypeReference(functionType: KtFunctionType, typeRef: KtTypeReference?): KtTypeReference?
Replaces the receiver type on functionType with typeRef, adds it if missing, or removes it when typeRef is null.
setFunctionTypeReference¶
abstract fun setFunctionTypeReference(function: KtNamedFunction, typeRef: KtTypeReference?): KtTypeReference?
setImportAliasName¶
abstract fun setImportAliasName(importAlias: KtImportAlias, name: String): PsiElement
Renames importAlias.
setLabeledExpressionName¶
abstract fun setLabeledExpressionName(expression: KtLabeledExpression, name: String): PsiElement
Renames expression by replacing its target label.
setModifierList¶
abstract fun setModifierList(owner: KtModifierListOwner, newModifierList: KtModifierList)
Replaces the existing modifier list on owner with newModifierList, or adds it if missing.
setNamedDeclarationName¶
abstract fun setNamedDeclarationName(declaration: KtNamedDeclaration, name: String): PsiElement
Renames declaration by replacing its name identifier directly.
setNamedDeclarationStubName¶
abstract fun setNamedDeclarationStubName(declaration: KtNamedDeclarationStub<*>, name: String): PsiElement?
Renames declaration, including operator-specific modifier adjustments.
setObjectDeclarationName¶
abstract fun setObjectDeclarationName(declaration: KtObjectDeclaration, name: String): PsiElement
Renames declaration, adding an explicit identifier when necessary.
setPackageFqName¶
abstract fun setPackageFqName(file: KtCommonFile, fqName: FqName)
Replaces the package name of file, adding a package directive when needed.
abstract fun setPackageFqName(packageDirective: KtPackageDirective, fqName: FqName)
Replaces the package name of packageDirective with fqName.
setParameterTypeReference¶
abstract fun setParameterTypeReference(parameter: KtParameter, typeRef: KtTypeReference?): KtTypeReference?
setPropertyInitializer¶
abstract fun setPropertyInitializer(property: KtProperty, initializer: KtExpression?): KtExpression?
Replaces the initializer on property with initializer, adds it if missing, or removes it when initializer is null.
setPropertyTypeReference¶
abstract fun setPropertyTypeReference(property: KtProperty, typeRef: KtTypeReference?): KtTypeReference?
setTypeParameterExtendsBound¶
abstract fun setTypeParameterExtendsBound(typeParameter: KtTypeParameter, typeReference: KtTypeReference?): KtTypeReference?
Replaces the extends bound on typeParameter with typeReference, adds it if missing, or removes it when typeReference is null.
updateKDocSectionText¶
abstract fun updateKDocSectionText(section: KDocSection, text: String): PsiLanguageInjectionHost
Updates the text of section for language injection.
updateStringTemplateText¶
abstract fun updateStringTemplateText(expression: KtStringTemplateExpression, text: String): PsiLanguageInjectionHost
Updates the text of expression for language injection.