Skip to content

Commit

Permalink
fix(mps-model-adapters): add fallbacks for properties and childLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuster23 committed Feb 21, 2024
1 parent 1126052 commit 3fbf248
Showing 1 changed file with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ package org.modelix.model.mpsadapters
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations
import jetbrains.mps.smodel.MPSModuleRepository
import jetbrains.mps.smodel.adapter.MetaAdapterByDeclaration
import jetbrains.mps.smodel.adapter.ids.SContainmentLinkId
import jetbrains.mps.smodel.adapter.ids.SPropertyId
import jetbrains.mps.smodel.adapter.ids.SReferenceLinkId
import jetbrains.mps.smodel.adapter.structure.link.SContainmentLinkAdapterById
import jetbrains.mps.smodel.adapter.structure.property.SPropertyAdapterById
import jetbrains.mps.smodel.adapter.structure.ref.SReferenceLinkAdapterById
import org.jetbrains.mps.openapi.language.SContainmentLink
import org.jetbrains.mps.openapi.model.SNode
import org.modelix.model.api.BuiltinLanguages
import org.modelix.model.api.ConceptReference
Expand Down Expand Up @@ -77,9 +82,8 @@ data class MPSNode(val node: SNode) : IDeprecatedNodeDefaults {
}

override fun moveChild(role: IChildLink, index: Int, child: INode) {
require(role is MPSChildLink) { "role must be an MPSChildLink" }
val link = getMPSContainmentLink(role)

val link = role.link
val children = node.getChildren(link).toList()
require(index <= children.size) { "index out of bounds: $index > ${children.size}" }

Expand All @@ -95,9 +99,8 @@ data class MPSNode(val node: SNode) : IDeprecatedNodeDefaults {
}

override fun addNewChild(role: IChildLink, index: Int, concept: IConcept?): INode {
require(role is MPSChildLink) { "role must be an MPSChildLink" }
val link = getMPSContainmentLink(role)

val link = role.link
val children = node.getChildren(link).toList()
require(index <= children.size) { "index out of bounds: $index > ${children.size}" }

Expand Down Expand Up @@ -160,7 +163,18 @@ data class MPSNode(val node: SNode) : IDeprecatedNodeDefaults {
}

override fun setPropertyValue(property: IProperty, value: String?) {
val mpsProperty = node.properties.first { MPSProperty(it).getUID() == property.getUID() }
val mpsProperty = when (property) {
is MPSProperty -> property.property
else -> node.properties.find { MPSProperty(it).getUID() == property.getUID() }
?: node.concept.properties.find { MPSProperty(it).getUID() == property.getUID() }
?: SPropertyAdapterById(SPropertyId.deserialize(property.getUID()), "")
}
node.setProperty(mpsProperty, value)
}

private fun getMPSContainmentLink(childLink: IChildLink): SContainmentLink = when (childLink) {
is MPSChildLink -> childLink.link
else -> node.concept.containmentLinks.find { MPSChildLink(it).getUID() == childLink.getUID() }
?: SContainmentLinkAdapterById(SContainmentLinkId.deserialize(childLink.getUID()), "")
}
}

0 comments on commit 3fbf248

Please sign in to comment.