Expression input field with code completion

We want to show a dialog where the user can enter an expression resulting in
a certain type. We can specify the source file location defining the scope,
so local variables of the method, method parameters or fields should be found.

Thanks in advance,
Tom

15 comments
Comment actions Permalink

Hello Tom,

We want to show a dialog where the user can enter an expression
resulting in a certain type. We can specify the source file location
defining the scope, so local variables of the method, method
parameters or fields should be found.


Use PsiElementFactory.createExpressionCodeFragment() to create a PsiCodeFragment
instance, then use EditorTextField and call editorTextField.setDocument(PsiDocumentManager.getInstance(project).getDocument(codeFragment))

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thank you, Dmitry. Where can I define the type the expression should have?

Tom

0
Comment actions Permalink

Hello Tom,

Thank you, Dmitry. Where can I define the type the expression should
have?


Third parameter to createExpressionCodeFragment().

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

More questions:

Use PsiElementFactory.createExpressionCodeFragment() to create a
PsiCodeFragment instance


createExpressionCodeFragment has 4 parameters:
String ... what should I pass?
PsiElement ... the "location" defining the scope?
PsiType ... javaParserFacade.createTypeFromText(className, element); ?
boolean ... what should I pass?

then use EditorTextField


How do I obtain such a class? Ctrl+N does not show me such a class.

Tom

0
Comment actions Permalink

Hello Tom,

>> Use PsiElementFactory.createExpressionCodeFragment() to create a
>> PsiCodeFragment instance
>>

createExpressionCodeFragment has 4 parameters:


PsiElementFactory is actually one of the few classes in our API that has
complete JavaDoc. And yet you're asking questions for which the answers can
be found in JavaDoc.

String ... what should I pass?


The default text of the expression. An empty string is a valid Java expressoin,
so it will do.

PsiElement ... the "location" defining the scope?


The context for resolving references from the code fragment.

PsiType ... javaParserFacade.createTypeFromText(className, element); ?


If you have a PsiClass, you should better use PsiElementFactory.createType(PsiClass).

boolean ... what should I pass?


true

>> then use EditorTextField
>>

How do I obtain such a class? Ctrl+N does not show me such a class.


Add idea.jar to the classpath of the IDEA SDK.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

PsiElementFactory is actually one of the few classes in our API that has
complete JavaDoc. And yet you're asking questions for which the answers
can be found in JavaDoc.


Unfortunately, I can't find the JavaDoc in my IDEA installation directory
and IDEA did not found it either when creating the IDEA-SDK.

>> String ... what should I pass?


The default text of the expression. An empty string is a valid Java
expressoin, so it will do.

>> PsiElement ... the "location" defining the scope?


The context for resolving references from the code fragment.

>> PsiType ... javaParserFacade.createTypeFromText(className, element); ?


If you have a PsiClass, you should better use
PsiElementFactory.createType(PsiClass).

>> boolean ... what should I pass?


true

>>> then use EditorTextField
>>>
>> How do I obtain such a class? Ctrl+N does not show me such a class.


Add idea.jar to the classpath of the IDEA SDK.


Thank you. Now it compiles. But when I press Ctrl-Space in the generated
input field, I'm getting following exception:

java.lang.NullPointerException
at com.intellij.codeInsight.lookup.LookupManager.getInstance(LookupManager.java:2)
at com.intellij.codeInsight.completion.CompletionProgressIndicator.<init>(CompletionProgressIndicator.java:38)
at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.doComplete(CodeCompletionHandlerBase.java:148)
at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.invokeCompletion(CodeCompletionHandlerBase.java:115)
at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.invoke(CodeCompletionHandlerBase.java:132)
at com.intellij.codeInsight.completion.actions.BaseCodeCompletionAction.actionPerformedImpl(BaseCodeCompletionAction.java:2)
at com.intellij.codeInsight.completion.actions.CodeCompletionAction.actionPerformedImpl(CodeCompletionAction.java:2)
at com.intellij.codeInsight.actions.CodeInsightAction.actionPerformed(CodeInsightAction.java:39)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$1.performAction(IdeKeyEventDispatcher.java:2)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:3)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.d(IdeKeyEventDispatcher.java:1)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(IdeKeyEventDispatcher.java:170)
at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:211)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:217)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
at java.awt.Dialog$1.run(Dialog.java:1047)
at java.awt.Dialog$3.run(Dialog.java:1099)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Dialog.java:1097)
at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl$MyDialog.show(DialogWrapperPeerImpl.java:91)
at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl.show(DialogWrapperPeerImpl.java:128)
at com.intellij.openapi.ui.DialogWrapper.show(DialogWrapper.java:849)
at com.syntevo.visuallayout.ui.VLComponentMouseListener$ComponentPropertiesAction.actionPerformed(VLComponentMouseListener.java:264)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1225)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1266)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at com.intellij.ide.IdeEventQueue.c(IdeEventQueue.java:35)
at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:223)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:217)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
at java.awt.Dialog$1.run(Dialog.java:1047)
at java.awt.Dialog$3.run(Dialog.java:1099)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Dialog.java:1097)
at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl$MyDialog.show(DialogWrapperPeerImpl.java:91)
at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl.show(DialogWrapperPeerImpl.java:128)
at com.intellij.openapi.ui.DialogWrapper.show(DialogWrapper.java:849)
at com.syntevo.visuallayout.VisualLayoutWriteActionHandler.executeWriteAction(VisualLayoutWriteActionHandler.java:109)
at com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler$1.run(EditorWriteActionHandler.java:51)
at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:340)
at com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler.execute(EditorWriteActionHandler.java:35)
at com.intellij.openapi.editor.actionSystem.EditorAction$1.run(EditorAction.java:76)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:2)
at com.intellij.openapi.editor.actionSystem.EditorAction.actionPerformed(EditorAction.java:88)
at com.intellij.openapi.editor.actionSystem.EditorAction.actionPerformed(EditorAction.java:62)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$1.performAction(IdeKeyEventDispatcher.java:2)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:3)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.d(IdeKeyEventDispatcher.java:1)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(IdeKeyEventDispatcher.java:170)
at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:211)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:217)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


