-
Notifications
You must be signed in to change notification settings - Fork 155
/
FileSystem.fs
517 lines (397 loc) · 18.2 KB
/
FileSystem.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
namespace FsAutoComplete
open FSharp.Compiler.CodeAnalysis
open System
open FsAutoComplete.Logging
open FSharp.UMX
open FSharp.Compiler.Text
open System.Runtime.CompilerServices
open FsToolkit.ErrorHandling
open System.IO
open FSharp.Compiler.IO
[<AutoOpen>]
module PositionExtensions =
type FSharp.Compiler.Text.Position with
/// Excluding current line
member x.LinesToBeginning() =
if x.Line <= 1 then
Seq.empty
else
seq {
for i = x.Line - 1 downto 1 do
yield Position.mkPos i 0
}
member x.IncLine() = Position.mkPos (x.Line + 1) x.Column
member x.DecLine() = Position.mkPos (x.Line - 1) x.Column
member x.IncColumn() = Position.mkPos x.Line (x.Column + 1)
member x.IncColumn n = Position.mkPos x.Line (x.Column + n)
let inline (|Pos|) (p: FSharp.Compiler.Text.Position) = p.Line, p.Column
[<AutoOpen>]
module RangeExtensions =
type FSharp.Compiler.Text.Range with
member x.WithFileName(fileName: string) = Range.mkRange fileName x.Start x.End
/// the checker gives us back wacky ranges sometimes, so what we're going to do is check if the text of the triggering
/// symbol use is in each of the ranges we plan to rename, and if we're looking at a range that is _longer_ than our rename range,
/// do some splicing to find just the range we need to replace.
/// TODO: figure out where the caps are coming from in the compilation, maybe something wrong in the
member x.NormalizeDriveLetterCasing() =
if System.Char.IsUpper(x.FileName[0]) then
// we've got a case where the compiler is reading things from the file system that we'd rather it not -
// if we're adjusting the range's filename, we need to construct a whole new range or else indexing won't work
let fileName =
string (System.Char.ToLowerInvariant x.FileName[0]) + (x.FileName.Substring(1))
let newRange = Range.mkRange fileName x.Start x.End
newRange
else
x
/// utility method to get the tagged filename for use in our state storage
/// TODO: should we enforce this/use the Path members for normalization?
member x.TaggedFileName: string<LocalPath> = UMX.tag x.FileName
/// A copy of the StringText type from F#.Compiler.Text, which is private.
/// Adds a UOM-typed filename to make range manipulation easier, as well as
/// safer traversals
[<Sealed>]
type NamedText(fileName: string<LocalPath>, str: string) =
let getLines (str: string) =
use reader = new StringReader(str)
[| let mutable line = reader.ReadLine()
while not (isNull line) do
yield line
line <- reader.ReadLine()
if str.EndsWith("\n", StringComparison.Ordinal) then
// last trailing space not returned
// http://stackoverflow.com/questions/19365404/stringreader-omits-trailing-linebreak
yield String.Empty |]
let getLines =
// This requires allocating and getting all the lines.
// However, likely whoever is calling it is using a different implementation of ISourceText
// So, it's ok that we do this for now.
lazy getLines str
let lastCharPos =
lazy
(let lines = getLines.Value
if lines.Length > 0 then
(lines.Length, lines.[lines.Length - 1].Length)
else
(0, 0))
let safeLastCharPos =
lazy
(let (endLine, endChar) = lastCharPos.Value
Position.mkPos endLine endChar)
let totalRange =
lazy (Range.mkRange (UMX.untag fileName) Position.pos0 safeLastCharPos.Value)
member _.String = str
override _.GetHashCode() = str.GetHashCode()
override _.Equals(obj: obj) =
match obj with
| :? NamedText as other -> other.String.Equals(str)
| :? string as other -> other.Equals(str)
| _ -> false
override _.ToString() = str
/// The local absolute path of the file whose contents this NamedText represents
member x.FileName = fileName
/// The unwrapped local abolute path of the file whose contents this NamedText represents.
/// Should only be used when interoping with the Compiler/Serialization
member x.RawFileName = UMX.untag fileName
/// Cached representation of the final position in this file
member x.LastFilePosition = safeLastCharPos.Value
/// Cached representation of the entire contents of the file, for inclusion checks
member x.TotalRange = totalRange.Value
/// Provides safe access to a substring of the file via FCS-provided Range
member x.GetText(m: FSharp.Compiler.Text.Range) : Result<string, string> =
if not (Range.rangeContainsRange x.TotalRange m) then
Error $"%A{m} is outside of the bounds of the file"
else if m.StartLine = m.EndLine then // slice of a single line, just do that
let lineText = (x :> ISourceText).GetLineString(m.StartLine - 1)
lineText.Substring(m.StartColumn, m.EndColumn - m.StartColumn) |> Ok
else
// multiline, use a builder
let builder = new System.Text.StringBuilder()
// potential slice of the first line, including newline
// because we know there are lines after the first line
let firstLine = (x :> ISourceText).GetLineString(m.StartLine - 1)
builder.AppendLine(firstLine.Substring(Math.Min(firstLine.Length, m.StartColumn)))
|> ignore<System.Text.StringBuilder>
// whole intermediate lines, including newlines
for line in (m.StartLine + 1) .. (m.EndLine - 1) do
builder.AppendLine((x :> ISourceText).GetLineString(line - 1))
|> ignore<System.Text.StringBuilder>
// final part, potential slice, so we do not include the trailing newline
let lastLine = (x :> ISourceText).GetLineString(m.EndLine - 1)
builder.Append(lastLine.Substring(0, Math.Min(lastLine.Length, m.EndColumn)))
|> ignore<System.Text.StringBuilder>
Ok(builder.ToString())
member private x.GetLineUnsafe(pos: FSharp.Compiler.Text.Position) =
(x :> ISourceText).GetLineString(pos.Line - 1)
/// Provides safe access to a line of the file via FCS-provided Position
member x.GetLine(pos: FSharp.Compiler.Text.Position) : string option =
if pos.Line < 1 || pos.Line > getLines.Value.Length then
None
else
Some(x.GetLineUnsafe pos)
member x.GetLineLength(pos: FSharp.Compiler.Text.Position) =
if pos.Line > getLines.Value.Length then
None
else
Some (x.GetLineUnsafe pos).Length
member x.GetCharUnsafe(pos: FSharp.Compiler.Text.Position) : char = x.GetLine(pos).Value[pos.Column - 1]
/// <summary>Provides safe access to a character of the file via FCS-provided Position.
/// Also available in indexer form: <code lang="fsharp">x[pos]</code></summary>
member x.TryGetChar(pos: FSharp.Compiler.Text.Position) : char option =
option {
do! Option.guard (Range.rangeContainsPos (x.TotalRange) pos)
let lineText = x.GetLineUnsafe(pos)
if pos.Column = 0 then
return! None
else
let lineIndex = pos.Column - 1
if lineText.Length <= lineIndex then
return! None
else
return lineText[lineIndex]
}
member x.NextLine(pos: FSharp.Compiler.Text.Position) =
if pos.Line < getLines.Value.Length then
Position.mkPos (pos.Line + 1) 0 |> Some
else
None
/// Provides safe incrementing of a position in the file via FCS-provided Position
member x.NextPos(pos: FSharp.Compiler.Text.Position) : FSharp.Compiler.Text.Position option =
option {
let! currentLine = x.GetLine pos
if pos.Column - 1 = currentLine.Length then
if getLines.Value.Length > pos.Line then
// advance to the beginning of the next line
return Position.mkPos (pos.Line + 1) 0
else
return! None
else
return Position.mkPos pos.Line (pos.Column + 1)
}
/// Provides safe incrementing of positions in a file while returning the character at the new position.
/// Intended use is for traversal loops.
member x.TryGetNextChar(pos: FSharp.Compiler.Text.Position) : (FSharp.Compiler.Text.Position * char) option =
option {
let! np = x.NextPos pos
return np, x.GetCharUnsafe np
}
/// Provides safe decrementing of a position in the file via FCS-provided Position
member x.PrevPos(pos: FSharp.Compiler.Text.Position) : FSharp.Compiler.Text.Position option =
option {
if pos.Column <> 0 then
return Position.mkPos pos.Line (pos.Column - 1)
else if pos.Line <= 1 then
return! None
else if getLines.Value.Length > pos.Line - 2 then
let prevLine = (x :> ISourceText).GetLineString(pos.Line - 2)
// retreat to the end of the previous line
return Position.mkPos (pos.Line - 1) (prevLine.Length - 1)
else
return! None
}
/// Provides safe decrementing of positions in a file while returning the character at the new position.
/// Intended use is for traversal loops.
member x.TryGetPrevChar(pos: FSharp.Compiler.Text.Position) : (FSharp.Compiler.Text.Position * char) option =
option {
let! np = x.PrevPos pos
return np, x.GetCharUnsafe np
}
/// split the TotalRange of this document at the range specified.
/// for cases of new content, the start and end of the provided range will be equal.
/// for text replacements, the start and end may be different.
member inline x.SplitAt(m: FSharp.Compiler.Text.Range) : Range * Range =
let startRange = Range.mkRange x.RawFileName x.TotalRange.Start m.Start
let endRange = Range.mkRange x.RawFileName m.End x.TotalRange.End
startRange, endRange
/// create a new NamedText for this file with the given text inserted at the given range.
member x.ModifyText(m: FSharp.Compiler.Text.Range, text: string) : Result<NamedText, string> =
result {
let startRange, endRange = x.SplitAt(m)
let! startText = x[startRange] |> Result.mapError (fun x -> $"startRange -> {x}")
and! endText = x[endRange] |> Result.mapError (fun x -> $"endRange -> {x}")
let totalText = startText + text + endText
return NamedText(x.FileName, totalText)
}
/// Safe access to the contents of a file by Range
member x.Item
with get (m: FSharp.Compiler.Text.Range) = x.GetText(m)
/// Safe access to the char in a file by Position
member x.Item
with get (pos: FSharp.Compiler.Text.Position) = x.TryGetChar(pos)
member private x.Walk
(
start: FSharp.Compiler.Text.Position,
(posChange: FSharp.Compiler.Text.Position -> FSharp.Compiler.Text.Position option),
terminal,
condition
) =
/// if the condition is never met, return None
let firstPos = Position.pos0
let finalPos = x.LastFilePosition
let rec loop (pos: FSharp.Compiler.Text.Position) : FSharp.Compiler.Text.Position option =
option {
let! charAt = x[pos]
do! Option.guard (firstPos <> pos && finalPos <> pos)
do! Option.guard (not (terminal charAt))
if condition charAt then
return pos
else
let! nextPos = posChange pos
return! loop nextPos
}
loop start
member x.WalkForward(start, terminal, condition) =
x.Walk(start, x.NextPos, terminal, condition)
member x.WalkBackwards(start, terminal, condition) =
x.Walk(start, x.PrevPos, terminal, condition)
/// Provides line-by-line access to the underlying text.
/// This can lead to unsafe access patterns, consider using one of the range or position-based
/// accessors instead
member x.Lines = getLines.Value
interface ISourceText with
member _.Item
with get index = str.[index]
member _.GetLastCharacterPosition() = lastCharPos.Value
member _.GetLineString(lineIndex) = getLines.Value.[lineIndex]
member _.GetLineCount() = getLines.Value.Length
member _.GetSubTextString(start, length) = str.Substring(start, length)
member _.SubTextEquals(target, startIndex) =
if startIndex < 0 || startIndex >= str.Length then
invalidArg "startIndex" "Out of range."
if String.IsNullOrEmpty(target) then
invalidArg "target" "Is null or empty."
let lastIndex = startIndex + target.Length
if lastIndex <= startIndex || lastIndex >= str.Length then
invalidArg "target" "Too big."
str.IndexOf(target, startIndex, target.Length) <> -1
member _.Length = str.Length
member this.ContentEquals(sourceText) =
match sourceText with
| :? NamedText as sourceText when sourceText = this || sourceText.String = str -> true
| _ -> false
member _.CopyTo(sourceIndex, destination, destinationIndex, count) =
str.CopyTo(sourceIndex, destination, destinationIndex, count)
type VolatileFile =
{ Touched: DateTime
Lines: NamedText
Version: int option }
member this.FileName = this.Lines.FileName
/// <summary>Updates the Lines value</summary>
member this.SetLines(lines) = { this with Lines = lines }
/// <summary>Updates the Lines value with supplied text</summary>
member this.SetText(text) =
this.SetLines(NamedText(this.Lines.FileName, text))
/// <summary>Updates the Touched value</summary>
member this.SetTouched touched = { this with Touched = touched }
/// <summary>Updates the Touched value attempting to use the file on disk's GetLastWriteTimeUtc otherwise uses DateTime.UtcNow. </summary>
member this.UpdateTouched() =
let path = UMX.untag this.Lines.FileName
let dt =
if File.Exists path then
File.GetLastWriteTimeUtc path
else
DateTime.UtcNow
this.SetTouched dt
/// <summary>Helper method to create a VolatileFile</summary>
static member Create(lines, version, touched) =
{ Lines = lines
Version = version
Touched = touched }
/// <summary>Helper method to create a VolatileFile</summary>
static member Create(path, text, version, touched) =
VolatileFile.Create(NamedText(path, text), version, touched)
/// <summary>Helper method to create a VolatileFile, attempting to use the file on disk's GetLastWriteTimeUtc otherwise uses DateTime.UtcNow.</summary>
static member Create(path: string<LocalPath>, text, version) =
let touched =
let path = UMX.untag path
if File.Exists path then
File.GetLastWriteTimeUtc path
else
DateTime.UtcNow
VolatileFile.Create(path, text, version, touched)
type FileSystem(actualFs: IFileSystem, tryFindFile: string<LocalPath> -> VolatileFile option) =
let fsLogger = LogProvider.getLoggerByName "FileSystem"
let getContent (filename: string<LocalPath>) =
filename
|> tryFindFile
|> Option.map (fun file ->
fsLogger.debug (
Log.setMessage "Getting content of `{path}` - {hash}"
>> Log.addContext "path" filename
>> Log.addContext "hash" (file.Lines.GetHashCode())
)
file.Lines.ToString() |> System.Text.Encoding.UTF8.GetBytes)
/// translation of the BCL's Windows logic for Path.IsPathRooted.
///
/// either the first char is '/', or the first char is a drive identifier followed by ':'
let isWindowsStyleRootedPath (p: string) =
let isAlpha (c: char) =
(c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
(p.Length >= 1 && p.[0] = '/')
|| (p.Length >= 2 && isAlpha p.[0] && p.[1] = ':')
/// translation of the BCL's Unix logic for Path.IsRooted.
///
/// if the first character is '/' then the path is rooted
let isUnixStyleRootedPath (p: string) = p.Length > 0 && p.[0] = '/'
interface IFileSystem with
(* for these two members we have to be incredibly careful to root/extend paths in an OS-agnostic way,
as they handle paths for windows and unix file systems regardless of your host OS.
Therefore, you cannot use the BCL's Path.IsPathRooted/Path.GetFullPath members *)
member _.IsPathRootedShim(p: string) =
let r = isWindowsStyleRootedPath p || isUnixStyleRootedPath p
fsLogger.debug (
Log.setMessage "Is {path} rooted? {result}"
>> Log.addContext "path" p
>> Log.addContext "result" r
)
r
member _.GetFullPathShim(f: string) =
let expanded = Path.FilePathToUri f |> Path.FileUriToLocalPath
fsLogger.debug (
Log.setMessage "{path} expanded to {expanded}"
>> Log.addContext "path" f
>> Log.addContext "expanded" expanded
)
expanded
member _.GetLastWriteTimeShim(filename: string) =
let result =
filename
|> Utils.normalizePath
|> tryFindFile
|> Option.map (fun f -> f.Touched)
|> Option.defaultWith (fun () -> actualFs.GetLastWriteTimeShim filename)
// fsLogger.debug (
// Log.setMessage "GetLastWriteTimeShim of `{path}` - {date} "
// >> Log.addContext "path" filename
// >> Log.addContext "date" result
// )
result
member _.NormalizePathShim(f: string) = f |> Utils.normalizePath |> UMX.untag
member _.IsInvalidPathShim(f) = actualFs.IsInvalidPathShim f
member _.GetTempPathShim() = actualFs.GetTempPathShim()
member _.IsStableFileHeuristic(f) = actualFs.IsStableFileHeuristic f
member _.CopyShim(src, dest, o) = actualFs.CopyShim(src, dest, o)
member _.DirectoryCreateShim p = actualFs.DirectoryCreateShim p
member _.DirectoryDeleteShim p = actualFs.DirectoryDeleteShim p
member _.DirectoryExistsShim p = actualFs.DirectoryExistsShim p
member _.EnumerateDirectoriesShim p = actualFs.EnumerateDirectoriesShim p
member _.EnumerateFilesShim(p, pat) = actualFs.EnumerateFilesShim(p, pat)
member _.FileDeleteShim f = actualFs.FileDeleteShim f
member _.FileExistsShim f = actualFs.FileExistsShim f
member _.GetCreationTimeShim p = actualFs.GetCreationTimeShim p
member _.GetDirectoryNameShim p = actualFs.GetDirectoryNameShim p
member _.GetFullFilePathInDirectoryShim dir f =
actualFs.GetFullFilePathInDirectoryShim dir f
member _.OpenFileForReadShim(filePath: string, useMemoryMappedFile, shouldShadowCopy) =
filePath
|> Utils.normalizePath
|> getContent
|> Option.map (fun bytes -> new MemoryStream(bytes) :> Stream)
|> Option.defaultWith (fun _ ->
actualFs.OpenFileForReadShim(
filePath,
?useMemoryMappedFile = useMemoryMappedFile,
?shouldShadowCopy = shouldShadowCopy
))
member _.OpenFileForWriteShim(filePath: string, fileMode, fileAccess, fileShare) =
actualFs.OpenFileForWriteShim(filePath, ?fileMode = fileMode, ?fileAccess = fileAccess, ?fileShare = fileShare)
member _.AssemblyLoader = actualFs.AssemblyLoader