Skip to content

Commit e7495fb

Browse files
committed
Don't show inline hint for arguments with same names as the parameters in DU
1 parent 7fd0aa5 commit e7495fb

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

vsintegration/src/FSharp.Editor/Hints/HintService.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module HintService =
3030
|> Seq.collect (InlineParameterNameHints(parseResults).GetHintsForMemberOrFunctionOrValue sourceText symbol)
3131
| HintKind.ParameterNameHint, (:? FSharpUnionCase as symbol) ->
3232
symbolUses
33-
|> Seq.collect (InlineParameterNameHints(parseResults).GetHintsForUnionCase symbol)
33+
|> Seq.collect (InlineParameterNameHints(parseResults).GetHintsForUnionCase sourceText symbol)
3434
| _ -> []
3535

3636
hintKinds |> Set.toList |> List.map getHintsPerKind

vsintegration/src/FSharp.Editor/Hints/InlineParameterNameHints.fs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,25 @@ type InlineParameterNameHints(parseResults: FSharpParseFileResults) =
147147
else
148148
[]
149149

150-
member _.GetHintsForUnionCase (symbol: FSharpUnionCase) (symbolUse: FSharpSymbolUse) =
150+
member _.GetHintsForUnionCase (sourceText: SourceText) (symbol: FSharpUnionCase) (symbolUse: FSharpSymbolUse) =
151151
if isUnionCaseValidForHint symbol symbolUse then
152152

153153
let fields = Seq.toList symbol.Fields
154154

155155
let ranges =
156156
parseResults.GetAllArgumentsForFunctionApplicationAtPosition symbolUse.Range.Start
157157

158+
let argumentNames =
159+
match ranges with
160+
| Some ranges -> (List.map (getSourceTextAtRange sourceText)) ranges
161+
| None -> []
158162
// When not all field values are provided (as the user is typing), don't show anything yet
159163
match ranges with
160164
| Some ranges when ranges.Length = fields.Length ->
161165
fields
162-
|> List.zip ranges
163-
|> List.where (snd >> fieldNameExists)
164-
|> List.map getFieldHint
166+
|> List.zip3 argumentNames ranges
167+
|> List.where (fun (argumentName, _, parameter) -> fieldNameExists parameter && argumentName <> parameter.DisplayName)
168+
|> List.map (fun (_, range, parameter) -> getFieldHint (range, parameter))
165169

166170
| _ -> []
167171
else

vsintegration/tests/FSharp.Editor.Tests/Hints/InlineParameterNameHintTests.fs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,39 @@ let b = Rectangle (1, 2)
347347

348348
Assert.Equal(expected, actual)
349349

350+
[<Fact>]
351+
let ``Hints are not shown for discriminated union case fields with the same names as arguements`` () =
352+
let code =
353+
"""
354+
type Shape =
355+
| Square of side: int
356+
| Rectangle of width: int * height: int
357+
358+
let width = 5
359+
let a = Square 1
360+
let b = Rectangle (width, 2)
361+
"""
362+
363+
let document = getFsDocument code
364+
365+
let expected =
366+
[
367+
{
368+
Content = "side = "
369+
Location = (6, 16)
370+
Tooltip = "field side"
371+
}
372+
{
373+
Content = "height = "
374+
Location = (7, 27)
375+
Tooltip = "field height"
376+
}
377+
]
378+
379+
let actual = getParameterNameHints document
380+
381+
Assert.Equal(expected, actual)
382+
350383
[<Fact>]
351384
let ``Hints for discriminated union case fields are not shown when names are generated`` () =
352385
let code =

0 commit comments

Comments
 (0)