forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inline type hints for F# (dotnet#14159)
- Loading branch information
Showing
25 changed files
with
534 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.VisualStudio.FSharp.Editor.Hints | ||
|
||
open Microsoft.CodeAnalysis | ||
open Microsoft.VisualStudio.FSharp.Editor | ||
open FSharp.Compiler.CodeAnalysis | ||
open FSharp.Compiler.Symbols | ||
open FSharp.Compiler.Text | ||
|
||
module HintService = | ||
|
||
// Relatively convenient for testing | ||
type NativeHint = { | ||
Range: range | ||
Parts: TaggedText list | ||
} | ||
|
||
let private isValidForHint | ||
(parseFileResults: FSharpParseFileResults) | ||
(symbol: FSharpMemberOrFunctionOrValue) | ||
(symbolUse: FSharpSymbolUse) = | ||
|
||
let isNotAnnotatedManually = | ||
not (parseFileResults.IsTypeAnnotationGivenAtPosition symbolUse.Range.Start) | ||
|
||
let isNotAfterDot = | ||
symbolUse.IsFromDefinition | ||
&& not symbol.IsMemberThisValue | ||
|
||
let isNotTypeAlias = | ||
not symbol.IsConstructorThisValue | ||
|
||
symbol.IsValue // we'll be adding other stuff gradually here | ||
&& isNotAnnotatedManually | ||
&& isNotAfterDot | ||
&& isNotTypeAlias | ||
|
||
let private getHintParts | ||
(symbol: FSharpMemberOrFunctionOrValue) | ||
(symbolUse: FSharpSymbolUse) = | ||
|
||
match symbol.GetReturnTypeLayout symbolUse.DisplayContext with | ||
| Some typeInfo -> | ||
let colon = TaggedText(TextTag.Text, ": ") | ||
colon :: (typeInfo |> Array.toList) | ||
|
||
// not sure when this can happen but better safe than sorry | ||
| None -> | ||
[] | ||
|
||
let private getHintsForSymbol parseResults (symbolUse: FSharpSymbolUse) = | ||
match symbolUse.Symbol with | ||
| :? FSharpMemberOrFunctionOrValue as mfvSymbol | ||
when isValidForHint parseResults mfvSymbol symbolUse -> | ||
|
||
[ { | ||
Range = symbolUse.Range | ||
Parts = getHintParts mfvSymbol symbolUse | ||
} ] | ||
|
||
// we'll be adding other stuff gradually here | ||
| _ -> | ||
[] | ||
|
||
let getHintsForDocument (document: Document) userOpName cancellationToken = | ||
async { | ||
if isSignatureFile document.FilePath | ||
then | ||
return [] | ||
else | ||
let! parseResults, checkResults = | ||
document.GetFSharpParseAndCheckResultsAsync userOpName | ||
|
||
return | ||
checkResults.GetAllUsesOfAllSymbolsInFile cancellationToken | ||
|> Seq.toList | ||
|> List.collect (getHintsForSymbol parseResults) | ||
} |
27 changes: 27 additions & 0 deletions
27
vsintegration/src/FSharp.Editor/Hints/NativeToRoslynHintConverter.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.VisualStudio.FSharp.Editor.Hints | ||
|
||
open System.Collections.Immutable | ||
open Microsoft.CodeAnalysis.Text | ||
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.InlineHints | ||
open Microsoft.VisualStudio.FSharp.Editor | ||
open FSharp.Compiler.Text | ||
open HintService | ||
|
||
module NativeToRoslynHintConverter = | ||
|
||
let rangeToSpan range sourceText = | ||
let symbolSpan = RoslynHelpers.FSharpRangeToTextSpan(sourceText, range) | ||
let overshadowLength = 0 // anything >0 means overlaying the code | ||
TextSpan(symbolSpan.End, overshadowLength) | ||
|
||
let nativeToRoslynText (taggedText: TaggedText) = | ||
let tag = RoslynHelpers.roslynTag taggedText.Tag | ||
let text = taggedText.Text | ||
RoslynTaggedText(tag, text) | ||
|
||
let convert sourceText hint = | ||
let span = rangeToSpan hint.Range sourceText | ||
let displayParts = hint.Parts |> Seq.map nativeToRoslynText | ||
FSharpInlineHint(span, displayParts.ToImmutableArray()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.VisualStudio.FSharp.Editor.Hints | ||
|
||
open System.Collections.Immutable | ||
open System.ComponentModel.Composition | ||
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.InlineHints | ||
open Microsoft.VisualStudio.FSharp.Editor | ||
|
||
// So the Roslyn interface is called IFSharpInlineHintsService | ||
// but our implementation is called just HintsService. | ||
// That's because we'll likely use this API for things other than inline hints, | ||
// e.g. signature hints above the line, pipeline hints on the side and so on. | ||
|
||
[<Export(typeof<IFSharpInlineHintsService>)>] | ||
type internal RoslynAdapter | ||
[<ImportingConstructor>] | ||
(settings: EditorOptions) = | ||
|
||
static let userOpName = "Hints" | ||
|
||
interface IFSharpInlineHintsService with | ||
member _.GetInlineHintsAsync(document, _, cancellationToken) = | ||
async { | ||
if not settings.Advanced.IsInlineHintsEnabled | ||
then return ImmutableArray.Empty | ||
|
||
else | ||
let! sourceText = document.GetTextAsync cancellationToken |> Async.AwaitTask | ||
let! nativeHints = | ||
HintService.getHintsForDocument document userOpName cancellationToken | ||
|
||
let roslynHints = | ||
nativeHints | ||
|> Seq.map (NativeToRoslynHintConverter.convert sourceText) | ||
|
||
return roslynHints.ToImmutableArray() | ||
} |> RoslynHelpers.StartAsyncAsTask cancellationToken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.