on 54dfb21 help sign in
elk/src/Reporting/Error/Helpers.hs
39 lines 902 B history raw
1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Error.Helpers where
4import Data.Function (on)
5import qualified Data.List as List
6import qualified Text.EditDistance as Dist
8import Elm.Utils ((|>))
11-- NEARBY NAMES
13nearbyNames :: (a -> String) -> a -> [a] -> [a]
14nearbyNames format name names =
15 let editDistance =
16 if length (format name) < 3 then 1 else 2
17 in
18 names
19 |> map (\x -> (distance (format name) (format x), x))
20 |> List.sortBy (compare `on` fst)
21 |> filter ( (<= editDistance) . abs . fst )
22 |> map snd
25distance :: String -> String -> Int
26distance x y =
27 Dist.restrictedDamerauLevenshteinDistance Dist.defaultEditCosts x y
30maybeYouWant :: [String] -> String
31maybeYouWant suggestions =
32 case suggestions of
33 [] ->
34 ""
36 _:_ ->
37 "Maybe you want one of the following?\n"
38 ++ concatMap ("\n "++) (take 4 suggestions)