on master help sign in
Trim the public API of Reporting.Suggest
cce1029Evan Czaplicki
builder/src/Elm/Project/Licenses.hs+2−6
@@ -52,17 +52,13 @@ check rawName =
52 Nothing ->
52 Nothing ->
53 let
53 let
54 toPairs (License name code) =
54 toPairs (License name code) =
55− [ (code, code), (name, code) ]
55+ [ (Text.unpack code, code), (Text.unpack name, code) ]
56
56
57 pairs =
57 pairs =
58 concatMap toPairs (Map.elems osiApprovedSpdxLicenses)
58 concatMap toPairs (Map.elems osiApprovedSpdxLicenses)
59−
60− toSuggestion (suggestion, _) =
61− Text.unpack suggestion
62 in
59 in
63 Left $ map snd $
60 Left $ map snd $
64− Suggest.nearbyNames toSuggestion (rawName, rawName) pairs
61+ Suggest.sort (Text.unpack rawName) fst pairs
65−
66
62
67
63
68
64
compiler/src/Reporting/Error/Canonicalize.hs+4−4
@@ -313,7 +313,7 @@ toReport source err =
313 let
313 let
314 suggestions =
314 suggestions =
315 map N.toString $ take 4 $
315 map N.toString $ take 4 $
316− Suggest.nearbyNames N.toString rawName possibleNames
316+ Suggest.sort (N.toString rawName) N.toString possibleNames
317 in
317 in
318 Report.Report "UNKNOWN EXPORT" region suggestions $
318 Report.Report "UNKNOWN EXPORT" region suggestions $
319 let (a, thing, name) = toKindInfo kind rawName in
319 let (a, thing, name) = toKindInfo kind rawName in
@@ -399,7 +399,7 @@ toReport source err =
399 let
399 let
400 suggestions =
400 suggestions =
401 map N.toString $ take 4 $
401 map N.toString $ take 4 $
402− Suggest.nearbyNames N.toString home possibleNames
402+ Suggest.sort (N.toString home) N.toString possibleNames
403 in
403 in
404 Report.Report "BAD IMPORT" region suggestions $
404 Report.Report "BAD IMPORT" region suggestions $
405 Report.toCodeSnippet source region Nothing
405 Report.toCodeSnippet source region Nothing
@@ -491,7 +491,7 @@ toReport source err =
491 let
491 let
492 suggestions =
492 suggestions =
493 map N.toString $ take 2 $
493 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)
495
495
496 format altOp =
496 format altOp =
497 D.green $ "(" <> altOp <> ")"
497 D.green $ "(" <> altOp <> ")"
@@ -1059,7 +1059,7 @@ notFound source region maybePrefix name thing (PossibleNames locals quals) =
1059 Map.foldrWithKey addQuals (map N.toString (Set.toList locals)) quals
1059 Map.foldrWithKey addQuals (map N.toString (Set.toList locals)) quals
1060
1060
1061 nearbyNames =
1061 nearbyNames =
1062− take 4 (Suggest.nearbyNames id givenName possibleNames)
1062+ take 4 (Suggest.sort givenName id possibleNames)
1063
1063
1064 toDetails noSuggestionDetails yesSuggestionDetails =
1064 toDetails noSuggestionDetails yesSuggestionDetails =
1065 case nearbyNames of
1065 case nearbyNames of
compiler/src/Reporting/Suggest.hs+14−65
@@ -1,86 +1,35 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Suggest
3module Reporting.Suggest
4− ( findPotentialTypos, findTypoPairs, vetTypos
4+ ( distance
5− , nearbyNames, distance
5+ , sort
6 )
6 )
7 where
7 where
8
8
9
9
10−import Data.Function (on)
10+import qualified Data.Char as Char
11import qualified Data.List as List
11import 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
15import qualified Text.EditDistance as Dist
12import qualified Text.EditDistance as Dist
16
13
17
14
18
15
19−-- FIND TYPOS
16+-- DISTANCE
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
58
17
59
18
19+distance :: String -> String -> Int
20+distance x y =
21+ Dist.restrictedDamerauLevenshteinDistance Dist.defaultEditCosts x y
60
22
61−-- NEARBY NAMES
62
23
63
24
64−nearbyNames :: (a -> String) -> a -> [a] -> [a]
25+-- SORT
65−nearbyNames toString value possibleValues =
66− let
67− name =
68− toString value
69
26
70− editDistance =
71− if length name < 3 then 1 else 2
72
27
73− getDistance pValue =
28+sort :: String -> (a -> String) -> [a] -> [a]
74− let
29+sort target toString values =
75− dist = abs (distance name (toString pValue))
30+ List.sortOn (distance (toLower target) . toLower . toString) values
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
82
31
83
32
84−distance :: String -> String -> Int
33+toLower :: String -> String
85−distance x y =
34+toLower string =
86− Dist.restrictedDamerauLevenshteinDistance Dist.defaultEditCosts x y
35+ map Char.toLower string
ui/terminal/src/Terminal/Args/Error.hs+1−1
@@ -235,7 +235,7 @@ exitWithUnknown :: String -> [String] -> IO a
235exitWithUnknown unknown knowns =
235exitWithUnknown unknown knowns =
236 let
236 let
237 suggestions =
237 suggestions =
238− case map toGreen (Suggest.nearbyNames id unknown knowns) of
238+ case map toGreen (Suggest.sort unknown id knowns) of
239 [] ->
239 [] ->
240 []
240 []
241
241