on master help sign in
Trim the public API of Reporting.Suggest
commitcce1029f9ca9d5d944b3f0556544b9bf2b3109b1 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 24 Apr 2018, 00:10 (9 years ago) parent4940dfc
4 files changed+21−76
builder/src/Elm/Project/Licenses.hs+2−6 compiler/src/Reporting/Error/Canonicalize.hs+4−4 compiler/src/Reporting/Suggest.hs+14−65 ui/terminal/src/Terminal/Args/Error.hs+1−1
builder/src/Elm/Project/Licenses.hs+2−6
@@ -52,17 +52,13 @@ check rawName =
5252 Nothing ->
5353 let
5454 toPairs (License name code) =
55− [ (code, code), (name, code) ]
55+ [ (Text.unpack code, code), (Text.unpack name, code) ]
5656
5757 pairs =
5858 concatMap toPairs (Map.elems osiApprovedSpdxLicenses)
59−
60− toSuggestion (suggestion, _) =
61− Text.unpack suggestion
6259 in
6360 Left $ map snd $
64− Suggest.nearbyNames toSuggestion (rawName, rawName) pairs
65−
61+ Suggest.sort (Text.unpack rawName) fst pairs
6662
6763
6864
compiler/src/Reporting/Error/Canonicalize.hs+4−4
@@ -313,7 +313,7 @@ toReport source err =
313313 let
314314 suggestions =
315315 map N.toString $ take 4 $
316− Suggest.nearbyNames N.toString rawName possibleNames
316+ Suggest.sort (N.toString rawName) N.toString possibleNames
317317 in
318318 Report.Report "UNKNOWN EXPORT" region suggestions $
319319 let (a, thing, name) = toKindInfo kind rawName in
@@ -399,7 +399,7 @@ toReport source err =
399399 let
400400 suggestions =
401401 map N.toString $ take 4 $
402− Suggest.nearbyNames N.toString home possibleNames
402+ Suggest.sort (N.toString home) N.toString possibleNames
403403 in
404404 Report.Report "BAD IMPORT" region suggestions $
405405 Report.toCodeSnippet source region Nothing
@@ -491,7 +491,7 @@ toReport source err =
491491 let
492492 suggestions =
493493 map N.toString $ take 2 $
494− Suggest.nearbyNames N.toString op (Set.toList locals)
494+ Suggest.sort (N.toString op) N.toString (Set.toList locals)
495495
496496 format altOp =
497497 D.green $ "(" <> altOp <> ")"
@@ -1059,7 +1059,7 @@ notFound source region maybePrefix name thing (PossibleNames locals quals) =
10591059 Map.foldrWithKey addQuals (map N.toString (Set.toList locals)) quals
10601060
10611061 nearbyNames =
1062− take 4 (Suggest.nearbyNames id givenName possibleNames)
1062+ take 4 (Suggest.sort givenName id possibleNames)
10631063
10641064 toDetails noSuggestionDetails yesSuggestionDetails =
10651065 case nearbyNames of
compiler/src/Reporting/Suggest.hs+14−65
@@ -1,86 +1,35 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE OverloadedStrings #-}
33module Reporting.Suggest
4− ( findPotentialTypos, findTypoPairs, vetTypos
5− , nearbyNames, distance
4+ ( distance
5+ , sort
66 )
77 where
88
99
10−import Data.Function (on)
10+import qualified Data.Char as Char
1111import qualified Data.List as List
12−import qualified Data.Map as Map
13−import qualified Data.Maybe as Maybe
14−import qualified Data.Set as Set
1512import qualified Text.EditDistance as Dist
1613
1714
1815
19−-- FIND TYPOS
20−
21−
22−findPotentialTypos :: [String] -> String -> [String]
23−findPotentialTypos knownNames badName =
24− filter (\name -> distance badName name == 1) knownNames
25−
26−
27−findTypoPairs :: [String] -> [String] -> [(String, String)]
28−findTypoPairs leftOnly rightOnly =
29− let
30− veryNear leftName =
31− map ((,) leftName) (findPotentialTypos rightOnly leftName)
32− in
33− concatMap veryNear leftOnly
34−
35−
36−vetTypos :: [(String, String)] -> Maybe (Set.Set String, Set.Set String)
37−vetTypos potentialTypos =
38− let
39− tallyNames (ln, rn) (lc, rc) =
40− ( Map.insertWith (+) ln 1 lc
41− , Map.insertWith (+) rn 1 rc
42− )
43−
44− (leftCounts, rightCounts) =
45− foldr tallyNames (Map.empty, Map.empty) potentialTypos
46−
47− acceptable :: Map.Map String Int -> Bool
48− acceptable counts =
49− not (Map.null counts)
50− &&
51− Map.foldr (\n unique -> n < 2 && unique) True counts
52− in
53− if acceptable leftCounts && acceptable rightCounts then
54− Just (Map.keysSet leftCounts, Map.keysSet rightCounts)
55−
56− else
57− Nothing
16+-- DISTANCE
5817
5918
19+distance :: String -> String -> Int
20+distance x y =
21+ Dist.restrictedDamerauLevenshteinDistance Dist.defaultEditCosts x y
6022
61−-- NEARBY NAMES
6223
6324
64−nearbyNames :: (a -> String) -> a -> [a] -> [a]
65−nearbyNames toString value possibleValues =
66− let
67− name =
68− toString value
25+-- SORT
6926
70− editDistance =
71− if length name < 3 then 1 else 2
7227
73− getDistance pValue =
74− let
75− dist = abs (distance name (toString pValue))
76− in
77− if dist <= editDistance then Just (dist, pValue) else Nothing
78− in
79− map snd $
80− List.sortBy (compare `on` fst) $
81− Maybe.mapMaybe getDistance possibleValues
28+sort :: String -> (a -> String) -> [a] -> [a]
29+sort target toString values =
30+ List.sortOn (distance (toLower target) . toLower . toString) values
8231
8332
84−distance :: String -> String -> Int
85−distance x y =
86− Dist.restrictedDamerauLevenshteinDistance Dist.defaultEditCosts x y
33+toLower :: String -> String
34+toLower string =
35+ map Char.toLower string
ui/terminal/src/Terminal/Args/Error.hs+1−1
@@ -235,7 +235,7 @@ exitWithUnknown :: String -> [String] -> IO a
235235exitWithUnknown unknown knowns =
236236 let
237237 suggestions =
238− case map toGreen (Suggest.nearbyNames id unknown knowns) of
238+ case map toGreen (Suggest.sort unknown id knowns) of
239239 [] ->
240240 []
241241