Skip to content

Conversation

@lspitzner
Copy link
Owner

This turned out to be a rather simple change that might make brittany usage a good bit more pleasant if you use any yet-unsupported stuff (TemplateHaskell comes to mind).

Could need a review/a bit more manual testing, as I don't think the test suite would catch a problem here.

- Omit unnecessary show-invocation
- Use showOutputable for the error span (location)

before/after:

  "RealSrcSpan SrcSpanPoint \"stdin\" 2 1: parse error (possibly incorrect indentation or mismatched brackets)"
  stdin:2:1: parse error (possibly incorrect indentation or mismatched brackets)
@eborden
Copy link
Collaborator

eborden commented Jan 3, 2020

@lspitzner could you add a bit more color to this? What about this makes it more pleasant? Maybe some examples?

@lspitzner
Copy link
Owner Author

eh, sure: Take Test.hs

{-# LANGUAGE TemplateHaskell #-}
main = do
  print                      42

myTh = [t|Maybe
  Int|]

Previous behaviour

> command/stdin
. stdout
! stderr

> brittany Test.hs
! ERROR: brittany pretty printer returned syntactically invalid result.
! ERROR: encountered unknown syntactical constructs:
! HsBracket{} at stdin:(5,8)-(6,7)
> brittany --output-on-errors Test.hs
! ERROR: brittany pretty printer returned syntactically invalid result.
! ERROR: encountered unknown syntactical constructs:
! HsBracket{} at stdin:(5,8)-(6,7)
. {-# LANGUAGE TemplateHaskell #-}
. main = do
.   print 42
. 
. myTh = {- BRITTANY ERROR UNHANDLED SYNTACTICAL CONSTRUCT -}

with this PR

> brittany Test.hs
! WARNING: encountered unknown syntactical constructs:
!   HsBracket{} at stdin:(5,8)-(6,7)
!   -> falling back on exactprint for this element of the module
. {-# LANGUAGE TemplateHaskell #-}
. main = do
.   print 42
. 
. myTh = [t|Maybe
.   Int|]

The behaviour is roughly "take each top-level module element, pass it through brittany, if you get errors, pass it through brittany --exactprint-only instead".

@lspitzner
Copy link
Owner Author

But the granularity is really the top-level module elements; if you have

{-# LANGUAGE TemplateHaskell #-}
func =      42
 where
  cantbeformatted = [t|Maybe
    Int|]

then it says "somewhere in the func is something that is not handled, fall back on exactprint for the whole func, and the whitespaces before "42" would remain. Making this work recursively would be much more work; for that we'd rather handle all syntax that is missing yet :p

@eborden
Copy link
Collaborator

eborden commented Jan 3, 2020

Thanks! Agreed this is much nicer behavior.

Copy link
Collaborator

@tfausak tfausak left a comment

Choose a reason for hiding this comment

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

Awesome!

@lspitzner
Copy link
Owner Author

Thanks for the feedback.

After testing with multiple inputs, I got sidetracked thinking about proper exit codes for warnings. But this PR certainly does not make this any worse (we just now a few more instances of exit-code-0 plus warnings on stderr). And I think that is the right choice anyway. Merging, finally.

@lspitzner lspitzner merged commit 7fd2bef into master Jan 23, 2020
@lspitzner lspitzner deleted the error-handling branch January 23, 2020 00: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.

4 participants