Tom

0
Comment actions Permalink

Hello Tom,

>> PsiElementFactory is actually one of the few classes in our API that
>> has complete JavaDoc. And yet you're asking questions for which the
>> answers can be found in JavaDoc.
>>

Unfortunately, I can't find the JavaDoc in my IDEA installation
directory and IDEA did not found it either when creating the IDEA-SDK.


Please download the Plugin Development Kit:
http://www.jetbrains.com/idea/download/index.html#kit

>>>> then use EditorTextField
>>>>
>>> How do I obtain such a class? Ctrl+N does not show me such a class.
>>>
>> Add idea.jar to the classpath of the IDEA SDK.
>>

Thank you. Now it compiles. But when I press Ctrl-Space in the
generated input field, I'm getting following exception:

>> java.lang.NullPointerException
>>
>> at
>> com.intellij.codeInsight.lookup.LookupManager.getInstance(LookupManag
>> er.java:2)

You didn't pass a Project to the EditorTextField constructor.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

On Fri, 14 Nov 2008 13:27:50 +0000, Dmitry Jemerov wrote:

Hi Dmitry,

PsiElementFactory is actually one of the few classes in our API that has
complete JavaDoc. And yet you're asking questions for which the answers
can be found in JavaDoc.


the irony of your first sentence surely cannot be lost on you? I'm one
of the first to lose patience when asked questions that a little research
by the questioner would have answered, but it seems not a little churlish
of you to respond like this while, by your own admission, most of the
OpenAPI remains undocumented.

Sadly, expecting to find helpful OpenAPI JavaDoc really is a triumph of
hope over experience. IMHO you shouldn't be surprised when someone's
first thought is to post to a newsgroup rather than to launch their web
browser.

http://www.jetbrains.net/jira/browse/IDEA-20395 adds insult to injury :)

I've (seriously) wondered on several occasions if the lack of API
documentation is a deliberate ploy to filter out those who're not truly
motivated to write plug-ins. The barrier to entry really is much higher
than it should be.

I'm sorry to be critical, particularly since your newsgroup posts are
generally very helpful and appreciated because of that.

--
Mark Scott
mark@codebrewer.com

0
Comment actions Permalink

Hello Mark,

I had to look up "churlish" in the dictionary. :)

Yes, of course, I understand the irony, and in general I'm OK with people
asking simple questions (they're easier for me to answer because I don't
need to do much research). I tried a number of times to start efforts to
improve quality of our API documentation, but given that I have to divide
my time between lots of different projects and tasks, I only managed to produce
several incomplete documents.

Hopefully the company will be able to allocate more resources to API documentation
during version 9 development. However, unfortunately I can't give any definite
promise on that.

>> PsiElementFactory is actually one of the few classes in our API that
>> has complete JavaDoc. And yet you're asking questions for which the
>> answers can be found in JavaDoc.
>>

the irony of your first sentence surely cannot be lost on you? I'm
one of the first to lose patience when asked questions that a little
research by the questioner would have answered, but it seems not a
little churlish of you to respond like this while, by your own
admission, most of the OpenAPI remains undocumented.

Sadly, expecting to find helpful OpenAPI JavaDoc really is a triumph
of hope over experience. IMHO you shouldn't be surprised when
someone's first thought is to post to a newsgroup rather than to
launch their web browser.

http://www.jetbrains.net/jira/browse/IDEA-20395 adds insult to injury
:)

I've (seriously) wondered on several occasions if the lack of API
documentation is a deliberate ploy to filter out those who're not
truly motivated to write plug-ins. The barrier to entry really is
much higher than it should be.

I'm sorry to be critical, particularly since your newsgroup posts are
generally very helpful and appreciated because of that.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

On Fri, 14 Nov 2008 16:14:45 +0000, Dmitry Jemerov wrote:

Hi Dmitry,

I had to look up "churlish" in the dictionary. :)


Oh well, I'm glad your English vocabulary is improving even if the
OpenAPI doc isn't ;)

Hopefully the company will be able to allocate more resources to API
documentation during version 9 development. However, unfortunately I
can't give any definite promise on that.


I understand that adding API documentation won't directly increase sales
so I'm sure that commercial considerations mean time spent on such a
project won't be considered worthwhile. However, in the longer term, an
increasing number of high-quality, useful plug-ins can only be helpful.
But I guess I'm preaching to the converted...

--
Mark Scott
mark@codebrewer.com

0
Comment actions Permalink

Thank you. Now it compiles, runs without an exception, but still does not
work - just shows a spinning wheel without showing results. Everything
worked perfect with IDEA 7, your massive changes to the OpenAPI cost a lot
of effort.

Tom



Attachment(s):
codecompletion.png
0
Comment actions Permalink

Dmitry, do you have more ideas related with this problem?

Tom

0
Comment actions Permalink

Hello Tom,

Dmitry, do you have more ideas related with this problem?


I'm afraid not. I would need to run your code under the debugger in order
to investigate why exactly the completion list isn't getting filled.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hi Dmitry,

Did you receive my code from 24th November?

Tom

0
Comment actions Permalink

Hello Tom,

Did you receive my code from 24th November?


Yes, but unfortunately I haven't had a chance to look at it yet.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.