1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Helpers
6 , Doc, (<+>), (<>), cat, colon, comma, dullyellow, equals, fillSep, hang
7 , hsep, indent, lbrace, lparen, parens, rbrace, rparen, sep, text, underline
12 , hintLink, stack, reflowParagraph
13 , commaSep, capitalize, ordinalize, drawCycle
14 , findPotentialTypos, findTypoPairs, vetTypos
15 , nearbyNames, distance, 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"
76hintLink :: Text -> Text
78 "<https://github.com/elm-lang/elm-compiler/blob/"
79 <> Text.pack (Pkg.versionToString Compiler.version)
80 <> "/hints/" <> fileName <> ".md>"
87 error "function `stack` requires a non-empty list of docs"
90 List.foldl' (\a b -> a <> hardline <> hardline <> b) doc docs
93reflowParagraph :: Text -> Doc
94reflowParagraph paragraph =
95 fillSep (map text (Text.words paragraph))
98commaSep :: [Text] -> Text
105 " " <> token1 <> " and " <> token2
108 " " <> Text.intercalate ", " (init tokens) <> ", and " <> last tokens
111capitalize :: Text -> Text
113 case Text.uncons txt of
118 Text.cons (Char.toUpper c) rest
121ordinalize :: Int -> Text
131 | remainder100 `elem` [11..13] = "th"
132 | remainder10 == 1 = "st"
133 | remainder10 == 2 = "nd"
134 | remainder10 == 3 = "rd"
141drawCycle :: [Text] -> Doc
148 text "│ " <> dullyellow (text name)
156 vcat (topLine : List.intersperse midLine (map nameLine names) ++ [ bottomLine ])
163findPotentialTypos :: [Text] -> Text -> [Text]
164findPotentialTypos knownNames badName =
165 filter ((==1) . distance badName) knownNames
168findTypoPairs :: [Text] -> [Text] -> [(Text, Text)]
169findTypoPairs leftOnly rightOnly =
172 map ((,) leftName) (findPotentialTypos rightOnly leftName)
174 concatMap veryNear leftOnly
177vetTypos :: [(Text, Text)] -> Maybe (Set.Set Text, Set.Set Text)
178vetTypos potentialTypos =
180 tallyNames (ln, rn) (lc, rc) =
181 ( Map.insertWith (+) ln 1 lc
182 , Map.insertWith (+) rn 1 rc
185 (leftCounts, rightCounts) =
186 foldr tallyNames (Map.empty, Map.empty) potentialTypos
188 acceptable :: Map.Map Text Int -> Bool
190 not (Map.null counts)
192 Map.foldr (\n unique -> n < 2 && unique) True counts
194 if acceptable leftCounts && acceptable rightCounts then
195 Just (Map.keysSet leftCounts, Map.keysSet rightCounts)
205nearbyNames :: (a -> Text) -> a -> [a] -> [a]
206nearbyNames format name names =
208 if Text.length (format name) < 3 then 1 else 2
211 |> map (\x -> (distance (format name) (format x), x))
212 |> List.sortBy (compare `on` fst)
213 |> filter ( (<= editDistance) . abs . fst )
217distance :: Text -> Text -> Int
219 Dist.restrictedDamerauLevenshteinDistance
220 Dist.defaultEditCosts
225maybeYouWant :: [Text] -> Text
226maybeYouWant suggestions =
232 "Maybe you want one of the following?\n"
234 Text.concat (map ("\n " <>) (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