mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[workspace model] docs: describe how to add and remove entities in KDoc of MutableEntityStorage (IDEA-324734)
...and remove Storage-filling.md article from IJ Platform docs since its content is now covered in KDocs. GitOrigin-RevId: 082ccd9201c0cea107021f1d4ce5bef204af713c
This commit is contained in:
committed by
intellij-monorepo-bot
parent
31b751fde6
commit
7b41650c5b
+62
@@ -15,6 +15,68 @@ import org.jetbrains.annotations.NonNls
|
||||
*
|
||||
* Instances of this interface are not thread safe.
|
||||
*
|
||||
* ## Adding, modifying and removing entities
|
||||
*
|
||||
* In order to add a new entity to the storage, create it by calling the companion object of its interface and then pass it to [addEntity]
|
||||
* function:
|
||||
* ```kotlin
|
||||
* //mandatory properties are passed as parameters
|
||||
* val module = ModuleEntity(moduleName, dependencies, entitySource) {
|
||||
* //optional properties can be initialized in the lambda passed as the last parameter
|
||||
* type = ModuleTypeId.JAVA_MODULE
|
||||
* }
|
||||
* ...
|
||||
* WorkspaceModel.getInstance(project).updateProjectModel("Add module") { builder ->
|
||||
* builder.addEntity(module)
|
||||
* }
|
||||
* ```
|
||||
* You may first prepare a whole tree of entities, and then add the root one to the storage, [addEntity] will automatically add all the
|
||||
* children.
|
||||
*
|
||||
* In order to modify or remove an entity, you first need to find its instance in this instance of [MutableEntityStorage]. You can do this
|
||||
* by [resolving][SymbolicEntityId.resolve] its [SymbolicEntityId], or by [resolving][EntityReference.resolve] an [EntityReference], or
|
||||
* iterating by children of another entity:
|
||||
* ```
|
||||
* WorkspaceModel.getInstance(project).updateProjectModel("Update module") { builder ->
|
||||
* val module = ModuleId(moduleName).resolve(builder) ?: ...
|
||||
* val groupPath = module.groupPath ?: ...
|
||||
* builder.removeEntity(groupPath)
|
||||
* //a special extension function 'modifyEntity' is generated for each entity type
|
||||
* builder.modifyEntity(module) {
|
||||
* name = prefix + name
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* ## Adding and removing child entities
|
||||
*
|
||||
* There are two equivalent ways to add a child entity to an existing parent entity:
|
||||
* * specify parent when creating the child, and add the child via [addEntity]:
|
||||
* ```
|
||||
* val contentRoot = ContentRootEntity(url, emptyList(), entitySource) {
|
||||
* this.module = module
|
||||
* }
|
||||
* builder.addEntity(contentRoot)
|
||||
* ```
|
||||
* * call [modifyEntity] on the parent entity and modify its property to include the new child:
|
||||
* ```
|
||||
* val contentRoot = ContentRootEntity(url, emptyList(), entitySource)
|
||||
* builder.modifyEntity(module) {
|
||||
* contentRoots = contentRoots + contentRoot
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* In order to remove a child entity, it's enough to call [removeEntity] for it, the reference in its parent will be update automatically.
|
||||
* Also, if the reference to the parent is declared as non-null in the child interface, it's enough to modify reference to the children
|
||||
* in the parent entity:
|
||||
* ```
|
||||
* builder.modifyEntity(module) {
|
||||
* contentRoots = contentRoots.filter { it.url != contentUrlToRemove }
|
||||
* }
|
||||
* ```
|
||||
* If you do that for a child with nullable reference to the parent, the child will be detached from the parent but won't be removed from
|
||||
* the storage.
|
||||
*
|
||||
* ## Batch operations
|
||||
* Besides operation with individual entities, [MutableEntityStorage] supports two batch operations: [addDiff] and [replaceBySource].
|
||||
*
|
||||
|
||||
+3
-1
@@ -2,8 +2,8 @@
|
||||
|
||||
package com.intellij.platform.workspace.storage.tests
|
||||
|
||||
import com.intellij.platform.workspace.storage.testEntities.entities.*
|
||||
import com.intellij.platform.workspace.storage.MutableEntityStorage
|
||||
import com.intellij.platform.workspace.storage.testEntities.entities.*
|
||||
import junit.framework.TestCase.*
|
||||
import org.junit.Test
|
||||
|
||||
@@ -116,6 +116,8 @@ class AddChildrenTest {
|
||||
builder.modifyEntity(right) {
|
||||
this.children = listOf(MiddleEntity("prop", MySource))
|
||||
}
|
||||
|
||||
assertEquals(right, builder.entities(MiddleEntity::class.java).single().parentEntity)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user