Skip to content

Commit 9853f31

Browse files
committed
feat: new method runWithRepository for overriding the global SRepository
The Git import feature in modelix.core has its own SRepository implementation. It doesn't run inside a project and doesn't register modules in the global repository.
1 parent 075f3c7 commit 9853f31

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.modelix.mps.api
2+
3+
import org.jetbrains.mps.openapi.module.SRepository
4+
5+
object ContextRepository {
6+
private val threadLocal = ThreadLocal<SRepository>()
7+
8+
fun <R> runWith(repository: SRepository, body: () -> R): R {
9+
val oldValue = threadLocal.get()
10+
try {
11+
threadLocal.set(repository)
12+
return body()
13+
} finally {
14+
threadLocal.set(oldValue)
15+
}
16+
}
17+
18+
fun getRepository(): SRepository? = threadLocal.get()
19+
}

api/src/main/kotlin/org/modelix/mps/api/IModelixMpsApi.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import org.jetbrains.mps.openapi.project.Project
1111
import java.awt.Component
1212

1313
interface IModelixMpsApi {
14-
fun getRepository(): SRepository = getProjectRepository() ?: getGlobalRepository()
14+
fun getRepository(): SRepository = ContextRepository.getRepository() ?: getProjectRepository() ?: getGlobalRepository()
1515
fun getGlobalRepository(): SRepository
1616
fun getProjectRepository(): SRepository?
1717
fun getRepository(project: Project): SRepository
@@ -40,4 +40,5 @@ interface IModelixMpsApi {
4040
fun <R> runWithProject(project: Project, body: () -> R): R =
4141
ContextProject.runWith(project, body)
4242
fun <R> runWithProject(project: com.intellij.openapi.project.Project, body: () -> R): R = runWithProject(getMPSProject(project), body)
43+
fun <R> runWithRepository(repository: SRepository, body: () -> R): R = ContextRepository.runWith(repository, body)
4344
}

lib/api/lib.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public final class org/modelix/mps/api/ModelixMpsApi : org/modelix/mps/api/IMode
1818
public fun getVirtualFolders (Lorg/jetbrains/mps/openapi/module/SModule;)Ljava/util/List;
1919
public fun runWithProject (Lcom/intellij/openapi/project/Project;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
2020
public fun runWithProject (Lorg/jetbrains/mps/openapi/project/Project;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
21+
public fun runWithRepository (Lorg/jetbrains/mps/openapi/module/SRepository;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
2122
public fun setReference (Lorg/jetbrains/mps/openapi/model/SNode;Lorg/jetbrains/mps/openapi/language/SReferenceLink;Lorg/jetbrains/mps/openapi/model/SNodeReference;)V
2223
public fun setVirtualFolder (Lorg/jetbrains/mps/openapi/module/SModule;Ljava/lang/String;)V
2324
public fun setVirtualFolder (Lorg/jetbrains/mps/openapi/project/Project;Lorg/jetbrains/mps/openapi/module/SModule;Ljava/lang/String;)V

0 commit comments

Comments
 (0)