-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathScriptWriter.cs
More file actions
800 lines (698 loc) · 34.7 KB
/
Copy pathScriptWriter.cs
File metadata and controls
800 lines (698 loc) · 34.7 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//#define VISIBLE_WHITESPACE
//------------------------------------------------------------------------------
// <copyright file="ScriptWriter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Microsoft.SqlServer.TransactSql.ScriptDom.ScriptGenerator
{
/// <summary>
/// Writes tokens to a token stream which can then be outputted to text
/// </summary>
internal partial class ScriptWriter
{
#region private static members
private static NewLineElement _newLine = new NewLineElement();
private static TSqlParserToken _newLineToken = new TSqlParserToken(TSqlTokenType.WhiteSpace, Environment.NewLine);
#endregion
#region Private Fields
//private Int32 _indentation; // number of white space characters to be inserted for indentation
private SqlScriptGeneratorOptions _options;
private Dictionary<AlignmentPoint, AlignmentPointData> _alignmentPointDataMap; // AlignmentPoints to their AlignmentPointData for all alignment points
private Dictionary<String, AlignmentPoint> _alignmentPointNameMapForCurrentScope; // Name to AlignmentPoints for all pushes
private Stack<Dictionary<String, AlignmentPoint>> _alignmentPointNameMaps; // Name to AlignmentPoints for the alignment points found after the nearest push
private List<ScriptWriterElement> _scriptWriterElements;
private Stack<AlignmentPoint> _newLineAlignmentPoints;
#endregion
#region Public Constructors
public ScriptWriter(SqlScriptGeneratorOptions options)
{
_options = options;
_alignmentPointDataMap = new Dictionary<AlignmentPoint, AlignmentPointData>();
_alignmentPointNameMapForCurrentScope = new Dictionary<String, AlignmentPoint>();
_alignmentPointNameMaps = new Stack<Dictionary<String, AlignmentPoint>>();
_scriptWriterElements = new List<ScriptWriterElement>();
_newLineAlignmentPoints = new Stack<AlignmentPoint>();
}
#endregion
#region public methods
public void AddKeyword(TSqlTokenType keywordId)
{
String text = ScriptGeneratorSupporter.GetTokenString(keywordId, _options.KeywordCasing);
TSqlParserToken token = new TSqlParserToken(keywordId, text);
AddToken(token);
}
public void AddIdentifierWithCasing(String text)
{
ScriptGeneratorSupporter.CheckForNullReference(text, "text");
AddIdentifier(text, true);
}
public void AddIdentifierWithoutCasing(String text)
{
ScriptGeneratorSupporter.CheckForNullReference(text, "text");
AddIdentifier(text, false);
}
public void AddToken(TSqlParserToken token)
{
ScriptGeneratorSupporter.CheckForNullReference(token, "token");
AddTokenWrapper(new TokenWrapper(token));
}
public void NewLine()
{
AddNewLine();
// if we have some AlignmentPoints on stack, we set to the top one
if (_newLineAlignmentPoints.Count > 0)
{
// The top entry may be null when a named-alignment scope was pushed without its own
// newline-restoration point (see PushNamedAlignmentScope); nothing to restore then.
AlignmentPoint newLineAlignmentPoint = _newLineAlignmentPoints.Peek();
if (newLineAlignmentPoint != null)
{
Mark(newLineAlignmentPoint);
}
}
}
// Returns true if the most recently added meaningful token is a semicolon, ignoring any
// trailing whitespace, newlines and alignment points. Used to avoid emitting a redundant
// separating semicolon when the previous statement already ended with one.
public Boolean LastMeaningfulTokenIsSemicolon()
{
for (Int32 index = _scriptWriterElements.Count - 1; index >= 0; --index)
{
ScriptWriterElement element = _scriptWriterElements[index];
if (element.ElementType == ScriptWriterElementType.NewLine ||
element.ElementType == ScriptWriterElementType.AlignmentPoint)
{
continue;
}
TokenWrapper tokenWrapper = element as TokenWrapper;
if (tokenWrapper != null)
{
TSqlTokenType tokenType = tokenWrapper.Token.TokenType;
// Skip trailing comments so a semicolon emitted before them is still detected;
// otherwise the caller would append the separator into the comment text.
if (tokenType == TSqlTokenType.WhiteSpace ||
tokenType == TSqlTokenType.SingleLineComment ||
tokenType == TSqlTokenType.MultilineComment)
{
continue;
}
return tokenType == TSqlTokenType.Semicolon;
}
return false;
}
return false;
}
public void Indent(Int32 size)
{
AddSpace(size);
}
// In Tabs indentation mode, rewrite the leading whitespace of every line so that it uses
// tab characters only. The script is first generated (and aligned) entirely with spaces;
// this pass then replaces the leading run of spaces on each line with the number of tab
// stops needed to reach (rounding up) that column: ceil(leadingSpaces / IndentationSize)
// tabs, with no trailing spaces. Whitespace that appears after content on a line (mid-line
// alignment padding) is left untouched. This pass is a no-op unless UseTabsForIndentation
// is set (never when CommaPlacement is Leading).
private List<TSqlParserToken> ConvertLeadingWhitespaceToTabs(List<TSqlParserToken> tokens)
{
if (_options.UseTabsForIndentation == false || _options.IndentationSize <= 0)
{
return tokens;
}
Int32 size = _options.IndentationSize;
List<TSqlParserToken> result = new List<TSqlParserToken>(tokens.Count);
Boolean atLineStart = true;
Int32 leadingSpaces = 0;
for (Int32 index = 0; index < tokens.Count; ++index)
{
TSqlParserToken token = tokens[index];
// Whether the NEXT token starts a new line is determined by whether this token ENDS
// with a newline, not merely contains one: a multi-line block comment or string
// literal contains newlines but ends with content (e.g. "*/" or "'"), so tokens
// that follow it on the same line must not be treated as leading indentation.
Boolean endsWithNewLine = TokenEndsWithNewLine(token);
if (atLineStart && !endsWithNewLine && IsAllSpaces(token.Text))
{
// Accumulate the leading run of spaces (which may span multiple tokens).
leadingSpaces += token.Text.Length;
continue;
}
if (atLineStart)
{
// Reached the end of the leading whitespace run: emit it as tab characters,
// rounding up to the next tab stop so no trailing spaces remain.
if (leadingSpaces > 0)
{
Int32 tabs = (leadingSpaces + size - 1) / size;
result.Add(ScriptGeneratorSupporter.CreateTabToken(tabs));
leadingSpaces = 0;
}
}
result.Add(token);
atLineStart = endsWithNewLine;
}
return result;
}
private static Boolean TokenEndsWithNewLine(TSqlParserToken token)
{
String text = token.Text;
if (String.IsNullOrEmpty(text))
{
return false;
}
Char last = text[text.Length - 1];
return last == '\n' || last == '\r';
}
private static Boolean IsAllSpaces(String text)
{
if (String.IsNullOrEmpty(text))
{
return false;
}
foreach (Char c in text)
{
if (c != ' ')
{
return false;
}
}
return true;
}
// Tabs mode only. True when the element right after 'index' is a single-space separator
// token. Such an alignment point is a "field separator" (for example the gap between a
// clause keyword and its body, or between column-definition fields) whose gap is rendered
// with tabs and whose trailing space is then swallowed.
private Boolean IsFollowedBySeparatorSpace(Int32 index)
{
if (index + 1 >= _scriptWriterElements.Count)
{
return false;
}
TokenWrapper nextToken = _scriptWriterElements[index + 1] as TokenWrapper;
return nextToken != null && nextToken.Token != null && nextToken.Token.Text == " ";
}
// Tabs mode only. Computes where this alignment point snaps to a tab stop, once its space
// offset and its left neighbours' shifts are final. A field separator snaps to the first
// tab stop strictly past its (shifted) column; every other point simply inherits the shift
// of its left neighbours. TabShift records how far the snap moved the point versus the
// space layout, and is propagated rightward so later columns on the line stay aligned.
private void ComputeTabSnappedLayout(AlignmentPointData ap)
{
Int32 size = _options.IndentationSize;
if (size <= 0)
{
return;
}
if (ap.AbsorbsSeparator)
{
Int32 shiftedColumn = ap.Offset + ap.MaxLeftTabShift;
ap.TabTarget = ((shiftedColumn / size) + 1) * size;
ap.TabShift = ap.TabTarget - ap.Offset - 1;
}
else
{
ap.TabShift = ap.MaxLeftTabShift;
}
}
// Tabs mode only. When 'ap' is a field separator, emits its aligned gap as tab characters
// (advancing 'offset' to the precomputed tab-stop target) and asks the caller to swallow
// the single separator space that follows. Returns false in the default Spaces mode, or for
// points that are not tab-snapped, so the caller falls back to the normal space padding.
private Boolean TryEmitTabSnappedGap(AlignmentPointData ap, List<TSqlParserToken> tokens, ref Int32 offset, ref Boolean absorbSeparatorSpace)
{
if (_options.UseTabsForIndentation == false
|| _options.IndentationSize <= 0
|| ap.AbsorbsSeparator == false)
{
return false;
}
// The target already accounts for the cumulative shift introduced by any earlier
// tab-snapped points on this line, so columns stay aligned.
Int32 size = _options.IndentationSize;
// If the precomputed target is not ahead of the current offset (which can happen when
// earlier tab-snapped points on this line advanced 'offset' past this point's target),
// advance to the next tab stop from the current offset so 'offset' never moves backward
// and the tabs emitted actually land on the position we record.
Int32 target = ap.TabTarget;
if (target <= offset)
{
target = ((offset / size) + 1) * size;
}
Int32 tabCount = target / size - offset / size;
if (tabCount < 1)
{
tabCount = 1;
}
tokens.Add(ScriptGeneratorSupporter.CreateTabToken(tabCount));
offset = target;
absorbSeparatorSpace = true;
return true;
}
public void Mark(AlignmentPoint ap)
{
if (String.IsNullOrEmpty(ap.Name) == false &&
_alignmentPointNameMapForCurrentScope.ContainsKey(ap.Name) == false)
{
_alignmentPointNameMapForCurrentScope.Add(ap.Name, ap);
}
AddAlignmentPoint(ap);
}
// Add a comma-and-space separator that is right-aligned so that it ends exactly at the
// offset of the alignment point that immediately follows it. Used for leading comma
// placement in keyword-aligned lists (e.g. the SELECT column list).
public void AddRightAlignedCommaSeparator()
{
List<TSqlParserToken> tokens = new List<TSqlParserToken>(2)
{
new TSqlParserToken(TSqlTokenType.Comma, ScriptGeneratorSupporter.GetTokenString(TSqlTokenType.Comma, _options.KeywordCasing))
};
if (_options.LeadingCommaSpaceCount > 0)
{
tokens.Add(ScriptGeneratorSupporter.CreateWhitespaceToken(_options.LeadingCommaSpaceCount));
}
_scriptWriterElements.Add(new RightAlignedSeparatorElement(tokens));
}
public void PushNewLineAlignmentPoint(AlignmentPoint ap)
{
PushNewLineAlignmentPoint(ap, resetNameScope: true);
}
// When resetNameScope is true (the default) the current named-alignment-point scope is
// replaced with a fresh one, so named alignment points inside the pushed scope are
// independent of the outer scope (used e.g. for nested constructs). When false, the same
// named-alignment-point scope is kept, so named alignment points (e.g. column-definition
// field alignment) remain shared across the pushed scopes; this is needed when the push
// exists only to provide a newline-restoration point (comment preservation) and must not
// defeat cross-line alignment.
public void PushNewLineAlignmentPoint(AlignmentPoint ap, Boolean resetNameScope)
{
_newLineAlignmentPoints.Push(ap);
_alignmentPointNameMaps.Push(_alignmentPointNameMapForCurrentScope);
if (resetNameScope)
{
_alignmentPointNameMapForCurrentScope = new Dictionary<String, AlignmentPoint>();
}
}
public void PopNewLineAlignmentPoint()
{
_newLineAlignmentPoints.Pop();
_alignmentPointNameMapForCurrentScope = _alignmentPointNameMaps.Pop();
}
// Isolates the named-alignment-point scope for a list: named alignment points created after
// this call are shared among the list's items but do not leak into the enclosing scope, so
// separate lists rendered later in the same parent scope do not align against each other.
// Unlike PushNewLineAlignmentPoint(ap), this neither adds an alignment point at the current
// position nor changes newline restoration: the current newline-restoration point (if any)
// is reused so NewLine() behavior inside the scope is unchanged.
public void PushNamedAlignmentScope()
{
AlignmentPoint currentNewLineAlignmentPoint = _newLineAlignmentPoints.Count > 0 ? _newLineAlignmentPoints.Peek() : null;
PushNewLineAlignmentPoint(currentNewLineAlignmentPoint, resetNameScope: true);
}
public void PopNamedAlignmentScope()
{
PopNewLineAlignmentPoint();
}
public AlignmentPoint FindOrCreateAlignmentPoint(String name)
{
AlignmentPoint ap = null;
if (_alignmentPointNameMapForCurrentScope.TryGetValue(name, out ap) == false)
{
// may not be necessary, just want to make it explicit
ap = null;
}
if (ap == null)
{
ap = new AlignmentPoint(name);
}
return ap;
}
/// <summary>
/// Writes the textual contents of this script writer to the specified text writer
/// </summary>
/// <remarks>This method calls Dispose after completing to dispose of the script writer</remarks>
/// <param name="writer">The text writer to write contents to</param>
public void WriteTo(TextWriter writer)
{
List<TSqlParserToken> alignedTokens = TryGetAlignedTokens();
foreach (TSqlParserToken token in alignedTokens)
{
writer.Write(token.Text);
}
writer.Flush();
}
/// <summary>
/// Writes the tokens in this script writer to the specified list
/// </summary>
/// <remarks>This method calls Dispose after completing to dispose of the script writer</remarks>
/// <param name="tokens">A list of tokens to write the contents of this writer to</param>
public void WriteTo(IList<TSqlParserToken> tokens)
{
List<TSqlParserToken> alignedTokens = TryGetAlignedTokens();
foreach (TSqlParserToken token in alignedTokens)
{
tokens.Add(token);
}
}
#endregion
#region supporting methods
private void AddIdentifier(String text, Boolean applyCasing)
{
if (applyCasing)
{
text = ScriptGeneratorSupporter.GetCasedString(text, _options.KeywordCasing);
}
TSqlParserToken token = new TSqlParserToken(TSqlTokenType.Identifier, text);
AddToken(token);
}
private void AddSpace(int count)
{
AddToken(ScriptGeneratorSupporter.CreateWhitespaceToken(count));
}
private void AddTokenWrapper(TokenWrapper token)
{
_scriptWriterElements.Add(token);
}
#if DEBUG
HashSet<AlignmentPoint> _alignmentPointsForCurrentLine = new HashSet<AlignmentPoint>();
#endif
private void AddAlignmentPoint(AlignmentPoint ap)
{
#if DEBUG
if (_alignmentPointsForCurrentLine.Contains(ap))
{
Debug.Assert(false, "Duplicated alignment points found in the same line");
}
_alignmentPointsForCurrentLine.Add(ap);
#endif
_scriptWriterElements.Add(FindOrCreateAlignmentPointData(ap));
}
private void AddNewLine()
{
#if DEBUG
_alignmentPointsForCurrentLine.Clear();
#endif
_scriptWriterElements.Add(_newLine);
}
private ScriptWriterElement FindOrCreateAlignmentPointData(AlignmentPoint ap)
{
AlignmentPointData apd;
if (_alignmentPointDataMap.TryGetValue(ap, out apd) == false)
{
apd = new AlignmentPointData(ap.Name);
_alignmentPointDataMap.Add(ap, apd);
}
return apd;
}
// try to return the aligned tokens
// return unaligned tokens if failing aligning the tokens
private List<TSqlParserToken> TryGetAlignedTokens()
{
List<TSqlParserToken> result = Align();
if (result == null)
result = GetAllTokens();
// Tabs mode only post-pass; returns 'result' unchanged in the default Spaces mode.
return ConvertLeadingWhitespaceToTabs(result);
}
private List<TSqlParserToken> Align()
{
// keep all alignment points
HashSet<AlignmentPointData> allPoints = new HashSet<AlignmentPointData>();
// find out the width for each alignment point and populate relationship among alignment points
Int32 width = 0; // keep the width between two alignment points
AlignmentPointData previousPoint = null;
for (Int32 index = 0; index < _scriptWriterElements.Count; ++index)
{
ScriptWriterElement element = _scriptWriterElements[index];
switch (element.ElementType)
{
case ScriptWriterElementType.AlignmentPoint:
AlignmentPointData ap = element as AlignmentPointData;
#if !PIMODLANGUAGE
Debug.Assert(ap != null, "AlignmentPointData is expected");
#endif
allPoints.Add(ap);
if (previousPoint != null)
{
// this is not the first alignment point of the current line, so establish the relationships
ap.AddLeftPoint(previousPoint, width);
previousPoint.AddRightPoint(ap);
}
else
{
// this is the first alignment point of the current line, so the width is also its offset
ap.Offset = Math.Max(ap.Offset, width);
}
// Tabs mode only: record whether this point is a field separator so its
// gap can later be rendered as tabs. In the default Spaces mode this is
// skipped and AbsorbsSeparator stays false, so nothing downstream changes.
if (_options.UseTabsForIndentation)
{
// AlignmentPointData is shared across every occurrence of the same
// alignment point, so OR the flag instead of overwriting it: the point
// is snapped to a tab stop if it is a field separator on ANY line. The
// per-occurrence decision to actually emit the tab gap is made during
// emission below.
ap.AbsorbsSeparator |= IsFollowedBySeparatorSpace(index);
}
width = 0;
previousPoint = ap;
break;
case ScriptWriterElementType.Token:
TokenWrapper tokenWrapper = element as TokenWrapper;
Debug.Assert(tokenWrapper != null, "TokenWrapper is expected");
Debug.Assert(tokenWrapper.Token.Text != null, "TokenWrapper.Token.Text should not be null");
if (tokenWrapper != null && tokenWrapper.Token != null && tokenWrapper.Token.Text != null)
{
width += tokenWrapper.Token.Text.Length;
}
break;
case ScriptWriterElementType.RightAlignedSeparator:
// The separator occupies its own width between the previous alignment
// point and the following one (which it is right-aligned against).
RightAlignedSeparatorElement separatorWidthElement = element as RightAlignedSeparatorElement;
Debug.Assert(separatorWidthElement != null, "RightAlignedSeparatorElement is expected");
if (separatorWidthElement != null)
{
width += separatorWidthElement.Width;
}
break;
case ScriptWriterElementType.NewLine:
Debug.Assert(element is NewLineElement, "NewLineElement is expected");
width = 0;
previousPoint = null;
break;
default:
Debug.Assert(false, "Unknown ScriptWriterElement type");
break;
}
}
// we have established previous-next relationships among all alignmnet points
// now, we perform the alignment
while (true)
{
if (allPoints.Count == 0)
{
// all the alignment points have been aligned, so we are done
break;
}
AlignmentPointData ap = FindOneAlignmentPointWithOutDependent(allPoints);
if (ap == null)
{
// if we can't find any, we have a circle among alignment points
return null;
}
// Tabs mode only: compute where this point snaps to a tab stop, now that its
// offset and its left neighbours' shifts are final. Skipped in the default Spaces
// mode, where the offsets computed above are used exactly as-is.
if (_options.UseTabsForIndentation)
{
ComputeTabSnappedLayout(ap);
}
// let's align ap up
HashSet<AlignmentPointData> rightPoints = ap.RightPoints;
foreach (AlignmentPointData rightPoint in rightPoints)
{
rightPoint.AlignAndRemoveLeftPoint(ap);
// Tabs mode only: carry this point's tab shift to its right neighbours so they
// stay aligned once earlier gaps have been snapped to tab stops.
if (_options.UseTabsForIndentation)
{
rightPoint.MaxLeftTabShift = Math.Max(rightPoint.MaxLeftTabShift, ap.TabShift);
}
}
// ap is done; let's remove it;
allPoints.Remove(ap);
}
// generate aligned token stream
List<TSqlParserToken> tokens = new List<TSqlParserToken>(_scriptWriterElements.Count);
Int32 offset = 0;
RightAlignedSeparatorElement pendingSeparator = null;
// When a mid-line alignment point has just been snapped to a tab stop in Tabs mode, the
// single separator space the generator emits after it is redundant and is swallowed so
// the following content follows the tab run directly.
Boolean absorbSeparatorSpace = false;
for (Int32 index = 0; index < _scriptWriterElements.Count; ++index)
{
ScriptWriterElement element = _scriptWriterElements[index];
switch (element.ElementType)
{
case ScriptWriterElementType.AlignmentPoint:
AlignmentPointData ap = element as AlignmentPointData;
#if !PIMODLANGUAGE
Debug.Assert(ap != null, "AlignmentPointData is expected");
#endif
// In Tabs mode a clause body can be snapped forward onto a tab stop, which
// can leave 'offset' ahead of a later alignment point's precomputed offset.
Debug.Assert(_options.UseTabsForIndentation || ap.Offset >= offset, "Incorrect offset");
if (pendingSeparator != null)
{
// Right-align the buffered separator so that it ends exactly at this
// alignment point's offset: pad up to (offset - separatorWidth), then
// emit the separator so the following item starts at the aligned column.
Int32 padBeforeSeparator = ap.Offset - offset - pendingSeparator.Width;
if (padBeforeSeparator > 0)
{
tokens.Add(ScriptGeneratorSupporter.CreateWhitespaceToken(padBeforeSeparator));
offset += padBeforeSeparator;
}
foreach (TSqlParserToken separatorToken in pendingSeparator.Tokens)
{
tokens.Add(separatorToken);
offset += separatorToken.Text.Length;
}
pendingSeparator = null;
}
// Tabs mode only: if THIS occurrence is followed by the single separator
// space token, emit its aligned gap as tabs and skip the default space
// padding below. The per-occurrence IsFollowedBySeparatorSpace check makes
// occurrences of a shared alignment point that are not separators fall
// through to normal padding, even though the shared AbsorbsSeparator flag is
// true. Skipped in the default Spaces mode, so that path runs unchanged.
if (_options.UseTabsForIndentation
&& IsFollowedBySeparatorSpace(index)
&& TryEmitTabSnappedGap(ap, tokens, ref offset, ref absorbSeparatorSpace))
{
break;
}
if (ap.Offset > offset)
{
tokens.Add(ScriptGeneratorSupporter.CreateWhitespaceToken(ap.Offset - offset));
}
// Use Math.Max because emitting a right-aligned separator above can advance
// 'offset' to exactly ap.Offset; never move the offset backwards here.
offset = Math.Max(offset, ap.Offset);
break;
case ScriptWriterElementType.Token:
FlushPendingSeparator(tokens, ref pendingSeparator, ref offset);
TokenWrapper tokenWrapper = element as TokenWrapper;
Debug.Assert(tokenWrapper != null, "TokenWrapper is expected");
Debug.Assert(tokenWrapper.Token.Text != null, "TokenWrapper.Token.Text should not be null");
if (tokenWrapper != null && tokenWrapper.Token != null && tokenWrapper.Token.Text != null)
{
// Swallow the single separator space that follows a tab-snapped alignment point.
if (absorbSeparatorSpace && tokenWrapper.Token.Text == " ")
{
absorbSeparatorSpace = false;
break;
}
absorbSeparatorSpace = false;
tokens.Add(tokenWrapper.Token);
offset += tokenWrapper.Token.Text.Length;
}
break;
case ScriptWriterElementType.RightAlignedSeparator:
// Defer emission until the following alignment point is known so the
// separator can be right-aligned against it.
FlushPendingSeparator(tokens, ref pendingSeparator, ref offset);
pendingSeparator = element as RightAlignedSeparatorElement;
break;
case ScriptWriterElementType.NewLine:
FlushPendingSeparator(tokens, ref pendingSeparator, ref offset);
Debug.Assert(element is NewLineElement, "NewLineElement is expected");
tokens.Add(_newLineToken);
offset = 0;
absorbSeparatorSpace = false;
break;
default:
Debug.Assert(false, "Unknown ScriptWriterElement type");
break;
}
}
FlushPendingSeparator(tokens, ref pendingSeparator, ref offset);
return tokens;
}
// Emit a buffered right-aligned separator inline (used when it is not immediately
// followed by an alignment point to right-align against).
private static void FlushPendingSeparator(List<TSqlParserToken> tokens, ref RightAlignedSeparatorElement pendingSeparator, ref Int32 offset)
{
if (pendingSeparator == null)
{
return;
}
foreach (TSqlParserToken separatorToken in pendingSeparator.Tokens)
{
tokens.Add(separatorToken);
offset += separatorToken.Text.Length;
}
pendingSeparator = null;
}
// get all tokens without alignment
private List<TSqlParserToken> GetAllTokens()
{
List<TSqlParserToken> tokens = new List<TSqlParserToken>();
for (Int32 index = 0; index < _scriptWriterElements.Count; ++index)
{
ScriptWriterElement element = _scriptWriterElements[index];
switch (element.ElementType)
{
case ScriptWriterElementType.Token:
TokenWrapper tokenWrapper = element as TokenWrapper;
Debug.Assert(tokenWrapper != null, "TokenWrapper is expected");
tokens.Add(tokenWrapper.Token);
break;
case ScriptWriterElementType.RightAlignedSeparator:
RightAlignedSeparatorElement separatorElement = element as RightAlignedSeparatorElement;
Debug.Assert(separatorElement != null, "RightAlignedSeparatorElement is expected");
if (separatorElement != null)
{
foreach (TSqlParserToken separatorToken in separatorElement.Tokens)
{
tokens.Add(separatorToken);
}
}
break;
case ScriptWriterElementType.NewLine:
Debug.Assert(element is NewLineElement, "NewLineElement is expected");
tokens.Add(_newLineToken);
break;
case ScriptWriterElementType.AlignmentPoint:
// we don't do anything for the alignement points
break;
default:
Debug.Assert(false, "Unknown ScriptWriterElement type");
break;
}
}
return tokens;
}
private static AlignmentPointData FindOneAlignmentPointWithOutDependent(HashSet<AlignmentPointData> points)
{
AlignmentPointData value = null;
foreach (var item in points)
{
if (item.HasNoLeftPoints)
{
value = item;
break;
}
}
return value;
}
#endregion
}
}