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

ExtensionChain.cs « MonoDevelop.Projects « MonoDevelop.Core « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 066ea2a66e0e4aff64897990e1cd13ab0ce06c5c (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// ExtensionChain.cs
//
// Author:
//       Lluis Sanchez Gual <lluis@xamarin.com>
//
// Copyright (c) 2014 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;

namespace MonoDevelop.Projects
{
	/// <summary>
	/// A collection of extension objects
	/// </summary>
	public class ExtensionChain
	{
		Dictionary<Type,ChainedExtensionSentinel> chains = new Dictionary<Type, ChainedExtensionSentinel> ();
		// Maybe an array is not the best solution here, given chains grow and decrease, a list might be better.
		ChainedExtension [] extensions;
		ChainedExtension defaultInsertBefore;
		BatchModifier batchModifier;

		public static ExtensionChain Create<T> (T[] extensions) where T:ChainedExtension
		{
			var c = new ExtensionChain ();

			for (int n = extensions.Length - 2; n >= 0; n--)
				extensions [n].InitChain (c, extensions [n + 1]);

			c.extensions = extensions;
			return c;
		}

		public T GetExtension<T> () where T:ChainedExtension, new()
		{
			if (!chains.TryGetValue (typeof(T), out ChainedExtensionSentinel e)) {
				e = new ChainedExtensionSentinel (new T());
				e.Update (this, typeof (T), -1);
				chains [typeof(T)] = e;
			}
			return (T)e.Extension;
		}

		public void SetDefaultInsertionPosition (ChainedExtension insertBefore)
		{
			defaultInsertBefore = insertBefore;
		}

		public IEnumerable<ChainedExtension> GetAllExtensions ()
		{
			return extensions;
		}

		public IDisposable BatchModify () => batchModifier = new BatchModifier (this);

		public void AddExtension (ChainedExtension ext, ChainedExtension insertAfter = null, ChainedExtension insertBefore = null)
		{
			int index = -1;
			if (insertBefore != null) {
				index = Array.IndexOf (extensions, insertBefore);
			} else if (insertAfter != null) {
				index = Array.IndexOf (extensions, insertAfter);
				if (index != -1)
					index++;
			} else if (defaultInsertBefore != null) {
				index = Array.IndexOf (extensions, defaultInsertBefore);
			}

			if (index == -1) {
				index = extensions.Length;
			}

			Array.Resize (ref extensions, extensions.Length + 1);
			for (int n = extensions.Length - 1; n > index; n--)
				extensions [n] = extensions [n - 1];
			extensions [index] = ext;

			Rechain (index);
		}

		public void RemoveExtension (ChainedExtension ext)
		{
			if (extensions == null)
				return;

			int index = extensions.Length;
			extensions = extensions.Where ((e, eindex) => {
				bool shouldRemove = e == ext;
				if (shouldRemove)
					index = eindex;
				return !shouldRemove;
			}).ToArray ();

			Rechain (index);
		}

		void Rechain (int firstChangeIndex)
		{
			// If we are in a batch update, only update where to start rechaining.
			if (batchModifier != null) {
				batchModifier.UpdateFirstIndex (firstChangeIndex);
				return;
			}

			// Re-chain every extension
			for (int n = extensions.Length - 2; n >= 0; n--)
				extensions [n].InitChain (this, extensions [n + 1]);

			// The first extension object in type-specific chains is a placeholder extension used only to hold
			// a reference to the real first extension.
			foreach (var kvp in chains) {
				ChainedExtensionSentinel fex = kvp.Value;
				fex.Update (this, kvp.Key, firstChangeIndex);
			}
		}

		public void Dispose ()
		{
			var first = extensions [0];
			extensions = null;
			first.DisposeChain ();

			foreach (var kvp in chains) {
				// Dispose the placeholder extension just in case the extension itself registers something
				// in InitializeChain that it cleans up in Dispose.
				var extension = kvp.Value;
				extension.Dispose ();
			}
		}

		class ChainedExtensionSentinel
		{
			public ChainedExtension Extension { get; }
			int extensionIndex = -1;

			public ChainedExtensionSentinel (ChainedExtension extension)
			{
				Extension = extension;
			}

			public void Update (ExtensionChain chain, Type type, int firstChainChangeIndex)
			{
				// We only want to update an extension if we insert somewhere before the extension we found.
				if (extensionIndex < firstChainChangeIndex)
					return;

				// Maybe it would be useful to skip extensions until min(indices), as they've already been scanned
				// in a previous check
				var impl = ChainedExtension.FindNextImplementation (type, chain.extensions[0], out extensionIndex);
				Extension.InitChain (chain, impl);
			}

			public void Dispose () => Extension.Dispose ();
		}

		class BatchModifier : IDisposable
		{
			readonly ExtensionChain chain;
			int minChangedIndex;

			public BatchModifier (ExtensionChain chain)
			{
				this.chain = chain;
				minChangedIndex = chain.extensions.Length;
			}

			public void UpdateFirstIndex (int firstChainChangeIndex)
			{
				// If we added a node at firstChainChangeIndex then removed that one
				// it might help not to rechain in that case and reset the index.
				// Maybe we can keep track of that and handle it.
				// Regardless, the code is simpler this way and it should not be a bottleneck
				minChangedIndex = Math.Min (firstChainChangeIndex, minChangedIndex);
			}

			public void Dispose ()
			{
				chain.batchModifier = null;
				chain.Rechain (minChangedIndex);
			}
		}
	}
}