How do I open a file if I have the path to it in the project?
Hello!
I am new to plugin development for IntelliJ. I would like to make a simple plugin for learning purpose where I can open a file in a project. Let´s say the path to the file is project/src/mypackage/myfile.java. How can I open that file with code (like if I opened it with Ctrl+Shift+N)?
I have looked at VirtualFile and it seems that is the IDEA`s representation of the physical file on the IDEA Virtual File System, right?
Do I have to use FileDocumentManager in some way?
Please sign in to leave a comment.
See com.intellij.ide.actions.GotoFileItemProvider#filterElements how to determine PsiFileSystemItem from given path. Then simply call com.intellij.pom.Navigatable#navigate to open editor.
To get VirtualFile by path use LocalFileSystem.getInstance().findFileByPath(). Or see this doc.
Then call FileEditorManager.getInstance(project).openFile(file, true);
Thanks for linking to a very good FAQ! I solved the question stated in the topic.
Another question, how do I get the line number on witch the mouse pointer is at on a document? I have tried with the below code, but it does not quiet work.
I wouldn't expect getVisualLineStart() and getVisualLineEnd() to return current line especially after reading its docs.
Document.getLineNumber() is ok. CaretModel.getLogicalPosition() should work as well.
CaretModel has several moveXXX() methods to set caret position. See javadocs and see other usages in Community Edition source code.
Can someone point me to the right resource which has the detailed explanation as how to open a file in editor programmatically
Ramyaram2092 please do not 7 years old thread, please start new thread and provide more details on what data you have about "a file" and what you want to achieve