From c6f3a8202be0f1c95602449fdd08e724de77d61d Mon Sep 17 00:00:00 2001 From: FalsePattern Date: Thu, 13 Mar 2025 23:02:46 +0100 Subject: [PATCH] fix: make workspace handler logic more error-resistant --- CHANGELOG.md | 1 + .../toolchain/stdlib/ZigSyntheticLibrary.kt | 42 +++++++------------ 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e6e4acb..8bea6c20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Changelog structure reference: - Project - Occasional "AWT events are not allowed inside write action" error coming from LSP + - IllegalStateException coming from the standard library handler ## [22.0.0] diff --git a/core/src/main/kotlin/com/falsepattern/zigbrains/project/toolchain/stdlib/ZigSyntheticLibrary.kt b/core/src/main/kotlin/com/falsepattern/zigbrains/project/toolchain/stdlib/ZigSyntheticLibrary.kt index 3ac2ad89..5b7fac8d 100644 --- a/core/src/main/kotlin/com/falsepattern/zigbrains/project/toolchain/stdlib/ZigSyntheticLibrary.kt +++ b/core/src/main/kotlin/com/falsepattern/zigbrains/project/toolchain/stdlib/ZigSyntheticLibrary.kt @@ -79,31 +79,23 @@ class ZigSyntheticLibrary(val project: Project) : SyntheticLibrary(), ItemPresen suspend fun reload(project: Project, state: ZigProjectSettings) { val moduleId = ModuleId(ZIG_MODULE_ID) 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 libRoot = LibraryRoot(root.toVirtualFileUrl(workspaceModel.getVirtualFileUrlManager()), LibraryRootTypeId.SOURCES) val libraryTableId = LibraryTableId.ProjectLibraryTableId val libraryId = LibraryId(ZIG_LIBRARY_ID, libraryTableId) - if (libraryId in workspaceModel.currentSnapshot) { - val library = workspaceModel.currentSnapshot.resolve(libraryId) ?: return - workspaceModel.update("Update library") { builder -> - builder.modifyLibraryEntity(library) { - roots.clear() - roots.add(libRoot) - } + val baseModuleDir = project.guessProjectDir()?.toVirtualFileUrl(workspaceModel.getVirtualFileUrlManager()) ?: return + workspaceModel.update("Update Zig std") { builder -> + builder.resolve(moduleId)?.let { moduleEntity -> + builder.removeEntity(moduleEntity) + } + 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 .getInstance(project) .createEntitySourceForProjectLibrary(null) @@ -114,13 +106,11 @@ class ZigSyntheticLibrary(val project: Project) : SyntheticLibrary(), ItemPresen ) { roots.add(libRoot) } - workspaceModel.update("Add new library") { builder -> - builder.addEntity(libraryEntity) - } - } - workspaceModel.update("Link dep") { builder -> + builder.addEntity(libraryEntity) builder.modifyModuleEntity(moduleEntity) { - dependencies.add(LibraryDependency(libraryId, false, DependencyScope.COMPILE)) + val dep = LibraryDependency(libraryId, false, DependencyScope.COMPILE) + dependencies.clear() + dependencies.add(dep) } } }