forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplicitExpressionEditHandler.cs
More file actions
374 lines (325 loc) · 15.6 KB
/
Copy pathImplicitExpressionEditHandler.cs
File metadata and controls
374 lines (325 loc) · 15.6 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// 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;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web.Razor.Parser;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;
using System.Web.Razor.Tokenizer.Symbols;
using Microsoft.Internal.Web.Utils;
namespace System.Web.Razor.Editor
{
public class ImplicitExpressionEditHandler : SpanEditHandler
{
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Func<T> is the recommended delegate type and requires this level of nesting.")]
public ImplicitExpressionEditHandler(Func<string, IEnumerable<ISymbol>> tokenizer, ISet<string> keywords, bool acceptTrailingDot)
: base(tokenizer)
{
Initialize(keywords, acceptTrailingDot);
}
public bool AcceptTrailingDot { get; private set; }
public ISet<string> Keywords { get; private set; }
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture, "{0};ImplicitExpression[{1}];K{2}", base.ToString(), AcceptTrailingDot ? "ATD" : "RTD", Keywords.Count);
}
public override bool Equals(object obj)
{
ImplicitExpressionEditHandler other = obj as ImplicitExpressionEditHandler;
return other != null &&
base.Equals(other) &&
Keywords.SetEquals(other.Keywords) &&
AcceptTrailingDot == other.AcceptTrailingDot;
}
public override int GetHashCode()
{
return HashCodeCombiner.Start()
.Add(base.GetHashCode())
.Add(AcceptTrailingDot)
.Add(Keywords)
.CombinedHash;
}
protected override PartialParseResult CanAcceptChange(Span target, TextChange normalizedChange)
{
if (AcceptedCharacters == AcceptedCharacters.Any)
{
return PartialParseResult.Rejected;
}
// In some editors intellisense insertions are handled as "dotless commits". If an intellisense selection is confirmed
// via something like '.' a dotless commit will append a '.' and then insert the remaining intellisense selection prior
// to the appended '.'. This 'if' statement attempts to accept the intermediate steps of a dotless commit via
// intellisense. It will accept two cases:
// 1. '@foo.' -> '@foobaz.'.
// 2. '@foobaz..' -> '@foobaz.bar.'. Includes Sub-cases '@foobaz()..' -> '@foobaz().bar.' etc.
// The key distinction being the double '.' in the second case.
if (IsDotlessCommitInsertion(target, normalizedChange))
{
return HandleDotlessCommitInsertion(target);
}
if (IsAcceptableIdentifierReplacement(target, normalizedChange))
{
return TryAcceptChange(target, normalizedChange);
}
if (IsAcceptableReplace(target, normalizedChange))
{
return HandleReplacement(target, normalizedChange);
}
int changeRelativePosition = normalizedChange.OldPosition - target.Start.AbsoluteIndex;
// Get the edit context
char? lastChar = null;
if (changeRelativePosition > 0 && target.Content.Length > 0)
{
lastChar = target.Content[changeRelativePosition - 1];
}
// Don't support 0->1 length edits
if (lastChar == null)
{
return PartialParseResult.Rejected;
}
// Accepts cases when insertions are made at the end of a span or '.' is inserted within a span.
if (IsAcceptableInsertion(target, normalizedChange))
{
// Handle the insertion
return HandleInsertion(target, lastChar.Value, normalizedChange);
}
if (IsAcceptableDeletion(target, normalizedChange))
{
return HandleDeletion(target, lastChar.Value, normalizedChange);
}
return PartialParseResult.Rejected;
}
private void Initialize(ISet<string> keywords, bool acceptTrailingDot)
{
Keywords = keywords ?? new HashSet<string>();
AcceptTrailingDot = acceptTrailingDot;
}
// A dotless commit is the process of inserting a '.' with an intellisense selection.
private static bool IsDotlessCommitInsertion(Span target, TextChange change)
{
return IsNewDotlessCommitInsertion(target, change) || IsSecondaryDotlessCommitInsertion(target, change);
}
// Completing 'DateTime' in intellisense with a '.' could result in: '@DateT' -> '@DateT.' -> '@DateTime.' which is accepted.
private static bool IsNewDotlessCommitInsertion(Span target, TextChange change)
{
return !IsAtEndOfSpan(target, change) &&
change.NewPosition > 0 &&
change.NewLength > 0 &&
target.Content.Last() == '.' &&
ParserHelpers.IsIdentifier(change.NewText, requireIdentifierStart: false) &&
(change.OldLength == 0 || ParserHelpers.IsIdentifier(change.OldText, requireIdentifierStart: false));
}
// Once a dotless commit has been performed you then have something like '@DateTime.'. This scenario is used to detect the
// situation when you try to perform another dotless commit resulting in a textchange with '..'. Completing 'DateTime.Now'
// in intellisense with a '.' could result in: '@DateTime.' -> '@DateTime..' -> '@DateTime.Now.' which is accepted.
private static bool IsSecondaryDotlessCommitInsertion(Span target, TextChange change)
{
// Do not need to worry about other punctuation, just looking for double '.' (after change)
return change.NewLength == 1 &&
!String.IsNullOrEmpty(target.Content) &&
target.Content.Last() == '.' &&
change.NewText == "." &&
change.OldLength == 0;
}
private bool IsAcceptableIdentifierReplacement(Span target, TextChange change)
{
if (!change.IsReplace)
{
return false;
}
foreach (ISymbol isymbol in target.Symbols)
{
CSharpSymbol symbol = isymbol as CSharpSymbol;
if (symbol == null)
{
break;
}
int symbolStartIndex = target.Start.AbsoluteIndex + symbol.Start.AbsoluteIndex;
int symbolEndIndex = symbolStartIndex + symbol.Content.Length;
// We're looking for the first symbol that contains the TextChange.
if (symbolEndIndex > change.OldPosition)
{
if (symbolEndIndex >= change.OldPosition + change.OldLength && symbol.Type == CSharpSymbolType.Identifier)
{
// The symbol we're changing happens to be an identifier. Need to check if its transformed state is also one.
// We do this transformation logic to capture the case that the new text change happens to not be an identifier;
// i.e. "5". Alone, it's numeric, within an identifier it's classified as identifier.
string transformedContent = change.ApplyChange(symbol.Content, symbolStartIndex);
IEnumerable<ISymbol> newSymbols = Tokenizer(transformedContent);
if (newSymbols.Count() != 1)
{
// The transformed content resulted in more than one symbol; we can only replace a single identifier with
// another single identifier.
break;
}
CSharpSymbol newSymbol = (CSharpSymbol)newSymbols.First();
if (newSymbol.Type == CSharpSymbolType.Identifier)
{
return true;
}
}
// Change is touching a non-identifier symbol or spans multiple symbols.
break;
}
}
return false;
}
private static bool IsAcceptableReplace(Span target, TextChange change)
{
return IsEndReplace(target, change) ||
(change.IsReplace && RemainingIsWhitespace(target, change));
}
private static bool IsAcceptableDeletion(Span target, TextChange change)
{
return IsEndDeletion(target, change) ||
(change.IsDelete && RemainingIsWhitespace(target, change));
}
// Acceptable insertions can occur at the end of a span or when a '.' is inserted within a span.
private static bool IsAcceptableInsertion(Span target, TextChange change)
{
return change.IsInsert &&
(IsAcceptableEndInsertion(target, change) ||
IsAcceptableInnerInsertion(target, change));
}
// Accepts character insertions at the end of spans. AKA: '@foo' -> '@fooo' or '@foo' -> '@foo ' etc.
private static bool IsAcceptableEndInsertion(Span target, TextChange change)
{
Debug.Assert(change.IsInsert);
return IsAtEndOfSpan(target, change) ||
RemainingIsWhitespace(target, change);
}
// Accepts '.' insertions in the middle of spans. Ex: '@foo.baz.bar' -> '@foo..baz.bar'
// This is meant to allow intellisense when editing a span.
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "target", Justification = "The 'target' parameter is used in Debug to validate that the function is called in the correct context.")]
private static bool IsAcceptableInnerInsertion(Span target, TextChange change)
{
Debug.Assert(change.IsInsert);
// Ensure that we're actually inserting in the middle of a span and not at the end.
// This case will fail if the IsAcceptableEndInsertion does not capture an end insertion correctly.
Debug.Assert(!IsAtEndOfSpan(target, change));
return change.NewPosition > 0 &&
change.NewText == ".";
}
private static bool RemainingIsWhitespace(Span target, TextChange change)
{
int offset = (change.OldPosition - target.Start.AbsoluteIndex) + change.OldLength;
return String.IsNullOrWhiteSpace(target.Content.Substring(offset));
}
private PartialParseResult HandleDotlessCommitInsertion(Span target)
{
PartialParseResult result = PartialParseResult.Accepted;
if (!AcceptTrailingDot && target.Content.LastOrDefault() == '.')
{
result |= PartialParseResult.Provisional;
}
return result;
}
private PartialParseResult HandleReplacement(Span target, TextChange change)
{
// Special Case for IntelliSense commits.
// When IntelliSense commits, we get two changes (for example user typed "Date", then committed "DateTime" by pressing ".")
// 1. Insert "." at the end of this span
// 2. Replace the "Date." at the end of the span with "DateTime."
// We need partial parsing to accept case #2.
string oldText = GetOldText(target, change);
PartialParseResult result = PartialParseResult.Rejected;
if (EndsWithDot(oldText) && EndsWithDot(change.NewText))
{
result = PartialParseResult.Accepted;
if (!AcceptTrailingDot)
{
result |= PartialParseResult.Provisional;
}
}
return result;
}
private PartialParseResult HandleDeletion(Span target, char previousChar, TextChange change)
{
// What's left after deleting?
if (previousChar == '.')
{
return TryAcceptChange(target, change, PartialParseResult.Accepted | PartialParseResult.Provisional);
}
else if (ParserHelpers.IsIdentifierPart(previousChar))
{
return TryAcceptChange(target, change);
}
else
{
return PartialParseResult.Rejected;
}
}
private PartialParseResult HandleInsertion(Span target, char previousChar, TextChange change)
{
// What are we inserting after?
if (previousChar == '.')
{
return HandleInsertionAfterDot(target, change);
}
else if (ParserHelpers.IsIdentifierPart(previousChar) || previousChar == ')' || previousChar == ']')
{
return HandleInsertionAfterIdPart(target, change);
}
else
{
return PartialParseResult.Rejected;
}
}
private PartialParseResult HandleInsertionAfterIdPart(Span target, TextChange change)
{
// If the insertion is a full identifier part, accept it
if (ParserHelpers.IsIdentifier(change.NewText, requireIdentifierStart: false))
{
return TryAcceptChange(target, change);
}
else if (EndsWithDot(change.NewText))
{
// Accept it, possibly provisionally
PartialParseResult result = PartialParseResult.Accepted;
if (!AcceptTrailingDot)
{
result |= PartialParseResult.Provisional;
}
return TryAcceptChange(target, change, result);
}
else
{
return PartialParseResult.Rejected;
}
}
private static bool EndsWithDot(string content)
{
return (content.Length == 1 && content[0] == '.') ||
(content[content.Length - 1] == '.' &&
content.Take(content.Length - 1).All(ParserHelpers.IsIdentifierPart));
}
private PartialParseResult HandleInsertionAfterDot(Span target, TextChange change)
{
// If the insertion is a full identifier or another dot, accept it
if (ParserHelpers.IsIdentifier(change.NewText) || change.NewText == ".")
{
return TryAcceptChange(target, change);
}
return PartialParseResult.Rejected;
}
private PartialParseResult TryAcceptChange(Span target, TextChange change, PartialParseResult acceptResult = PartialParseResult.Accepted)
{
string content = change.ApplyChange(target);
if (StartsWithKeyword(content))
{
return PartialParseResult.Rejected | PartialParseResult.SpanContextChanged;
}
return acceptResult;
}
private bool StartsWithKeyword(string newContent)
{
using (StringReader reader = new StringReader(newContent))
{
return Keywords.Contains(reader.ReadWhile(ParserHelpers.IsIdentifierPart));
}
}
}
}