Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6632a57
fix(projectional-editor): keep side transforms when the completion pa…
slisson Oct 23, 2026
e7ac2b9
feat(projectional-editor): support node attributes
slisson Oct 23, 2026
daa49d7
perf(projectional-editor): run the MPS model checkers in the background
slisson Oct 30, 2026
288fecf
fix(mps-plugin): don't block the MPS shutdown while a web editor page…
slisson Oct 30, 2026
7f275cc
fix(mps-plugin): write a reference target as its MPS presentation
slisson Oct 30, 2026
36825c5
fix(mps-plugin): support user objects, setReference and broken refere…
slisson Oct 30, 2026
743131a
test(mps-plugin): set the variable reference by link name
slisson Oct 30, 2026
bd43a50
feat(projectional-editor): cell parameters for notation components
slisson Nov 6, 2026
6866816
refactor(projectional-editor): share baseLanguage wrappers with cell …
slisson Nov 6, 2026
8af1936
feat(projectional-editor): notations for the node attributes of baseL…
slisson Nov 6, 2026
f072211
feat(projectional-editor): context actions menu for adding node attri…
slisson Nov 6, 2026
fa2f74d
feat(projectional-editor): MPS intentions in the context actions menu
slisson Nov 13, 2026
279065f
feat(projectional-editor): quick fixes of the model checker in the co…
slisson Nov 13, 2026
31eadb1
feat(projectional-editor): fit the context actions menu into the visi…
slisson Nov 13, 2026
72d8a0c
fix(mps-plugin): offer only the concepts of the languages a model uses
slisson Nov 13, 2026
810bee8
feat(projectional-editor): type code the way it is written in a text …
slisson Nov 13, 2026
2009dfd
feat(projectional-editor): operator precedence declared in the notation
slisson Nov 13, 2026
1acb465
fix(mps-plugin): every child of a wrapped node has a containment link
slisson Nov 20, 2026
50c46f9
chore(projectional-editor): sandbox with baseLanguage code for trying…
slisson Nov 20, 2026
d3f59cb
fix(mps-plugin): the containment link of a wrapped node is the one MP…
slisson Nov 20, 2026
a194e60
fix(mps-plugin): don't check a deleted node as a root node
slisson Nov 20, 2026
cfc3d87
chore(projectional-editor): don't generate the sandbox
slisson Nov 20, 2026
49acba9
build: drop the kotlinx-html repository on JetBrains Space
slisson Nov 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ subprojects {

repositories {
maven { url = uri("https://artifacts.itemis.cloud/repository/maven-mps/") }
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven") }
mavenCentral()
mavenLocal()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import jetbrains.mps.typechecking.TypecheckingFacade
import jetbrains.mps.typechecking.TypecheckingSession
import jetbrains.mps.typesystem.LegacyTypecheckingQueries
import jetbrains.mps.typesystem.inference.TypeCheckingContext
import jetbrains.mps.util.Cancellable
import org.jetbrains.mps.openapi.language.SConceptFeature
import org.jetbrains.mps.openapi.model.SNode
import org.jetbrains.mps.openapi.util.Consumer
import org.modelix.incremental.IIncrementalEngine
import org.modelix.incremental.TrackableValue
import org.modelix.incremental.incrementalFunction
import org.modelix.model.api.BuiltinLanguages
import org.modelix.model.api.INode
Expand All @@ -29,23 +31,31 @@ import org.modelix.model.mpsadapters.tomps.ModelixNodeAsMPSNode
*
* The typesystem and non-typesystem rules are cached per root node, because type inference is root-wide.
* The constraints are cached per node, so a model change only re-executes them for the nodes whose input changed.
*
* The checks can be cancelled with [runCancellable], e.g. to run them in the background. A cancelled check is not
* cached as failed forever, but re-executed after [retryCancelledChecks].
*/
class IncrementalModelChecker(
private val engine: IIncrementalEngine,
) {
private val constraintsChecker = ConstraintsChecker(null)

/**
* Only read by checks that were cancelled, so that modifying it invalidates exactly those.
*/
private val cancellations = TrackableValue(0)

private val fCheckTypesystemForRoot =
incrementalFunction<List<NodeReportItem>, INode>("checkTypesystemForRoot") { context, node ->
runTypesystemChecks(ModelixNodeAsMPSNode.toMPSNode(node))
cancellable { runTypesystemChecks(ModelixNodeAsMPSNode.toMPSNode(node)) }
}

/**
* [ConstraintsChecker] checks a single node at a time, which allows caching its messages per node.
*/
private val fCheckNodeConstraints =
incrementalFunction<List<NodeReportItem>, INode>("checkNodeConstraints") { context, node ->
runConstraintsCheck(ModelixNodeAsMPSNode.toMPSNode(node))
cancellable { runConstraintsCheck(ModelixNodeAsMPSNode.toMPSNode(node)) }
}

/**
Expand All @@ -55,25 +65,26 @@ class IncrementalModelChecker(
*/
private val fCheckSubtreeConstraints =
incrementalFunction<List<NodeReportItem>, INode>("checkSubtreeConstraints") { context, node ->
val skipCondition = constraintsChecker.skipCondition()
val mpsNode = ModelixNodeAsMPSNode.toMPSNode(node)
if (skipCondition.skipSubtree(mpsNode)) {
emptyList()
} else {
val ownMessages = if (skipCondition.skipSingleNode(mpsNode)) emptyList() else checkNodeConstraints(node)
ownMessages + node.allChildren.flatMap { checkSubtreeConstraints(it) }
cancellable {
val skipCondition = constraintsChecker.skipCondition()
val mpsNode = ModelixNodeAsMPSNode.toMPSNode(node)
if (skipCondition.skipSubtree(mpsNode)) {
emptyList()
} else {
val ownMessages = if (skipCondition.skipSingleNode(mpsNode)) emptyList() else checkNodeConstraints(node)
ownMessages + node.allChildren.flatMap { checkSubtreeConstraints(it) }
}
}
}

private val fGetRootNode =
incrementalFunction<INode, INode>("getRootNode") { context, node ->
if (node.getContainmentLink()?.getUID() ==
BuiltinLanguages.MPSRepositoryConcepts.Model.rootNodes
.getUID()
) {
node
} else {
getRootNode(node.parent ?: return@incrementalFunction node)
cancellable {
if (node.isRootNode()) {
node
} else {
node.parent?.let { getRootNode(it) } ?: node
}
}
}

Expand All @@ -91,6 +102,46 @@ class IncrementalModelChecker(
target: MessageTarget,
): List<NodeReportItem> = getAllMessages(node).filter { it.messageTarget.sameAs(target) }

/**
* The messages of all nodes in the root node that contains [node], grouped by node.
* Returns null if [node] is not part of a model.
*/
fun getMessagesOfRoot(node: INode): Map<INodeReference, List<NodeReportItem>>? = getRootNodeOrNull(node)?.let { checkRoot(it) }

/**
* The root node that contains [node], or null if [node] is not part of a model.
*/
fun getRootNodeOrNull(node: INode): INode? = getRootNode(node).takeIf { it.isRootNode() }

/**
* Executes [body] so that the checks it runs stop early when [isCancelled] returns true.
* Returns null if they were cancelled.
*
* MPS executes model access in a non-cancelable section, so the checks can't be cancelled through the
* ProgressManager.
*/
fun <R : Any> runCancellable(
isCancelled: () -> Boolean,
body: () -> R,
): R? {
val previous = cancellationCheck.get()
cancellationCheck.set(isCancelled)
try {
return body()
} catch (ex: CheckCancelledException) {
return null
} finally {
cancellationCheck.set(previous)
}
}

/**
* Re-executes the checks that were cancelled when they are requested the next time.
*/
fun retryCancelledChecks() {
cancellations.setValue(cancellations.getValue() + 1)
}

private fun checkRoot(rootNode: INode): Map<INodeReference, List<NodeReportItem>> {
val messages = checkTypesystemForRoot(rootNode) + checkSubtreeConstraints(rootNode)
return messages.groupBy { it.node.toModelix() }
Expand All @@ -104,6 +155,40 @@ class IncrementalModelChecker(

private fun getRootNode(node: INode): INode = fGetRootNode(node).bind(engine).invoke()

/**
* Executes the body of an incremental function so that a cancellation isn't cached as its result.
* A cancelled MPS checker returns an incomplete result, which is why the cancellation is checked again afterward.
*/
private fun <R> cancellable(body: () -> R): R {
try {
checkCancelled()
val result = body()
checkCancelled()
return result
} catch (ex: CheckCancelledException) {
cancellations.getValue()
throw ex
}
}

private fun isCancelled() = cancellationCheck.get()?.invoke() == true

private fun checkCancelled() {
if (isCancelled()) throw CheckCancelledException()
}

private class CheckCancelledException : RuntimeException("Model check cancelled")

/**
* A root node is a child of its model. An MPS node that isn't part of a model, like one that was deleted, has the
* role of a root node too, but no parent.
*/
private fun INode.isRootNode() =
parent != null &&
getContainmentLink()?.getUID() ==
BuiltinLanguages.MPSRepositoryConcepts.Model.rootNodes
.getUID()

/**
* Checks the typesystem and the non-typesystem rules in a single typechecking session.
* [jetbrains.mps.typesystemEngine.checker.TypesystemChecker] and
Expand Down Expand Up @@ -147,7 +232,10 @@ class IncrementalModelChecker(
val typeCheckingContext = legacyQueries.typeCheckingContext
try {
typeCheckingContext.setNonTypesystemComputationMode(TypeCheckingContext.NonTypesystemComputationMode.NORMAL)
typeCheckingContext.baseNodeTypesComponent.applyNonTypesystemRulesToRoot(typeCheckingContext)
typeCheckingContext.baseNodeTypesComponent.applyNonTypesystemRulesToRoot(
typeCheckingContext,
Cancellable { isCancelled() },
)
} finally {
typeCheckingContext.setNonTypesystemComputationMode(TypeCheckingContext.NonTypesystemComputationMode.OFF)
}
Expand Down Expand Up @@ -189,6 +277,8 @@ class IncrementalModelChecker(

private val instances = java.util.WeakHashMap<IIncrementalEngine, IncrementalModelChecker>()

private val cancellationCheck = ThreadLocal<(() -> Boolean)?>()

/**
* Returns a checker whose results are cached in the given engine.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.modelix.mps.editor.common

import com.intellij.ide.AppLifecycleListener
import com.intellij.ide.plugins.DynamicPluginListener
import com.intellij.ide.plugins.IdeaPluginDescriptor
import com.intellij.ide.plugins.PluginManager
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import io.ktor.server.application.Application
import io.ktor.server.application.serverConfig
import io.ktor.server.engine.EmbeddedServer
Expand All @@ -9,6 +16,9 @@ import io.ktor.server.netty.Netty
import io.ktor.server.netty.NettyApplicationEngine
import io.ktor.util.logging.KtorSimpleLogger
import kotlinx.coroutines.GlobalScope
import kotlin.concurrent.thread

private val LOG = Logger.getInstance("org.modelix.mps.editor.common.KtorUtils")

fun embeddedServer(
port: Int,
Expand All @@ -33,3 +43,64 @@ fun embeddedServer(
this.connectors += EngineConnectorBuilder().also { it.port = portParam }
})
}

/**
* Like [EmbeddedServer.stop], but returns after [maxWaitMillis] even if the server isn't stopped yet.
*
* Netty terminates an event loop only after the task it is currently running returned. [EmbeddedServer.stop] waits
* for that without any timeout, [gracePeriodMillis] and [timeoutMillis] only apply between tasks. A request handler
* that is blocked in an MPS read action, while stop is called inside a write action, blocks it forever.
*/
fun EmbeddedServer<*, *>.stopBounded(
gracePeriodMillis: Long,
timeoutMillis: Long,
maxWaitMillis: Long,
) {
val stopThread =
thread(name = "modelix-ktor-server-stop", isDaemon = true) {
try {
stop(gracePeriodMillis, timeoutMillis)
} catch (ex: Throwable) {
LOG.warn("Failed to stop the server", ex)
}
}
stopThread.join(maxWaitMillis)
if (stopThread.isAlive) {
LOG.warn("Server didn't stop within $maxWaitMillis ms. A request handler is probably blocked.")
}
}

/**
* Calls [stop] when the application is about to close or when the plugin that contains [pluginClass] is about to be
* unloaded, as long as [parentDisposable] isn't disposed.
*
* Both happen before the services are disposed. That happens inside a write action, where request handlers that wait
* for a read action can't finish and stopping a server has to give up on them (see [stopBounded]).
*/
fun stopBeforeDisposal(
parentDisposable: Disposable,
pluginClass: Class<*>,
stop: () -> Unit,
) {
val pluginId = PluginManager.getPluginByClass(pluginClass)?.pluginId
val connection = ApplicationManager.getApplication().messageBus.connect(parentDisposable)
connection.subscribe(
AppLifecycleListener.TOPIC,
object : AppLifecycleListener {
override fun appWillBeClosed(isRestart: Boolean) {
stop()
}
},
)
connection.subscribe(
DynamicPluginListener.TOPIC,
object : DynamicPluginListener {
override fun beforePluginUnload(
pluginDescriptor: IdeaPluginDescriptor,
isUpdate: Boolean,
) {
if (pluginId != null && pluginDescriptor.pluginId == pluginId) stop()
}
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ class CodeCompletionTest {
val knownDuplicates =
setOf(
"it",
"ParamRef { <shortDescription> <virtualPackage> <param> <smodelAttribute> }",
"StripUnitExpression { <shortDescription> <virtualPackage> <expr> <smodelAttribute> }",
"ValExpression { <shortDescription> <virtualPackage> <smodelAttribute> }"
"ParamRef { <shortDescription> <virtualPackage> <param> }",
"StripUnitExpression { <shortDescription> <virtualPackage> <expr> }",
"ValExpression { <shortDescription> <virtualPackage> }"
)
val actualDuplicates = actions.groupBy { it.getCompletionPattern() }.filter { it.value.size > 1 }.map { it.key }
val unexpectedDuplicates = actualDuplicates - knownDuplicates
Expand Down
Loading
Loading