forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGeneratorPaddingHelper.cs
More file actions
204 lines (169 loc) · 7.42 KB
/
CodeGeneratorPaddingHelper.cs
File metadata and controls
204 lines (169 loc) · 7.42 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
202
203
204
// 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.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Web.Razor.Parser;
using System.Web.Razor.Parser.SyntaxTree;
namespace System.Web.Razor.Generator
{
internal static class CodeGeneratorPaddingHelper
{
private static readonly char[] _newLineChars = { '\r', '\n' };
// there is some duplicity of code here, but its very simple and since this is a host path, I'd rather not create another class to encapsulate the data.
public static int PaddingCharCount(RazorEngineHost host, Span target, int generatedStart)
{
int padding = CalculatePadding(host, target, generatedStart);
if (host.DesignTimeMode && host.IsIndentingWithTabs)
{
int spaces;
int tabs = Math.DivRem(padding, host.TabSize, out spaces);
return tabs + spaces;
}
else
{
return padding;
}
}
// Special case for statement padding to account for brace positioning in the editor.
public static string PadStatement(RazorEngineHost host, string code, Span target, ref int startGeneratedCode, out int paddingCharCount)
{
if (host == null)
{
throw new ArgumentNullException("host");
}
if (target == null)
{
throw new ArgumentNullException("target");
}
// We are passing 0 rather than startgeneratedcode intentionally (keeping v2 behavior).
int padding = CalculatePadding(host, target, 0);
// We treat statement padding specially so for brace positioning, so that in the following example:
// @if (foo > 0)
// {
// }
//
// the braces shows up under the @ rather than under the if.
if (host.DesignTimeMode &&
padding > 0 &&
target.Previous.Kind == SpanKind.Transition && // target.Previous is guaranteed to be none null if you got any padding.
String.Equals(target.Previous.Content, SyntaxConstants.TransitionString))
{
padding--;
startGeneratedCode--;
}
string generatedCode = PadInternal(host, code, padding, out paddingCharCount);
return generatedCode;
}
public static string Pad(RazorEngineHost host, string code, Span target, out int paddingCharCount)
{
int padding = CalculatePadding(host, target, 0);
return PadInternal(host, code, padding, out paddingCharCount);
}
public static string Pad(RazorEngineHost host, string code, Span target, int generatedStart, out int paddingCharCount)
{
int padding = CalculatePadding(host, target, generatedStart);
return PadInternal(host, code, padding, out paddingCharCount);
}
// internal for unit testing only, not intended to be used directly in code
internal static int CalculatePadding(RazorEngineHost host, Span target, int generatedStart)
{
if (host == null)
{
throw new ArgumentNullException("host");
}
if (target == null)
{
throw new ArgumentNullException("target");
}
int padding;
padding = CollectSpacesAndTabs(target, host.TabSize) - generatedStart;
// if we add generated text that is longer than the padding we wanted to insert we have no recourse and we have to skip padding
// example:
// Razor code at column zero: @somecode()
// Generated code will be:
// In design time: __o = somecode();
// In Run time: Write(somecode());
//
// In both cases the padding would have been 1 space to remote the space the @ symbol takes, which will be smaller than the 6 chars the hidden generated code takes.
if (padding < 0)
{
padding = 0;
}
return padding;
}
private static string PadInternal(RazorEngineHost host, string code, int padding, out int paddingCharCount)
{
if (host.DesignTimeMode && host.IsIndentingWithTabs)
{
int spaces;
int tabs = Math.DivRem(padding, host.TabSize, out spaces);
paddingCharCount = tabs + spaces;
return new string('\t', tabs) + new string(' ', spaces) + code;
}
else
{
paddingCharCount = padding;
return code.PadLeft(padding + code.Length, ' ');
}
}
private static int CollectSpacesAndTabs(Span target, int tabSize)
{
Span firstSpanInLine = target;
string currentContent = null;
while (firstSpanInLine.Previous != null)
{
// When scanning previous spans we need to be break down the spans with spaces.
// Because the parser doesn't so for example a span looking like \n\n\t needs to be broken down, and we should just grab the \t.
String previousContent = firstSpanInLine.Previous.Content ?? String.Empty;
int lastNewLineIndex = previousContent.LastIndexOfAny(_newLineChars);
if (lastNewLineIndex < 0)
{
firstSpanInLine = firstSpanInLine.Previous;
}
else
{
if (lastNewLineIndex != previousContent.Length - 1)
{
firstSpanInLine = firstSpanInLine.Previous;
currentContent = previousContent.Substring(lastNewLineIndex + 1);
}
break;
}
}
// We need to walk from the beginning of the line, because space + tab(tabSize) = tabSize columns, but tab(tabSize) + space = tabSize+1 columns.
Span currentSpanInLine = firstSpanInLine;
if (currentContent == null)
{
currentContent = currentSpanInLine.Content;
}
int padding = 0;
while (currentSpanInLine != target)
{
if (currentContent != null)
{
for (int i = 0; i < currentContent.Length; i++)
{
if (currentContent[i] == '\t')
{
// Example:
// <space><space><tab><tab>:
// iter 1) 1
// iter 2) 2
// iter 3) 4 = 2 + (4 - 2)
// iter 4) 8 = 4 + (4 - 0)
padding = padding + (tabSize - (padding % tabSize));
}
else
{
padding++;
}
}
}
currentSpanInLine = currentSpanInLine.Next;
currentContent = currentSpanInLine.Content;
}
return padding;
}
}
}