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.

IE:
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"

}

7 comments
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

editor
manager

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

1
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();
        scrollingModel.scrollToCaret(ScrollType.CENTER);

        return true;
    }

1
Comment actions Permalink

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

--ekiM

0
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.

0
Comment actions Permalink

Hey,

Did you get the solution. Can you post it.

Regards

0
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,

Wojtek

0
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);
}

0

Please sign in to leave a comment.