OpenFile in IDEA and goto Line#

I would like to have some code that when invoked would open the given file, or focus it if it is open and pop the cursor to the given line.

public void gotoLine(String strFile, String strLineNo)
// where strFile is the full class path
// com.mine.yours.MyClass
// and strLineNo is the line number "34"


Comment actions Permalink

This code can get the file:

But how to load it in IDEA and goto the lini number. Well play a little with IDEA's


and you probably will get closer. And remember if you find a solution, please post it here.

Comment actions Permalink

I find no reference to PsiManager or PsiJavaFile in the openapi?


Comment actions Permalink

The PsiXXX is not an official part of the openAPI, and might change in the future. But I think you need to use it if you need to go to a certain linenumber. So you your imagination, the codehelper and look at the other plugins.

Comment actions Permalink


Did you get the solution. Can you post it.


Comment actions Permalink

Create com.intellij.openapi.fileEditor.OpenFileDescriptor. You will need VirtualFile to do that. VirtualFile is easly retrieved when you have com.intellij.psi.PsiFile.
Then use one of its navigate* methods.

I hope it helps,


Comment actions Permalink

Thanks for the reply..

To goto line #, i did somthing like this

    public boolean gotoLine(int lineNumber) {

        DataContext dataContext = DataManager.getInstance().getDataContext();
        Project project = getProject(dataContext);

        Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();

        if( editor == null )
            return false;

        CaretModel caretModel = editor.getCaretModel();
        int totalLineCount = editor.getDocument().getLineCount();

        if( lineNumber > totalLineCount )
            return false;

        //Moving caret to line number
        caretModel.moveToLogicalPosition(new LogicalPosition(lineNumber-1,0));

        //Scroll to the caret
        ScrollingModel scrollingModel = editor.getScrollingModel();

        return true;

Comment actions Permalink

another easy way (not complete openapi)to implement a scrollToSource feature is:

use AutoScrollToSourceHandler, DataProvider

inside getData() do something like:

if ("Navigatable".equals(s)) {

return new OpenFileDescriptor(_project, virtualFile, line, column);


Please sign in to leave a comment.