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
|
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//
// This file contain implementations details that are subject to change without notice.
// Use at your own risk.
//
namespace Microsoft.VisualStudio.Text.Implementation
{
using System;
using System.Collections.Generic;
sealed class PersistentSpanSet : IDisposable
{
internal FileNameKey FileKey;
internal ITextDocument Document;
internal readonly HashSet<PersistentSpan> Spans = new HashSet<PersistentSpan>();
private readonly PersistentSpanFactory Factory;
private ITextSnapshot _savedSnapshot = null;
internal PersistentSpanSet(FileNameKey filePath, ITextDocument document, PersistentSpanFactory factory)
{
this.FileKey = filePath;
this.Document = document;
this.Factory = factory;
if (document != null)
{
document.FileActionOccurred += this.OnFileActionOccurred;
}
}
public void Dispose()
{
Assumes.True(this.Spans.Count == 0);
if (this.Document != null)
{
this.Document.FileActionOccurred -= this.OnFileActionOccurred;
this.Document = null;
}
}
internal PersistentSpan Create(int startLine, int startIndex, int endLine, int endIndex, SpanTrackingMode trackingMode)
{
PersistentSpan persistentSpan = new PersistentSpan(startLine, startIndex, endLine, endIndex, trackingMode, this);
this.Spans.Add(persistentSpan);
return persistentSpan;
}
internal PersistentSpan Create(Span span, SpanTrackingMode trackingMode)
{
var persistentSpan = new PersistentSpan(span, trackingMode, this);
this.Spans.Add(persistentSpan);
return persistentSpan;
}
internal PersistentSpan Create(ITextSnapshot snapshot, int startLine, int startIndex, int endLine, int endIndex, SpanTrackingMode trackingMode)
{
var start = PersistentSpan.LineIndexToSnapshotPoint(startLine, startIndex, snapshot);
var end = PersistentSpan.LineIndexToSnapshotPoint(endLine, endIndex, snapshot);
if (end < start)
{
end = start;
}
return this.Create(new SnapshotSpan(start, end), trackingMode);
}
internal PersistentSpan Create(SnapshotSpan span, SpanTrackingMode trackingMode)
{
var persistentSpan = new PersistentSpan(span, trackingMode, this);
this.Spans.Add(persistentSpan);
return persistentSpan;
}
internal void Delete(PersistentSpan span)
{
this.Factory.Delete(this, span);
}
internal void DocumentReopened(ITextDocument document)
{
Requires.NotNull(document, nameof(document));
Assumes.Null(this.Document);
this.Document = document;
document.FileActionOccurred += this.OnFileActionOccurred;
foreach (var s in this.Spans)
{
s.DocumentReopened();
}
}
internal void DocumentClosed()
{
Assumes.NotNull(this.Document);
this.FileKey = new FileNameKey(this.Document.FilePath);
foreach (var s in this.Spans)
{
s.DocumentClosed(_savedSnapshot);
}
this.Document.FileActionOccurred -= this.OnFileActionOccurred;
this.Document = null;
}
private void OnFileActionOccurred(object sender, TextDocumentFileActionEventArgs e)
{
if (e.FileActionType == FileActionTypes.ContentSavedToDisk)
{
_savedSnapshot = this.Document.TextBuffer.CurrentSnapshot;
}
else if (e.FileActionType == FileActionTypes.DocumentRenamed)
{
this.FileKey = new FileNameKey(this.Document.FilePath);
this.Factory.DocumentRenamed(this);
}
}
}
}
|