jyp / attrap

ATtempt To Repair At Point (emacs flycheck extension)
GNU General Public License v3.0
43 stars 7 forks source link

Fix handling of operators in the 'add-to-import-list option #8

Closed sergv closed 5 years ago

sergv commented 6 years ago

Currently when attrap responds to an error message like

• Variable not in scope:
    (<+>)
      :: m0 a0
         -> Data.Text.Prettyprint.Doc.Internal.Doc ann0 -> m Constants
• Perhaps you meant one of these:
    ‘<>’ (imported from Prelude), ‘<*>’ (imported from Prelude),
    ‘<$>’ (imported from Prelude)
  Perhaps you want to add ‘<+>’ to the import list in the import of
  ‘Data.Text.Prettyprint.Doc’
  (/foo/bar/baz/Quux.hs:25:1-45).

it will do the following fix

import Data.Text.Prettyprint.Doc (Pretty(..),<+>)

But that's syntactically invalid! This PR makes the fix look like

import Data.Text.Prettyprint.Doc (Pretty(..),(<+>))

The only tricky part here is to detect whether a name should be enclosed in parens, i.e. whether it's an operator. Since there's no dependency on haskell-mode which does have regexps to reliably ascertain that, I've settled on a less principled but more lightweight solution of treating anything that does not start with an uppercase, lowercase, _ or ' letter as an operator. Please let me know if you have any ideas on how this could be improved.

jyp commented 5 years ago

Thanks, and sorry for not noticing for this long.