Opening of file from plugin

Does anybody have any idea why this wouldn’t work?


VirtualFile file = getVirtualFile();
int textOffset = getTextOffset();

if(file!=null)
{
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
Editor editor = null;

fileEditorManager.openFile(file, true);
editor = fileEditorManager.getSelectedTextEditor();
editor.getCaretModel().moveToOffset(textOffset);
logger.debug("File is open cursor moved to: "+textOffset);
}

It actually opens the file the first time around (but doesn't move the caret), then subsequent calls have no effect. I have tried shutting the file down before re-opening but this didn’t work either.

Basically I have added a toolbar component that displays a list when the user clicks on an item, the relevant file is opened (supposedly:). After this is called once and the file closed there is no way to open the file again, even with the project explorer!

Any ideas or does anyone have any code that will open a virtual file correctly??

And if you have time then why doesn't the caret move??

Thanks in advance.

Laurence

4 comments
Comment actions Permalink

Hi,

FileEditorManager.getInstance(result.getManager().getProject()).openTextEditor(
new OpenFileDescriptor(
virtualFile,
textOffset
),
true // request focus to editor
);

Laurence wrote:

Does anybody have any idea why this wouldn?t work?


VirtualFile file = getVirtualFile();
int textOffset = getTextOffset();

if(file!=null)
{
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
Editor editor = null;

fileEditorManager.openFile(file, true);
editor = fileEditorManager.getSelectedTextEditor();
editor.getCaretModel().moveToOffset(textOffset);
logger.debug("File is open cursor moved to: "+textOffset);
}

It actually opens the file the first time around (but doesn't move the caret), then subsequent calls have no effect. I have tried shutting the file down before re-opening but this didn?t work either.

Basically I have added a toolbar component that displays a list when the user clicks on an item, the relevant file is opened (supposedly:). After this is called once and the file closed there is no way to open the file again, even with the project explorer!

Any ideas or does anyone have any code that will open a virtual file correctly??

And if you have time then why doesn't the caret move??

Thanks in advance.

Laurence



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Must have missed that one in the API. That worked great thanks.

0
Comment actions Permalink

The only thing is it still once the file has been closed, it will not open again. Any ideas as to why this is?

0
Comment actions Permalink

And it only seems to do this in certain projects, so sometimes it works!

0

Please sign in to leave a comment.