diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-05-30 16:32:19 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-05-30 16:32:19 +0200 |
commit | 1c148d8dc222913d6e0ec7a8a30254fcbf25daab (patch) | |
tree | 59afeb21a4a961372097698e2c0c11ed7bf4fda4 | |
parent | 5fd71ceb920f2027766a40a5a829234a21060c8f (diff) | |
download | stylish-haskell-1c148d8dc222913d6e0ec7a8a30254fcbf25daab.tar.gz |
Can now replace -> by →
-rw-r--r-- | .stylish-haskell.yaml | 3 | ||||
-rw-r--r-- | src/StylishHaskell/Stylish/Catalog.hs | 2 | ||||
-rw-r--r-- | src/StylishHaskell/Stylish/UnicodeSyntax.hs | 73 | ||||
-rw-r--r-- | stylish-haskell.cabal | 1 |
4 files changed, 61 insertions, 18 deletions
diff --git a/.stylish-haskell.yaml b/.stylish-haskell.yaml index 9f3bf08..4ccc4d3 100644 --- a/.stylish-haskell.yaml +++ b/.stylish-haskell.yaml @@ -1,4 +1,3 @@ enabled: - - Imports - LanguagePragmas - - TrailingWhitespace + - Imports diff --git a/src/StylishHaskell/Stylish/Catalog.hs b/src/StylishHaskell/Stylish/Catalog.hs index 4aa95a8..3fe695a 100644 --- a/src/StylishHaskell/Stylish/Catalog.hs +++ b/src/StylishHaskell/Stylish/Catalog.hs @@ -17,6 +17,7 @@ import qualified StylishHaskell.Stylish.Imports import qualified StylishHaskell.Stylish.LanguagePragmas import qualified StylishHaskell.Stylish.Tabs import qualified StylishHaskell.Stylish.TrailingWhitespace +import qualified StylishHaskell.Stylish.UnicodeSyntax -------------------------------------------------------------------------------- @@ -26,6 +27,7 @@ catalog = M.fromList , ("LanguagePragmas", StylishHaskell.Stylish.LanguagePragmas.stylish) , ("Tabs", StylishHaskell.Stylish.Tabs.stylish) , ("TrailingWhitespace", StylishHaskell.Stylish.TrailingWhitespace.stylish) + , ("UnicodeSyntax", StylishHaskell.Stylish.UnicodeSyntax.stylish) ] diff --git a/src/StylishHaskell/Stylish/UnicodeSyntax.hs b/src/StylishHaskell/Stylish/UnicodeSyntax.hs index 3373a50..1a62127 100644 --- a/src/StylishHaskell/Stylish/UnicodeSyntax.hs +++ b/src/StylishHaskell/Stylish/UnicodeSyntax.hs @@ -1,24 +1,67 @@ -------------------------------------------------------------------------------- --- module StylishHaskell.Stylish.UnicodeSyntax where +{-# LANGUAGE UnicodeSyntax #-} +module StylishHaskell.Stylish.UnicodeSyntax + ( stylish + ) where -------------------------------------------------------------------------------- +import Data.List (isPrefixOf, sort) +import Data.Map (Map) +import qualified Data.Map as M +import Data.Maybe (maybeToList) import qualified Language.Haskell.Exts.Annotated as H -import System.Environment -import Data.Maybe (maybeToList) -import Data.List (isPrefixOf) -------------------------------------------------------------------------------- -import StylishHaskell.Stylish -import StylishHaskell.Util -import StylishHaskell.Parse +import StylishHaskell.Block +import StylishHaskell.Editor +import StylishHaskell.Stylish +import StylishHaskell.Util -------------------------------------------------------------------------------- -types :: H.Module H.SrcSpanInfo -> Lines -> [(Int, Int)] +unicodeReplacements :: Map String String +unicodeReplacements = M.fromList + [ ("->", "→") + ] + + +-------------------------------------------------------------------------------- +replaceAll :: [(Int, [(Int, String)])] -> Lines -> [Change String] +replaceAll positions ls = + zipWith changeLine' positions $ selectLines (map fst positions) ls + where + changeLine' (r, ns) str = changeLine r $ return $ flip applyChanges str + [ change (Block c ec) repl + | (c, needle) <- sort ns + , let ec = c + length needle - 1 + , repl <- maybeToList $ M.lookup needle unicodeReplacements + ] + + + +-------------------------------------------------------------------------------- +selectLines :: [Int] -> Lines -> [String] +selectLines = go 1 + where + go _ [] _ = [] + go _ _ [] = [] + go r (x : xs) (l : ls) + | r == x = l : go (r + 1) xs ls + | otherwise = go (r + 1) (x : xs) ls + + +-------------------------------------------------------------------------------- +groupPerLine :: [((Int, Int), a)] -> [(Int, [(Int, a)])] +groupPerLine = M.toList . M.fromListWith (++) . + map (\((r, c), x) -> (r, [(c, x)])) + + +-------------------------------------------------------------------------------- +types :: H.Module H.SrcSpanInfo -> Lines -> [((Int, Int), String)] types module' ls = - [ pos + [ (pos, "->") | H.TyFun _ t1 t2 <- everything module' , let start = H.srcSpanEnd $ H.srcInfoSpan $ H.ann t1 , let end = H.srcSpanStart $ H.srcInfoSpan $ H.ann t2 @@ -53,10 +96,8 @@ between (startRow, startCol) (endRow, endCol) needle = -------------------------------------------------------------------------------- -main :: IO () -main = do - (filePath : _) <- getArgs - contents <- readFile filePath - case parseModule (Just filePath) contents of - Left err -> error err - Right (module', _) -> print $ types module' (lines contents) +stylish :: Stylish +stylish ls (module', _) = applyChanges changes ls + where + changes = replaceAll perLine ls + perLine = sort $ groupPerLine $ types module' ls diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal index 0136031..221c429 100644 --- a/stylish-haskell.cabal +++ b/stylish-haskell.cabal @@ -35,6 +35,7 @@ Executable stylish-haskell StylishHaskell.Stylish.LanguagePragmas StylishHaskell.Stylish.Tabs StylishHaskell.Stylish.TrailingWhitespace + StylishHaskell.Stylish.UnicodeSyntax StylishHaskell.Util Build-depends: |