-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBracketedExpressionToken.cs
69 lines (62 loc) · 2.01 KB
/
BracketedExpressionToken.cs
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
// Written by Joseph Albahari, LINQPad. See license.txt
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExpressionFormatter
{
/// <summary>
/// An ExpressionToken, that when formatted, is surrounded in brackets.
/// </summary>
class BracketedExpressionToken : ExpressionToken
{
public readonly string OpenBracket, CloseBracket;
public readonly ExpressionToken Body;
public bool NewLineBefore;
public bool OmitBracketsForSingle;
public BracketedExpressionToken (string openBracket, string closeBracket, ExpressionToken body)
: this (openBracket, closeBracket, false, body)
{
}
public BracketedExpressionToken (string openBracket, string closeBracket, bool omitBracketsForSingle, ExpressionToken body)
{
OpenBracket = openBracket;
CloseBracket = closeBracket;
OmitBracketsForSingle = omitBracketsForSingle;
Body = body;
}
public override void Write (StringBuilder sb, int indent)
{
bool single = !(Body is CompositeExpressionToken) || ((CompositeExpressionToken)Body).Tokens.Count () == 1;
if (!OmitBracketsForSingle || NewLineBefore) single = false;
int openIndent = indent;
if (NewLineBefore)
{
WriteNewLine (sb, indent);
openIndent = indent;
sb.Append (OpenBracket);
}
bool alreadyIndented = sb.Length > 2 && char.IsWhiteSpace (sb[sb.Length - 1]) && char.IsWhiteSpace (sb[sb.Length - 2]);
if (Body.MultiLine) indent++;
if (NewLineBefore || alreadyIndented)
WriteNewLine (sb, indent);
if (!NewLineBefore && !single)
{
sb.Append (OpenBracket);
openIndent = indent;
}
Body.Write (sb, indent + Body.SplitIndent);
if (Body.MultiLine) WriteNewLine (sb, openIndent);
if (!single) sb.Append (CloseBracket);
}
public override int Length
{
get { return Body.Length + OpenBracket.Length + CloseBracket.Length; }
}
public override bool MultiLine
{
get { return Body.MultiLine || NewLineBefore; }
set { }
}
}
}