stylish-haskell icon indicating copy to clipboard operation
stylish-haskell copied to clipboard

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

Open thoughtpolice opened this issue 10 years ago • 13 comments

Consider:

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.

thoughtpolice avatar Jul 21 '13 05:07 thoughtpolice

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 https://github.com/jaspervdj/stylish-haskell/blob/105a1845152876bb7c49acda190e995c64659d01/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, https://github.com/snapframework/io-streams/pull/24 //AnneTheAgile

AnneTheAgile avatar Apr 07 '14 15:04 AnneTheAgile

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!

jaspervdj avatar Apr 07 '14 15:04 jaspervdj

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

AnneTheAgile avatar Apr 08 '14 17:04 AnneTheAgile

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'
  where
    input' = unlines
        [ "module Herp where"
        , ""
        , "ipmort Data.Monoid -- from base"
        ]

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

AnneTheAgile avatar Apr 22 '14 20:04 AnneTheAgile

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. http://www.meetup.com/Haskell_For_Cats/

AnneTheAgile avatar Apr 22 '14 20:04 AnneTheAgile

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)

AnneTheAgile avatar Apr 23 '14 20:04 AnneTheAgile

Note that this also happens on LANGUAGE pragmas.

{-# LANGUAGE ScopedTypeVariables #-} -- test

becomes

{-# LANGUAGE ScopedTypeVariables #-}

langston-barrett avatar Jan 20 '17 05:01 langston-barrett

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!

andrewufrank avatar Mar 08 '17 09:03 andrewufrank

It also happens with comments in the middle of import statements, e.g. https://github.com/jaspervdj/stylish-haskell/issues/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 avatar Apr 15 '17 20:04 neongreen

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...

andrewufrank avatar May 23 '17 08:05 andrewufrank

@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 :)

chshersh avatar Jul 06 '17 15:07 chshersh

+1 just ran into this, please fix! note hindent also has bad comment problems :( https://github.com/commercialhaskell/hindent/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+comment

eflister avatar Apr 24 '18 02:04 eflister

Anyone here willing to write PR for this? I'm open for some pairprogramming session via hangouts to get you started.

EncodePanda avatar Jan 23 '20 17:01 EncodePanda