haskell-ide-engine icon indicating copy to clipboard operation
haskell-ide-engine copied to clipboard

"remove redundant import" removes used imports

Open wedens opened this issue 5 years ago • 3 comments

When I have an import string:

import           Path (Abs, Dir, File, Path, reldir, absfile, (</>))

and absfile is unused, there is a suggestion to remove redundant import. When I apply the suggestion, it removes whole import string instead of just absfile. Tooltip correctly shows that only absfile is unused.

Versions: haskell-ide-engine: 77ff1fa (HEAD at the moment) ghc: 8.4.3 vscode-hie-server: 0.0.24 stack: 1.7.1

wedens avatar Oct 13 '18 09:10 wedens

Solution is to use hsimport which we have patched to actually support this!

fendor avatar Dec 20 '19 21:12 fendor

@fendor can you give quick explanation how?

majkrzak avatar Mar 18 '20 18:03 majkrzak

Currently, we remove the line that contains the import here: https://github.com/haskell/haskell-ide-engine/blob/master/src/Haskell/Ide/Engine/Plugin/Generic.hs#L200

Which is just wrong, we have to modify the import line correctly. My idea would be to reuse our hsimport plugin which is responsible for offering import Code-Actions for symbols that it can find in the local hoogle db. Instead of importing, we want to hide a symbol now. hsimport offers an API to hide a symbol from an import and we could use that for hie.

What needs to be done

  • Extend https://github.com/haskell/haskell-ide-engine/blob/35f62cffb6bae6c3f86113cb0c55f52b7192689d/src/Haskell/Ide/Engine/Plugin/HsImport.hs#L524 to obtain the unused symbol from a GHC message. This is quite difficult, iirc, since the GHC warnings are not as nice as we would hope. Then we have to iron out any problems the current implementation has. IIRC, I already paved the road for hiding code-actions, but some stuff needs to change, e.g. we dont have to query the hoogle db for symbols to hide, since we know which symbol to hide.

I think that we mostly have to change the generation of the code-actions, e.g. https://github.com/haskell/haskell-ide-engine/blob/master/src/Haskell/Ide/Engine/Plugin/HsImport.hs#L275 Also, the arguments to hsimport which are generated here: https://github.com/haskell/haskell-ide-engine/blob/master/src/Haskell/Ide/Engine/Plugin/HsImport.hs#L225

If there are more questions, just ask.

fendor avatar Mar 18 '20 19:03 fendor