Skip to content

Commit 8277c93

Browse files
committed
Fix reference fields gives too many results
References to record fields gives too many results This commit adds tests for references to record fields and updates the symbol retrieval logic to ensure that references to record fields are handled correctly. The changes is small: - The `getNamesAtPoint` function in `AtPoint.hs` now only searches for `Name` that are in the source node from `HieAst`.
1 parent 8d96270 commit 8277c93

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

ghcide-test/data/references/Fields.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{-# LANGUAGE RecordWildCards #-}
2+
module Fields where
3+
4+
data Foo = MkFoo
5+
{
6+
barr :: String,
7+
bazz :: String
8+
}
9+
10+
fooUse0 :: Foo -> String
11+
fooUse0 MkFoo{barr} = "5"
12+
13+
fooUse1 :: Foo -> String
14+
fooUse1 MkFoo{..} = "6"
15+
16+
fooUse2 :: String -> String -> Foo
17+
fooUse2 bar baz =
18+
MkFoo{..}

ghcide-test/data/references/Main.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Main where
22

33
import References
4-
4+
import Fields
55
main :: IO ()
66
main = return ()
77

@@ -12,3 +12,6 @@ b = a + 1
1212

1313
acc :: Account
1414
acc = Savings
15+
16+
fooUse3 :: String -> String -> Foo
17+
fooUse3 bar baz = MkFoo{barr = bar, bazz = baz}

ghcide-test/data/references/hie.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cradle: {direct: {arguments: ["Main","OtherModule","OtherOtherModule","References"]}}
1+
cradle: {direct: {arguments: ["Main","OtherModule","OtherOtherModule","References", "Fields"]}}

ghcide-test/exe/ReferenceTests.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ tests = testGroup "references"
156156
, ("References.hs", 16, 0)
157157
]
158158
]
159+
-- Fields.hs does not depend on Main.hs
160+
-- so we can only find references in Fields.hs
161+
, testGroup "references to record fields"
162+
[ referenceTest "references record fields in the same file"
163+
("Fields.hs", 5, 4)
164+
YesIncludeDeclaration
165+
[ ("Fields.hs", 5, 4)
166+
, ("Fields.hs", 10, 14)
167+
, ("Fields.hs", 13, 14)
168+
]
169+
170+
-- Main.hs depends on Fields.hs, so we can find references
171+
-- from Main.hs to Fields.hs
172+
, referenceTest "references record fields cross modules"
173+
("Main.hs", 16, 24)
174+
YesIncludeDeclaration
175+
[ ("Fields.hs", 5, 4)
176+
, ("Fields.hs", 10, 14)
177+
, ("Fields.hs", 13, 14)
178+
, ("Main.hs", 16, 24)
179+
]
180+
]
159181
]
160182

161183
-- | When we ask for all references to symbol "foo", should the declaration "foo

ghcide/src/Development/IDE/Spans/AtPoint.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ foiReferencesAtPoint file pos (FOIReferences asts) =
113113

114114
getNamesAtPoint :: HieASTs a -> Position -> PositionMapping -> [Name]
115115
getNamesAtPoint hf pos mapping =
116-
concat $ pointCommand hf posFile (rights . M.keys . getNodeIds)
116+
concat $ pointCommand hf posFile (rights . M.keys . getSourceNodeIds)
117117
where
118118
posFile = fromMaybe pos $ fromCurrentPosition mapping pos
119119

0 commit comments

Comments
 (0)