
Hi, Is there any simple way of pretty printing haskell source code while preserving the comments? I am looking at the haskell-src-ext library. It can parse files with comments and it can pretty print but, for what I can see it cannot do both :-) (prettyPrint won't work on the structure returned by parseFileWithComments). Thanks, titto

Hi Pasqualino,
I am looking at the haskell-src-ext library.
It can parse files with comments and it can pretty print but, for what I can see it cannot do both :-) (prettyPrint won't work on the structure returned by parseFileWithComments).
What you want is exactPrint, defined in Language.Haskell.Exts.Annotated.ExactPrint. :-) Cheers, /Niklas

Thanks Niklas,
in fact this produced a source with comments:
import Language.Haskell.Exts.Annotated
main = do
(ParseOk (mod,comments)) <- parseFileWithComments defaultParseMode "Test.hs"
let pretty = exactPrint mod comments
writeFile "Test_PRETTY.hs" pretty
However:
- The source code produced was incorrect:
class Dir d where
was rewritten as:
class Dir dwhere{
And:
instance Dir Directory where
localDir (Local f) = return f
type URL= String
was rewritten as:
instance Dir Directory where
localDir (Local f) = return f
typeURL= String
Are these known bugs?
- Also, the printing is a bit too exact :-), I would like to keep my
comments AND get the code nicely reformatted.
Is there a way?
What people use to keep their haskell source files in tip-top shape?
Thanks
titto
2009/10/20 Niklas Broberg
Hi Pasqualino,
I am looking at the haskell-src-ext library.
It can parse files with comments and it can pretty print but, for what I can see it cannot do both :-) (prettyPrint won't work on the structure returned by parseFileWithComments).
What you want is exactPrint, defined in Language.Haskell.Exts.Annotated.ExactPrint. :-)
Cheers,
/Niklas

It's not an easy problem to pretty print (i.e. change indentation) and
preserve the comments. And always get it right.
On Tue, Oct 20, 2009 at 1:28 PM, Pasqualino "Titto" Assini
Thanks Niklas,
in fact this produced a source with comments:
import Language.Haskell.Exts.Annotated
main = do (ParseOk (mod,comments)) <- parseFileWithComments defaultParseMode "Test.hs" let pretty = exactPrint mod comments writeFile "Test_PRETTY.hs" pretty
However: - The source code produced was incorrect:
class Dir d where
was rewritten as:
class Dir dwhere{
And:
instance Dir Directory where localDir (Local f) = return f
type URL= String
was rewritten as:
instance Dir Directory where localDir (Local f) = return f
typeURL= String
Are these known bugs?
- Also, the printing is a bit too exact :-), I would like to keep my comments AND get the code nicely reformatted.
Is there a way?
What people use to keep their haskell source files in tip-top shape?
Thanks
titto
2009/10/20 Niklas Broberg
: Hi Pasqualino,
I am looking at the haskell-src-ext library.
It can parse files with comments and it can pretty print but, for what I can see it cannot do both :-) (prettyPrint won't work on the structure returned by parseFileWithComments).
What you want is exactPrint, defined in Language.Haskell.Exts.Annotated.ExactPrint. :-)
Cheers,
/Niklas
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi again,
However: - The source code produced was incorrect:
class Dir d where
was rewritten as:
class Dir dwhere{
That's weird, I'll look into it, thanks.
instance Dir Directory where localDir (Local f) = return f
type URL= String
was rewritten as:
instance Dir Directory where localDir (Local f) = return f
typeURL= String
Sorry, I don't see the difference between these two. Did you write the correct one twice by mistake?
Are these known bugs?
No, there are no known bugs so please report everything you find! exactPrint is new and most likely sparsely used as of yet. I'd expect there to be a few bugs lying around here and there, and I appreciate all reports!
- Also, the printing is a bit too exact :-), I would like to keep my comments AND get the code nicely reformatted.
Is there a way?
No. Like Lennart points out that's a really difficult problem, not to say impossible, where do you place the comments after reformatting? Cheers, /Niklas

2009/10/20 Niklas Broberg
Hi again,
However: - The source code produced was incorrect:
class Dir d where
was rewritten as:
class Dir dwhere{
That's weird, I'll look into it, thanks.
instance Dir Directory where localDir (Local f) = return f
type URL= String
was rewritten as:
instance Dir Directory where localDir (Local f) = return f
typeURL= String
Sorry, I don't see the difference between these two. Did you write the correct one twice by mistake?
In case Pasqualino doesn't see your mail real soon... Maybe the difference is the missing space between "type" and "URL". Cheers, Thu

instance Dir Directory where localDir (Local f) = return f
typeURL= String
Sorry, I don't see the difference between these two. Did you write the correct one twice by mistake?
In case Pasqualino doesn't see your mail real soon... Maybe the difference is the missing space between "type" and "URL".
Ah, of course, I see it now, thanks! Cheers, /Niklas

�typeURL= String
Maybe the difference is the missing space between "type" and "URL".
Ah, of course, I see it now, thanks!
It ocurred to me that you could catch these kind of errors faster if haskell-src-exts included a basic program to pretty print a Haskell module that we could use out of the box. What do you think? Should it better be a separate package? Maurício

Hi,
there is also another difference:
type URL= String
was a top level definition but it has been moved under the previous
instance declaration.
I attach a source fragment and another file with the pretty printed
output containing these three errors.
Regarding the problem of pretty printing while keeping the comments I
imagine that you would like the comments to be associated with the
previous syntactical element (module, top level declaration or
expression) and be rendered in the same relative position in the
pretty printed output.
I agree that it doesn't sound like an easy task.
However, similar systems exists for most languages (I loved my Java
reformatter in Eclipse) and it would be very handy in Haskell as well.
Best,
titto
2009/10/20 Niklas Broberg
instance Dir Directory where localDir (Local f) = return f
typeURL= String
Sorry, I don't see the difference between these two. Did you write the correct one twice by mistake?
In case Pasqualino doesn't see your mail real soon... Maybe the difference is the missing space between "type" and "URL".
Ah, of course, I see it now, thanks!
Cheers,
/Niklas
-- Pasqualino "Titto" Assini, Ph.D. http://quicquid.org/

Hi again,
I attach a source fragment and another file with the pretty printed output containing these three errors.
Thanks, I've opened a ticket for this issue here: http://trac.haskell.org/haskell-src-exts/ticket/65 I'll see when I can get to fixing it, hopefully it won't be long. Cheers, /Niklas

I'll see when I can get to fixing it, hopefully it won't be long.
Two fairly easy bugs to fix, darcs repo is updated. It's probably high time I did another release, stay tuned. I just can't believe I didn't have a single class declaration in my test suite to catch the first one! Or that no one else triggered it before (I guess no one has actually started using exactPrint in their code yet). Thanks again for reporting! :-) Cheers, /Niklas
participants (5)
-
Lennart Augustsson
-
Maurício CA
-
minh thu
-
Niklas Broberg
-
Pasqualino "Titto" Assini