How to take the completions offered by Github CoPilot and re-offer them with proper inserting control

Answered

There are countless complaints on Copilot for inserting a ton of garbage when someone wants only one line or a part of the first line. It's unanswered since 2021 by the devs and their marketing focuses on "40%-60% more efficient coding", if they'd allow word-by-word inserts their efficiency would lower from 60% to the actual realworld ~5%. 

So I am looking for a way to intercept those completions of Github copilot and then directly offer them as "word-by-word" inserts.
The easiest way would be appreciated as I am not experienced in plugins, so if it can be done with the IDE scripting console that would be easiest.

Sadly there is literally not one working example on the documentation for the scripting console. The only code is a minimalistic "Kotlin" script which is not supported by PHPStorm, for Javascript you find not one code online and the other recommended plugin "Flora" directly crashes the IDE on start.

My idea of implementation:
1) Watch for TAB and take a copy of the source code open
2) watch for the code to change (completion inserted)
3) restore previous version of source and offer the change as completion for word-by-word inserts through a shortcut (like CTRL TAB etc)

Another option that would be even better:
1) When copilot shows a completion that whole text must be available somewhere in the IDE context.
2) Take that completion and insert word-by-word on keypress of an alternative shortcut

I'm just not sure if it's possible to get that data from the IDE by plugin/script ?

ANY source code, preferred for that javascript "Nashorn" IDE would be very appreciated.
Any snippet that shows how I could get the completion from the IDE and insert text at the cursor position on shurtcut click.

7 comments
Comment actions Permalink

IDK how GH Copilot plugin works internally and what API it uses to show and insert completion suggestions. If it uses "regular" API, then this might be possible by providing custom com.intellij.codeInsight.completion.CompletionContributor and intercepting all items provided by GH Copilot plugin and provide alternative/changed variants instead.

0
Comment actions Permalink

Hi Yann,

I've decompiled it and been reading a lot (given I am not a java dev it was a bit slow)
They do not seem to use that method, the completions are rendered as images.

com.intellij.openapi.editor.Inlay

They use the Inlay method with a custom renderer.
So the text is not present, it might be present in the Inlay itself but I've not been able to get one enumerated yet, maybe because they are deleted when I press CTRL+ENTER in the console.

Would you happen to know how I can bind a shortcut to a javascript IDE scripting console script ?


0
Comment actions Permalink

Could you please rephrase your question about IDE scripting console? What exactly do you want to bind?

0
Comment actions Permalink

I'll try to summarize the problem and what I want:
The problem:
The CoPilot completions are in most cases 5-10 lines long, they start at the caret position and extend to multiple lines of code.
What I want in 99% of all cases: I just want from caret position to the end of line (or even less) because the code after the line is already correct and finished. 
So currently if I want to use a completion I press "TAB" and then I have to navigate down and start removing all the excessive code that was inserted.

What I want to change:
A plugin script that takes the intercepts the completion CoPilot offers and presents it as a word-by-word completion through a shortcut.
So I would split up the 5-10 lines of completion text by whitespaces and then fill one word after another until I am happy.

Where I am standing:
Using the IDE scripting console with the native Nashorn javascript:
I can trigger copilot natively to apply a completion, as if I pressed TAB:

[REMOVED]

I can not access the text of it before it is applied, that is part of a private variable in that class implementation but I can trigger it. 
So I plan to: 1) capture IDE source 2) applyCompletion 3) remove the difference from source 4) offer it as word-by-word completion
Better would be to REFLECT into the private variable (KEY_LAST_REQUEST)

From the java class:
[REMOVED]


Where I need help:

1) I tried "LivePlugin" (Kotlin) and there I can define an Action shortcut key easily, but I can not access the CoPilot anymore. I can't import it.
#1 Priority:
I need a snippet/example for the IDE-Scripting-Console Javascript/Nashorn to bind a shortcut/action key to a function.
Right now I can only test it with CTRL+ENTER inside the javascript editor.
I played around like that:

actionmanager=com.intellij.openapi.actionSystem.ActionManager.getInstance();
anaction=new com.intellij.openapi.actionSystem.AnAction({});

I was not able to get anything working

2) I need a minimal example or any guideline on how to offer a completion that can be inserted from the Javascript "plugin".

3) Awesome would be a way to get the private variable KEY_LAST_REQUEST somehow. "Reflect" seems not to be available natively in Nashorn. Though "java.lang.reflect" is maybe an option? This way I could avoid having to apply the wrong completion and can directly offer it word-by-word without the hack.
Most important is the shortcut right now so I can get started testing.

4) Alternatively I would like to use the LivePlugin plugin with Kotlin, that looks like a cleaner method. But that's only possible if it can access the copilot classes/JavaPackages. It looks like that's not possible.

0
Comment actions Permalink

1) I responded there, sadly those examples are not javascript and didn't work when I tried to adapt
2) I will dive into that, thanks!
3) Understood
4) So a comment "// depends-on-plugin com.github.copilot" will do it ? I'll try that ;)

0
Comment actions Permalink

I solved it all, copilot is finally working as it should.
Thanks for the hints.

1

Please sign in to leave a comment.