Skip to content

Commit 07f14e3

Browse files
Remove unlawful Ord instance and replace it by a compare function (#3271) (#3279)
1 parent e176eca commit 07f14e3

File tree

1 file changed

+11
-9
lines changed
  • plugins/hls-refactor-plugin/src/Development/IDE/Plugin

1 file changed

+11
-9
lines changed

plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ suggestNewImport packageExportsMap ps fileContents Diagnostic{_message}
15201520
, Just (range, indent) <- newImportInsertRange ps fileContents
15211521
, extendImportSuggestions <- matchRegexUnifySpaces msg
15221522
"Perhaps you want to add ‘[^’]*’ to the import list in the import of ‘([^’]*)’"
1523-
= let suggestions = nubSort
1523+
= let suggestions = nubSortBy simpleCompareImportSuggestion
15241524
(constructNewImportSuggestions packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions) in
15251525
map (\(ImportSuggestion _ kind (unNewImport -> imp)) -> (imp, kind, TextEdit range (imp <> "\n" <> T.replicate indent " "))) suggestions
15261526
where
@@ -1529,7 +1529,7 @@ suggestNewImport _ _ _ _ = []
15291529

15301530
constructNewImportSuggestions
15311531
:: ExportsMap -> (Maybe T.Text, NotInScope) -> Maybe [T.Text] -> [ImportSuggestion]
1532-
constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules = nubOrd
1532+
constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules = nubOrdBy simpleCompareImportSuggestion
15331533
[ suggestion
15341534
| Just name <- [T.stripPrefix (maybe "" (<> ".") qual) $ notInScope thingMissing] -- strip away qualified module names from the unknown name
15351535
, identInfo <- maybe [] Set.toList $ Map.lookup name (getExportsMap exportsMap) -- look up the modified unknown name in the export map
@@ -1568,16 +1568,18 @@ constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules =
15681568
| otherwise = 0
15691569
m = moduleNameText identInfo
15701570

1571-
-- | Implements a lexicographic order for import suggestions.
1572-
-- First compares the importance score in DESCENDING order.
1573-
-- If the scores are equal it compares the import names alphabetical order.
15741571
data ImportSuggestion = ImportSuggestion !Int !CodeActionKind !NewImport
15751572
deriving ( Eq )
15761573

1577-
instance Ord ImportSuggestion where
1578-
compare (ImportSuggestion s1 _ i1) (ImportSuggestion s2 _ i2)
1579-
| s1 == s2 = compare i1 i2
1580-
| otherwise = flip compare s1 s2
1574+
-- | Implements a lexicographic order for import suggestions that ignores the code action.
1575+
-- First it compares the importance score in DESCENDING order.
1576+
-- If the scores are equal it compares the import names alphabetical order.
1577+
--
1578+
-- TODO: this should be a correct Ord instance but CodeActionKind does not implement a Ord
1579+
-- which would lead to an unlawful Ord instance.
1580+
simpleCompareImportSuggestion :: ImportSuggestion -> ImportSuggestion -> Ordering
1581+
simpleCompareImportSuggestion (ImportSuggestion s1 _ i1) (ImportSuggestion s2 _ i2)
1582+
= flip compare s1 s2 <> compare i1 i2
15811583

15821584
newtype NewImport = NewImport {unNewImport :: T.Text}
15831585
deriving (Show, Eq, Ord)

0 commit comments

Comments
 (0)