Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit ebb46bbc5ebe2b39dd6edeee1fa0d0c3617e75fc 0 parents
@jaspervdj authored
1  .ghci
@@ -0,0 +1 @@
+:set -isrc
1  .gitignore
@@ -0,0 +1 @@
+dist
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c) 2012, Jasper Van der Jeugt <m@jaspervdj.be>
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Jasper Van der Jeugt <m@jaspervdj.be> nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4 README.markdown
@@ -0,0 +1,4 @@
+stylish-haskell-imports
+=======================
+
+TODO
2  Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
21 src/Main.hs
@@ -0,0 +1,21 @@
+module Main
+ ( main
+ ) where
+
+
+--------------------------------------------------------------------------------
+import Control.Monad ((>=>))
+import System.Environment (getArgs)
+
+
+--------------------------------------------------------------------------------
+import StylishHaskellImports
+
+
+--------------------------------------------------------------------------------
+main :: IO ()
+main = do
+ args <- getArgs
+ case args of
+ [] -> interact stylishHaskellImports
+ _ -> mapM_ (readFile >=> putStr . stylishHaskellImports) args
72 src/StylishHaskellImports.hs
@@ -0,0 +1,72 @@
+module StylishHaskellImports
+ ( stylishHaskellImports
+ ) where
+
+
+--------------------------------------------------------------------------------
+import Data.Either (lefts, rights)
+import Data.Function (on)
+import Data.List (groupBy, sort)
+import Control.Arrow ((|||))
+
+
+--------------------------------------------------------------------------------
+data Qualified = Unqualified | Qualified deriving (Eq, Ord, Show)
+
+
+--------------------------------------------------------------------------------
+stylishQualified :: Qualified -> String
+stylishQualified Unqualified = " "
+stylishQualified Qualified = "qualified"
+
+
+--------------------------------------------------------------------------------
+data Import = Import String Qualified String deriving (Eq, Ord, Show)
+
+
+--------------------------------------------------------------------------------
+importName :: Import -> String
+importName (Import name _ _) = name
+
+
+--------------------------------------------------------------------------------
+parseImport :: String -> Maybe Import
+parseImport str = case words str of
+ ("import" : "qualified" : n : e) -> Just $ Import n Qualified (unwords e)
+ ("import" : n : e) -> Just $ Import n Unqualified (unwords e)
+ _ -> Nothing
+
+
+--------------------------------------------------------------------------------
+stylishImports :: Int -> [Import] -> [String]
+stylishImports _ [] = []
+stylishImports longest is = map stylishImport $ sort is
+ where
+ pad str = str ++ replicate (longest - length str) ' '
+
+ stylishImport (Import n q "") = unwords
+ ["import", stylishQualified q, pad n]
+ stylishImport (Import n q e) = unwords
+ ["import", stylishQualified q, pad n, e]
+
+
+--------------------------------------------------------------------------------
+stylishHaskellImports :: String -> String
+stylishHaskellImports = unlines .
+ concat .
+ stylish .
+ map rls .
+ groupBy ((==) `on` isRight) .
+ map (\i -> maybe (Left i) Right (parseImport i)) .
+ lines
+ where
+ isRight (Right _) = True
+ isRight _ = False
+
+ stylish ls =
+ let longest = maximum $ map (length . importName) $ concat $ rights ls
+ in map (id ||| stylishImports longest) ls
+
+ rls xs
+ | not (null xs) && isRight (head xs) = Right (rights xs)
+ | otherwise = Left (lefts xs)
26 stylish-haskell-imports.cabal
@@ -0,0 +1,26 @@
+Name: stylish-haskell-imports
+Version: 0.1.0.0
+Synopsis: Align and sort imports in Haskell source code
+Description: Align and sort imports in Haskell source code
+Homepage: https://github.com/jaspervdj/stylish-haskell-imports
+License: BSD3
+License-file: LICENSE
+Author: Jasper Van der Jeugt <m@jaspervdj.be>
+Maintainer: Jasper Van der Jeugt <m@jaspervdj.be>
+Copyright: 2012 Jasper Van der Jeugt
+Category: Language
+Build-type: Simple
+Cabal-version: >= 1.8
+
+Executable stylish-haskell-imports
+ Ghc-options: -Wall -O2
+ Hs-source-dirs: src
+ Main-is: Main.hs
+ Other-modules: StylishHaskellImports
+
+ Build-depends:
+ base >= 4 && < 5
+
+Source-repository head
+ Type: git
+ Location: https://github.com/jaspervdj/websockets
Please sign in to comment.
Something went wrong with that request. Please try again.