本文整理汇总了C#中SyntaxTree类的典型用法代码示例。如果您正苦于以下问题:C# SyntaxTree类的具体用法?C# SyntaxTree怎么用?C# SyntaxTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SyntaxTree类属于命名空间,在下文中一共展示了SyntaxTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BeginsWithAutoGeneratedComment
private static bool BeginsWithAutoGeneratedComment(SyntaxTree tree, Func<SyntaxTrivia, bool> isComment, CancellationToken cancellationToken)
{
var root = tree.GetRoot(cancellationToken);
if (root.HasLeadingTrivia)
{
var leadingTrivia = root.GetLeadingTrivia();
foreach (var trivia in leadingTrivia)
{
if (!isComment(trivia))
{
continue;
}
var text = trivia.ToString();
// Check to see if the text of the comment contains an auto generated comment.
foreach (var autoGenerated in s_autoGeneratedStrings)
{
if (text.Contains(autoGenerated))
{
return true;
}
}
}
}
return false;
}
示例2: PathSyntaxReference
public PathSyntaxReference(SyntaxNode node)
{
_tree = node.SyntaxTree;
_kind = node.Kind();
_textSpan = node.Span;
_pathFromRoot = ComputePathFromRoot(node);
}
示例3: GetClassificationForToken
/// <summary>
/// Determine the classification type for a given token.
/// </summary>
/// <param name="classificationTypes">A classification service to retrieve classification types.</param>
/// <param name="token">The token.</param>
/// <param name="syntaxTree">The tree containing the token (can be null for tokens that are
/// unparented).</param>
/// <returns>The correct syntactic classification for the token.</returns>
public static IClassificationType GetClassificationForToken(this IClassificationTypes classificationTypes, SyntaxToken token, SyntaxTree syntaxTree)
{
if (SyntaxFacts.IsKeywordKind(token.Kind))
{
return classificationTypes.Keyword;
}
else if (token.Kind.IsPunctuation())
{
return GetClassificationForPunctuation(classificationTypes, token);
}
else if (token.Kind == SyntaxKind.IdentifierToken)
{
return GetClassificationForIdentifer(classificationTypes, token, syntaxTree);
}
else if (token.Kind == SyntaxKind.StringLiteralToken || token.Kind == SyntaxKind.CharacterLiteralToken)
{
return token.IsVerbatimStringLiteral()
? classificationTypes.VerbatimStringLiteral
: classificationTypes.StringLiteral;
}
else if (token.Kind == SyntaxKind.NumericLiteralToken)
{
return classificationTypes.NumericLiteral;
}
return null;
}
示例4: GetClassificationForIdentifer
private static IClassificationType GetClassificationForIdentifer(IClassificationTypes classificationTypes, SyntaxToken token, SyntaxTree syntaxTree)
{
if (token.Parent is TypeDeclarationSyntax &&
((token.Parent as TypeDeclarationSyntax).Identifier == token))
{
return GetClassificationForTypeDeclarationIdentifier(classificationTypes, token);
}
else if (token.Parent is EnumDeclarationSyntax &&
(token.Parent as EnumDeclarationSyntax).Identifier == token)
{
return classificationTypes.EnumTypeName;
}
else if (token.Parent is DelegateDeclarationSyntax &&
(token.Parent as DelegateDeclarationSyntax).Identifier == token)
{
return classificationTypes.DelegateTypeName;
}
else if (token.Parent is TypeParameterSyntax &&
(token.Parent as TypeParameterSyntax).Identifier == token)
{
return classificationTypes.TypeParameterName;
}
else if (syntaxTree != null && (syntaxTree.IsActualContextualKeyword(token) || syntaxTree.CouldBeVarKeywordInDeclaration(token)))
{
return classificationTypes.Keyword;
}
else
{
return classificationTypes.Identifier;
}
}
示例5: GetIndenter
protected override AbstractIndenter GetIndenter(
ISyntaxFactsService syntaxFacts, SyntaxTree syntaxTree, TextLine lineToBeIndented, IEnumerable<IFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken)
{
return new Indenter(
syntaxFacts, syntaxTree, formattingRules,
optionSet, lineToBeIndented, cancellationToken);
}
示例6: Init
void Init(string code)
{
syntaxTree = SyntaxTree.Parse(code, "test.cs");
unresolvedFile = syntaxTree.ToTypeSystem();
compilation = TypeSystemHelper.CreateCompilation(unresolvedFile);
findReferences = new FindReferences();
}
示例7: GetRegions
private IEnumerable<OutliningSpan> GetRegions(SyntaxTree syntaxTree, SyntaxTrivia trivia)
{
var outliner = new DisabledTextTriviaOutliner();
var spans = new List<OutliningSpan>();
outliner.CollectOutliningSpans(syntaxTree, trivia, spans, CancellationToken.None);
return spans;
}
示例8: Resolve
public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location, out AstNode node,
CancellationToken cancellationToken = default(CancellationToken))
{
node = syntaxTree.GetNodeAt(location);
if (node == null || node is ArrayInitializerExpression)
return null;
if (node.Parent is UsingAliasDeclaration && node.Role == UsingAliasDeclaration.AliasRole) {
var r = new CSharpAstResolver(compilation.Value, syntaxTree, unresolvedFile);
return r.Resolve(((UsingAliasDeclaration)node.Parent).Import, cancellationToken);
}
if (CSharpAstResolver.IsUnresolvableNode(node)) {
if (node is Identifier) {
node = node.Parent;
} else if (node.NodeType == NodeType.Token) {
if (node.Parent is IndexerExpression || node.Parent is ConstructorInitializer) {
// There's no other place where one could hover to see the indexer's tooltip,
// so we need to resolve it when hovering over the '[' or ']'.
// For constructor initializer, the same applies to the 'base'/'this' token.
node = node.Parent;
} else {
return null;
}
} else {
// don't resolve arbitrary nodes - we don't want to show tooltips for everything
return null;
}
} else {
// It's a resolvable node.
// However, we usually don't want to show the tooltip everywhere
// For example, hovering with the mouse over an empty line between two methods causes
// node==TypeDeclaration, but we don't want to show any tooltip.
if (!node.GetChildByRole(Roles.Identifier).IsNull) {
// We'll suppress the tooltip for resolvable nodes if there is an identifier that
// could be hovered over instead:
return null;
}
}
if (node == null)
return null;
if (node.Parent is ObjectCreateExpression && node.Role == Roles.Type) {
node = node.Parent;
}
InvocationExpression parentInvocation = null;
if ((node is IdentifierExpression || node is MemberReferenceExpression || node is PointerReferenceExpression) && node.Role != Roles.Argument) {
// we also need to resolve the invocation
parentInvocation = node.Parent as InvocationExpression;
}
// TODO: I think we should provide an overload so that an existing CSharpAstResolver can be reused
CSharpAstResolver resolver = new CSharpAstResolver(compilation.Value, syntaxTree, unresolvedFile);
ResolveResult rr = resolver.Resolve(node, cancellationToken);
if (rr is MethodGroupResolveResult && parentInvocation != null)
return resolver.Resolve(parentInvocation);
else
return rr;
}
示例9: Create
public static StringSplitter Create(
Document document, int position,
SyntaxTree syntaxTree, SyntaxNode root, SourceText sourceText,
bool useTabs, int tabSize, CancellationToken cancellationToken)
{
var token = root.FindToken(position);
if (token.IsKind(SyntaxKind.StringLiteralToken))
{
return new SimpleStringSplitter(
document, position, syntaxTree, root,
sourceText, token, useTabs, tabSize,
cancellationToken);
}
var interpolatedStringExpression = TryGetInterpolatedStringExpression(token, position);
if (interpolatedStringExpression != null)
{
return new InterpolatedStringSplitter(
document, position, syntaxTree, root,
sourceText, interpolatedStringExpression,
useTabs, tabSize, cancellationToken);
}
return null;
}
示例10: setSource
public void setSource(string source)
{
this.source = source;
this.tree = ASTUtil.GetSyntaxTreeFromSource(source);
methods = ASTUtil.GetAllMethodDeclarations(tree);
blocks = new List<string>();
methodNames = new List<string>();
foreach (MethodDeclarationSyntax method in methods)
{
SyntaxNode block = ASTUtil.GetBlockOfMethod(method);
if(block != null)
{
StringBuilder sb = new StringBuilder();
IEnumerable<SyntaxNode> stats = ASTUtil.GetStatementsInNode(block);
foreach(StatementSyntax st in stats)
{
sb.AppendLine(st.GetText());
}
blocks.Add(sb.ToString());
}
else
{
blocks.Add("");
}
methodNames.Add(method.Identifier.GetText());
}
}
示例11: AbstractIndenter
public AbstractIndenter(
ISyntaxFactsService syntaxFacts,
SyntaxTree syntaxTree,
IEnumerable<IFormattingRule> rules,
OptionSet optionSet,
TextLine lineToBeIndented,
CancellationToken cancellationToken)
{
var syntaxRoot = syntaxTree.GetRoot(cancellationToken);
this._syntaxFacts = syntaxFacts;
this.OptionSet = optionSet;
this.Tree = syntaxTree;
this.LineToBeIndented = lineToBeIndented;
this.TabSize = this.OptionSet.GetOption(FormattingOptions.TabSize, syntaxRoot.Language);
this.CancellationToken = cancellationToken;
this.Rules = rules;
this.Finder = new BottomUpBaseIndentationFinder(
new ChainedFormattingRules(this.Rules, OptionSet),
this.TabSize,
this.OptionSet.GetOption(FormattingOptions.IndentationSize, syntaxRoot.Language),
tokenStream: null,
lastToken: default(SyntaxToken));
}
示例12: GetSpanIn
private TextSpan GetSpanIn(SyntaxTree syntaxTree, string textToFind)
{
string s = syntaxTree.GetText().ToString();
int index = s.IndexOf(textToFind);
Assert.True(index >= 0, "textToFind not found in the tree");
return new TextSpan(index, textToFind.Length);
}
示例13: visit
public override void visit(SyntaxTree.var_statement defs)
{
indef = true;
ProcessNode(defs.var_def.vars); // исключаем типы -
// просматриваем только имена переменных
indef = false;
}
示例14: ToolTipData
public ToolTipData (ICSharpCode.NRefactory.CSharp.SyntaxTree unit, ICSharpCode.NRefactory.Semantics.ResolveResult result, ICSharpCode.NRefactory.CSharp.AstNode node, CSharpAstResolver file)
{
this.Unit = unit;
this.Result = result;
this.Node = node;
this.Resolver = file;
}
示例15: AssertMappedSpanEqual
private void AssertMappedSpanEqual(
SyntaxTree syntaxTree,
string sourceText,
string expectedPath,
int expectedStartLine,
int expectedStartOffset,
int expectedEndLine,
int expectedEndOffset,
bool hasMappedPath)
{
var span = GetSpanIn(syntaxTree, sourceText);
var mappedSpan = syntaxTree.GetMappedLineSpan(span);
var actualDisplayPath = syntaxTree.GetDisplayPath(span, s_resolver);
Assert.Equal(hasMappedPath, mappedSpan.HasMappedPath);
Assert.Equal(expectedPath, mappedSpan.Path);
if (expectedPath == "")
{
Assert.Equal("", actualDisplayPath);
}
else
{
Assert.Equal(string.Format("[{0};{1}]", expectedPath, hasMappedPath ? syntaxTree.FilePath : null), actualDisplayPath);
}
Assert.Equal(expectedStartLine, mappedSpan.StartLinePosition.Line);
Assert.Equal(expectedStartOffset, mappedSpan.StartLinePosition.Character);
Assert.Equal(expectedEndLine, mappedSpan.EndLinePosition.Line);
Assert.Equal(expectedEndOffset, mappedSpan.EndLinePosition.Character);
}