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

Code action: remove redundant constraints for type signature #692

Conversation

DenisFrezzato
Copy link
Contributor

This closes #60.

@DenisFrezzato DenisFrezzato force-pushed the code-action-remove-redundant-constraint branch from b0e9906 to c50e74b Compare July 13, 2020 11:05
<&> (head >>> parseConstraints)

findConstraints :: T.Text -> T.Text -> T.Text
findConstraints contents typeSignatureName = contents
Copy link
Contributor Author

Choose a reason for hiding this comment

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

On a second thought, I've realised that this will fail if the type signature is not formatted as expected, for example with an arbitrary number of spaces or written over multiple lines (see also lines 608 and 611).

While I don't reckon it could brake the code, this is rather poorly handled. I see two ways to improve it:

  • trivial one: make it a bit safer and do nothing if the code is not formatted as expected;
  • trickier: improve the logic in order to handle any format. I'm not sure this would be achievable with regular expressions and Text functions though, so maybe some text parsing should be involved?

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Usually do the trivial one now, and then handle the trickier one in a follow up PR if its a problem in practice.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Doing the trivial one now is totally fine but we should do nothing if the formatting doesn’t match our expectations instead of crashing which the last and head calls will do.

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, thank you! As mentioned in the comment, I think it’s perfectly fine to ignore the case where the formatting doesn’t match what we expect but I don’t want to crash. Probably easiest to turn the partial functions into something returning a Maybe and then add that to the pattern guard.

<&> (head >>> parseConstraints)

findConstraints :: T.Text -> T.Text -> T.Text
findConstraints contents typeSignatureName = contents
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doing the trivial one now is totally fine but we should do nothing if the formatting doesn’t match our expectations instead of crashing which the last and head calls will do.

@DenisFrezzato
Copy link
Contributor Author

@cocreature Yes, that is exactly what I was planning to do, but I didn't have time yet. Since I'm hanging here, is it fine with you if I add safe as a dependency?

@cocreature
Copy link
Collaborator

Yes safe is fine.

Make the content parsing safe for type signature formatted with an
arbitrary and unexpected number of spaces and/or line feeds.
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.

Thanks!

@cocreature cocreature merged commit 4890baf into haskell:master Jul 27, 2020
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…/ghcide#692)

* Code action: remove redundant constraints for type signature

* Handle peculiar formatting

Make the content parsing safe for type signature formatted with an
arbitrary and unexpected number of spaces and/or line feeds.
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…/ghcide#692)

* Code action: remove redundant constraints for type signature

* Handle peculiar formatting

Make the content parsing safe for type signature formatted with an
arbitrary and unexpected number of spaces and/or line feeds.
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…/ghcide#692)

* Code action: remove redundant constraints for type signature

* Handle peculiar formatting

Make the content parsing safe for type signature formatted with an
arbitrary and unexpected number of spaces and/or line feeds.
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.

Code action: remove redundant constraints
3 participants