@@ -5,12 +5,17 @@ module Reporting.Exit.Deps
55 )
66 where
77
8+
9+import Data.Function (on)
10+import qualified Data.List as List
11+import qualified Data.Text as Text
812import Text.PrettyPrint.ANSI.Leijen ((<>))
913import qualified Text.PrettyPrint.ANSI.Leijen as P
1014
1115import qualified Elm.Compiler as Compiler
1216import qualified Elm.Package as Pkg
1317import qualified Elm.Project.Constraint as Con
18+import qualified Elm.Utils as Utils
1419import qualified Reporting.Exit.Help as Help
1520
1621
@@ -46,12 +51,21 @@ toReport exit =
4651 \ so you can delete it and see if that fixes the issue."
4752 ]
4853
49− PackageNotFound package suggestions ->
50− Help.report "PACKAGE NOT FOUND" Nothing
51− ( "Could not find any packages named " ++ Pkg.toString package
54+ PackageNotFound package allPackages ->
55+ let
56+ suggestions =
57+ nearbyNames package allPackages
58+ in
59+ Help.docReport "PACKAGE NOT FOUND" Nothing
60+ ( P.fillSep
61+ ["I","cannot","find","a"
62+ ,P.red (P.text (Pkg.toString package))
63+ ,"package","on","the","package","website."
64+ ]
5265 )
53− [ P.text $ "Maybe you want one of these instead?"
54− , P.indent 4 $ P.vcat $ map (P.text . Pkg.toString) suggestions
66+ [ "Maybe you want one of these instead?"
67+ , P.indent 4 $ P.dullyellow $ P.vcat $ map (P.text . Pkg.toString) suggestions
68+ , "But check <https://package.elm-lang.org> to see all possibilities!"
5569 ]
5670
5771 AppBadElm version ->
@@ -97,3 +111,34 @@ toReport exit =
97111 "This probably means the downloaded files got corrupted somehow.\
98112 \ Maybe try deleting your ELM_HOME and see if that resolves the issue?"
99113 ]
114+
115+
116+
117+-- NEARBY NAMES
118+
119+
120+nearbyNames :: Pkg.Name -> [Pkg.Name] -> [Pkg.Name]
121+nearbyNames (Pkg.Name author1 project1) possibleNames =
122+ let
123+ authorDist = authorDistance (Text.unpack author1)
124+ projectDist = projectDistance (Text.unpack project1)
125+
126+ addDistance name@(Pkg.Name author2 project2) =
127+ ( authorDist author2 + projectDist project2, name )
128+ in
129+ map snd $ take 4 $
130+ List.sortBy (compare `on` fst) $
131+ map addDistance possibleNames
132+
133+
134+authorDistance :: String -> Text.Text -> Int
135+authorDistance bad possibility =
136+ abs (Utils.distance bad (Text.unpack possibility))
137+
138+
139+projectDistance :: String -> Text.Text -> Int
140+projectDistance bad possibility =
141+ if possibility == "elm-lang" || possibility == "elm-explorations" then
142+ 0
143+ else
144+ abs (Utils.distance bad (Text.unpack possibility))