diff options
author | Łukasz Gołębiewski <lukasz.golebiewski@gmail.com> | 2020-10-07 13:20:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-07 13:20:15 +0200 |
commit | 1f7f23db8ab5c048b837140d9633a82b1e642bae (patch) | |
tree | 45c22d6fc4a2ee32bc9b38e5672d83357efe0e27 | |
parent | 10ce71bb79cf9f6ab47ac9dfef503529c41bef00 (diff) | |
download | stylish-haskell-1f7f23db8ab5c048b837140d9633a82b1e642bae.tar.gz |
Refactor UnicodeSyntax.hs (#317)
* Refactor UnicodeSyntax.hs
Co-authored-by: Jasper Van der Jeugt <jaspervdj@gmail.com>
-rw-r--r-- | lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs | 42 |
1 files changed, 10 insertions, 32 deletions
diff --git a/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs b/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs index 2f0def6..ff01dee 100644 --- a/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs +++ b/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs @@ -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 @@ -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 |