|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace Microsoft.VisualStudio.FSharp.Editor.Hints |
| 4 | + |
| 5 | +open Microsoft.CodeAnalysis |
| 6 | +open Microsoft.VisualStudio.FSharp.Editor |
| 7 | +open FSharp.Compiler.CodeAnalysis |
| 8 | +open FSharp.Compiler.Symbols |
| 9 | +open FSharp.Compiler.Text |
| 10 | + |
| 11 | +module HintService = |
| 12 | + |
| 13 | + // Relatively convenient for testing |
| 14 | + type NativeHint = { |
| 15 | + Range: range |
| 16 | + Parts: TaggedText list |
| 17 | + } |
| 18 | + |
| 19 | + let private isValidForHint |
| 20 | + (parseFileResults: FSharpParseFileResults) |
| 21 | + (symbol: FSharpMemberOrFunctionOrValue) |
| 22 | + (symbolUse: FSharpSymbolUse) = |
| 23 | + |
| 24 | + let isNotAnnotatedManually = |
| 25 | + not (parseFileResults.IsTypeAnnotationGivenAtPosition symbolUse.Range.Start) |
| 26 | + |
| 27 | + let isNotAfterDot = |
| 28 | + symbolUse.IsFromDefinition |
| 29 | + && not symbol.IsMemberThisValue |
| 30 | + |
| 31 | + let isNotTypeAlias = |
| 32 | + not symbol.IsConstructorThisValue |
| 33 | + |
| 34 | + symbol.IsValue // we'll be adding other stuff gradually here |
| 35 | + && isNotAnnotatedManually |
| 36 | + && isNotAfterDot |
| 37 | + && isNotTypeAlias |
| 38 | + |
| 39 | + let private getHintParts |
| 40 | + (symbol: FSharpMemberOrFunctionOrValue) |
| 41 | + (symbolUse: FSharpSymbolUse) = |
| 42 | + |
| 43 | + match symbol.GetReturnTypeLayout symbolUse.DisplayContext with |
| 44 | + | Some typeInfo -> |
| 45 | + let colon = TaggedText(TextTag.Text, ": ") |
| 46 | + colon :: (typeInfo |> Array.toList) |
| 47 | + |
| 48 | + // not sure when this can happen but better safe than sorry |
| 49 | + | None -> |
| 50 | + [] |
| 51 | + |
| 52 | + let private getHintsForSymbol parseResults (symbolUse: FSharpSymbolUse) = |
| 53 | + match symbolUse.Symbol with |
| 54 | + | :? FSharpMemberOrFunctionOrValue as mfvSymbol |
| 55 | + when isValidForHint parseResults mfvSymbol symbolUse -> |
| 56 | + |
| 57 | + [ { |
| 58 | + Range = symbolUse.Range |
| 59 | + Parts = getHintParts mfvSymbol symbolUse |
| 60 | + } ] |
| 61 | + |
| 62 | + // we'll be adding other stuff gradually here |
| 63 | + | _ -> |
| 64 | + [] |
| 65 | + |
| 66 | + let getHintsForDocument (document: Document) userOpName cancellationToken = |
| 67 | + async { |
| 68 | + if isSignatureFile document.FilePath |
| 69 | + then |
| 70 | + return [] |
| 71 | + else |
| 72 | + let! parseResults, checkResults = |
| 73 | + document.GetFSharpParseAndCheckResultsAsync userOpName |
| 74 | + |
| 75 | + return |
| 76 | + checkResults.GetAllUsesOfAllSymbolsInFile cancellationToken |
| 77 | + |> Seq.toList |
| 78 | + |> List.collect (getHintsForSymbol parseResults) |
| 79 | + } |
0 commit comments