How to take the completions offered by Github CoPilot and re-offer them with proper inserting control Follow
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.
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 snippet that shows how I could get the completion from the IDE and insert text at the cursor position on shurtcut click.
Please sign in to leave a comment.
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.
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.
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.
Could you please rephrase your question about IDE scripting console? What exactly do you want to bind?
I'll try to summarize the problem and what I want:
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:
I can trigger copilot natively to apply a completion, as if I pressed TAB:
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)
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.
I played around like that:
I was not able to get anything working
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.
I've redacted the decompiled snippets, please don't post any confidential code here.
2) see https://plugins.jetbrains.com/docs/intellij/code-completion.html#contributor-based-completion API
3) don't rely on reflection, it will break at some point
4) see https://gist.github.com/dkandalov/708664109a37c3c0ff15?permalink_comment_id=4109595#gistcomment-4109595 on how to declare dependencies on plugins
2) I will dive into that, thanks!
4) So a comment "// depends-on-plugin com.github.copilot" will do it ? I'll try that ;)
I solved it all, copilot is finally working as it should.
Thanks for the hints.
So did you release a plugin or something so the rest of us can get it?
Co-pilot suggestions are helpful but really annoying so basically just turned it off and went back to caveman style.