diff --git a/plugins/ByteCodeViewer/src/com/intellij/byteCodeViewer/BytecodeToolWindowService.kt b/plugins/ByteCodeViewer/src/com/intellij/byteCodeViewer/BytecodeToolWindowService.kt index e4275785df75..efcaec80b0c1 100644 --- a/plugins/ByteCodeViewer/src/com/intellij/byteCodeViewer/BytecodeToolWindowService.kt +++ b/plugins/ByteCodeViewer/src/com/intellij/byteCodeViewer/BytecodeToolWindowService.kt @@ -1,13 +1,14 @@ // Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.byteCodeViewer +import com.intellij.filename.UniqueNameBuilder import com.intellij.ide.ui.UISettings import com.intellij.openapi.components.Service import com.intellij.openapi.project.Project import com.intellij.openapi.util.Disposer +import com.intellij.openapi.util.NlsSafe import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.wm.ToolWindow -import com.intellij.ui.content.Content import com.intellij.ui.content.ContentManagerEvent import com.intellij.ui.content.ContentManagerListener import java.util.concurrent.ConcurrentHashMap @@ -61,31 +62,28 @@ class BytecodeToolWindowService(private val project: Project) { * Updates tab titles to avoid duplicate names by adding path information when necessary. */ fun deduplicateTabNames(toolWindow: ToolWindow) { - val titlesToContents = mutableMapOf>() - for (content in toolWindow.contentManager.contents) { - val classFileName = content.getUserData(JAVA_CLASS_FILE)?.name - ?: throw IllegalStateException("Content entry has no JAVA_CLASS_FILE or it is null. Entry: $content") - titlesToContents.getOrPut(classFileName) { mutableListOf() }.add(content) + 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 ((classFileName, contents) in titlesToContents) { - if (contents.size == 1) { - contents[0].displayName = classFileName - } - else if (contents.size > 1) { - val paths = contents.map { - it.getUserData(JAVA_CLASS_FILE) ?: throw IllegalStateException("No class file path for content entry $it") - } - val commonAncestor = VfsUtil.getCommonAncestor(paths) ?: continue + if (javaClassFiles.size == 1) { + toolWindow.contentManager.contents[0].displayName = javaClassFiles[0].name + return + } - for (i in contents.indices) { - val content = contents[i] - content.displayName = VfsUtil.getRelativePath( - content.getUserData(JAVA_CLASS_FILE) ?: throw IllegalStateException("No class file path for content entry $content"), - commonAncestor, - ) - } - } + val commonAncestor = VfsUtil.getCommonAncestor(javaClassFiles)?.path ?: return + val uniqueNameBuilder = UniqueNameBuilder(commonAncestor, "/") + + 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 } }