Skip to content

Commit

Permalink
feat(bulk-model-sync-lib): add childFilter for ModelImporter
Browse files Browse the repository at this point in the history
This is now needed to only sync the included modules.
Otherwise the importer would try to delete all other modules.
  • Loading branch information
mhuster23 committed Feb 20, 2024
1 parent 621f5e8 commit df02d20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import kotlin.jvm.JvmName
*
* @param root the root node to be updated
*/
class ModelImporter(private val root: INode, private val continueOnError: Boolean) {
class ModelImporter(
private val root: INode,
private val continueOnError: Boolean,
private val childFilter: (INode) -> Boolean = { true },
) {

private val originalIdToExisting: MutableMap<String, INode> = mutableMapOf()
private val postponedReferences = ArrayList<() -> Unit>()
Expand Down Expand Up @@ -123,7 +127,7 @@ class ModelImporter(private val root: INode, private val continueOnError: Boolea
val allRoles = (expectedParent.children.map { it.role } + existingParent.allChildren.map { it.roleInParent }).distinct()
for (role in allRoles) {
val expectedNodes = expectedParent.children.filter { it.role == role }
val existingNodes = existingParent.getChildren(role).toList()
val existingNodes = existingParent.getChildren(role).filter(childFilter).toList()

// optimization that uses the bulk operation .addNewChildren
if (existingNodes.isEmpty() && expectedNodes.all { originalIdToExisting[it.originalId()] == null }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.ProjectManager
import jetbrains.mps.ide.project.ProjectHelper
import org.jetbrains.mps.openapi.module.SRepository
import org.modelix.model.api.BuiltinLanguages
import org.modelix.model.api.INode
import org.modelix.model.mpsadapters.MPSModuleAsNode
import org.modelix.model.mpsadapters.MPSRepositoryAsNode
import org.modelix.model.sync.bulk.ModelExporter
Expand Down Expand Up @@ -78,8 +80,15 @@ object MPSBulkSynchronizer {
access.runWriteInEDT {
access.executeCommand {
val repoAsNode = MPSRepositoryAsNode(repository)

// Without the filter MPS would attempt to delete all modules that are not included
fun moduleFilter(node: INode): Boolean {
if (node.getConceptReference()?.getUID() != BuiltinLanguages.MPSRepositoryConcepts.Module.getUID()) return true
val moduleName = node.getPropertyValue(BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name) ?: return false
return isModuleIncluded(moduleName, includedModuleNames, includedModulePrefixes)
}
println("Importing modules...")
ModelImporter(repoAsNode, continueOnError).importFilesAsRootChildren(jsonFiles)
ModelImporter(repoAsNode, continueOnError, childFilter = ::moduleFilter).importFilesAsRootChildren(jsonFiles)
println("Import finished.")
}
}
Expand Down

0 comments on commit df02d20

Please sign in to comment.