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"
}
请先登录再写评论。
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.
I find no reference to PsiManager or PsiJavaFile in the openapi?
--ekiM
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.
Hey,
Did you get the solution. Can you post it.
Regards
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
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;
}
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);
}