From e7495fb786ebb9a085824e51ce639646dc79bcfe Mon Sep 17 00:00:00 2001 From: Sudqi Jawabreh Date: Sat, 3 Jun 2023 17:38:42 +0300 Subject: [PATCH] Don't show inline hint for arguments with same names as the parameters in DU --- .../src/FSharp.Editor/Hints/HintService.fs | 2 +- .../Hints/InlineParameterNameHints.fs | 12 ++++--- .../Hints/InlineParameterNameHintTests.fs | 33 +++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/vsintegration/src/FSharp.Editor/Hints/HintService.fs b/vsintegration/src/FSharp.Editor/Hints/HintService.fs index c257ffa010..756ee0e498 100644 --- a/vsintegration/src/FSharp.Editor/Hints/HintService.fs +++ b/vsintegration/src/FSharp.Editor/Hints/HintService.fs @@ -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 diff --git a/vsintegration/src/FSharp.Editor/Hints/InlineParameterNameHints.fs b/vsintegration/src/FSharp.Editor/Hints/InlineParameterNameHints.fs index f428a144cf..867377cc33 100644 --- a/vsintegration/src/FSharp.Editor/Hints/InlineParameterNameHints.fs +++ b/vsintegration/src/FSharp.Editor/Hints/InlineParameterNameHints.fs @@ -147,7 +147,7 @@ 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 @@ -155,13 +155,17 @@ type InlineParameterNameHints(parseResults: FSharpParseFileResults) = 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 diff --git a/vsintegration/tests/FSharp.Editor.Tests/Hints/InlineParameterNameHintTests.fs b/vsintegration/tests/FSharp.Editor.Tests/Hints/InlineParameterNameHintTests.fs index 1607662a96..c1cf532607 100644 --- a/vsintegration/tests/FSharp.Editor.Tests/Hints/InlineParameterNameHintTests.fs +++ b/vsintegration/tests/FSharp.Editor.Tests/Hints/InlineParameterNameHintTests.fs @@ -347,6 +347,39 @@ let b = Rectangle (1, 2) Assert.Equal(expected, actual) + [] + 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) + [] let ``Hints for discriminated union case fields are not shown when names are generated`` () = let code =