Intercept file open to prevent new editor tab

Hello,

We are currently working on a plugin that adds a custom editor. We have implemented a FileEditorProvider that accepts files with certain extensions, and creates a custom FileEditor. It works very well; however, after the first editor is open, we would like to somehow intercept any attempt to open a file with the supported extensions, prevent opening a new editor tab, and instead obtain the already opened editor and perform some custom logic.

What we've tried: in FileEditorProvider.createEditor(), we return the already existing editor, but a new editor tab is still opened, with no content. Any way to prevent this? We've also tried registering an AnActionListener to intercept the open action, but it does not seem to be called on double-click.

Any pointers on how to achieve this would be very appreciated,

Mariana

5 comments
Comment actions Permalink

Right now there is no API to achieve that. You're welcome to file a feature request in YouTrack.

Alternatively, you can allow the user to open any files of your type as regular text, and add a yellow bar suggesting them to switch to your main custom editor.

0
Comment actions Permalink

I see, thank you for your reply!

On further investigation, we reached this situation: say the user double-clicks file1 in the navigator. Our custom FileEditorProvider creates a new FileEditor, which is displayed in a new editor tab (titled file1). Next, the user double-clicks file2; we return the previously created FileEditor. A new editor tab is opened (titled file2), but it shows no content. Switching to the file1 editor tab displays a "This view is not available until indices are built", then switching back to file2 editor tab displayed our FileEditor, with the content updated via our custom logic.

Question: maybe we could close the first editor tab in our FileEditorProvider when createEditor() is called for file2? And how would we "force" the previously created editor content to be displayed when file2 is opened?

Thank you in advance,

Mariana

0
Comment actions Permalink

Once again: what you're trying to do is unsupported. You may be able to achieve something that looks like it works, but it's fairly likely to break other plugins and to cause issues in situations that you didn't think of testing (e.g. editors open in separate windows, split editors etc.) Please consider designing your plugin in such a way that it relies only on intended usage of the platform APIs.

0
Comment actions Permalink

The reply above sounded too negative; sorry about that.

One thing you can do with the existing APIs is use the TreeStructureProvider interface to modify the presentation of your files in the project view. You can replace regular file nodes with custom nodes which will navigate to your single custom editor tab when the user double-clicks them or presses F4. The user will still be able to navigate to the underlying files and edit them as text if they use the Goto File shortcut or some other navigation feature that gives direct access to files (Find in Path, for example).

Will such an approach work for you?

0
Comment actions Permalink

No worries; the reason why we asked this question here was exactly because we wanted to use the API as intended and avoid any hacks.

The TreeStructureProvider approach sounds like just the thing we need, thank you very much!

Mariana

0

Please sign in to leave a comment.