How to obtain scratch file tab name (for a DB connection)

By overriding 


I can get the VirtualFile that I currently switched to (any source file opened in a project in IntelliJ IDEA).

I use the getName() method to get the file name (without the path), and display it in my plugin.

If I open a database tool window and double-click a connection (say, MySQL), I will get a new editor with a file, where I can write some DB requests. Turns out that the file is actually a scratch file. If I get its name the same way I did previously, I receive a very long name. getPresentableName() returns the same long name.

However, the editor tab for this file shows a short name (usually the name of the DB connection).

Is there a way I can obtain the name of the scratch as displayed in the tab?

Official comment

com.intellij.openapi.fileEditor.impl.EditorTabPresentationUtil#getEditorTabTitle is correct approach to query the same name as displayed on editor tabs.

After hours of googling and experimenting... This kinda did the job, but not sure if this is the correct approach:

Window window = WindowManagerEx.getInstanceEx().getMostRecentFocusedWindow();
EditorWindow editorWindow = FileEditorManagerEx.getInstanceEx(project).getSplittersFor(window).getCurrentWindow();
String tabTitle = EditorTabPresentationUtil.getEditorTabTitle(project, file, editorWindow);

I am calling this code to display the name of the tab of an Editor that was just opened


Please sign in to leave a comment.