Skip to content

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(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(constructor: KtPrimaryConstructor, modifier: KtModifierKeywordToken)

Adds modifier to constructor using primary-constructor-specific behavior.

addSuperType

abstract fun addSuperType(declaration: KtClassOrObject, superTypeListEntry: KtSuperTypeListEntry): KtSuperTypeListEntry
abstract fun addSuperType(superTypeList: KtSuperTypeList, superTypeListEntry: KtSuperTypeListEntry): KtSuperTypeListEntry

appendParameter

abstract fun appendParameter(parameterList: KtParameterList, parameter: KtParameter): KtParameter

appendTypeArgument

abstract fun appendTypeArgument(typeArgumentList: KtTypeArgumentList, typeArgument: KtTypeProjection): KtTypeProjection
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

appendValueArgument

astReplace

abstract fun astReplace(element: PsiElement, newElement: PsiElement)

Replaces element with newElement on the AST level.

convertImplicitDelegationCallToExplicit

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

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

Returns the existing value parameter list for functionLiteral, or creates an empty one together with the arrow token.

getOrCreatePrimaryConstructor

Returns the existing primary constructor for klass, or creates one if missing.

getOrCreatePrimaryConstructorParameterList

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

Adds argument to argumentList after anchor.

insertValueArgumentBefore

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

Removes modifier from owner.

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

Removes the redundant constructor keyword and the following whitespace from constructor.

removeSuperType

abstract fun removeSuperType(declaration: KtClassOrObject, superTypeListEntry: KtSuperTypeListEntry)
abstract fun removeSuperType(superTypeList: KtSuperTypeList, superTypeListEntry: KtSuperTypeListEntry)

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

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

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

Replaces the type reference on entry with typeRef, adds it if missing, or removes it when typeRef is null.

setDoubleColonReceiverExpression

abstract fun setDoubleColonReceiverExpression(expression: KtDoubleColonExpression, newReceiverExpression: KtExpression)

Replaces the receiver expression on expression with newReceiverExpression, or adds it if missing.

setFunctionTypeReceiverTypeReference

Replaces the receiver type on functionType with typeRef, adds it if missing, or removes it when typeRef is null.

setFunctionTypeReference

Replaces the type reference on function with typeRef, adds it if missing, or removes it when typeRef is null.

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

Replaces the type reference on parameter with typeRef, adds it if missing, or removes it when typeRef is null.

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

Replaces the type reference on property with typeRef, adds it if missing, or removes it when typeRef is null.

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.