Skip to content

Commit a6ebc83

Browse files
Make using statements in util language agnostic
1 parent 8004d3b commit a6ebc83

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

CodeConverter/Util/SyntaxExtensions.cs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,56 @@
33
using System.Linq;
44
using System.Threading;
55
using Microsoft.CodeAnalysis;
6-
using Microsoft.CodeAnalysis.CSharp;
7-
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
6+
using CS = Microsoft.CodeAnalysis.CSharp;
7+
using VBasic = Microsoft.CodeAnalysis.VisualBasic;
8+
using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
89

910
namespace ICSharpCode.CodeConverter.Util
1011
{
1112
internal static class SyntaxExtensions
1213
{
13-
1414
/// <summary>
1515
/// return only skipped tokens
1616
/// </summary>
1717
private static IEnumerable<SyntaxToken> GetSkippedTokens(SyntaxTriviaList list)
1818
{
19-
return list.Where(trivia => trivia.RawKind == (int)SyntaxKind.SkippedTokensTrivia)
20-
.SelectMany(t => ((SkippedTokensTriviaSyntax)t.GetStructure()).Tokens);
19+
return list.Where(trivia => trivia.RawKind == (int)CS.SyntaxKind.SkippedTokensTrivia)
20+
.SelectMany(t => ((VBSyntax.SkippedTokensTriviaSyntax)t.GetStructure()).Tokens);
2121
}
2222

23-
public static Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax SkipParens(this Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax expression)
23+
public static CS.Syntax.ExpressionSyntax SkipParens(this CS.Syntax.ExpressionSyntax expression)
2424
{
2525
if (expression == null)
2626
return null;
27-
while (expression is Microsoft.CodeAnalysis.CSharp.Syntax.ParenthesizedExpressionSyntax pes) {
27+
while (expression is CS.Syntax.ParenthesizedExpressionSyntax pes) {
2828
expression = pes.Expression;
2929
}
3030
return expression;
3131
}
3232

33-
public static Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax SkipParens(this Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax expression)
33+
public static VBSyntax.ExpressionSyntax SkipParens(this VBSyntax.ExpressionSyntax expression)
3434
{
3535
if (expression == null)
3636
return null;
37-
while (expression is Microsoft.CodeAnalysis.VisualBasic.Syntax.ParenthesizedExpressionSyntax pes) {
37+
while (expression is VBSyntax.ParenthesizedExpressionSyntax pes) {
3838
expression = pes.Expression;
3939
}
4040
return expression;
4141
}
4242

43-
public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind)
43+
public static bool IsParentKind(this SyntaxNode node, CS.SyntaxKind kind)
4444
{
4545
return node != null && node.Parent.IsKind(kind);
4646
}
4747

48-
public static bool IsParentKind(this SyntaxToken node, SyntaxKind kind)
48+
public static bool IsParentKind(this SyntaxNode node, VBasic.SyntaxKind kind)
49+
{
50+
return node?.Parent.IsKind(kind) == true;
51+
}
52+
53+
public static bool IsParentKind(this SyntaxToken node, CS.SyntaxKind kind)
4954
{
50-
return node.Parent != null && node.Parent.IsKind(kind);
55+
return node.Parent?.IsKind(kind) == true;
5156
}
5257

5358
public static TSymbol GetEnclosingSymbol<TSymbol>(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
@@ -74,28 +79,28 @@ internal static bool IsValidSymbolInfo(ISymbol symbol)
7479
private static SyntaxNode FindImmediatelyEnclosingLocalVariableDeclarationSpace(SyntaxNode syntax)
7580
{
7681
for (var declSpace = syntax; declSpace != null; declSpace = declSpace.Parent) {
77-
switch (declSpace.Kind()) {
82+
switch (CS.CSharpExtensions.Kind(declSpace)) {
7883
// These are declaration-space-defining syntaxes, by the spec:
79-
case SyntaxKind.MethodDeclaration:
80-
case SyntaxKind.IndexerDeclaration:
81-
case SyntaxKind.OperatorDeclaration:
82-
case SyntaxKind.ConstructorDeclaration:
83-
case SyntaxKind.Block:
84-
case SyntaxKind.ParenthesizedLambdaExpression:
85-
case SyntaxKind.SimpleLambdaExpression:
86-
case SyntaxKind.AnonymousMethodExpression:
87-
case SyntaxKind.SwitchStatement:
88-
case SyntaxKind.ForEachKeyword:
89-
case SyntaxKind.ForStatement:
90-
case SyntaxKind.UsingStatement:
84+
case CS.SyntaxKind.MethodDeclaration:
85+
case CS.SyntaxKind.IndexerDeclaration:
86+
case CS.SyntaxKind.OperatorDeclaration:
87+
case CS.SyntaxKind.ConstructorDeclaration:
88+
case CS.SyntaxKind.Block:
89+
case CS.SyntaxKind.ParenthesizedLambdaExpression:
90+
case CS.SyntaxKind.SimpleLambdaExpression:
91+
case CS.SyntaxKind.AnonymousMethodExpression:
92+
case CS.SyntaxKind.SwitchStatement:
93+
case CS.SyntaxKind.ForEachKeyword:
94+
case CS.SyntaxKind.ForStatement:
95+
case CS.SyntaxKind.UsingStatement:
9196

9297
// SPEC VIOLATION: We also want to stop walking out if, say, we are in a field
9398
// initializer. Technically according to the wording of the spec it should be
9499
// legal to use a simple name inconsistently inside a field initializer because
95100
// it does not define a local variable declaration space. In practice of course
96101
// we want to check for that. (As the native compiler does as well.)
97102

98-
case SyntaxKind.FieldDeclaration:
103+
case CS.SyntaxKind.FieldDeclaration:
99104
return declSpace;
100105
}
101106
}

0 commit comments

Comments
 (0)