Comments should not be stripped from 'import' statements and language pragmas #39

thoughtpolice opened this Issue Jul 21, 2013 · 11 comments


None yet
7 participants


module Foo where
import Data.Monoid -- from base
import Data.List   -- from base

And then:

$ stylish-haskell /tmp/Foo.hs
module Foo where
import           Data.List
import           Data.Monoid

The comments shouldn't be stripped from the end, ideally. At the very least this is inconsistent with the default behavior which will preserve comments in most other positions.

This seems like possibly a nice small thing I could help contribute, especially since I am often wont to add remarks about imports :).

I'd like to start with a test on my branch. Would it be right for a test to be added to each section in the imports test file? Or would I only need to add one test? (Maybe I'm asking the wrong question, possibly it depends on the implementation, which I haven't investigated yet.)

I love that you have Travis CI and tests all set up !

Notes to self;
1.I found the right place to add one or more tests, stylish-haskell/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
2.That code is really clean and easy to read.
2a.WISH Haskell had the Ruby RSpec DSL style of test namings.
3.The types of possible import configuration values are the following , taken from the config;
Import cleanup
imports: # There are different ways we can align names and lists.
# - global: Align the import names and import list throughout the entire # file.
# - file: Like global, but don't add padding when there are no qualified # imports in the file. #
# - group: Only align the imports per group (a group is formed by adjacent # import lines).
# - none: Do not perform any alignment. #
# Default: global.
align: global ## Used by SnapFramework
4.I found this project via the NYC Haskell Hackathon via my neighbor's commit,


jaspervdj commented Apr 7, 2014

I think only one test should do in this case. I'd add the test for the global configuration, since that is most commonly used (as far as I'm aware off). Thanks for tackling this, and let me know if you have any questions!

thank you @jaspervdj ! I will give it a go.

I ginned up a test to add to the end, I think this will work but I haven't tested it yet.
Now -- #39 Allow comments after import statements.

case08 :: Assertion
case08 = expected @=? testStep (step 80 Global) input'
    input' = unlines
        [ "module Herp where"
        , ""
        , "ipmort Data.Monoid -- from base"

    expected = unlines
        [ "module Herp where"
        , ""
        , "ipmort Data.Monoid -- from base"

Hmm, so I'm not sure where the comments are stripped out? It seems like Imports.hs relies on no-comments in a couple of places, eg counting longest import. So then maybe the comments are not parsed out in that file but rather before it gets there? Any easy hints for me? thank you!

p.s. Evie at our Haskell Cats might be able to help me out as well if there is no obvious hint to be given.

Ticket #7 Treatment of long imports may help. They sought to parse;

import Happstack.Server              (Happstack, Response, badRequest, guessContentTypeM, lookPairs,
 mimeTypes, neverExpires, ok, setHeaderM, toResponse)

siddharthist commented Jan 20, 2017

Note that this also happens on LANGUAGE pragmas.

{-# LANGUAGE ScopedTypeVariables #-} -- test


{-# LANGUAGE ScopedTypeVariables #-}

i observed the same "disappearing" comments on language pragmas and import. please fix soon - is required to document! (and putting them on separate lines makes things less stylish
thank you for a useful tool!

neongreen added the lossy label Apr 15, 2017

neongreen changed the title from Comments should not be stripped from the end of 'import' statements. to Comments should not be stripped from 'import' statements Apr 15, 2017


neongreen commented Apr 15, 2017

It also happens with comments in the middle of import statements, e.g. #151:

{-# OPTIONS_GHC -F -pgmF htfpp #-}
module Main where

import Test.Framework
import Test.Framework.BlackBoxTest
import {-@ HTF_TESTS @-} MyPkg.A
import {-@ HTF_TESTS @-} MyPkg.B

main = htfMain htf_importedTests

neongreen changed the title from Comments should not be stripped from 'import' statements to Comments should not be stripped from 'import' statements and language pragmas Apr 15, 2017

any fix in sight ? - it makes it impossible to use stylish haskell with HTF testing (and HTF testing is in many projects crucial). i am sorry that i had to de-install stylish...

ChShersh commented Jul 6, 2017

@andrewufrank De-installing stylish is too radical solution. It would be better if instead you fix stylish-haskell ;)
Though I'm also find it rather unconvenient when comments are stripped out. @AnneTheAgile how is it going in 2017 since 2014? I saw you would like to contribute such a nice small thing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment