Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Cleanup addBindingToImportList #942

Merged
merged 7 commits into from
Dec 8, 2020
Merged

Conversation

berberman
Copy link
Contributor

#916 was tricky and didn't work in some cases. Now the procedure has become clearer, and I add some comments to show how it works. I'm not sure all cases are covered, and we may need more tests.

@berberman berberman mentioned this pull request Dec 7, 2020
2 tasks
Copy link
Collaborator

@pepeiborra pepeiborra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! I really appreciate the new code, its' much easier to understand and to maintain. And more correct too!

src/Development/IDE/Plugin/CodeAction.hs Outdated Show resolved Hide resolved
Comment on lines 1128 to 1131
-- case 2: no children but parentheses, e.g. `import A(Foo(),...)` --> `import A(Foo(Cons), ...)`
Just ('(', T.uncons -> Just (')', rest'')) -> T.concat [pre, "(", leading, parent, "(", rendered, ")", rest'']
-- case 3: children with parentheses, e.g. `import A(Foo(ConsA),...)` --> `import A(Foo(Cons, ConsA), ...)`
Just ('(', T.breakOn ")" -> (children, rest''))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

T.stripStart doesn't strip comments, so

import M(Foo({-Cons-}))` 

will fall through case 3 and unexpectedly become

import M(Foo(ConsB, {-Cons-})

Not your fault though, your code is as good as it can reasonably be. But the only fully correct way to make edits in the user AST is with ghc-exactprint

Copy link
Contributor Author

@berberman berberman Dec 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things become quite complicated involving comments:

  • block comments are considered to be constructors
import M(Foo({-Cons-}))

import M(Foo(ConsB, {-Cons-})
  • unexpected -} after we matched the parent
import M({-Foo-}Foo)

error "unknown case"
  • line comments
  import M(
--Foo
  Foo)

error "unknown case"
  • ...

It's knotty to resolve these cases simply relying on breakOn logic. As you suggested, manipulating ASTs would be an appropriate way.

@pepeiborra
Copy link
Collaborator

The tests are unreliable because the code action needs the package exports map to be populated, which is done implicitly by kick. To wait until kick is done, I think it should be enough to change the test template to wait for progress after waiting for diagnostics:

      void (skipManyTill anyMessage message :: Session WorkDoneProgressEndNotification)

@berberman
Copy link
Contributor Author

Thanks! Should we adopt this implementation? If so, how to handle unknown cases? I don't think error is suitable here, since it will crash ghcide.

@pepeiborra
Copy link
Collaborator

I would handle the unknown cases by returning a Nothing so that no code action is provided. After all, we don't know how to apply GHC's suggestion.
In a follow-up change, rework this using ghc-exactprint. For this, I suggest asking @isovector for permission to inline the very useful https://github.com/haskell/haskell-language-server/blob/master/plugins/tactics/src/Ide/TreeTransform.hs in ghcide

@berberman berberman requested a review from pepeiborra December 8, 2020 10:53
@pepeiborra pepeiborra merged commit 6ec0b99 into haskell:master Dec 8, 2020
@berberman berberman deleted the patch-extend branch December 8, 2020 14:45
@isovector
Copy link
Contributor

@pepeiborra please do. It's only where it is for convenience to me, but always was intended to be upstreamed.

pepeiborra added a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Cleanup addBindingToImportList

* Remove redundant $

* Fix missing leading identifiers

* Simplify

* Wait package exports map in tests

* Don't show code action if we can't handle this case

* Remove redundant parens

Co-authored-by: Pepe Iborra <[email protected]>
pepeiborra added a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Cleanup addBindingToImportList

* Remove redundant $

* Fix missing leading identifiers

* Simplify

* Wait package exports map in tests

* Don't show code action if we can't handle this case

* Remove redundant parens

Co-authored-by: Pepe Iborra <[email protected]>
pepeiborra added a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Cleanup addBindingToImportList

* Remove redundant $

* Fix missing leading identifiers

* Simplify

* Wait package exports map in tests

* Don't show code action if we can't handle this case

* Remove redundant parens

Co-authored-by: Pepe Iborra <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants