tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
958 stars 83 forks source link

Adding to HIE as a formatter option #426

Closed DavSanchez closed 4 years ago

DavSanchez commented 4 years ago

Hi guys,

I am trying to add Ormolu support on HIE (see https://github.com/haskell/haskell-ide-engine/issues/1410). First Haskell contribution ever for me!

I'm looking into src/Ormolu.hs... where exactly does the code handles the --mode inplace option in order to overwrite the file being formatted with the ormolu function? I assume overwriting the file on save would be the default behaviour of Ormolu on HIE.

I am pretty new to Haskell to be honest, so I still have a little difficulty getting used to reading codebases in the language and understanding all the Haskell-specific way of handling things, coming from the usual C-likes background...😅 If this is not the place for questions like this (as it's not an issue per se) please feel free to move them or point me to the appropriate place.

Best!

curiousleo commented 4 years ago

Hi! The --mode=inplace stuff is actually handled in Main.hs:

So this is something that the executable (Main.hs) does, and not something directly exposed by the library (Ormolu.hs).

But it should not be difficult to use the library for formatting and then just write the result back to the original file as a user of the library.

DavSanchez commented 4 years ago

Ah I see! Thanks a lot @curiousleo !

DavSanchez commented 4 years ago

Hi,

Still with this task. I'm trying to cover the cases of formatting errors, be they parsing errors or whatever. I see in the comments that the ormolu function can return an OrmoluException in some circumstances, and I'm trying to somehow catch this in order to output the error message through HIE.

The usual method in which the other formatters do this seems to be that their exposed functions return an Either <ErrorType> <SuccessType> so they have a course of action in whichever case, but the ormolu function seems to return Text in all cases?

I'm wondering if I can implement some kind of wrapper function in which I can catch this OrmoluException and return an Either <something> <Text/String>, but I have no idea how to do this TBH, I'm reading on catching Haskell exceptions but I've had no luck when trying... Do you have any pointers to the right direction?

Thanks in advance!

neongreen commented 4 years ago

https://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception.html#v:try

try @OrmoluException (ormolu ...) will give you an IO (Either OrmoluException Text). You'll have to put {-# LANGUAGE TypeApplications #-} on the top of the file for it to work.

DavSanchez commented 4 years ago

Thanks @neongreen ! Trying your suggestion literally on a line like the following

result = try @OrmoluException (ormolu defaultConfig file (T.unpack selectedContents))

makes this error appear:

Cannot apply expression of type 't1' to a visible type argument 'OrmoluException'

When testing (in another project altogether) how to obtain an formatted output from the use of ormolu like this (codeSnippet is of type String)

ormoluTry = ormolu defaultConfig "test-file.hs" codeSnippet

I get:

Ambiguous type variable ‘m0’ arising from a use of ‘ormolu’ prevents the constraint ‘(MonadIO m0)’ from being solved.

I assume this last example stems from GHC not knowing what type to assign to ormoluTry, ash I have not added any type signature for that binding... I actually have lots of questions regarding all of this.

Again, I know these questions stem from me not knowing enough about Haskell and all the monad stuff, so if this is not the place to discuss this please feel free to point me to some other place or resource.

Cheers!

neongreen commented 4 years ago

The #haskell channel is a good place to ask – you'll get answers much fatter than here.

https://fpchat-invite.herokuapp.com/

On Fri, Nov 8, 2019, 19:00 David Sánchez notifications@github.com wrote:

Thanks @neongreen https://github.com/neongreen ! Trying your suggestion literally on a line like the following

result = try @OrmoluException (ormolu defaultConfig file (T.unpack selectedContents))

makes this error appear:

Cannot apply expression of type 't1' to a visible type argument 'OrmoluException'

When testing (in another project altogether) how to obtain an formatted output from the use of ormolu like this (codeSnippet is of type String)

ormoluTry = ormolu defaultConfig "test-file.hs" codeSnippet

I get:

Ambiguous type variable ‘m0’ arising from a use of ‘ormolu’ prevents the constraint ‘(MonadIO m0)’ from being solved.

I assume this last example stems from GHC not knowing what type to assign to ormoluTry, ash I have not added any type signature for that binding... I actually have lots of questions regarding all of this.

  • There must be a way to extract a Text from the m Text output of ormolu, right?
  • What is the actual relation between the context (ormolu :: MonadIO m) and its output type (m Text)?

Again, I know these questions stem from me not knowing enough about Haskell and all the monad stuff, so if this is not the place to discuss this please feel free to point me to some other place or resource.

Cheers!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tweag/ormolu/issues/426?email_source=notifications&email_token=AALT42RQ3XW7VCJKTNHOGOLQSWEIZA5CNFSM4JGB5YW2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDSRLLY#issuecomment-551884207, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALT42WB34YHRJM5ZKF2V73QSWEIZANCNFSM4JGB5YWQ .

DavSanchez commented 4 years ago

Thanks a lot for the invite @neongreen ! I'll try to make some advances there.