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

Simplify Line type in Quasi module, always use NonEmpty #1231

Merged
merged 6 commits into from
Apr 20, 2021

Conversation

danbroooks
Copy link
Contributor

@danbroooks danbroooks commented Apr 20, 2021

Before submitting your PR, check that you've:

  • Documented new APIs with Haddock markup
  • Added @since declarations to the Haddock
  • Ran stylish-haskell on any changed files.
  • Adhered to the code style (see the .editorconfig file for details)

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Bumped the version number if there isn't an (unreleased) on the Changelog
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

This is a followon from #1206 that addresses the TODO comment here:

-- TODO: refactor to return (Line' NonEmpty), made possible by
-- https://github.com/yesodweb/persistent/pull/1206 but left out
-- in order to minimize the diff
parseLine :: Text -> Maybe Line
parseLine txt =
case tokenize txt of
[] ->
Nothing
toks ->
pure $ Line (parseIndentationAmount txt) toks

Following recent refactoring there is not actually any instance of Line where the contents of the list are not NonEmpty, so this PR changes that type to always use NonEmpty, rather than being polymorphic, which greatly simplifies a lot of the code around Line, and means we can also get rid of the skipEmpty function, as NonEmpty guarantees us nonempty-ness 👍

@danbroooks danbroooks changed the title Simplify Line type, always use NonEmpty Simplify Line type in Quasi module, always use NonEmpty Apr 20, 2021
Copy link
Collaborator

@parsonsmatt parsonsmatt left a comment

Choose a reason for hiding this comment

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

looks great!

made a note that the version is going to be 2.11.1.1, which i'm planning on releasing today

@@ -2,6 +2,8 @@

## 2.12.1.0

* [#1231](https://github.com/yesodweb/persistent/pull/1231)
* Simplify Line type in Quasi module, always use NonEmpty
Copy link
Collaborator

Choose a reason for hiding this comment

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

2.12.1.0 has already been released, this is going to get bundled in with 2.12.1.1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No probs I will shift this up 👍

pure $ Line (parseIndentationAmount txt) toks
parseLine txt = do
parsedTokens <- NEL.nonEmpty (tokenize txt)
pure $ Line (parseIndentationAmount txt) parsedTokens
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice 😄

lowestIndent
:: Functor f
=> Foldable f
=> Functor g
=> f (Line' g)
=> f Line
-> Int
lowestIndent = minimum . fmap lineIndent
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we specialize this function, too? I feel like we can get away with specializing this, possibly even to NonEmpty which would make it even safer (mimumum [] = undefined).

@@ -212,7 +214,7 @@ main = hspec $ do
it "mid-token quote in later token" $
parseLine "foo bar baz=(bin\")" `shouldBe`
Just
( Line 0
( Line 0 $ nonEmptyOrFail
Copy link
Collaborator

Choose a reason for hiding this comment

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

For the test suite, we could enable OverloadedLists, and then we can use list literal syntax for NonEmpty a - it's a gross kludge to write [] :: NonEmpty Int, but, lmao, it does make the test syntax nicer.

asTokens :: [T.Text] -> [Token]
asTokens = fmap Token
nonEmptyOrFail :: [a] -> NonEmpty a
nonEmptyOrFail = maybe failure id . NEL.nonEmpty
Copy link
Collaborator

Choose a reason for hiding this comment

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

there's the horribly namedNEL.fromList :: [a] -> NonEmpty a unless by some act of grace it was removed

Copy link
Collaborator

Choose a reason for hiding this comment

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

buuut if we just use list literal syyntax I think this can go away

Just
[ Line 0 [Token "Foo"]
, Line 0 [DocComment "Hello"]
]
Copy link
Contributor Author

@danbroooks danbroooks Apr 20, 2021

Choose a reason for hiding this comment

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

The switch to using OverloadedLists caused this to give an ambiguous type error:

    • Couldn't match expected type ‘GHC.Exts.Item (t0 Line)’
                  with actual type ‘Line’
      The type variable ‘t0’ is ambiguous
    • In the expression: Line 0 [Token "Foo"]
      In the first argument of ‘Just’, namely
        ‘[Line 0 [Token "Foo"], Line 0 [DocComment "Hello"]]’
      In the second argument of ‘shouldBe’, namely
        ‘Just [Line 0 [Token "Foo"], Line 0 [DocComment "Hello"]]’
    |       
236 |                             [ Line 0 [Token "Foo"]
    |                               ^^^^^^^^^^^^^^^^^^^^        

I've opted to actually just delete this test, this function is being tested already with the other tests here (the one above and below it), and also in the context of multiple lines, in tests like this one:

it "recognizes comments" $ do
let text = "Foo\n x X\n-- | Hello\nBar\n name String"
let expected =
Line { lineIndent = 0, tokens = asTokens ["Foo"] } :|
[ Line { lineIndent = 2, tokens = asTokens ["x", "X"] }
, Line { lineIndent = 0, tokens = [DocComment "Hello"] }
, Line { lineIndent = 0, tokens = asTokens ["Bar"] }
, Line { lineIndent = 1, tokens = asTokens ["name", "String"] }
]
preparse text `shouldBe` Just expected

Copy link
Collaborator

Choose a reason for hiding this comment

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

Cool works for me!

@parsonsmatt parsonsmatt merged commit 7bb6abf into yesodweb:master Apr 20, 2021
@danbroooks
Copy link
Contributor Author

Hey I wasn't finished! 😂 sorry, maybe I shouldn't have pushed that stuff up. I will have another PR shortly, it should still fit in fine as a follow on I think :)

@parsonsmatt
Copy link
Collaborator

Oh, shoot!

I've already released as 2.11.1.1 😅

Try to mark a PR as a draft if it's not ready yet?

@danbroooks
Copy link
Contributor Author

danbroooks commented Apr 20, 2021

Yeah its no big deal, what I meant was I wasn't done with the review updates, I've still yet to remove the nonEmptyOrFail function, this PR is totally fine to be a part of that release 👍

@danbroooks danbroooks deleted the simplify-line branch April 27, 2021 16:17
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