1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Error.Helpers
4 , functionName, hintLink, stack, reflowParagraph
5 , commaSep, capitalize, ordinalize, drawCycle
6 , nearbyNames, distance, maybeYouWant
10import Data.Function (on)
11import qualified Data.Char as Char
12import qualified Data.List as List
13import qualified Text.EditDistance as Dist
14import Text.PrettyPrint.ANSI.Leijen
15 ( Doc, (<>), dullyellow, fillSep, hardline, text, vcat )
17import qualified AST.Helpers as Help
18import qualified Elm.Compiler.Version as Compiler
19import qualified Elm.Package as Pkg
26(|>) :: a -> (a -> b) -> b
38functionName :: String -> String
40 if Help.isOp opName then
47hintLink :: String -> String
49 "<https://github.com/elm-lang/elm-compiler/blob/"
50 ++ Pkg.versionToString Compiler.version
51 ++ "/hints/" ++ fileName ++ ".md>"
58 error "function `stack` requires a non-empty list of docs"
61 List.foldl' (\a b -> a <> hardline <> hardline <> b) doc docs
64reflowParagraph :: String -> Doc
65reflowParagraph paragraph =
66 fillSep (map text (words paragraph))
69commaSep :: [String] -> String
76 " " ++ token1 ++ " and " ++ token2
79 " " ++ List.intercalate ", " (init tokens) ++ ", and " ++ last tokens
82capitalize :: String -> String
90ordinalize :: Int -> String
100 | remainder100 `elem` [11..13] = "th"
101 | remainder10 == 1 = "st"
102 | remainder10 == 2 = "nd"
103 | remainder10 == 3 = "rd"
106 show number ++ ending
110drawCycle :: [String] -> Doc
117 text "│ " <> dullyellow (text str)
125 vcat (topLine : List.intersperse midLine (map line strings) ++ [ bottomLine ])
132nearbyNames :: (a -> String) -> a -> [a] -> [a]
133nearbyNames format name names =
135 if length (format name) < 3 then 1 else 2
138 |> map (\x -> (distance (format name) (format x), x))
139 |> List.sortBy (compare `on` fst)
140 |> filter ( (<= editDistance) . abs . fst )
144distance :: String -> String -> Int
146 Dist.restrictedDamerauLevenshteinDistance Dist.defaultEditCosts x y
149maybeYouWant :: [String] -> String
150maybeYouWant suggestions =
156 "Maybe you want one of the following?\n"
157 ++ concatMap ("\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