Skip to content

Commit 91aceb8

Browse files
committed
feat: Add import suggestion for missing in scope constructor
For example, `deriving instance Generic (Sum Int)`, but it should work for other deriving which indirectly requires a complete access to the type constructor. ``` Can't make a derived instance of ‘Generic (Sum Int)’: The data constructors of ‘Sum’ are not all in scope so you cannot derive an instance for it ```
1 parent 91ccaa6 commit 91aceb8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,8 @@ extractNotInScopeName x
18401840
= Just $ NotInScopeDataConstructor name
18411841
| Just [name] <- matchRegexUnifySpaces x "ot in scope: type constructor or class [^‘]*‘([^’]*)’"
18421842
= Just $ NotInScopeTypeConstructorOrClass name
1843+
| Just [name] <- matchRegexUnifySpaces x "The data constructors of ‘([^ ]+)’ are not all in scope"
1844+
= Just $ NotInScopeDataConstructor name
18431845
| Just [name] <- matchRegexUnifySpaces x "of newtype ‘([^’]*)’ is not in scope"
18441846
= Just $ NotInScopeThing name
18451847
| Just [name] <- matchRegexUnifySpaces x "ot in scope: \\(([^‘ ]+)\\)"

plugins/hls-refactor-plugin/test/Main.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ codeActionTests = testGroup "code actions"
301301
, suggestImportTests
302302
, suggestAddRecordFieldImportTests
303303
, suggestAddCoerceMissingConstructorImportTests
304+
, suggestAddGenericMissingConstructorImportTests
304305
, suggestHideShadowTests
305306
, fixConstructorImportTests
306307
, fixModuleImportTypoTests
@@ -1896,6 +1897,31 @@ suggestAddCoerceMissingConstructorImportTests = testGroup "suggest imports of ne
18961897
contentAfterAction <- documentContents doc
18971898
liftIO $ after @=? contentAfterAction
18981899

1900+
suggestAddGenericMissingConstructorImportTests :: TestTree
1901+
suggestAddGenericMissingConstructorImportTests = testGroup "suggest imports of type constructors when using generic deriving"
1902+
[ testGroup "The type constructors are suggested when not in scope"
1903+
[ theTest
1904+
]
1905+
]
1906+
where
1907+
theTest = testSessionWithExtraFiles "hover" def $ \dir -> do
1908+
configureCheckProject False
1909+
let
1910+
before = T.unlines ["module A where", "import GHC.Generics", "import Data.Semigroup (Sum)", "deriving instance Generic (Sum Int)"]
1911+
after = T.unlines ["module A where", "import GHC.Generics", "import Data.Semigroup (Sum)", "import Data.Semigroup (Sum(..))", "deriving instance Generic (Sum Int)"]
1912+
cradle = "cradle: {direct: {arguments: [-hide-all-packages, -package, base, -package, text, -package-env, -, A]}}"
1913+
liftIO $ writeFileUTF8 (dir </> "hie.yaml") cradle
1914+
doc <- createDoc "Test.hs" "haskell" before
1915+
waitForProgressDone
1916+
_ <- waitForDiagnostics
1917+
let defLine = 3
1918+
range = Range (Position defLine 0) (Position defLine maxBound)
1919+
actions <- getCodeActions doc range
1920+
action <- pickActionWithTitle "import Data.Semigroup (Sum(..))" actions
1921+
executeCodeAction action
1922+
contentAfterAction <- documentContents doc
1923+
liftIO $ after @=? contentAfterAction
1924+
18991925

19001926
suggestImportDisambiguationTests :: TestTree
19011927
suggestImportDisambiguationTests = testGroup "suggest import disambiguation actions"

0 commit comments

Comments
 (0)