Skip to content
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

Fix rendering of conditionals #83

Merged
merged 7 commits into from
Jun 17, 2024
Merged

Fix rendering of conditionals #83

merged 7 commits into from
Jun 17, 2024

Conversation

tfausak
Copy link
Owner

@tfausak tfausak commented Jun 17, 2024

Fixes #72.

@tfausak
Copy link
Owner Author

tfausak commented Jun 17, 2024

This was more involved than you might think at first. I have been trying to lean on Cabal-syntax as much as possible. However for conditionals I wasn't able to do that. First of all there are no Parsec or Pretty instances for them. But even if there were, the Condition data type doesn't have a constructor for parentheses, so those would be lost. Also the parsers would have to be "permissive", which they might not be. So that's why I made my own Condition and Variable types. Everything else followed from that.

isElif :: CabalSpecVersion.CabalSpecVersion -> Fields.Name a -> Bool
isElif csv n =
csv >= CabalSpecVersion.CabalSpecV2_2
&& value n == String.toUtf8 "elif"
Copy link
Owner Author

Choose a reason for hiding this comment

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

The elif section only became a thing with Cabal 2.2. So for versions before that, we no longer try to format it. Hopefully this will help people realize that something is wonky.

( Parse.choice
[ Par <$> Parse.parens (parseCondition parseVariable),
Not <$> (Parse.token "!" *> parseCondition parseVariable),
Lit <$> Parse.try parseLit,
Copy link
Owner Author

Choose a reason for hiding this comment

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

This needed try because trying to parse flag(x) would fail because the f would be taken for false, but then it would fail and not back track.

parseIdent :: (Parsec.CabalParsing m) => m String
parseIdent =
let isIdent c = Char.isAlphaNum c || c == '_' || c == '-'
in Parse.munch1 isIdent <* Parse.spaces
Copy link
Owner Author

Choose a reason for hiding this comment

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

This is very lenient. The actual parsers in Cabal aren't as forgiving.

@tfausak tfausak enabled auto-merge (squash) June 17, 2024 14:03
@tfausak tfausak merged commit 809b9af into main Jun 17, 2024
9 checks passed
@tfausak tfausak deleted the gh-72-conditionals branch June 17, 2024 14:16
@tfausak tfausak added the bug Something isn't working label Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad conditional formatting
1 participant