@@ -1,15 +1,12 @@
11{-# OPTIONS_GHC -Wall #-}
22module Reporting.Error.Canonicalize where
33
4−import Data.Function (on)
5−import qualified Data.List as List
6−import qualified Text.EditDistance as Dist
74import qualified Text.PrettyPrint as P
85
96import qualified AST.Module as Module
107import qualified AST.Type as Type
118import qualified AST.Variable as Var
12−import Elm.Utils ((|>))
9+import qualified Reporting.Error.Helpers as Help
1310import qualified Reporting.PrettyPrint as P
1411import qualified Reporting.Region as Region
1512import qualified Reporting.Report as Report
@@ -109,25 +106,6 @@ port name isInbound rootType localType maybeMessage =
109106 Port (PortError name isInbound rootType localType maybeMessage)
110107
111108
112−-- NEARBY NAMES
113−
114−nearbyNames :: (a -> String) -> a -> [a] -> [a]
115−nearbyNames format name names =
116− let editDistance =
117− if length (format name) < 3 then 1 else 2
118− in
119− names
120− |> map (\x -> (distance (format name) (format x), x))
121− |> List.sortBy (compare `on` fst)
122− |> filter ( (<= editDistance) . abs . fst )
123− |> map snd
124−
125−
126−distance :: String -> String -> Int
127−distance x y =
128− Dist.restrictedDamerauLevenshteinDistance Dist.defaultEditCosts x y
129−
130−
131109-- TO REPORT
132110
133111namingError :: String -> String -> Report.Report
@@ -145,26 +123,26 @@ toReport dealiaser err =
145123 Ambiguous ->
146124 namingError
147125 ("This usage of " ++ var ++ " is ambiguous.")
148− (maybeYouWant suggestions)
126+ (Help.maybeYouWant suggestions)
149127
150128 UnknownQualifier qualifier localName ->
151129 namingError
152130 ("Cannot find " ++ var ++ ".")
153131 ( "The qualifier `" ++ qualifier ++ "` is not in scope. "
154− ++ maybeYouWant (map (\modul -> modul ++ "." ++ localName) suggestions)
132+ ++ Help.maybeYouWant (map (\modul -> modul ++ "." ++ localName) suggestions)
155133 )
156134
157135 QualifiedUnknown qualifier localName ->
158136 namingError
159137 ("Cannot find " ++ var ++ ".")
160138 ( "`" ++ qualifier ++ "` does not expose `" ++ localName ++ "`. "
161− ++ maybeYouWant (map (\v -> qualifier ++ "." ++ v) suggestions)
139+ ++ Help.maybeYouWant (map (\v -> qualifier ++ "." ++ v) suggestions)
162140 )
163141
164142 ExposedUnknown ->
165143 namingError
166144 ("Cannot find " ++ var)
167− (maybeYouWant suggestions)
145+ (Help.maybeYouWant suggestions)
168146
169147 Pattern patternError ->
170148 case patternError of
@@ -205,17 +183,17 @@ toReport dealiaser err =
205183 ModuleNotFound suggestions ->
206184 namingError
207185 ("Could not find a module named `" ++ moduleName ++ "`")
208− (maybeYouWant (map Module.nameToString suggestions))
186+ (Help.maybeYouWant (map Module.nameToString suggestions))
209187
210188 ValueNotFound value suggestions ->
211189 namingError
212190 ("Module `" ++ moduleName ++ "` does not expose `" ++ value ++ "`")
213− (maybeYouWant suggestions)
191+ (Help.maybeYouWant suggestions)
214192
215193 Export name suggestions ->
216194 namingError
217195 ("Could not export `" ++ name ++ "` which is not defined in this module.")
218− (maybeYouWant suggestions)
196+ (Help.maybeYouWant suggestions)
219197
220198 Port (PortError name isInbound _rootType localType maybeMessage) ->
221199 let
@@ -250,17 +228,6 @@ argMismatchReport kind var expected actual =
250228 Report.simple "ARGUMENT" preHint postHint
251229
252230
253−maybeYouWant :: [String] -> String
254−maybeYouWant suggestions =
255− case suggestions of
256− [] ->
257− ""
258−
259− _:_ ->
260− "Maybe you want one of the following?\n"
261− ++ concatMap ("\n "++) (take 4 suggestions)
262−
263−
264231extractSuggestions :: Error -> Maybe [String]
265232extractSuggestions err =
266233 case err of