1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Helpers
6 , Doc, (<+>), (<>), cat, dullyellow, fillSep, green, hang
7 , hsep, indent, parens, sep, text, underline, vcat
10 , functionName, args, moreArgs
11 , toSimpleHint, toFancyHint, hintLink
12 , stack, reflowParagraph
13 , commaSep, capitalize, ordinalize, drawCycle
14 , findPotentialTypos, findTypoPairs, vetTypos
15 , nearbyNames, distance, maybeYouWant, maybeYouWant'
19import Data.Function (on)
20import qualified Data.Char as Char
21import qualified Data.List as List
22import qualified Data.Map as Map
23import Data.Monoid ((<>))
24import qualified Data.Set as Set
25import qualified Data.Text as Text
26import Data.Text (Text)
27import qualified Text.EditDistance as Dist
28import qualified Text.PrettyPrint.ANSI.Leijen as P
29import Text.PrettyPrint.ANSI.Leijen hiding ((<>), text)
31import qualified AST.Helpers as Help
32import qualified Elm.Compiler.Version as Compiler
33import qualified Elm.Package as Pkg
40(|>) :: a -> (a -> b) -> b
54 P.text (Text.unpack msg)
62functionName :: Text -> Text
64 if Help.isOp opName then
73 i2t n <> if n == 1 then " argument" else " arguments"
76moreArgs :: Int -> Text
78 i2t n <> " more" <> if n == 1 then " argument" else " arguments"
85toSimpleHint :: Text -> Doc
87 toFancyHint (map text (Text.words txt))
90toFancyHint :: [Doc] -> Doc
92 fillSep (underline (text "Hint") <> ":" : chunks)
95hintLink :: Text -> Text
97 "<https://github.com/elm-lang/elm-compiler/blob/"
98 <> Text.pack (Pkg.versionToString Compiler.version)
99 <> "/hints/" <> fileName <> ".md>"
106 error "function `stack` requires a non-empty list of docs"
109 List.foldl' (\a b -> a <> hardline <> hardline <> b) doc docs
112reflowParagraph :: Text -> Doc
113reflowParagraph paragraph =
114 fillSep (map text (Text.words paragraph))
117commaSep :: [Text] -> Text
124 " " <> token1 <> " and " <> token2
127 " " <> Text.intercalate ", " (init tokens) <> ", and " <> last tokens
130capitalize :: Text -> Text
132 case Text.uncons txt of
137 Text.cons (Char.toUpper c) rest
140ordinalize :: Int -> Text
150 | remainder100 `elem` [11..13] = "th"
151 | remainder10 == 1 = "st"
152 | remainder10 == 2 = "nd"
153 | remainder10 == 3 = "rd"
160drawCycle :: [Text] -> Doc
167 text "│ " <> dullyellow (text name)
175 vcat (topLine : List.intersperse midLine (map nameLine names) ++ [ bottomLine ])
182findPotentialTypos :: [Text] -> Text -> [Text]
183findPotentialTypos knownNames badName =
184 filter ((==1) . distance badName) knownNames
187findTypoPairs :: [Text] -> [Text] -> [(Text, Text)]
188findTypoPairs leftOnly rightOnly =
191 map ((,) leftName) (findPotentialTypos rightOnly leftName)
193 concatMap veryNear leftOnly
196vetTypos :: [(Text, Text)] -> Maybe (Set.Set Text, Set.Set Text)
197vetTypos potentialTypos =
199 tallyNames (ln, rn) (lc, rc) =
200 ( Map.insertWith (+) ln 1 lc
201 , Map.insertWith (+) rn 1 rc
204 (leftCounts, rightCounts) =
205 foldr tallyNames (Map.empty, Map.empty) potentialTypos
207 acceptable :: Map.Map Text Int -> Bool
209 not (Map.null counts)
211 Map.foldr (\n unique -> n < 2 && unique) True counts
213 if acceptable leftCounts && acceptable rightCounts then
214 Just (Map.keysSet leftCounts, Map.keysSet rightCounts)
224nearbyNames :: (a -> Text) -> a -> [a] -> [a]
225nearbyNames format name names =
227 if Text.length (format name) < 3 then 1 else 2
230 |> map (\x -> (distance (format name) (format x), x))
231 |> List.sortBy (compare `on` fst)
232 |> filter ( (<= editDistance) . abs . fst )
236distance :: Text -> Text -> Int
238 Dist.restrictedDamerauLevenshteinDistance
239 Dist.defaultEditCosts
244maybeYouWant :: Maybe Doc -> [Text] -> Doc
245maybeYouWant maybeStarter suggestions =
246 maybe P.empty id (maybeYouWant' maybeStarter suggestions)
249maybeYouWant' :: Maybe Doc -> [Text] -> Maybe Doc
250maybeYouWant' maybeStarter suggestions =
257 [ maybe id (<+>) maybeStarter "Maybe you want one of the following?"
258 , P.indent 4 $ P.vcat $ map (P.dullyellow . text) (take 4 suggestions)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list