Skip to content

Commit 0e9dca4

Browse files
berbermanpepeiborraisovectormergify[bot]
authored
Add custom code action kinds for import related code actions (#1570)
* Add custom code action kinds for import related code actions * Rename: identifier -> thing * Rename: thing -> list * Remove an assertion of code action kinds in func-test Co-authored-by: Pepe Iborra <[email protected]> Co-authored-by: Sandy Maguire <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 2fd2d8d commit 0e9dca4

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

ghcide/src/Development/IDE/Plugin/CodeAction.hs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import Data.Maybe
3535
import qualified Data.Rope.UTF16 as Rope
3636
import qualified Data.Set as S
3737
import qualified Data.Text as T
38+
import Data.Tuple.Extra (fst3)
3839
import Development.IDE.Core.RuleTypes
3940
import Development.IDE.Core.Rules
4041
import Development.IDE.Core.Service
@@ -740,7 +741,7 @@ getIndentedGroupsBy pred inp = case dropWhile (not.pred) inp of
740741
indentation :: T.Text -> Int
741742
indentation = T.length . T.takeWhile isSpace
742743

743-
suggestExtendImport :: ExportsMap -> ParsedSource -> Diagnostic -> [(T.Text, Rewrite)]
744+
suggestExtendImport :: ExportsMap -> ParsedSource -> Diagnostic -> [(T.Text, CodeActionKind, Rewrite)]
744745
suggestExtendImport exportsMap (L _ HsModule {hsmodImports}) Diagnostic{_range=_range,..}
745746
| Just [binding, mod, srcspan] <-
746747
matchRegexUnifySpaces _message
@@ -759,6 +760,7 @@ suggestExtendImport exportsMap (L _ HsModule {hsmodImports}) Diagnostic{_range=_
759760
Just decl <- findImportDeclByRange decls range,
760761
Just ident <- lookupExportMap binding mod
761762
= [ ( "Add " <> renderImportStyle importStyle <> " to the import list of " <> mod
763+
, quickFixImportKind' "extend" importStyle
762764
, uncurry extendImport (unImportStyle importStyle) decl
763765
)
764766
| importStyle <- NE.toList $ importStyles ident
@@ -1138,7 +1140,7 @@ removeRedundantConstraints mContents Diagnostic{..}
11381140

11391141
-------------------------------------------------------------------------------------------------
11401142

1141-
suggestNewOrExtendImportForClassMethod :: ExportsMap -> ParsedSource -> Diagnostic -> [(T.Text, [Either TextEdit Rewrite])]
1143+
suggestNewOrExtendImportForClassMethod :: ExportsMap -> ParsedSource -> Diagnostic -> [(T.Text, CodeActionKind, [Either TextEdit Rewrite])]
11421144
suggestNewOrExtendImportForClassMethod packageExportsMap ps Diagnostic {_message}
11431145
| Just [methodName, className] <-
11441146
matchRegexUnifySpaces
@@ -1157,6 +1159,7 @@ suggestNewOrExtendImportForClassMethod packageExportsMap ps Diagnostic {_message
11571159
-- extend
11581160
Just decl ->
11591161
[ ( "Add " <> renderImportStyle style <> " to the import list of " <> moduleNameText,
1162+
quickFixImportKind' "extend" style,
11601163
[Right $ uncurry extendImport (unImportStyle style) decl]
11611164
)
11621165
| style <- importStyle
@@ -1165,15 +1168,15 @@ suggestNewOrExtendImportForClassMethod packageExportsMap ps Diagnostic {_message
11651168
_
11661169
| Just (range, indent) <- newImportInsertRange ps
11671170
->
1168-
(\(unNewImport -> x) -> (x, [Left $ TextEdit range (x <> "\n" <> T.replicate indent " ")])) <$>
1169-
[ newUnqualImport moduleNameText rendered False
1171+
(\(kind, unNewImport -> x) -> (x, kind, [Left $ TextEdit range (x <> "\n" <> T.replicate indent " ")])) <$>
1172+
[ (quickFixImportKind' "new" style, newUnqualImport moduleNameText rendered False)
11701173
| style <- importStyle,
11711174
let rendered = renderImportStyle style
11721175
]
1173-
<> [newImportAll moduleNameText]
1176+
<> [(quickFixImportKind "new.all", newImportAll moduleNameText)]
11741177
| otherwise -> []
11751178

1176-
suggestNewImport :: ExportsMap -> ParsedSource -> Diagnostic -> [(T.Text, TextEdit)]
1179+
suggestNewImport :: ExportsMap -> ParsedSource -> Diagnostic -> [(T.Text, CodeActionKind, TextEdit)]
11771180
suggestNewImport packageExportsMap ps@(L _ HsModule {..}) Diagnostic{_message}
11781181
| msg <- unifySpaces _message
11791182
, Just thingMissing <- extractNotInScopeName msg
@@ -1186,14 +1189,14 @@ suggestNewImport packageExportsMap ps@(L _ HsModule {..}) Diagnostic{_message}
11861189
, Just (range, indent) <- newImportInsertRange ps
11871190
, extendImportSuggestions <- matchRegexUnifySpaces msg
11881191
"Perhaps you want to add ‘[^’]*’ to the import list in the import of ‘([^’]*)’"
1189-
= sortOn fst [(imp, TextEdit range (imp <> "\n" <> T.replicate indent " "))
1190-
| (unNewImport -> imp) <- constructNewImportSuggestions packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions
1192+
= sortOn fst3 [(imp, kind, TextEdit range (imp <> "\n" <> T.replicate indent " "))
1193+
| (kind, unNewImport -> imp) <- constructNewImportSuggestions packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions
11911194
]
11921195
suggestNewImport _ _ _ = []
11931196

11941197
constructNewImportSuggestions
1195-
:: ExportsMap -> (Maybe T.Text, NotInScope) -> Maybe [T.Text] -> [NewImport]
1196-
constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules = nubOrd
1198+
:: ExportsMap -> (Maybe T.Text, NotInScope) -> Maybe [T.Text] -> [(CodeActionKind, NewImport)]
1199+
constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules = nubOrdOn snd
11971200
[ suggestion
11981201
| Just name <- [T.stripPrefix (maybe "" (<> ".") qual) $ notInScope thingMissing]
11991202
, identInfo <- maybe [] Set.toList $ Map.lookup name (getExportsMap exportsMap)
@@ -1202,14 +1205,14 @@ constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules =
12021205
, suggestion <- renderNewImport identInfo
12031206
]
12041207
where
1205-
renderNewImport :: IdentInfo -> [NewImport]
1208+
renderNewImport :: IdentInfo -> [(CodeActionKind, NewImport)]
12061209
renderNewImport identInfo
12071210
| Just q <- qual
1208-
= [newQualImport m q]
1211+
= [(quickFixImportKind "new.qualified", newQualImport m q)]
12091212
| otherwise
1210-
= [newUnqualImport m (renderImportStyle importStyle) False
1213+
= [(quickFixImportKind' "new" importStyle, newUnqualImport m (renderImportStyle importStyle) False)
12111214
| importStyle <- NE.toList $ importStyles identInfo] ++
1212-
[newImportAll m]
1215+
[(quickFixImportKind "new.all", newImportAll m)]
12131216
where
12141217
m = moduleNameText identInfo
12151218

@@ -1554,3 +1557,10 @@ renderImportStyle (ImportViaParent x p) = p <> "(" <> x <> ")"
15541557
unImportStyle :: ImportStyle -> (Maybe String, String)
15551558
unImportStyle (ImportTopLevel x) = (Nothing, T.unpack x)
15561559
unImportStyle (ImportViaParent x y) = (Just $ T.unpack y, T.unpack x)
1560+
1561+
quickFixImportKind' :: T.Text -> ImportStyle -> CodeActionKind
1562+
quickFixImportKind' x (ImportTopLevel _) = CodeActionUnknown $ "quickfix.import." <> x <> ".list.topLevel"
1563+
quickFixImportKind' x (ImportViaParent _ _) = CodeActionUnknown $ "quickfix.import." <> x <> ".list.withParent"
1564+
1565+
quickFixImportKind :: T.Text -> CodeActionKind
1566+
quickFixImportKind x = CodeActionUnknown $ "quickfix.import." <> x

ghcide/src/Development/IDE/Plugin/CodeAction/Args.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ instance ToTextEdit a => ToCodeAction (CodeActionTitle, CodeActionKind, a) where
116116
toCodeAction caa (title, kind, te) = [(title, Just kind, Nothing, toTextEdit caa te)]
117117

118118
instance ToTextEdit a => ToCodeAction (CodeActionTitle, CodeActionPreferred, a) where
119-
toCodeAction caa (title, isPreferred, te) = [(title, Nothing, Just isPreferred, toTextEdit caa te)]
119+
toCodeAction caa (title, isPreferred, te) = [(title, Just CodeActionQuickFix, Just isPreferred, toTextEdit caa te)]
120120

121121
instance ToTextEdit a => ToCodeAction (CodeActionTitle, CodeActionKind, CodeActionPreferred, a) where
122122
toCodeAction caa (title, kind, isPreferred, te) = [(title, Just kind, Just isPreferred, toTextEdit caa te)]

test/functional/FunctionalCodeAction.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ importTests = testGroup "import suggestions" [
275275
importControlMonad <- liftIO $ inspectCodeAction actionsOrCommands ["import Control.Monad"]
276276
liftIO $ do
277277
expectCodeAction actionsOrCommands ["import Control.Monad (when)"]
278-
forM_ actns $ \a -> do
279-
a ^. L.kind @?= Just CodeActionQuickFix
280278
length actns >= 10 @? "There are some actions"
281279

282280
executeCodeAction importControlMonad

0 commit comments

Comments
 (0)