Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make getting source text async #15148

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Compiler/Facilities/BuildGraph.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type NodeCode =

static member Parallel: computations: (NodeCode<'T> seq) -> NodeCode<'T[]>

static member AwaitAsync: computation: Async<'T> -> NodeCode<'T>

static member AwaitTask: task: Task<'T> -> NodeCode<'T>

static member AwaitTask: task: Task -> NodeCode<unit>
Expand Down
19 changes: 13 additions & 6 deletions src/Compiler/Service/FSharpSource.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type FSharpSource internal () =

abstract TimeStamp: DateTime

abstract GetTextContainer: unit -> TextContainer
abstract GetTextContainer: unit -> Async<TextContainer>

type private FSharpSourceMemoryMappedFile(filePath: string, timeStamp: DateTime, openStream: unit -> Stream) =
inherit FSharpSource()
Expand All @@ -37,7 +37,8 @@ type private FSharpSourceMemoryMappedFile(filePath: string, timeStamp: DateTime,

override _.TimeStamp = timeStamp

override _.GetTextContainer() = openStream () |> TextContainer.Stream
override _.GetTextContainer() =
openStream () |> TextContainer.Stream |> async.Return

type private FSharpSourceByteArray(filePath: string, timeStamp: DateTime, bytes: byte[]) =
inherit FSharpSource()
Expand All @@ -48,6 +49,7 @@ type private FSharpSourceByteArray(filePath: string, timeStamp: DateTime, bytes:

override _.GetTextContainer() =
TextContainer.Stream(new MemoryStream(bytes, 0, bytes.Length, false) :> Stream)
|> async.Return

type private FSharpSourceFromFile(filePath: string) =
inherit FSharpSource()
Expand All @@ -56,7 +58,7 @@ type private FSharpSourceFromFile(filePath: string) =

override _.TimeStamp = FileSystem.GetLastWriteTimeShim(filePath)

override _.GetTextContainer() = TextContainer.OnDisk
override _.GetTextContainer() = TextContainer.OnDisk |> async.Return

type private FSharpSourceCustom(filePath: string, getTimeStamp, getSourceText) =
inherit FSharpSource()
Expand All @@ -66,9 +68,14 @@ type private FSharpSourceCustom(filePath: string, getTimeStamp, getSourceText) =
override _.TimeStamp = getTimeStamp ()

override _.GetTextContainer() =
getSourceText ()
|> Option.map TextContainer.SourceText
|> Option.defaultValue TextContainer.OnDisk
async {
let! sourceOpt = getSourceText ()

return
sourceOpt
|> Option.map TextContainer.SourceText
|> Option.defaultValue TextContainer.OnDisk
}

type FSharpSource with

Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Service/FSharpSource.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type internal FSharpSource =
abstract TimeStamp: DateTime

/// Gets the internal text container. Text may be on-disk, in a stream, or a source text.
abstract internal GetTextContainer: unit -> TextContainer
abstract internal GetTextContainer: unit -> Async<TextContainer>

/// Creates a FSharpSource from disk. Only used internally.
static member internal CreateFromFile: filePath: string -> FSharpSource
Expand All @@ -36,5 +36,5 @@ type internal FSharpSource =

/// Creates a FSharpSource.
static member Create:
filePath: string * getTimeStamp: (unit -> DateTime) * getSourceText: (unit -> ISourceText option) ->
filePath: string * getTimeStamp: (unit -> DateTime) * getSourceText: (unit -> Async<ISourceText option>) ->
FSharpSource
65 changes: 34 additions & 31 deletions src/Compiler/Service/IncrementalBuild.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,38 +136,41 @@ module IncrementalBuildSyntaxTree =
), sourceRange, fileName, [||]

let parse (source: FSharpSource) =
IncrementalBuilderEventTesting.MRU.Add(IncrementalBuilderEventTesting.IBEParsed fileName)
use _ =
Activity.start "IncrementalBuildSyntaxTree.parse"
[|
Activity.Tags.fileName, fileName
Activity.Tags.buildPhase, BuildPhase.Parse.ToString()
|]

try
let diagnosticsLogger = CompilationDiagnosticLogger("Parse", tcConfig.diagnosticsOptions)
// Return the disposable object that cleans up
use _holder = new CompilationGlobalsScope(diagnosticsLogger, BuildPhase.Parse)
use text = source.GetTextContainer()
let input =
match text with
| TextContainer.Stream(stream) ->
ParseOneInputStream(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, false, stream)
| TextContainer.SourceText(sourceText) ->
ParseOneInputSourceText(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, sourceText)
| TextContainer.OnDisk ->
ParseOneInputFile(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, true)

fileParsed.Trigger fileName

input, sourceRange, fileName, diagnosticsLogger.GetDiagnostics()
with exn ->
let msg = sprintf "unexpected failure in SyntaxTree.parse\nerror = %s" (exn.ToString())
System.Diagnostics.Debug.Assert(false, msg)
failwith msg
node {
IncrementalBuilderEventTesting.MRU.Add(IncrementalBuilderEventTesting.IBEParsed fileName)
use _ =
Activity.start "IncrementalBuildSyntaxTree.parse"
[|
Activity.Tags.fileName, fileName
Activity.Tags.buildPhase, BuildPhase.Parse.ToString()
|]

try
let diagnosticsLogger = CompilationDiagnosticLogger("Parse", tcConfig.diagnosticsOptions)
// Return the disposable object that cleans up
use _holder = new CompilationGlobalsScope(diagnosticsLogger, BuildPhase.Parse)
use! text = source.GetTextContainer() |> NodeCode.AwaitAsync
let input =
match text :?> TextContainer with
| TextContainer.Stream(stream) ->
ParseOneInputStream(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, false, stream)
| TextContainer.SourceText(sourceText) ->
ParseOneInputSourceText(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, sourceText)
| TextContainer.OnDisk ->
ParseOneInputFile(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, true)

fileParsed.Trigger fileName

return input, sourceRange, fileName, diagnosticsLogger.GetDiagnostics()
with exn ->
let msg = sprintf "unexpected failure in SyntaxTree.parse\nerror = %s" (exn.ToString())
System.Diagnostics.Debug.Assert(false, msg)
failwith msg
return Unchecked.defaultof<_>
}

/// Parse the given file and return the given input.
member val ParseNode : GraphNode<ParseResult> = node { return parse source } |> GraphNode
member val ParseNode : GraphNode<ParseResult> = parse source |> GraphNode

member _.Invalidate() =
SyntaxTree(tcConfig, fileParsed, lexResourceManager, file, hasSignature)
Expand Down Expand Up @@ -1563,7 +1566,7 @@ type IncrementalBuilder(initialState: IncrementalBuilderInitialState, state: Inc

let sourceFiles =
sourceFiles
|> List.map (fun (m, fileName, isLastCompiland) ->
|> List.map (fun (m, fileName, isLastCompiland) ->
{ Range = m; Source = getFSharpSource fileName; Flags = isLastCompiland } )

let initialState =
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Service/IncrementalBuild.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ type internal IncrementalBuilder =
dependencyProvider: DependencyProvider option *
parallelReferenceResolution: ParallelReferenceResolution *
captureIdentifiersWhenParsing: bool *
getSource: (string -> ISourceText option) option *
getSource: (string -> Async<ISourceText option>) option *
useChangeNotifications: bool *
useSyntaxTreeCache: bool ->
NodeCode<IncrementalBuilder option * FSharpDiagnostic[]>
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Service/service.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module EnvMisc =
[<RequireQualifiedAccess>]
type DocumentSource =
| FileSystem
| Custom of (string -> ISourceText option)
| Custom of (string -> Async<ISourceText option>)

/// Callback that indicates whether a requested result has become obsolete.
[<NoComparison; NoEquality>]
Expand Down Expand Up @@ -194,7 +194,7 @@ type BackgroundCompiler
enablePartialTypeChecking,
parallelReferenceResolution,
captureIdentifiersWhenParsing,
getSource: (string -> ISourceText option) option,
getSource: (string -> Async<ISourceText option>) option,
useChangeNotifications,
useSyntaxTreeCache
) as self =
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Service/service.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open FSharp.Compiler.Tokenization
[<RequireQualifiedAccess>]
type DocumentSource =
| FileSystem
| Custom of (string -> ISourceText option)
| Custom of (string -> Async<ISourceText option>)

/// Used to parse and check F# source code.
[<Sealed; AutoSerializable(false)>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1945,16 +1945,16 @@ FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryRe
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+MetadataOnlyFlag
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+ReduceMemoryFlag
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+Shim
FSharp.Compiler.CodeAnalysis.DocumentSource+Custom: Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]] Item
FSharp.Compiler.CodeAnalysis.DocumentSource+Custom: Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]] get_Item()
FSharp.Compiler.CodeAnalysis.DocumentSource+Custom: Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Control.FSharpAsync`1[Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]]] Item
FSharp.Compiler.CodeAnalysis.DocumentSource+Custom: Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Control.FSharpAsync`1[Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]]] get_Item()
FSharp.Compiler.CodeAnalysis.DocumentSource+Tags: Int32 Custom
FSharp.Compiler.CodeAnalysis.DocumentSource+Tags: Int32 FileSystem
FSharp.Compiler.CodeAnalysis.DocumentSource: Boolean IsCustom
FSharp.Compiler.CodeAnalysis.DocumentSource: Boolean IsFileSystem
FSharp.Compiler.CodeAnalysis.DocumentSource: Boolean get_IsCustom()
FSharp.Compiler.CodeAnalysis.DocumentSource: Boolean get_IsFileSystem()
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource FileSystem
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource NewCustom(Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]])
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource NewCustom(Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Control.FSharpAsync`1[Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]]])
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource get_FileSystem()
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource+Custom
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource+Tags
Expand Down Expand Up @@ -3972,7 +3972,7 @@ FSharp.Compiler.EditorServices.ToolTipElement: Boolean get_IsNone()
FSharp.Compiler.EditorServices.ToolTipElement: FSharp.Compiler.EditorServices.ToolTipElement NewCompositionError(System.String)
FSharp.Compiler.EditorServices.ToolTipElement: FSharp.Compiler.EditorServices.ToolTipElement NewGroup(Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.EditorServices.ToolTipElementData])
FSharp.Compiler.EditorServices.ToolTipElement: FSharp.Compiler.EditorServices.ToolTipElement None
FSharp.Compiler.EditorServices.ToolTipElement: FSharp.Compiler.EditorServices.ToolTipElement Single(FSharp.Compiler.Text.TaggedText[], FSharp.Compiler.Symbols.FSharpXmlDoc, Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.TaggedText[]]], Microsoft.FSharp.Core.FSharpOption`1[System.String], Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.TaggedText[]])
FSharp.Compiler.EditorServices.ToolTipElement: FSharp.Compiler.EditorServices.ToolTipElement Single(FSharp.Compiler.Text.TaggedText[], FSharp.Compiler.Symbols.FSharpXmlDoc, Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.TaggedText[]]], Microsoft.FSharp.Core.FSharpOption`1[System.String], Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.TaggedText[]], Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpSymbol])
FSharp.Compiler.EditorServices.ToolTipElement: FSharp.Compiler.EditorServices.ToolTipElement get_None()
FSharp.Compiler.EditorServices.ToolTipElement: FSharp.Compiler.EditorServices.ToolTipElement+CompositionError
FSharp.Compiler.EditorServices.ToolTipElement: FSharp.Compiler.EditorServices.ToolTipElement+Group
Expand All @@ -3993,12 +3993,14 @@ FSharp.Compiler.EditorServices.ToolTipElementData: Int32 GetHashCode()
FSharp.Compiler.EditorServices.ToolTipElementData: Int32 GetHashCode(System.Collections.IEqualityComparer)
FSharp.Compiler.EditorServices.ToolTipElementData: Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.TaggedText[]] TypeMapping
FSharp.Compiler.EditorServices.ToolTipElementData: Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.TaggedText[]] get_TypeMapping()
FSharp.Compiler.EditorServices.ToolTipElementData: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpSymbol] Symbol
FSharp.Compiler.EditorServices.ToolTipElementData: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpSymbol] get_Symbol()
FSharp.Compiler.EditorServices.ToolTipElementData: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.TaggedText[]] Remarks
FSharp.Compiler.EditorServices.ToolTipElementData: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.TaggedText[]] get_Remarks()
FSharp.Compiler.EditorServices.ToolTipElementData: Microsoft.FSharp.Core.FSharpOption`1[System.String] ParamName
FSharp.Compiler.EditorServices.ToolTipElementData: Microsoft.FSharp.Core.FSharpOption`1[System.String] get_ParamName()
FSharp.Compiler.EditorServices.ToolTipElementData: System.String ToString()
FSharp.Compiler.EditorServices.ToolTipElementData: Void .ctor(FSharp.Compiler.Text.TaggedText[], FSharp.Compiler.Symbols.FSharpXmlDoc, Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.TaggedText[]], Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.TaggedText[]], Microsoft.FSharp.Core.FSharpOption`1[System.String])
FSharp.Compiler.EditorServices.ToolTipElementData: Void .ctor(Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpSymbol], FSharp.Compiler.Text.TaggedText[], FSharp.Compiler.Symbols.FSharpXmlDoc, Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.TaggedText[]], Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.TaggedText[]], Microsoft.FSharp.Core.FSharpOption`1[System.String])
FSharp.Compiler.EditorServices.ToolTipText: Boolean Equals(FSharp.Compiler.EditorServices.ToolTipText)
FSharp.Compiler.EditorServices.ToolTipText: Boolean Equals(System.Object)
FSharp.Compiler.EditorServices.ToolTipText: Boolean Equals(System.Object, System.Collections.IEqualityComparer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1945,16 +1945,16 @@ FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryRe
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+MetadataOnlyFlag
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+ReduceMemoryFlag
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+Shim
FSharp.Compiler.CodeAnalysis.DocumentSource+Custom: Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]] Item
FSharp.Compiler.CodeAnalysis.DocumentSource+Custom: Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]] get_Item()
FSharp.Compiler.CodeAnalysis.DocumentSource+Custom: Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Control.FSharpAsync`1[Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]]] Item
FSharp.Compiler.CodeAnalysis.DocumentSource+Custom: Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Control.FSharpAsync`1[Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]]] get_Item()
FSharp.Compiler.CodeAnalysis.DocumentSource+Tags: Int32 Custom
FSharp.Compiler.CodeAnalysis.DocumentSource+Tags: Int32 FileSystem
FSharp.Compiler.CodeAnalysis.DocumentSource: Boolean IsCustom
FSharp.Compiler.CodeAnalysis.DocumentSource: Boolean IsFileSystem
FSharp.Compiler.CodeAnalysis.DocumentSource: Boolean get_IsCustom()
FSharp.Compiler.CodeAnalysis.DocumentSource: Boolean get_IsFileSystem()
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource FileSystem
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource NewCustom(Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]])
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource NewCustom(Microsoft.FSharp.Core.FSharpFunc`2[System.String,Microsoft.FSharp.Control.FSharpAsync`1[Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText]]])
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource get_FileSystem()
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource+Custom
FSharp.Compiler.CodeAnalysis.DocumentSource: FSharp.Compiler.CodeAnalysis.DocumentSource+Tags
Expand Down
4 changes: 2 additions & 2 deletions tests/FSharp.Compiler.Service.Tests/TooltipTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource line colAtEn
"A.fs",
SourceText.ofString implSource |]

let documentSource fileName = Map.tryFind fileName files
let documentSource fileName = Map.tryFind fileName files |> async.Return

let projectOptions =
let _, projectOptions = mkTestFileAndOptions "" Array.empty
Expand Down Expand Up @@ -267,7 +267,7 @@ let testToolTipSquashing source line colAtEndOfNames lineText names tokenTag =
[| "A.fs",
SourceText.ofString source |]

let documentSource fileName = Map.tryFind fileName files
let documentSource fileName = Map.tryFind fileName files |> async.Return

let projectOptions =
let _, projectOptions = mkTestFileAndOptions "" Array.empty
Expand Down
1 change: 1 addition & 0 deletions tests/FSharp.Test.Utilities/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ module rec Compiler =
fun (name: string) ->
Map.tryFind name project
|> Option.bind (Option.map SourceText.ofString)
|> async.Return

let sourceFiles = Array.map fst sourceFiles
CompilerAssert.TypeCheckProject(options, sourceFiles, getSourceText)
Expand Down
Loading