forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.cs
More file actions
201 lines (181 loc) · 6.39 KB
/
Copy pathBlock.cs
File metadata and controls
201 lines (181 loc) · 6.39 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Web.Razor.Generator;
using System.Web.Razor.Resources;
using System.Web.Razor.Text;
namespace System.Web.Razor.Parser.SyntaxTree
{
public class Block : SyntaxTreeNode
{
public Block(BlockBuilder source)
{
if (source.Type == null)
{
throw new InvalidOperationException(RazorResources.Block_Type_Not_Specified);
}
Type = source.Type.Value;
Children = source.Children;
Name = source.Name;
CodeGenerator = source.CodeGenerator;
source.Reset();
foreach (SyntaxTreeNode node in Children)
{
node.Parent = this;
}
}
internal Block(BlockType type, IEnumerable<SyntaxTreeNode> contents, IBlockCodeGenerator generator)
{
Type = type;
CodeGenerator = generator;
Children = contents;
}
[SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "Type is the most appropriate name for this property and there is little chance of confusion with GetType")]
public BlockType Type { get; private set; }
public IEnumerable<SyntaxTreeNode> Children { get; private set; }
public string Name { get; private set; }
public IBlockCodeGenerator CodeGenerator { get; private set; }
public override bool IsBlock
{
get { return true; }
}
public override SourceLocation Start
{
get
{
SyntaxTreeNode child = Children.FirstOrDefault();
if (child == null)
{
return SourceLocation.Zero;
}
else
{
return child.Start;
}
}
}
public override int Length
{
get { return Children.Sum(child => child.Length); }
}
public Span FindFirstDescendentSpan()
{
SyntaxTreeNode current = this;
while (current != null && current.IsBlock)
{
current = ((Block)current).Children.FirstOrDefault();
}
return current as Span;
}
public Span FindLastDescendentSpan()
{
SyntaxTreeNode current = this;
while (current != null && current.IsBlock)
{
current = ((Block)current).Children.LastOrDefault();
}
return current as Span;
}
public override void Accept(ParserVisitor visitor)
{
visitor.VisitBlock(this);
}
public override string ToString()
{
return String.Format(CultureInfo.CurrentCulture, "{0} Block at {1}::{2} (Gen:{3})", Type, Start, Length, CodeGenerator);
}
public override bool Equals(object obj)
{
Block other = obj as Block;
return other != null &&
Type == other.Type &&
Equals(CodeGenerator, other.CodeGenerator) &&
ChildrenEqual(Children, other.Children);
}
public override int GetHashCode()
{
return (int)Type;
}
public IEnumerable<Span> Flatten()
{
// Create an enumerable that flattens the tree for use by syntax highlighters, etc.
foreach (SyntaxTreeNode element in Children)
{
Span span = element as Span;
if (span != null)
{
yield return span;
}
else
{
Block block = element as Block;
foreach (Span childSpan in block.Flatten())
{
yield return childSpan;
}
}
}
}
public Span LocateOwner(TextChange change)
{
// Ask each child recursively
Span owner = null;
foreach (SyntaxTreeNode element in Children)
{
Span span = element as Span;
if (span == null)
{
owner = ((Block)element).LocateOwner(change);
}
else
{
if (change.OldPosition < span.Start.AbsoluteIndex)
{
// Early escape for cases where changes overlap multiple spans
// In those cases, the span will return false, and we don't want to search the whole tree
// So if the current span starts after the change, we know we've searched as far as we need to
break;
}
owner = span.EditHandler.OwnsChange(span, change) ? span : owner;
}
if (owner != null)
{
break;
}
}
return owner;
}
private static bool ChildrenEqual(IEnumerable<SyntaxTreeNode> left, IEnumerable<SyntaxTreeNode> right)
{
IEnumerator<SyntaxTreeNode> leftEnum = left.GetEnumerator();
IEnumerator<SyntaxTreeNode> rightEnum = right.GetEnumerator();
while (leftEnum.MoveNext())
{
if (!rightEnum.MoveNext() || // More items in left than in right
!Equals(leftEnum.Current, rightEnum.Current))
{
// Nodes are not equal
return false;
}
}
if (rightEnum.MoveNext())
{
// More items in right than left
return false;
}
return true;
}
public override bool EquivalentTo(SyntaxTreeNode node)
{
Block other = node as Block;
if (other == null || other.Type != Type)
{
return false;
}
return Enumerable.SequenceEqual(Children, other.Children, new EquivalenceComparer());
}
}
}