Customize SmartType Code Completion?

Is it possible to change the list of suggestions returned by SmartType completion?
Or maybe just replace the suggestions completely in some cases.

The plugin I have in mind would deal with all the pre-Java5 method parameters that
are declared as int, but in fact should have been enums.
E.g. it's a petty that Idea is not smart enough to figure out the valid constants for
the parameter of frame.setDefaultCloseOperation(<ctrl-shift-space>).
Same for dozens of other usages in the JDK.

If this is possible I might have a go at such a plugin.
I'd provide a list of the most frequently used int values in the JDK and maybe that
list could grow through feedback from the community over time.

8 comments

Hate to follow up my own post.
Yet I have looked some more into the API and haven't found a solution.
Seems I can add proposals for a custom language, but cannot fiddle with the
completion for java.
As a workaround: Is there a way to have a custom action that mimicks the
code completion behavior? (Shows a popup that the user confirms and changes the
code accordingly.)

0

I still like to implement this - is there any way to do this in Demetra?

I have searched the forum and found references to CompletionData.
I tried to register my own CompletionData:

FileType javaFileType = FileTypeManager.getInstance().getFileTypeByExtension(".java");
SmartApiCompletionData smartApiCompletionData = new SmartApiCompletionData();
CompletionUtil.registerCompletionData(javaFileType, smartApiCompletionData);

It never gets called on Smart Completion?!
On basic completion "findPrefix()" gets called, but I have no idea how to handle it.

In the plugin docu I found references to implement PsiScopeProcessor, but I guess that's only possible for custom file types.

0

SmartType completion is AFAIK not accessible via any API (neither closed nor open).

Sascha

0

I tried to cheat and use classes from idea.jar (not in openapi).
Problem is some classes and methods I need are protected.
Even if I try to cheat and use the same package name in my plugin as in idea, the plugin classloader refuses access to these classes/methods.

Any way around that? Can I add a ]]> to my plugin.xml that allows me access?

0

I don't really understand where you're having trouble with protected members, but a ]]> entry certainly won't help you because IDEA core doesn't have an ID to depend on. Generally though, you can access protected/private stuff with reflection and even subclass/implement non-accessible classes/interfaces using CGLIB.

However I don't see where this would actually be necessary in this case, because it just seems impossible to me to hook into Smart Completion in any reasonable way (#5622). Would you mind sharing some code example?

Sascha

0

You're right: There is no reasonable way to hook into Smart Completion. That's why I tried the unreasonable ;)

Here's one of the several attempts I tried:
I created my own "SmarterCompletionAction" that uses my own SmarterCompletionHandler, that simply wraps a SmartCompletionHandler (and should later add behaviour of its own):

As soon as I invoke my action this is the exception I get:


I also had similar error when trying to access package private methods.

0

Why don't you just subclass SmarterCompletionHandler instead? It's public and should work just as well for creating your own action. That's not really nice, but not as ugly as putting code into a foreign package ;)

Sascha

0

I am on my way! Thanks for the suggestion.
The following code does indeed work. Just for testing it adds two dummy suggestions at the start of all smart completions. I have to set a final field to a new value using reflection, but who cares...

Unfortunately Idea still preselects one of its own suggestions. Guess I have to look into replacing the LookupItemPreferencePolicy somwhow.

Also it would be nice of course to replce the SmartType action rather than have my own action with a differemt shortcut.

0

Please sign in to leave a comment.