Skip to content

Commit

Permalink
Don't show inline hint for arguments with same names as the parameter…
Browse files Browse the repository at this point in the history
…s in DU (#15305)
  • Loading branch information
sudqijawabreh committed Jun 5, 2023
1 parent 7fd0aa5 commit aab21e5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion vsintegration/src/FSharp.Editor/Hints/HintService.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module HintService =
|> Seq.collect (InlineParameterNameHints(parseResults).GetHintsForMemberOrFunctionOrValue sourceText symbol)
| HintKind.ParameterNameHint, (:? FSharpUnionCase as symbol) ->
symbolUses
|> Seq.collect (InlineParameterNameHints(parseResults).GetHintsForUnionCase symbol)
|> Seq.collect (InlineParameterNameHints(parseResults).GetHintsForUnionCase sourceText symbol)
| _ -> []

hintKinds |> Set.toList |> List.map getHintsPerKind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,25 @@ type InlineParameterNameHints(parseResults: FSharpParseFileResults) =
else
[]

member _.GetHintsForUnionCase (symbol: FSharpUnionCase) (symbolUse: FSharpSymbolUse) =
member _.GetHintsForUnionCase (sourceText: SourceText) (symbol: FSharpUnionCase) (symbolUse: FSharpSymbolUse) =
if isUnionCaseValidForHint symbol symbolUse then

let fields = Seq.toList symbol.Fields

let ranges =
parseResults.GetAllArgumentsForFunctionApplicationAtPosition symbolUse.Range.Start

let argumentNames =
match ranges with
| Some ranges -> (List.map (getSourceTextAtRange sourceText)) ranges
| None -> []
// When not all field values are provided (as the user is typing), don't show anything yet
match ranges with
| Some ranges when ranges.Length = fields.Length ->
fields
|> List.zip ranges
|> List.where (snd >> fieldNameExists)
|> List.map getFieldHint
|> List.zip3 argumentNames ranges
|> List.where (fun (argumentName, _, parameter) -> fieldNameExists parameter && argumentName <> parameter.DisplayName)
|> List.map (fun (_, range, parameter) -> getFieldHint (range, parameter))

| _ -> []
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,39 @@ let b = Rectangle (1, 2)

Assert.Equal(expected, actual)

[<Fact>]
let ``Hints are not shown for discriminated union case fields with the same names as arguements`` () =
let code =
"""
type Shape =
| Square of side: int
| Rectangle of width: int * height: int
let width = 5
let a = Square 1
let b = Rectangle (width, 2)
"""

let document = getFsDocument code

let expected =
[
{
Content = "side = "
Location = (6, 16)
Tooltip = "field side"
}
{
Content = "height = "
Location = (7, 27)
Tooltip = "field height"
}
]

let actual = getParameterNameHints document

Assert.Equal(expected, actual)

[<Fact>]
let ``Hints for discriminated union case fields are not shown when names are generated`` () =
let code =
Expand Down

0 comments on commit aab21e5

Please sign in to comment.