@@ -39,7 +39,7 @@ data Mismatch = MismatchInfo
3939
4040data Reason
4141 = BadFields [(String, Maybe Reason)]
42− | MessyFields [String] [String]
42+ | MessyFields [String] [String] [String]
4343 | IntFloat
4444 | TooLongComparableTuple Int
4545 | MissingArgs Int
@@ -591,8 +591,8 @@ flipReason reason =
591591 BadFields fields ->
592592 BadFields (map (second (fmap flipReason)) fields)
593593
594− MessyFields leftOnly rightOnly ->
595− MessyFields rightOnly leftOnly
594+ MessyFields both left right ->
595+ MessyFields both right left
596596
597597 IntFloat ->
598598 IntFloat
@@ -667,10 +667,8 @@ reasonToStringHelp reason =
667667 ++ Help.commaSep (map fst fields) ++ " fields. "
668668 ++ badFieldElaboration
669669
670− MessyFields leftOnly rightOnly ->
671− do let typos = Help.findPotentialTypos leftOnly rightOnly
672− _ <- Help.vetTypos typos
673− misspellingMessage typos
670+ MessyFields both leftOnly rightOnly ->
671+ messyFieldsHelp both leftOnly rightOnly
674672
675673 IntFloat ->
676674 go $
@@ -741,28 +739,104 @@ toHint str =
741739 fillSep (hintDoc : map text (words str))
742740
743741
744−misspellingMessage :: [(String,String)] -> Maybe (String, [Doc])
745−misspellingMessage typos =
746− if null typos then
747− Nothing
742+messyFieldsHelp :: [String] -> [String] -> [String] -> Maybe (String, [Doc])
743+messyFieldsHelp both leftOnly rightOnly =
744+ case (leftOnly, rightOnly) of
745+ ([], [missingField]) ->
746+ oneMissingField both missingField
747+
748+ ([missingField], []) ->
749+ oneMissingField both missingField
750+
751+ ([], missingFields) ->
752+ manyMissingFields both missingFields
753+
754+ (missingFields, []) ->
755+ manyMissingFields both missingFields
756+
757+ _ ->
758+ let
759+ typoPairs =
760+ case Help.findTypoPairs leftOnly rightOnly of
761+ [] ->
762+ Help.findTypoPairs (both ++ leftOnly) (both ++ rightOnly)
763+
764+ pairs ->
765+ pairs
766+ in
767+ if null typoPairs then
768+ Just
769+ ( "The record fields do not match up. One has "
770+ ++ Help.commaSep leftOnly ++ ". The other has "
771+ ++ Help.commaSep rightOnly ++ "."
772+ , []
773+ )
774+
775+ else
776+ Just
777+ ( "The record fields do not match up. Maybe you made one of these typos?"
778+ , typoDocs "<->" typoPairs
779+ )
780+
781+
782+oneMissingField :: [String] -> String -> Maybe (String, [Doc])
783+oneMissingField knownFields missingField =
784+ case Help.findPotentialTypos knownFields missingField of
785+ [] ->
786+ Just
787+ ( "Looks like a record is missing the `" ++ missingField ++ "` field."
788+ , []
789+ )
790+
791+ [typo] ->
792+ Just
793+ ( "Looks like a record is missing the `" ++ missingField
794+ ++ "` field. Maybe it is a typo?"
795+ , typoDocs "->" [(missingField, typo)]
796+ )
797+
798+ typos ->
799+ Just
800+ ( "Looks like a record is missing the `" ++ missingField
801+ ++ "` field. It is close to names like "
802+ ++ Help.commaSep typos ++ " so maybe it is a typo?"
803+ , []
804+ )
805+
748806
749− else
750− let
751− maxLen =
752− maximum (map (length . fst) typos)
753− in
807+manyMissingFields :: [String] -> [String] -> Maybe (String, [Doc])
808+manyMissingFields knownFields missingFields =
809+ case Help.findTypoPairs missingFields knownFields of
810+ [] ->
754811 Just
755− ( "I compared the record fields and found some potential typos."
756− , text "" : map (pad maxLen) typos
812+ ( "Looks like a record is missing these fields: "
813+ ++ Help.commaSep missingFields
814+ , []
757815 )
758816
817+ typoPairs ->
818+ Just
819+ ( "Looks like a record is missing these fields: " ++ Help.commaSep missingFields
820+ ++ ". Potential typos include:"
821+ , typoDocs "->" typoPairs
822+ )
823+
824+
825+typoDocs :: String -> [(String, String)] -> [Doc]
826+typoDocs arrow typoPairs =
827+ let
828+ maxLen =
829+ maximum (map (length . fst) typoPairs)
830+ in
831+ text "" : map (padTypo arrow maxLen) typoPairs
832+
759833
760−pad :: Int -> (String, String) -> Doc
761−pad maxLen (leftField, rightField) =
762− text (replicate (maxLen - length leftField) ' ')
763− <> dullyellow (text leftField)
764− <+> text "<->"
765− <+> dullyellow (text rightField)
834+padTypo :: String -> Int -> (String, String) -> Doc
835+padTypo arrow maxLen (missingField, knownField) =
836+ text (replicate (maxLen - length missingField) ' ')
837+ <> dullyellow (text missingField)
838+ <+> text arrow
839+ <+> dullyellow (text knownField)
766840
767841
768842