diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-05-14 11:41:24 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-05-14 11:41:24 +0200 |
commit | ebb46bbc5ebe2b39dd6edeee1fa0d0c3617e75fc (patch) | |
tree | 7923bb631e8910ce5a5e0499e4f4dd9b41f5acd6 | |
download | stylish-haskell-ebb46bbc5ebe2b39dd6edeee1fa0d0c3617e75fc.tar.gz |
Initial commit
-rw-r--r-- | .ghci | 1 | ||||
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | LICENSE | 30 | ||||
-rw-r--r-- | README.markdown | 4 | ||||
-rw-r--r-- | Setup.hs | 2 | ||||
-rw-r--r-- | src/Main.hs | 21 | ||||
-rw-r--r-- | src/StylishHaskellImports.hs | 72 | ||||
-rw-r--r-- | stylish-haskell-imports.cabal | 26 |
8 files changed, 157 insertions, 0 deletions
@@ -0,0 +1 @@ +:set -isrc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1521c8b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dist @@ -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. diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..10d47ee --- /dev/null +++ b/README.markdown @@ -0,0 +1,4 @@ +stylish-haskell-imports +======================= + +TODO diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/src/Main.hs b/src/Main.hs new file mode 100644 index 0000000..9f61463 --- /dev/null +++ b/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 diff --git a/src/StylishHaskellImports.hs b/src/StylishHaskellImports.hs new file mode 100644 index 0000000..2022e8a --- /dev/null +++ b/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) diff --git a/stylish-haskell-imports.cabal b/stylish-haskell-imports.cabal new file mode 100644 index 0000000..4ec74fb --- /dev/null +++ b/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 |