Opening file in editor without moving focus to it

Hello,

I'm implementing a custom language plugin where I create a new file via a quick fix and open it in the editor.
This works great but I have found no way of not moving editor focus to the newly opened file.

To circumvent this I'm currently "opening" the file this call comes from again so the focus moves back immediately but this feels kindof hacky.
The code for opening the files currently looks like this:

FileEditorManager.getInstance(project).openFile(createdFile, false);
FileEditorManager.getInstance(project).openFile(currentFile, true);

I personally thought that the second argument focusEditor controlled this behavior but that is apparently false.
Sending in true or false seems to have no effect.

7 comments

In Project window you can open editor with Enter, it calls openFile() with <false>, original focus owner (project tree) doesn't lose the focus. What is original focus owner in your case?

0

Hi,

Thank you for taking the time to answer my question.
Am I understanding you correctly that focus is related things like project tree and text editor?
In my case the editor is already focused in the text editor and that part doesn't lose focus.
I was thinking about focus as in individual files open in the text editor.

Opening a new file moves the focus to that file but the text editor is always in focus in my case.
If the focus in openFile() is related to bigger areas then everything is working as it should.
I was thinking about the focus parameter as should focus be moved to the newly opened file, yes or no.
The solution I have now kind of works but on slower computers there is a small flash when the new file is opened and I immediately move focus back to the file the call is made from.
I'm still hoping to find a better way to solve this.

To explain exactly what I want to do: I have a quickfix that can be triggered in certain circumstances when working with a file in the text editor. When you trigger this quickfix you get to choose a folder and a new file is created in this folder. For convenience I want to open this file for the user but keep them in the file that they're working in, not the newly created file.

0

Oh, I see. You want to add editor tab but without selection. If it is what you want I'm afraid you cannot do it only with API abilities.

0

Do you know how I can do this in some other way?
Are there other classes/methods for doing this?

0

I don't think so. It's very unusual usecase.

0

Just as a sidenote, i don't think that the usecase is unusual. In browsers for example, it is very common to open tabs in the background. Nevertheless, i think it would be a nice addition to the Open API.

0

Please sign in to leave a comment.