Skip to content

Commit 0218e46

Browse files
committed
feat: add test and import suggestion for not in scope record field
In ghc 9.6 we had: ``` Not in scope ‘modelStatusTag’ ``` In 9.10 we have: ``` Not in scope: record field ‘modelStatusTag’ ``` Introducing the new regex in order to match it AND a test to ensure no future regression. The regression is due to the fact that there is a matcher which catch `Nat in scope: .*`, hence it was matching "record" instead of "modelStatusTag".
1 parent 3aae249 commit 0218e46

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,10 @@ extractNotInScopeName x
18701870
= Just $ NotInScopeThing name
18711871
| Just [_module, name] <- matchRegexUnifySpaces x "No instance for [‘(].*HasField \"[^\"]+\" \\(([^ .]+\\.)*([^ .]+)[^)]*\\).*[’)]"
18721872
= Just $ NotInScopeThing name
1873+
-- The order of the "Not in scope" is important, for example, some of the
1874+
-- matcher may catch the "record" value instead of the value later.
1875+
| Just [name] <- matchRegexUnifySpaces x "Not in scope: record field ‘([^’]*)’"
1876+
= Just $ NotInScopeThing name
18731877
| Just [name] <- matchRegexUnifySpaces x "ot in scope: \\(([^‘ ]+)\\)"
18741878
= Just $ NotInScopeThing name
18751879
| Just [name] <- matchRegexUnifySpaces x "ot in scope: ([^‘ ]+)"

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

+32
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ codeActionTests = testGroup "code actions"
320320
, addImplicitParamsConstraintTests
321321
, removeExportTests
322322
, Test.AddArgument.tests
323+
, suggestAddRecordFieldUpdateImportTests
323324
]
324325

325326
insertImportTests :: TestTree
@@ -1909,9 +1910,40 @@ suggestAddRecordFieldImportTests = testGroup "suggest imports of record fields w
19091910
contentAfterAction <- documentContents doc
19101911
liftIO $ after @=? contentAfterAction
19111912

1913+
suggestAddRecordFieldUpdateImportTests :: TestTree
1914+
suggestAddRecordFieldUpdateImportTests = testGroup "suggest imports of record fields in update"
1915+
[ testGroup "implicit import of type" [theTest ] ]
1916+
where
1917+
theTest = testSessionWithExtraFiles "hover" def $ \dir -> do
1918+
configureCheckProject True
1919+
1920+
let
1921+
before = T.unlines ["module C where", "import B", "biz = bar { foo = 100 }"]
1922+
after = T.unlines ["module C where", "import B", "import A (Foo(..))", "biz = bar { foo = 100 }"]
1923+
cradle = "cradle: {direct: {arguments: [-hide-all-packages, -package, base, -package, text, -package-env, -, A, B, C]}}"
1924+
liftIO $ writeFileUTF8 (dir </> "hie.yaml") cradle
1925+
liftIO $ writeFileUTF8 (dir </> "A.hs") $ unlines ["module A where", "data Foo = Foo { foo :: Int }"]
1926+
liftIO $ writeFileUTF8 (dir </> "B.hs") $ unlines ["module B where", "import A", "bar = Foo 10" ]
1927+
doc <- createDoc "Test.hs" "haskell" before
1928+
waitForProgressDone
1929+
diags <- waitForDiagnostics
1930+
liftIO $ print diags
1931+
let defLine = 2
1932+
range = Range (Position defLine 0) (Position defLine maxBound)
1933+
actions <- getCodeActions doc range
1934+
liftIO $ print actions
1935+
action <- pickActionWithTitle "import A (Foo(..))" actions
1936+
executeCodeAction action
1937+
contentAfterAction <- documentContents doc
1938+
liftIO $ after @=? contentAfterAction
1939+
19121940
extractNotInScopeNameTests :: TestTree
19131941
extractNotInScopeNameTests =
19141942
testGroup "extractNotInScopeName" [
1943+
testGroup "record field" [
1944+
testCase ">=ghc 910" $ Refactor.extractNotInScopeName "Not in scope: ‘foo’" @=? Just (NotInScopeThing "foo"),
1945+
testCase "<ghc 910" $ Refactor.extractNotInScopeName "Not in scope: record field ‘foo’" @=? Just (NotInScopeThing "foo")
1946+
],
19151947
testGroup "HasField" [
19161948
testGroup "unqualified" [
19171949
testGroup "nice ticks" [

0 commit comments

Comments
 (0)