@@ -150,6 +150,7 @@ iePluginDescriptor recorder plId =
150
150
, wrap suggestNewOrExtendImportForClassMethod
151
151
, wrap suggestHideShadow
152
152
, wrap suggestNewImport
153
+ , wrap suggestAddRecordFieldImport
153
154
]
154
155
plId
155
156
in mkExactprintPluginDescriptor recorder $ old {pluginHandlers = pluginHandlers old <> mkPluginHandler STextDocumentCodeAction codeAction }
@@ -1211,6 +1212,25 @@ suggestFixConstructorImport Diagnostic{_range=_range,..}
1211
1212
in [(" Fix import of " <> fixedImport, TextEdit _range fixedImport)]
1212
1213
| otherwise = []
1213
1214
1215
+ suggestAddRecordFieldImport :: ExportsMap -> DynFlags -> Annotated ParsedSource -> T. Text -> Diagnostic -> [(T. Text , CodeActionKind , TextEdit )]
1216
+ suggestAddRecordFieldImport exportsMap df ps fileContents Diagnostic {.. }
1217
+ | Just fieldName <- findMissingField _message
1218
+ , Just (range, indent) <- newImportInsertRange ps fileContents
1219
+ = let qis = qualifiedImportStyle df
1220
+ suggestions = nubSortBy simpleCompareImportSuggestion (constructNewImportSuggestions exportsMap (Nothing , NotInScopeThing fieldName) Nothing qis)
1221
+ in map (\ (ImportSuggestion _ kind (unNewImport -> imp)) -> (imp, kind, TextEdit range (imp <> " \n " <> T. replicate indent " " ))) suggestions
1222
+ | otherwise = []
1223
+ where
1224
+ findMissingField :: T. Text -> Maybe T. Text
1225
+ findMissingField t =
1226
+ let
1227
+ hasfieldRegex = " ((.+\\ .)?HasField) \" (.+)\" ([^ ]+) ([^ ]+)"
1228
+ regex = " (No instance for|Could not deduce):? (\\ (" <> hasfieldRegex <> " \\ )|‘" <> hasfieldRegex <> " ’|" <> hasfieldRegex <> " )"
1229
+ match = filter (/= " " ) <$> matchRegexUnifySpaces t regex
1230
+ in case match of
1231
+ Just [_, _, _, _, fieldName, _, _] -> Just fieldName
1232
+ _ -> Nothing
1233
+
1214
1234
-- | Suggests a constraint for a declaration for which a constraint is missing.
1215
1235
suggestConstraint :: DynFlags -> ParsedSource -> Diagnostic -> [(T. Text , Rewrite )]
1216
1236
suggestConstraint df (makeDeltaAst -> parsedModule) diag@ Diagnostic {.. }
0 commit comments