-
Notifications
You must be signed in to change notification settings - Fork 797
/
Copy pathFSharpSource.fs
94 lines (62 loc) · 2.68 KB
/
FSharpSource.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Compiler.CodeAnalysis
open System
open System.IO
open Internal.Utilities.Library
open FSharp.Compiler.IO
open FSharp.Compiler.Text
[<RequireQualifiedAccess>]
type TextContainer =
| OnDisk
| Stream of Stream
| SourceText of ISourceText
interface IDisposable with
member this.Dispose() =
match this with
| Stream stream -> stream.Dispose()
| _ -> ()
[<AbstractClass>]
type FSharpSource internal () =
abstract FilePath: string
abstract TimeStamp: DateTime
abstract GetTextContainer: unit -> Async<TextContainer>
type private FSharpSourceMemoryMappedFile(filePath: string, timeStamp: DateTime, openStream: unit -> Stream) =
inherit FSharpSource()
override _.FilePath = filePath
override _.TimeStamp = timeStamp
override _.GetTextContainer() =
openStream () |> TextContainer.Stream |> async.Return
type private FSharpSourceByteArray(filePath: string, timeStamp: DateTime, bytes: byte[]) =
inherit FSharpSource()
override _.FilePath = filePath
override _.TimeStamp = timeStamp
override _.GetTextContainer() =
TextContainer.Stream(new MemoryStream(bytes, 0, bytes.Length, false) :> Stream)
|> async.Return
type private FSharpSourceFromFile(filePath: string) =
inherit FSharpSource()
override _.FilePath = filePath
override _.TimeStamp = FileSystem.GetLastWriteTimeShim(filePath)
override _.GetTextContainer() = TextContainer.OnDisk |> async.Return
type private FSharpSourceCustom(filePath: string, getTimeStamp, getSourceText) =
inherit FSharpSource()
override _.FilePath = filePath
override _.TimeStamp = getTimeStamp ()
override _.GetTextContainer() =
async {
let! sourceOpt = getSourceText ()
return
sourceOpt
|> Option.map TextContainer.SourceText
|> Option.defaultValue TextContainer.OnDisk
}
type FSharpSource with
static member Create(filePath, getTimeStamp, getSourceText) =
FSharpSourceCustom(filePath, getTimeStamp, getSourceText) :> FSharpSource
static member CreateFromFile(filePath: string) =
FSharpSourceFromFile(filePath) :> FSharpSource
static member CreateCopyFromFile(filePath: string) =
let timeStamp = FileSystem.GetLastWriteTimeShim(filePath)
let openStream =
fun () -> FileSystem.OpenFileForReadShim(filePath, useMemoryMappedFile = true, shouldShadowCopy = true)
FSharpSourceMemoryMappedFile(filePath, timeStamp, openStream) :> FSharpSource