IntelliJ IDEA插件开发:如何修改Psi树? [英] IntelliJ IDEA plugin development: how to modify the Psi tree?

查看:2426
本文介绍了IntelliJ IDEA插件开发:如何修改Psi树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道创建新PsiElement实例并将它们添加到Psi树的正确方法是什么。

I would like to know what the "proper" way to create new PsiElement instances and add them to the Psi tree is.

我查看了提供的教程JetBrains( http://www.jetbrains.org/intellij/sdk /docs/tutorials/custom_language_support/quick_fix.html )。他们在那里使用的方法似乎适合他们引入的简单属性语言,但我认为这不是采用更复杂语法的方法,我想添加一个不能出现在PsiFile根级别的子PsiElement。

I looked at the tutorial provided by JetBrains (http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/quick_fix.html). The method they use there seems suitable for the simple properties language they introduce but I don't think it's the way to go with a more complicated syntax, where I want to add a child PsiElement that cannot appear at the root level of a PsiFile.

最适合我的是能够将文本片段解析为元素并将其添加到树中。我尝试过这样的事情:

What would be best for me is to be able to parse a text snippet as an element and have it added to the tree. I tried to do something like this:


myLangTopElement.getNode().addLeaf(MyLangTypes.CHILD_EXPRESSION, "fish = '42'", null);

它实际上似乎有效 - 文本被添加到文档中并创建了一个节点但是当我编辑引号内的文本时编辑 - 抛出一些例外...

It actually seems to work - the text is added to the document and a node is created but when I edit the text inside the quotes in the editor - some exceptions are thrown...

我做错了什么?添加新元素的正确方法是什么?

What am I doing wrong? What is the correct way to add new elements?

推荐答案

PSI很复杂:(。通常的方法是创建一个完整的文件从精心准备的文本(PsiFileFactory#createFileFromText)中,从中提取需要添加到树中的PSI元素,然后调用PsiElement #add / addBefore / addAfter将提取的元素作为参数传递。如果你的PSI元素是一个包装器通过AST(即AstDelegatePsiElement),它的add *方法已经为不要抛出的异常做了必要的魔法。

PSI is complicated :(. A usual way is to create a whole file from a carefully prepared text (PsiFileFactory#createFileFromText), extract from it the PSI element you need to add into the tree, and then call PsiElement#add/addBefore/addAfter passing the extracted element as an argument. If your PSI element is a wrapper over AST (i.e. AstDelegatePsiElement), its add* methods already do the magic necessary for the exceptions not to be thrown.

你可以从IDEA CE源学习GrCreateSubclassAction#startTemplate例如,以及它调用的createCodeReferenceElementFromText的实现。

You can study GrCreateSubclassAction#startTemplate from the IDEA CE source for an example, and the implementation of createCodeReferenceElementFromText that it calls.

这篇关于IntelliJ IDEA插件开发:如何修改Psi树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