IntelliJ插件中具有标准PsiElement自动完成功能的文本字段 [英] Text Field with Standard PsiElement Auto Completion in IntelliJ Plugin

查看:1019
本文介绍了IntelliJ插件中具有标准PsiElement自动完成功能的文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为IntelliJ插件创建一个具有自动完成功能的简单文本字段.我认为这应该很简单,但到目前为止,我已经陷入僵局...

I'm trying to create a simple text field with auto completion for my IntelliJ plugin. I think this should be pretty simple but so far I've run into dead ends...

例如据我了解,这应该可以工作:

E.g. this should work as far as I understand:

EditorTextField format = new TextFieldWithCompletion(currentEditor.getProject(),
                provider,
                "",
                true,
                true,
                true);

问题在于提供者.我希望看到的不是列表提供程序的提供程序.我只想在编辑器光标中显示与当前行匹配的补全,所以我想要完整的补全对话框,而不仅是一小部分选项.

The problem is the provider. I'd expect to see a provider that isn't a list provider. I just want to show the completion matching the current line in the editor cursor so I'd like the full completion dialog and not just a short list of options.

我也查看了TextFieldWithAutoCompletion,但是它似乎是为硬编码的字符串值而不是自由格式完成而设计的.

I also looked at TextFieldWithAutoCompletion but it seems to be designed for hardcoded string values instead of free form completion.

我只想要标准的Java/Kotlin补全.不是自定义列表或类似的东西.我看到了一些有关替换文本字段文档的讨论,但我也无法使它正常工作.我有一个PsiExpressionCodeFragment,并且希望有一个完成程序提供程序可以接受它,但是我找不到它.

I just want the standard Java/Kotlin completion. Not a custom list or anything like that. I saw some discussion with replacing the document of the text field but I couldn't get that to work either. I have a PsiExpressionCodeFragment and would expect there to be a completion provider that accepts that but I can't find it.

作为参考,我想做的事情与断点对话框中的条件语句非常相似.

For reference what I want to do is something very similar to the conditional statement in the breakpoint dialog.

此处所示的另一种方法是:

Another approach illustrated here is:

JavaCodeFragmentFactory jcff = JavaCodeFragmentFactory.getInstance(currentEditor.getProject());
PsiFile pf = PsiDocumentManager.getInstance(currentEditor.getProject()).getPsiFile(currentEditor.getDocument());
PsiElement psiElement = pf.findElementAt(currentEditor.getCaretModel().getOffset());
PsiExpressionCodeFragment fragment = jcff.createExpressionCodeFragment("", psiElement,null, false);
EditorTextField f = new EditorTextField(PsiDocumentManager.getInstance(currentEditor.getProject()).getDocument(fragment),
        currentEditor.getProject(),
        FileTypes.PLAIN_TEXT, false, true);

这将加载UI,但是无论我键入什么内容都不会弹出代码完成.

This loads the UI but doesn't popup code completion no matter what I type.

推荐答案

诀窍是创建一个包含

The trick is to create an editor that contains an instance of the Document. And this document refers to a language and a psi element context:

JPanel panel = new JPanel();

// Just detect an element under caret for context
PsiFile psiFile = PsiDocumentManager.getInstance(editor.getProject()).getPsiFile(editor.getDocument());
PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());

PsiExpressionCodeFragment code = JavaCodeFragmentFactory.getInstance(editor.getProject()).createExpressionCodeFragment("", element, null, true);
Document document = PsiDocumentManager.getInstance(editor.getProject()).getDocument(code);

EditorTextField myInput = new EditorTextField(document, editor.getProject(), JavaFileType.INSTANCE);
myInput.setPreferredWidth(300);

panel.add(myInput);

return panel;

这里的插入号曾经位于dsa5上,因此dsa5变量对于完成而言尚不可见.

Here the caret used to be located on dsa5, so the dsa5 variable is not yet visible for the completion.

这篇关于IntelliJ插件中具有标准PsiElement自动完成功能的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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