Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BufferTracker.cs « TextDataUtil « Util « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ed007f541a71fa8acba246dfdcddebeb9564a4f (plain)
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
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
namespace Microsoft.VisualStudio.Text.Utilities
{
    using System;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.Text.Projection;
    using Microsoft.VisualStudio.Utilities;

    internal sealed class BufferTracker : IDisposable
    {
        private static List<WeakReference> allocatedBuffers = new List<WeakReference>();

        private ITextBufferFactoryService textBufferFactoryService;
        private IProjectionBufferFactoryService projectionBufferFactoryService;

        public BufferTracker(ITextBufferFactoryService textBufferFactoryService,
                             IProjectionBufferFactoryService projectionBufferFactoryService)
        {
            this.textBufferFactoryService = textBufferFactoryService;
            this.projectionBufferFactoryService = projectionBufferFactoryService;
            textBufferFactoryService.TextBufferCreated += OnBufferCreated;
            projectionBufferFactoryService.ProjectionBufferCreated += OnBufferCreated;
        }

        public void Dispose()
        {
            this.textBufferFactoryService.TextBufferCreated -= OnBufferCreated;
            this.projectionBufferFactoryService.ProjectionBufferCreated -= OnBufferCreated;
            FreeSnapshotTrackers();
            allocatedBuffers.Clear();
        }

        private static void FreeSnapshotTrackers()
        {
            for (int b = 0; b < allocatedBuffers.Count; ++b)
            {
                ITextBuffer buffer = allocatedBuffers[b] as ITextBuffer;
                if (buffer != null)
                {
                    SnapshotTracker st;
                    if (buffer.Properties.TryGetProperty<SnapshotTracker>(typeof(SnapshotTracker), out st))
                    {
                        st.Dispose();
                        buffer.Properties.RemoveProperty(typeof(SnapshotTracker));
                    }
                }
            }
        }

        public static void TagBuffer(ITextBuffer buffer, string tag)
        {
            TextUtilities.TagBuffer(buffer, tag);
        }

        public static string GetTag(ITextBuffer buffer)
        {
            return TextUtilities.GetTag(buffer);
        }

        public static int ReportLiveBuffers(System.IO.TextWriter writer)
        {
            int totalBuffers = 0;
            if (allocatedBuffers != null)
            {
                for (int e = 0; e < allocatedBuffers.Count; ++e)
                {
                    if (allocatedBuffers[e].IsAlive)
                    {
                        ++totalBuffers;
                    }
                }

                if (writer != null)
                {
                    writer.Write
                        (String.Format
                            (System.Globalization.CultureInfo.CurrentCulture,
                             "{0}\r\n", totalBuffers));

                    int liveBuffers = 0;
                    for (int b = 0; b < allocatedBuffers.Count; ++b)
                    {
                        ITextBuffer buffer = allocatedBuffers[b].Target as ITextBuffer;
                        if (buffer != null)
                        {
                            ++liveBuffers;

                            string tag = GetTag(buffer);
                            writer.Write
                                (String.Format
                                    (System.Globalization.CultureInfo.CurrentCulture,
                                     "{0,5} {1}\r\n", b, !String.IsNullOrEmpty(tag) ? tag : "Untagged"));

                            SnapshotTracker st;
                            if (buffer.Properties.TryGetProperty<SnapshotTracker>(typeof(SnapshotTracker), out st))
                            {
                                st.ReportLiveSnapshots(writer);
                            }

                            foreach (KeyValuePair<Object, Object> pair in ((IPropertyOwner)buffer).Properties.PropertyList)
                            {
                                if (!string.Equals(pair.Key.ToString(), "tag", StringComparison.Ordinal))
                                {
                                    string rhsType;
                                    if (pair.Value == null)
                                    {
                                        rhsType = "?null";
                                    }
                                    else
                                    {
                                        rhsType = pair.Value.GetType().Name;
                                        if (pair.Value is WeakReference)
                                        {
                                            Object target = (pair.Value as WeakReference).Target;
                                            if (target != null)
                                            {
                                                rhsType = rhsType + "(" + target.GetType().Name + ")";
                                            }
                                        }
                                    }
                                    writer.Write(String.Format(System.Globalization.CultureInfo.CurrentCulture, "      {0}\r\n", rhsType));
                                }
                            }
                        }
                    }
                }
            }

            return totalBuffers;
        }

        private void OnBufferCreated(object sender, TextBufferCreatedEventArgs e)
        {
            // look for a hole in allocatedBuffers. This is linear in number of extant buffers
            // but we won't take this path except in diagnostic conditions.
            for (int b = 0; b < allocatedBuffers.Count; ++b)
            {
                if (!allocatedBuffers[b].IsAlive)
                {
                    allocatedBuffers[b].Target = e.TextBuffer;
                    return;
                }
            }
            allocatedBuffers.Add(new WeakReference(e.TextBuffer));
            e.TextBuffer.Properties.AddProperty(typeof(SnapshotTracker), new SnapshotTracker(e.TextBuffer));
        }
    }
}