Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,17 @@ groupPerLine :: [((Int, Int), a)] -> [(Int, [(Int, a)])]
groupPerLine = M.toList . M.fromListWith (++) .
map (\((r, c), x) -> (r, [(c, x)]))


--------------------------------------------------------------------------------
typeSigs :: Module -> Lines -> [((Int, Int), String)]
typeSigs module' ls =
[ (pos, "::")
-- | Find symbol positions in the module. Currently only searches in type
-- signatures.
findSymbol :: Module -> Lines -> String -> [((Int, Int), String)]
findSymbol module' ls sym =
[ (pos, sym)
| TypeSig _ funLoc typeLoc <- everything (rawModuleDecls $ moduleDecls module') :: [Sig GhcPs]
, (_, funEnd) <- infoPoints funLoc
, (typeStart, _) <- infoPoints [hsSigWcType typeLoc]
, pos <- maybeToList $ between funEnd typeStart "::" ls
]

--------------------------------------------------------------------------------
contexts :: Module -> Lines -> [((Int, Int), String)]
contexts module' ls =
[ (pos, "=>")
| TypeSig _ _ typeLoc <- everything (rawModuleDecls $ moduleDecls module') :: [Sig GhcPs]
, (start, end) <- infoPoints [hsSigWcType typeLoc]
, pos <- maybeToList $ between start end "=>" ls
, (funStart, _) <- infoPoints funLoc
, (_, typeEnd) <- infoPoints [hsSigWcType typeLoc]
, pos <- maybeToList $ between funStart typeEnd sym ls
]


--------------------------------------------------------------------------------
typeFuns :: Module -> Lines -> [((Int, Int), String)]
typeFuns module' ls =
[ (pos, "->")
| TypeSig _ _ typeLoc <- everything (rawModuleDecls $ moduleDecls module') :: [Sig GhcPs]
, (start, end) <- infoPoints [hsSigWcType typeLoc]
, pos <- maybeToList $ between start end "->" ls
]


--------------------------------------------------------------------------------
-- | Search for a needle in a haystack of lines. Only part the inside (startRow,
-- startCol), (endRow, endCol) is searched. The return value is the position of
Expand Down Expand Up @@ -113,7 +93,5 @@ step' alp lg ls module' = applyChanges changes ls
where
changes = (if alp then addLanguagePragma lg "UnicodeSyntax" module' else []) ++
replaceAll perLine
perLine = sort $ groupPerLine $
typeSigs module' ls ++
contexts module' ls ++
typeFuns module' ls
toReplace = [ "::", "=>", "->" ]
perLine = sort $ groupPerLine $ concatMap (findSymbol module' ls) toReplace