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

Codeaction for exporting unused top-level bindings #711

Merged
merged 10 commits into from
Jul 27, 2020

Conversation

sureyeaah
Copy link
Contributor

This codeaction lets you export unused top-level functions, data types, newtypes, type synonyms, type classes, type families and pattern synonyms.

  • When exporting data types/newtypes/typeclasses/type families, we export all (e.g. Foo(..)).
  • We don't support extracting the data constructor/fields when the corresponding type is already exported. e.g.
module X(Foo) where
Foo = Foo {foo :: ()}

This is done because it would require us to find the corresponding type in the export list.

Fixes haskell/haskell-language-server#157

@digitalasset-cla
Copy link

digitalasset-cla commented Jul 27, 2020

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@cocreature cocreature left a comment

Choose a reason for hiding this comment

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

Nice work, thanks a lot!
One question: I would have expected the code action to add the export at the end whereas your implementation adds it add the beginning. Was that an intentional choice or just because it was easier to implement?

@sureyeaah
Copy link
Contributor Author

sureyeaah commented Jul 27, 2020

I did that because it was easier to implement but now that I think about it, it would be just as easy to append it at the end (in the same line as the closing bracket). I'll update the PR.

@cocreature I've updated it :)

@sureyeaah
Copy link
Contributor Author

sureyeaah commented Jul 27, 2020

I just realized there is an edge case when appending to the export list -module X(foo,) where and module X(foo) where. We'll have to look for that last comma.
EDIT: Fixed.

@sureyeaah sureyeaah marked this pull request as draft July 27, 2020 10:42
@sureyeaah sureyeaah marked this pull request as ready for review July 27, 2020 11:25
Copy link
Collaborator

@cocreature cocreature left a comment

Choose a reason for hiding this comment

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

Thank you!

[ "{-# OPTIONS_GHC -Wunused-top-binds #-}"
, "module A"
, " ("
, " foo) where"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder how clever we should be in terms of inserting line breaks. At some point we venture into autoformatter territory which a code action is definitely not the right place for but maybe there is some middleground. E.g., insert a line break if the opening and closing parentheses are on different lines.

Let’s merge it like this but keep this in mind for a possible future extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When writing codeactions, I prefer to offload as much work as possible to the formatter.
If we were to insert a line break, we would also have to get the indentation right.

@cocreature cocreature merged commit 826e886 into haskell:master Jul 27, 2020
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Add PatSynBind to GHC.Compat

* Tests for "export unused top level binding" codeaction

* Add "export unused top-level binding" codeaction

* exportUnusedTests refactored

* Fix export unused codeaction

* NFC: remove unused import

* hlint

* add exports to the end of list instead

* handle the case where last export end with comma
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Add PatSynBind to GHC.Compat

* Tests for "export unused top level binding" codeaction

* Add "export unused top-level binding" codeaction

* exportUnusedTests refactored

* Fix export unused codeaction

* NFC: remove unused import

* hlint

* add exports to the end of list instead

* handle the case where last export end with comma
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Add PatSynBind to GHC.Compat

* Tests for "export unused top level binding" codeaction

* Add "export unused top-level binding" codeaction

* exportUnusedTests refactored

* Fix export unused codeaction

* NFC: remove unused import

* hlint

* add exports to the end of list instead

* handle the case where last export end with comma
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.

Add code action to add unused function to export list
3 participants