Skip to content

Commit 820538f

Browse files
author
Kapil Borle
committed
Add method to find tokens with preceding whitespace
1 parent d9da319 commit 820538f

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Engine/TokenOperations.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
2222
public class TokenOperations
2323
{
2424
private readonly Token[] tokens;
25+
private LinkedList<Token> tokensLL;
2526
private readonly Ast ast;
2627

2728
/// <summary>
@@ -43,6 +44,7 @@ public TokenOperations(Token[] tokens, Ast ast)
4344

4445
this.tokens = tokens;
4546
this.ast = ast;
47+
this.tokensLL = new LinkedList<Token>();
4648
}
4749

4850
/// <summary>
@@ -105,5 +107,43 @@ private IEnumerable<Token> GetBraceInCommandElement(TokenKind tokenKind)
105107
}
106108
}
107109
}
110+
111+
private IEnumerable<LinkedListNode<Token>> GetTokens(TokenKind kind)
112+
{
113+
var token = tokensLL.First;
114+
do
115+
{
116+
if (token.Value.Kind == kind) {
117+
yield return token;
118+
}
119+
} while (token.Next != null);
120+
}
121+
122+
private IEnumerable<Tuple<Token, int>> GetTokenAndPrecedingWhitespace(TokenKind kind)
123+
{
124+
var lCurlyTokens = GetTokens(TokenKind.LCurly);
125+
foreach (var item in lCurlyTokens)
126+
{
127+
if (item.Previous == null
128+
|| !OnSameLine(item.Previous.Value, item.Value))
129+
{
130+
continue;
131+
}
132+
133+
yield return new Tuple<Token, int>(
134+
item.Value,
135+
item.Value.Extent.StartColumnNumber - item.Previous.Value.Extent.EndColumnNumber);
136+
}
137+
}
138+
139+
public IEnumerable<Tuple<Token, int>> GetOpenBracesWithWhiteSpacesBefore()
140+
{
141+
return GetTokenAndPrecedingWhitespace(TokenKind.LCurly);
142+
}
143+
144+
private bool OnSameLine(Token token1, Token token2)
145+
{
146+
return token1.Extent.StartLineNumber == token2.Extent.EndLineNumber;
147+
}
108148
}
109149
}

0 commit comments

Comments
 (0)