diff --git a/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSDependency.kt b/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSDependency.kt new file mode 100644 index 0000000000..3ea166d99e --- /dev/null +++ b/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSDependency.kt @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.modelix.model.mpsadapters.mps + +import org.jetbrains.mps.openapi.module.SDependency +import org.jetbrains.mps.openapi.module.SDependencyScope +import org.jetbrains.mps.openapi.module.SModule +import org.jetbrains.mps.openapi.module.SModuleReference +import org.jetbrains.mps.openapi.module.SRepository +import org.modelix.model.api.INode +import org.modelix.model.mpsadapters.MPSModuleDependencyAsNode +import org.modelix.model.mpsadapters.MPSModuleReference + +data class NodeAsMPSDependency(val node: INode, val sRepository: SRepository?) : SDependency { + override fun getScope(): SDependencyScope { + return when (node) { + is MPSModuleDependencyAsNode -> node.dependencyScope + else -> null + } ?: error("Node is not a valid dependency") + } + + override fun isReexport(): Boolean { + return when (node) { + is MPSModuleDependencyAsNode -> node.reexport + else -> error("Node is not a valid dependency") + } + } + + override fun getTargetModule(): SModuleReference { + return when (node) { + is MPSModuleDependencyAsNode -> node.moduleReference + else -> null + } ?: error("Node is not a valid dependency") + } + + override fun getTarget(): SModule? { + val ref = MPSModuleReference(targetModule) + return node.getArea().resolveNode(ref)?.let { NodeAsMPSModule(it, sRepository) } + } +} diff --git a/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSModel.kt b/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSModel.kt index 7ce662d058..c3456153da 100644 --- a/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSModel.kt +++ b/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSModel.kt @@ -40,7 +40,7 @@ import org.modelix.model.mpsadapters.MPSConcept import org.modelix.model.mpsadapters.MPSModelReference import org.modelix.model.mpsadapters.Model -class NodeAsMPSModel private constructor(private val node: INode, private val sRepository: SRepository?) : SModel { +data class NodeAsMPSModel(val node: INode, val sRepository: SRepository?) : SModel { companion object { fun wrap(modelNode: INode, repository: SRepository?): SModel = NodeAsMPSModel(modelNode, repository) } @@ -137,15 +137,4 @@ class NodeAsMPSModel private constructor(private val node: INode, private val sR } override fun getProblems() = emptyList() - - override fun equals(other: Any?) = - if (this === other) { - true - } else if (other == null || other !is NodeAsMPSModel) { - false - } else { - node != other.node - } - - override fun hashCode() = 31 + node.hashCode() } diff --git a/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSModule.kt b/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSModule.kt index e0722eb99f..0d3039e7ad 100644 --- a/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSModule.kt +++ b/mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/mps/NodeAsMPSModule.kt @@ -18,15 +18,24 @@ package org.modelix.model.mpsadapters.mps import jetbrains.mps.smodel.MPSModuleRepository import org.jetbrains.mps.openapi.language.SLanguage +import org.jetbrains.mps.openapi.model.SModel import org.jetbrains.mps.openapi.model.SModelId +import org.jetbrains.mps.openapi.module.SDependency import org.jetbrains.mps.openapi.module.SModule +import org.jetbrains.mps.openapi.module.SModuleFacet +import org.jetbrains.mps.openapi.module.SModuleId import org.jetbrains.mps.openapi.module.SModuleListener +import org.jetbrains.mps.openapi.module.SModuleReference import org.jetbrains.mps.openapi.module.SRepository +import org.jetbrains.mps.openapi.persistence.PersistenceFacade import org.modelix.model.api.BuiltinLanguages import org.modelix.model.api.INode +import org.modelix.model.api.getDescendants +import org.modelix.model.mpsadapters.MPSJavaModuleFacetAsNode +import org.modelix.model.mpsadapters.MPSModuleReference import org.modelix.model.mpsadapters.Module -class NodeAsMPSModule private constructor(val node: INode, val sRepository: SRepository?) : SModule { +data class NodeAsMPSModule(val node: INode, val sRepository: SRepository?) : SModule { companion object { fun wrap(modelNode: INode, repository: SRepository?): SModule = NodeAsMPSModule(modelNode, repository) @@ -38,21 +47,42 @@ class NodeAsMPSModule private constructor(val node: INode, val sRepository: SRep override fun addModuleListener(listener: SModuleListener?) = throw UnsupportedOperationException("Not implemented") - override fun getDeclaredDependencies() = throw UnsupportedOperationException("Not implemented") + override fun getDeclaredDependencies(): MutableIterable { + val dependencies = node.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Module.dependencies) + return dependencies.map { NodeAsMPSDependency(node, sRepository) }.toMutableList() + } - override fun getFacets() = throw UnsupportedOperationException("Not implemented") + override fun getFacets(): MutableIterable { + val facets = node.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Module.facets) + return facets.mapNotNull { (it as? MPSJavaModuleFacetAsNode)?.facet }.toMutableList() + } - override fun getModel(id: SModelId?) = throw UnsupportedOperationException("Not implemented") + override fun getModel(id: SModelId?): SModel? { + if (id == null) return null + val modelId = PersistenceFacade.getInstance().asString(id) + val model = node.getDescendants(false).firstOrNull { + it.reference.serialize().endsWith(modelId) + } + return model?.let { NodeAsMPSModel(it, repository) } + } override fun getModelRoots() = throw UnsupportedOperationException("Not implemented") override fun getModels() = node.getChildren(Module.models).map { NodeAsMPSModel.wrap(it, sRepository) } - override fun getModuleId() = throw UnsupportedOperationException("Not implemented") + override fun getModuleId(): SModuleId { + val serialized = checkNotNull(node.getPropertyValue(BuiltinLanguages.MPSRepositoryConcepts.Module.id)) { + "Module id was null for $node" + } + return PersistenceFacade.getInstance().createModuleId(serialized) + } override fun getModuleName() = node.getPropertyValue(BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name) - override fun getModuleReference() = throw UnsupportedOperationException("Not implemented") + override fun getModuleReference(): SModuleReference { + val serialized = node.reference.serialize().substringAfter(MPSModuleReference.PREFIX) + return PersistenceFacade.getInstance().createModuleReference(serialized) + } override fun getRepository(): SRepository = sRepository ?: MPSModuleRepository.getInstance()