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

ContentTypeImpl.cs « ContentType « Impl « Core « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a6e3b9eeb45317c48a824a35b0b2fed821c1f48 (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
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
//
//  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.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Microsoft.VisualStudio.Utilities.Implementation
{
    internal partial class ContentTypeImpl : IContentType
    {
        private readonly string name;
        private readonly static ContentTypeImpl[] emptyBaseTypes = Array.Empty<ContentTypeImpl>();
        private ContentTypeImpl[] baseTypeList = emptyBaseTypes;

        internal ContentTypeImpl(string name, string mimeType = null, IEnumerable<string> baseTypes = null)
        {
            this.name = name;
            this.MimeType = mimeType;
            this.UnprocessedBaseTypes = baseTypes;
        }

        public string TypeName
        {
            get { return this.name; }
        }

        public string DisplayName
        {
            get { return this.name; }
        }

        public string MimeType { get; }

        public bool IsOfType(string type)
        {
            if (string.Compare(type, this.name, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return true;
            }
            else
            {
                for (int i = 0; i < this.baseTypeList.Length; i++)
                {
                    if (this.baseTypeList[i].IsOfType(type))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public IEnumerable<IContentType> BaseTypes
        {
            get { return this.baseTypeList; }
        }

        public override string ToString()
        {
            return this.name;
        }

        internal void ProcessBaseTypes(IDictionary<string, ContentTypeImpl> nameToContentTypeBuilder,
                                       IDictionary<string, ContentTypeImpl> mimeTypeToContentTypeBuilder)
        {
            if (this.UnprocessedBaseTypes != null)
            {
                List<ContentTypeImpl> newBaseTypes = new List<ContentTypeImpl>();
                foreach (var baseTypeName in this.UnprocessedBaseTypes)
                {
                    // The expectation is that the base type will already exists but (if it doesn't) add a stub for it (& the ctor for a stub base type leaves it in a state
                    // where the basetypes/state are set appropriately).
                    var baseType = ContentTypeRegistryImpl.AddContentTypeFromMetadata(baseTypeName, /* mime type */null, /* base types */null, nameToContentTypeBuilder, mimeTypeToContentTypeBuilder);
                    if (baseType == ContentTypeRegistryImpl.UnknownContentTypeImpl)
                    {
                        throw new InvalidOperationException(String.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                            Strings.ContentTypeRegistry_ContentTypesCannotDeriveFromUnknown, this.TypeName));
                    }

                    if (!newBaseTypes.Contains(baseType))
                        newBaseTypes.Add(baseType);
                }

                if (newBaseTypes.Count > 0)
                {
                    this.baseTypeList = newBaseTypes.ToArray();
                    this.state = VisitState.NotVisited;
                }
                else
                {
                    Debug.Assert(object.ReferenceEquals(this.baseTypeList, emptyBaseTypes));
                }

                this.UnprocessedBaseTypes = null;
            }
        }

        // used internally for cycle detection
        internal enum VisitState
        {
            NotVisited = 0, // The node hasn't been visited yet
            Visiting,       // The node (or one of its children) is being visited
            Visited         // The node and its children have been visited before
        }

        private VisitState state = VisitState.Visited;

        internal bool CheckForCycle(bool breakCycle)
        {
            try
            {
                if (this.baseTypeList.Length != 0)
                {
                    this.state = VisitState.Visiting;
                    foreach (var baseType in this.baseTypeList)
                    {
                        if (baseType.state == VisitState.Visiting)
                        {
                            if (breakCycle)
                            {
                                // There is a cycle of this -> basetype -> ... -> this
                                // Don't try a surgical fix: simply break the cycle the easiest way possible
                                // since this is an error in the definitions that shouldn't happen.
                                // TODO: log the error.
                                this.baseTypeList = emptyBaseTypes;
                            }

                            return true;
                        }
                        else if ((baseType.state == VisitState.NotVisited) && baseType.CheckForCycle(breakCycle))
                        {
                            return true;
                        }
                    }
                }
            }
            finally
            {
                this.state = VisitState.Visited;
            }

            return false;
        }

        // used internally when building up content types
        internal void AddUnprocessedBaseTypes(IEnumerable<string> newBaseTypes)
        {
            if (newBaseTypes != null)
            {
                if (object.ReferenceEquals(this.UnprocessedBaseTypes, emptyBaseTypes))
                {
                    this.UnprocessedBaseTypes = newBaseTypes;
                }
                else
                {
                    var allBaseTypes = new List<string>(this.UnprocessedBaseTypes);
                    allBaseTypes.AddRange(newBaseTypes);
                    this.UnprocessedBaseTypes = allBaseTypes;
                }
            }
        }

        internal IEnumerable<string> UnprocessedBaseTypes;

#if DEBUG
        internal bool IsProcessed
        {
            get
            {
                return (this.UnprocessedBaseTypes == null) && (this.baseTypeList != null) &&
                         ((this.baseTypeList.Length == 0)
                          ? (object.ReferenceEquals(this.baseTypeList, ContentTypeImpl.emptyBaseTypes) && (this.state == VisitState.Visited))
                          : (this.state == VisitState.NotVisited));
            }
        }

        internal bool IsCheckedForCycles
        {
            get { return (this.state == VisitState.Visited) && (this.UnprocessedBaseTypes == null) && (object.ReferenceEquals(this.baseTypeList, ContentTypeImpl.emptyBaseTypes) || (this.baseTypeList.Length > 0)); }
        }
#endif
    }
}