fix: make workspace handler logic more error-resistant

This commit is contained in:
FalsePattern 2025-03-13 23:02:46 +01:00
parent 4f9c324af1
commit c6f3a8202b
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
2 changed files with 17 additions and 26 deletions

View file

@ -21,6 +21,7 @@ Changelog structure reference:
- Project - Project
- Occasional "AWT events are not allowed inside write action" error coming from LSP - Occasional "AWT events are not allowed inside write action" error coming from LSP
- IllegalStateException coming from the standard library handler
## [22.0.0] ## [22.0.0]

View file

@ -79,31 +79,23 @@ class ZigSyntheticLibrary(val project: Project) : SyntheticLibrary(), ItemPresen
suspend fun reload(project: Project, state: ZigProjectSettings) { suspend fun reload(project: Project, state: ZigProjectSettings) {
val moduleId = ModuleId(ZIG_MODULE_ID) val moduleId = ModuleId(ZIG_MODULE_ID)
val workspaceModel = WorkspaceModel.getInstance(project) val workspaceModel = WorkspaceModel.getInstance(project)
if (moduleId !in workspaceModel.currentSnapshot) {
val baseModuleDir = project.guessProjectDir()?.toVirtualFileUrl(workspaceModel.getVirtualFileUrlManager()) ?: return
val moduleEntitySource = LegacyBridgeJpsEntitySourceFactory.getInstance(project)
.createEntitySourceForModule(baseModuleDir, null)
val moduleEntity = ModuleEntity(ZIG_MODULE_ID, emptyList(), moduleEntitySource)
workspaceModel.update("Add new module") {builder ->
builder.addEntity(moduleEntity)
}
}
val moduleEntity = workspaceModel.currentSnapshot.resolve(moduleId) ?: return
val root = getRoot(state, project) ?: return val root = getRoot(state, project) ?: return
val libRoot = LibraryRoot(root.toVirtualFileUrl(workspaceModel.getVirtualFileUrlManager()), LibraryRootTypeId.SOURCES) val libRoot = LibraryRoot(root.toVirtualFileUrl(workspaceModel.getVirtualFileUrlManager()), LibraryRootTypeId.SOURCES)
val libraryTableId = LibraryTableId.ProjectLibraryTableId val libraryTableId = LibraryTableId.ProjectLibraryTableId
val libraryId = LibraryId(ZIG_LIBRARY_ID, libraryTableId) val libraryId = LibraryId(ZIG_LIBRARY_ID, libraryTableId)
if (libraryId in workspaceModel.currentSnapshot) { val baseModuleDir = project.guessProjectDir()?.toVirtualFileUrl(workspaceModel.getVirtualFileUrlManager()) ?: return
val library = workspaceModel.currentSnapshot.resolve(libraryId) ?: return workspaceModel.update("Update Zig std") { builder ->
workspaceModel.update("Update library") { builder -> builder.resolve(moduleId)?.let { moduleEntity ->
builder.modifyLibraryEntity(library) { builder.removeEntity(moduleEntity)
roots.clear()
roots.add(libRoot)
} }
val moduleEntitySource = LegacyBridgeJpsEntitySourceFactory.getInstance(project)
.createEntitySourceForModule(baseModuleDir, null)
val moduleEntity = builder.addEntity(ModuleEntity(ZIG_MODULE_ID, emptyList(), moduleEntitySource))
builder.resolve(libraryId)?.let { libraryEntity ->
builder.removeEntity(libraryEntity)
} }
} else {
val libraryEntitySource = LegacyBridgeJpsEntitySourceFactory val libraryEntitySource = LegacyBridgeJpsEntitySourceFactory
.getInstance(project) .getInstance(project)
.createEntitySourceForProjectLibrary(null) .createEntitySourceForProjectLibrary(null)
@ -114,13 +106,11 @@ class ZigSyntheticLibrary(val project: Project) : SyntheticLibrary(), ItemPresen
) { ) {
roots.add(libRoot) roots.add(libRoot)
} }
workspaceModel.update("Add new library") { builder ->
builder.addEntity(libraryEntity) builder.addEntity(libraryEntity)
}
}
workspaceModel.update("Link dep") { builder ->
builder.modifyModuleEntity(moduleEntity) { builder.modifyModuleEntity(moduleEntity) {
dependencies.add(LibraryDependency(libraryId, false, DependencyScope.COMPILE)) val dep = LibraryDependency(libraryId, false, DependencyScope.COMPILE)
dependencies.clear()
dependencies.add(dep)
} }
} }
} }