Skip to content

Commit

Permalink
Making inline type hints less intrusive (dotnet#14386)
Browse files Browse the repository at this point in the history
* Make inline type hints less intrusive

* Update

* Reverting the breaking change

* IWSAM

* update

* extra tests
  • Loading branch information
psfinaki authored Nov 29, 2022
1 parent 23e22ea commit 71b6329
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
12 changes: 11 additions & 1 deletion vsintegration/src/FSharp.Editor/Hints/InlineTypeHints.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ module InlineTypeHints =
Parts = getHintParts symbol symbolUse
}

let private isSolved (symbol: FSharpMemberOrFunctionOrValue) =
if symbol.GenericParameters.Count > 0
then symbol.GenericParameters |> Seq.forall (fun p -> p.IsSolveAtCompileTime)

elif symbol.FullType.IsGenericParameter
then symbol.FullType.GenericParameter.DisplayNameCore <> "?"

else true

let isValidForHint
(parseFileResults: FSharpParseFileResults)
(symbol: FSharpMemberOrFunctionOrValue)
(symbolUse: FSharpSymbolUse) =

let isNotAnnotatedManually =
not (parseFileResults.IsTypeAnnotationGivenAtPosition symbolUse.Range.Start)

Expand All @@ -46,6 +55,7 @@ module InlineTypeHints =
not symbol.IsConstructorThisValue

symbol.IsValue // we'll be adding other stuff gradually here
&& isSolved symbol
&& isNotAnnotatedManually
&& isNotAfterDot
&& isNotTypeAlias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,67 @@ let zip4 (l1: 'a list) (l2: 'b list) (l3: 'c list) (l4: 'd list) =
let actual = getTypeHints document

CollectionAssert.AreEquivalent(expected, actual)

[<Test>]
let ``Hints are not shown for unfinished expressions`` () =
let code =
"""
let x
"""
let document = getFsDocument code

let result = getTypeHints document

Assert.IsEmpty(result)

[<Test>]
let ``Hints are not shown for unsolved types in _for_ expressions in collections`` () =
let code =
"""
let _ = [ for x ]
"""
let document = getFsDocument code

let result = getTypeHints document

Assert.IsEmpty(result)

[<Test>]
let ``Hints are not shown for unsolved types in _for_ expressions within computational expressions`` () =
let code =
"""
do task {
for x
do! Task.Delay 0
}
"""
let document = getFsDocument code

let result = getTypeHints document

Assert.IsEmpty(result)

[<Test>]
let ``Hints are shown for IWSAM`` () =
let code =
"""
type IAddition<'T when 'T :> IAddition<'T>> =
static abstract op_Addition: 'T * 'T -> 'T
type Number<'T when IAddition<'T>>(value: 'T) =
member _.Value with get() = value
interface IAddition<Number<'T>> with
static member op_Addition(a, b) = Number(a.Value + b.Value)
"""
let document = getFsDocument code

let expected =
[
{ Content = ": Number<'T>"; Location = (7, 36) }
{ Content = ": Number<'T>"; Location = (7, 39) }
]

let actual = getTypeHints document

CollectionAssert.AreEquivalent(expected, actual)

0 comments on commit 71b6329

Please sign in to comment.