Skip to content

rewrite import completion with ghc-exactprint #2694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

kokobd
Copy link
Collaborator

@kokobd kokobd commented Feb 6, 2022

This will solve #2414.

TODO List:

I will request code review when it's feature complete.

@kokobd kokobd self-assigned this Feb 6, 2022
@michaelpj michaelpj requested review from pepeiborra and removed request for pepeiborra February 7, 2022 09:43
@kokobd
Copy link
Collaborator Author

kokobd commented Feb 10, 2022

Both Completions.hs and CodeAction.hs has some code for adding new imports. I will try to eliminate this duplication.

@kokobd
Copy link
Collaborator Author

kokobd commented Feb 17, 2022

If we want to know where to insert the new import declaration, we will have to exact print the whole Located HsModule, because the only Located structure containing a Located ImportDecl is the whole module.

Then I encountered a problem (at least for ghc 8.10.7. I haven't tested it on ghc 9.2). In hls, the exactprint doesn't preserve lines starting with #!. As a quick test, you can try to extend import for this code (extending import already uses exactprint, both in master branch and latest release)

import
#! nix-shell --pure -i runghc -p "haskellPackages.ghcWithPackages (hp: with hp; [ turtle ])"
    Data.Monoid ( Monoid )

class Semigroup a => SomeData a
instance SomeData All

If we place the cursor on All and select code action "Add All to the import list of Data.Monoid", then the code becomes

import

    Data.Monoid ( Monoid, All )

class Semigroup a => SomeData a
instance SomeData All

I'm wondering if losing the #! line is intended and having some special considerations, or if it's just a previously unknown bug that need to be fixed.

@pepeiborra
Copy link
Collaborator

Then I encountered a problem (at least for ghc 8.10.7. I haven't tested it on ghc 9.2). In hls, the exactprint doesn't preserve lines starting with #!.

I believe ghc-exactprint comes with a round tripping test suite. Have you been able to repro the issue using it?

@kokobd
Copy link
Collaborator Author

kokobd commented Feb 17, 2022

@pepeiborra I've tried ghc-exactprint 0.6.4 in a standalone project. It exactprints perfectly. This problem seems to be related to hls (maybe we have some wrong configs for parsing).

I'm still investigating and trying to fix it in hls. However as changing fundamental parts like parsing might potentially affect other parts of hls, any related context information is welcome ;)

@kokobd
Copy link
Collaborator Author

kokobd commented Feb 19, 2022

I might have identified the root cause of the shebang exact printing issue. Created another draft PR #2725 to fix it seperately.

@pepeiborra
Copy link
Collaborator

@kokobd still around? Would be nice to land this

@kokobd
Copy link
Collaborator Author

kokobd commented Jun 19, 2022

Sorry for being absent these days. It has taken me a few months to get rid of my tiresome job.

I will probably fix this in the next few days, but less aggressively. Using exact print info to print the whole module incurs too much risk (performance, compatibility, etc.) and a lot of code change. Instead, using exact print information to locate the where keyword of the module is an easier way to fix this issue.

@kokobd
Copy link
Collaborator Author

kokobd commented Jun 22, 2022

Closing in favor of #2981.

@kokobd kokobd closed this Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants