copilot-emacs / copilot.el

An unofficial Copilot plugin for Emacs.
MIT License
1.74k stars 124 forks source link

[RFC] add copilot-balancer.el to fix unbalanced char pairs returned from Copilot #157

Closed raymond-w-ko closed 1 year ago

raymond-w-ko commented 1 year ago

One thing that has frustrated me greatly is that Copilot cannot balance parentheses et al, even when using VSCode + the official plugin, which is the golden standard. Since I program in Clojure for work, this is maddening as you have to these fix missing or extra char pairs. Almost all lisp editors run into issues once in this unbalanced state.

Thankfully, the brokenness follows a pattern where I think it is only the suffix portion that is mismatched, meaning it can be algorithmically fixed. I attempt to do so here.

There are other two changes:

  1. I coerce all Clojure modes to just clojure as that is the only official language ID.
  2. I rewrote the code so that save-excursion is in the outer loop and save-restriction is inside, as recommended by the docs. I also eliminated multiple calls as a micro-optimization.
zerolfx commented 1 year ago

LGTM! Great work!

I can't push to your branch, please resolve the conflict and I'll merge it soon.

raymond-w-ko commented 1 year ago

There is still one bug I'm working on, so let me see if I can fix it:

(defun add () | 
  )(EOF)
raymond-w-ko commented 1 year ago

I had to rewrite portions to deal with pairs inside strings, so I'm not 100% as I haven't used it for my work. However, it should be ready to merge.