Skip to content

Instantly share code, notes, and snippets.

@controlflow
Created October 7, 2016 22:22
Show Gist options
  • Save controlflow/89acfd4b18808f6e0c1960ba27c287a9 to your computer and use it in GitHub Desktop.
Save controlflow/89acfd4b18808f6e0c1960ba27c287a9 to your computer and use it in GitHub Desktop.
public static bool IsInContantContext([NotNull] this ITreeNode context)
{
foreach (var containingNode in context.ContainingNodes())
{
switch (containingNode)
{
case IAttribute attribute:
case IConstantDeclaration constant:
case ILocalConstantDeclaration localConstant:
case IEnumMemberDeclaration enumMember:
case ISwitchLabelStatement switchLabel:
case IGotoCaseStatement gotoCase:
return true;
case IInterpolatedStringInsert insert:
var alignmentExpression = insert.AlignmentExpression;
if (alignmentExpression != null && alignmentExpression.Contains(context))
return true;
break;
}
}
return false;
}
public static bool IsInContantContext([NotNull] this ITreeNode context)
{
foreach (var containingNode in context.ContainingNodes())
{
if (containingNode is IAttribute) return true;
if (containingNode is IConstantDeclaration) return true;
if (containingNode is ILocalConstantDeclaration) return true;
if (containingNode is IEnumMemberDeclaration) return true;
if (containingNode is ISwitchLabelStatement) return true;
if (containingNode is IGotoCaseStatement) return true;
var interpolatedStringInsert = containingNode as IInterpolatedStringInsert;
if (interpolatedStringInsert != null)
{
var alignmentExpression = interpolatedStringInsert.AlignmentExpression;
if (alignmentExpression != null && alignmentExpression.Contains(context))
{
return true;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment