Reputation: 135
I am new to Intellij Idea plugin development using gradle! I am hoping to develop a simple plugin to read the contents of java class and print it in the console(Toolwindow) in a live manner(i.e when I type a new word in the java class it should print the work in the console even if the class is saved or not)
Currently I am refering to the Intellij plugin architecture and components in https://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_components.html. I came across concepts such as editor panes and all But I have no idea how to read the contents in IDE editor(current java file)! How can I do it?
Upvotes: 0
Views: 1346
Reputation: 5142
You can grab the raw text of an editor window:
Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
editor.getDocument().getText();
If you want to get some structure from the contents of the editor window, you can use the PsiFile
API:
PsiFile psi = anActionEvent.getData(CommonDataKeys.PSI_FILE);
The PsiFile
API lets you walk through a file in whatever language(s) make sense. For example, for Java files there is a PsiJavaFile
interface that knows about Java specific features like package name, imports, etc.
http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/psi_files.html
Lastly, to print a message you can try normal System.out.print()
or you can use the ConsoleView
class to work with the IntelliJ console tool views:
TextConsoleBuilderFactory.getInstance()
.createBuilder(anActionEvent.getProject())
.getConsole()
.print("Hello", ConsoleViewContentType.NORMAL_OUTPUT);
One note: All of the above code assumes you're working with an ActionEvent
. You might want to check out the TypedActionHandler
interface to get notified when the editor text changes:
Upvotes: 1