From 3c603bcf30aefae1de202a3a1319a01e85ef7d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Go=C5=82=C4=99biewski?= Date: Mon, 5 Oct 2020 21:24:02 +0200 Subject: [PATCH 1/3] Refactor UnicodeSyntax.hs --- .../Haskell/Stylish/Step/UnicodeSyntax.hs | 40 +++++-------------- 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs b/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs index 2f0def63..cc21b6d8 100644 --- a/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs +++ b/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs @@ -53,36 +53,15 @@ groupPerLine = M.toList . M.fromListWith (++) . map (\((r, c), x) -> (r, [(c, x)])) --------------------------------------------------------------------------------- -typeSigs :: Module -> Lines -> [((Int, Int), String)] -typeSigs module' ls = - [ (pos, "::") +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 + , (funStart, _) <- infoPoints funLoc + , (_, typeEnd) <- infoPoints [hsSigWcType typeLoc] + , pos <- maybeToList $ between funStart typeEnd sym 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 - ] - - --------------------------------------------------------------------------------- -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 @@ -114,6 +93,7 @@ step' alp lg ls module' = applyChanges changes ls changes = (if alp then addLanguagePragma lg "UnicodeSyntax" module' else []) ++ replaceAll perLine perLine = sort $ groupPerLine $ - typeSigs module' ls ++ - contexts module' ls ++ - typeFuns module' ls + findSymbol' "::" ++ + findSymbol' "=>" ++ + findSymbol' "->" + findSymbol' = findSymbol module' ls From 80f0b98b7997b0feee2e9b68c063e3bba4867aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Go=C5=82=C4=99biewski?= Date: Mon, 5 Oct 2020 21:47:06 +0200 Subject: [PATCH 2/3] Use concatMap --- lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs b/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs index cc21b6d8..383bc2cc 100644 --- a/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs +++ b/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs @@ -92,8 +92,5 @@ step' alp lg ls module' = applyChanges changes ls where changes = (if alp then addLanguagePragma lg "UnicodeSyntax" module' else []) ++ replaceAll perLine - perLine = sort $ groupPerLine $ - findSymbol' "::" ++ - findSymbol' "=>" ++ - findSymbol' "->" - findSymbol' = findSymbol module' ls + toReplace = [ "::", "=>", "->" ] + perLine = sort $ groupPerLine $ concatMap (findSymbol module' ls) toReplace From b9837c154abe4c6263b7a8fe482a2f851667a782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Go=C5=82=C4=99biewski?= Date: Wed, 7 Oct 2020 13:12:18 +0200 Subject: [PATCH 3/3] Update lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs Co-authored-by: Jasper Van der Jeugt --- lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs b/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs index 383bc2cc..ff01deea 100644 --- a/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs +++ b/lib/Language/Haskell/Stylish/Step/UnicodeSyntax.hs @@ -52,7 +52,8 @@ groupPerLine :: [((Int, Int), a)] -> [(Int, [(Int, a)])] groupPerLine = M.toList . M.fromListWith (++) . map (\((r, c), x) -> (r, [(c, x)])) - +-- | 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)