Skip to content

Commit 5c869b0

Browse files
committed
Add an assist for OverloadedRecordDot
1 parent 6887387 commit 5c869b0

File tree

1 file changed

+20
-0
lines changed
  • plugins/hls-refactor-plugin/src/Development/IDE/Plugin

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ iePluginDescriptor recorder plId =
150150
, wrap suggestNewOrExtendImportForClassMethod
151151
, wrap suggestHideShadow
152152
, wrap suggestNewImport
153+
, wrap suggestAddRecordFieldImport
153154
]
154155
plId
155156
in mkExactprintPluginDescriptor recorder $ old {pluginHandlers = pluginHandlers old <> mkPluginHandler STextDocumentCodeAction codeAction }
@@ -1211,6 +1212,25 @@ suggestFixConstructorImport Diagnostic{_range=_range,..}
12111212
in [("Fix import of " <> fixedImport, TextEdit _range fixedImport)]
12121213
| otherwise = []
12131214

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+
12141234
-- | Suggests a constraint for a declaration for which a constraint is missing.
12151235
suggestConstraint :: DynFlags -> ParsedSource -> Diagnostic -> [(T.Text, Rewrite)]
12161236
suggestConstraint df (makeDeltaAst -> parsedModule) diag@Diagnostic {..}

0 commit comments

Comments
 (0)