From 3eecfc24b7f62ca03ccd4cffd526cdb781dd13a5 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 7 Jul 2025 12:52:38 +0200 Subject: [PATCH] [ByteCodeViewer] IDEA-375222 Update tab names when the setting is turned off Previously, if the setting was on, and we created some tabs, and then we turned the setting off, the existing tabs' names were not updated. Now they are updated. GitOrigin-RevId: a7c4c0ff6e9aa49fc26791e7738ae34b56b0c3fd --- .../BytecodeToolWindowService.kt | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/plugins/ByteCodeViewer/src/com/intellij/byteCodeViewer/BytecodeToolWindowService.kt b/plugins/ByteCodeViewer/src/com/intellij/byteCodeViewer/BytecodeToolWindowService.kt index 89d2d98b55f5..505ecefe1c15 100644 --- a/plugins/ByteCodeViewer/src/com/intellij/byteCodeViewer/BytecodeToolWindowService.kt +++ b/plugins/ByteCodeViewer/src/com/intellij/byteCodeViewer/BytecodeToolWindowService.kt @@ -38,19 +38,19 @@ internal class BytecodeToolWindowService(private val project: Project) { val messageBusConnection = project.messageBus.connect(toolWindow.disposable) messageBusConnection.subscribe(UISettingsListener.TOPIC, UISettingsListener { - deduplicateTabNames(toolWindow) + updateTabNames(toolWindow) }) val listener = object : ContentManagerListener { override fun contentAdded(event: ContentManagerEvent) { if (UISettings.getInstance().showDirectoryForNonUniqueFilenames) { - deduplicateTabNames(toolWindow) + updateTabNames(toolWindow) } } override fun contentRemoved(event: ContentManagerEvent) { if (UISettings.getInstance().showDirectoryForNonUniqueFilenames) { - deduplicateTabNames(toolWindow) + updateTabNames(toolWindow) } } } @@ -66,31 +66,45 @@ internal class BytecodeToolWindowService(private val project: Project) { } /** - * Updates tab titles to avoid duplicate names by adding path information when necessary. + * Updates tab titles based on UI settings: + * - If showDirectoryForNonUniqueFilenames is true: adds path information to disambiguate names + * - If showDirectoryForNonUniqueFilenames is false: uses just the filename */ - fun deduplicateTabNames(toolWindow: ToolWindow) { + fun updateTabNames(toolWindow: ToolWindow) { val javaClassFiles = toolWindow.contentManager.contents.map { content -> content.getUserData(JAVA_CLASS_FILE) ?: throw IllegalStateException("Content has no JAVA_CLASS_FILE or it is null. Content: $content") } + // For a single tab, always use just the filename if (javaClassFiles.size == 1) { toolWindow.contentManager.contents[0].displayName = javaClassFiles[0].name return } - val commonAncestor = VfsUtil.getCommonAncestor(javaClassFiles)?.path ?: return - val uniqueNameBuilder = UniqueNameBuilder(commonAncestor, "/") + // Check the UI setting to determine how to display tab names + if (UISettings.getInstance().showDirectoryForNonUniqueFilenames) { + // Add path information to disambiguate names + val commonAncestor = VfsUtil.getCommonAncestor(javaClassFiles)?.path ?: return + val uniqueNameBuilder = UniqueNameBuilder(commonAncestor, "/") - for (javaClassFile in javaClassFiles) { - uniqueNameBuilder.addPath(javaClassFile.path, javaClassFile.path) - } + for (javaClassFile in javaClassFiles) { + uniqueNameBuilder.addPath(javaClassFile.path, javaClassFile.path) + } - for (content in toolWindow.contentManager.contents) { - val javaClassFile = content.getUserData(JAVA_CLASS_FILE) - ?: throw IllegalStateException("Content has no JAVA_CLASS_FILE or it is null. Content: $content") - @NlsSafe val displayName = uniqueNameBuilder.getShortPath(javaClassFile.path) - ?: throw IllegalStateException("Cannot get short path for ${javaClassFile.path}") - content.displayName = displayName + for (content in toolWindow.contentManager.contents) { + val javaClassFile = content.getUserData(JAVA_CLASS_FILE) + ?: throw IllegalStateException("Content has no JAVA_CLASS_FILE or it is null. Content: $content") + @NlsSafe val displayName = uniqueNameBuilder.getShortPath(javaClassFile.path) + ?: throw IllegalStateException("Cannot get short path for ${javaClassFile.path}") + content.displayName = displayName + } + } else { + // Use just the filename for each tab + for (content in toolWindow.contentManager.contents) { + val javaClassFile = content.getUserData(JAVA_CLASS_FILE) + ?: throw IllegalStateException("Content has no JAVA_CLASS_FILE or it is null. Content: $content") + content.displayName = javaClassFile.name + } } }