【问题标题】:Intellij IDEA Plugin to read and print the contents in a IDE editor in consoleIntellij IDEA 插件,用于在控制台的 IDE 编辑器中读取和打印内容
【发布时间】:2019-01-13 13:48:35
【问题描述】:

我是使用 gradle 开发 Intellij Idea 插件的新手!我希望开发一个简单的插件来读取java类的内容并以实时方式在控制台(工具窗口)中打印它(即当我在java类中键入一个新单词时,它应该在控制台中打印工作,即使课程是否保存)

目前我指的是https://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_components.html 中的 Intellij 插件架构和组件。我遇到了诸如编辑器窗格之类的概念但是我不知道如何阅读 IDE 编辑器中的内容(当前的 java 文件)!我该怎么做?

【问题讨论】:

  • @Meo thanx 但我想编写自己的插件
  • 如何阅读编辑器:PlatformDataKeys.EDITOR.getData(anActionEvent.getDataContext()).getDocument().getText()
  • @meo 你能给我一个链接吗?

标签: intellij-idea plugins console printf editor


【解决方案1】:

您可以获取编辑器窗口的原始文本:

Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
editor.getDocument().getText();

如果你想从编辑器窗口的内容中获取一些结构,你可以使用PsiFile API:

PsiFile psi = anActionEvent.getData(CommonDataKeys.PSI_FILE);

PsiFile API 允许您以任何有意义的语言浏览文件。例如,对于 Java 文件,有一个 PsiJavaFile 接口知道 Java 特定的功能,如包名、导入等。

http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/psi_files.html

最后,要打印一条消息,您可以尝试普通的 System.out.print() 或者您可以使用 ConsoleView 类来使用 IntelliJ 控制台工具视图:

TextConsoleBuilderFactory.getInstance()
                         .createBuilder(anActionEvent.getProject())
                         .getConsole()
                         .print("Hello", ConsoleViewContentType.NORMAL_OUTPUT);

请注意:以上所有代码均假设您使用的是ActionEvent。您可能想查看TypedActionHandler 界面,以便在编辑器文本更改时收到通知:

http://www.jetbrains.org/intellij/sdk/docs/tutorials/editor_basics/editor_events.html#handling-keystrokes-in-the-editor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-07
    • 2021-11-16
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多