本文整理汇总了C#中IDocument.GetSyntaxTree方法的典型用法代码示例。如果您正苦于以下问题:C# IDocument.GetSyntaxTree方法的具体用法?C# IDocument.GetSyntaxTree怎么用?C# IDocument.GetSyntaxTree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDocument
的用法示例。
在下文中一共展示了IDocument.GetSyntaxTree方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetRefactoring
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
{
// Retrieve the token containing textSpan
var syntaxTree = document.GetSyntaxTree(cancellationToken);
var token = syntaxTree.GetRoot(cancellationToken).FindToken(textSpan.Start);
if (token.Parent == null) {
return null;
}
var classDeclaration = token.Parent.FirstAncestorOrSelf<ClassDeclarationSyntax>();
if (classDeclaration == null) return null;
var constructorDeclarations = (
from constructor in classDeclaration.DescendantNodes().OfType<ConstructorDeclarationSyntax>()
where constructor.ParameterList.Parameters.Count > 0
select constructor
).ToList();
if (!constructorDeclarations.Any()) return null;
var actions = constructorDeclarations
.SelectMany(x => x.ParameterList.Parameters)
.Select(p=>new AdaptInterfaceCodeAction(document, classDeclaration,p))
.ToList();
return new CodeRefactoring(
actions: actions,
textSpan:classDeclaration.Identifier.Span
);
}
示例2: GetRefactoring
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
{
var syntaxTree = (SyntaxTree) document.GetSyntaxTree(cancellationToken);
var token = syntaxTree.Root.FindToken(textSpan.Start);
if (token.Parent==null)
{
return null;
}
var fds = token.Parent.FirstAncestorOrSelf<FieldDeclarationSyntax>();
//check that we have a field
if (fds == null)
return null;
// Retrieve the class declaration of the specified member.
var theclass = fds.Parent as ClassDeclarationSyntax;
// Find the parent class
var parentClass = GetParentClass(syntaxTree, theclass);
//we must have a parent class, otherwise we cannot move the member
if (parentClass==null)
{
return null;
}
//trigger the refactoring
return new CodeRefactoring(
new[] { new CodeAction(editFactory, document, fds,parentClass) },
fds.Span);
}
示例3: GetIssues
public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
{
var classNode = (ClassDeclarationSyntax) node;
if (classNode.BaseListOpt == null) return null;
var walker = new MilSyntaxWalker();
walker.Visit((SyntaxNode) document.GetSyntaxTree(cancellationToken).Root);
var allCommandHandlersInProject = walker.CommandHandlers;
if (!allCommandHandlersInProject.Any() && !allCommandHandlersInProject.Contains(node)) return null;
var dupes = walker.CommandHandlers.SelectMany(x => x.BaseListOpt.Types).FindDuplicates();
if (!dupes.Any())
{
var desc = "{0} is implemented by multiple handlers:{1}{2}";
var issues = new List<CodeIssue>();
foreach (var dupe in dupes)
{
var listing = FormatHandlerListing(dupe.GetClassName(), allCommandHandlersInProject.ToDictionary(x => x, syntax => syntax.BaseListOpt.Types.OfType<GenericNameSyntax>()));
var text = string.Format(desc, dupe, Environment.NewLine, string.Join(Environment.NewLine, listing));
issues.Add(new CodeIssue(CodeIssue.Severity.Warning, classNode.Identifier.FullSpan, text));
}
return issues;
}
return null;
//if (allCommandHandlersInProject.Contains(node as ClassDeclarationSyntax))
//{
// return new CodeIssue[] { new CodeIssue(CodeIssue.Severity.Warning, node.FullSpan, "Command Handler detected") };
//}
}
示例4: GetRefactoring
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
{
var tree = (SyntaxTree)document.GetSyntaxTree(cancellationToken);
var token = tree.GetRoot().FindToken(textSpan.Start);
if (token.Parent is ClassDeclarationSyntax || token.Parent is StructDeclarationSyntax) {
var t = (TypeDeclarationSyntax)token.Parent;
if (!CanInferNonTrivialConstructor(t)) return null;
return new CodeRefactoring(new[] { new ReadyCodeAction("Infer Non-Trivial Constructor", document, t, () => {
var c = TryInferNonTrivialConstructor(t, document.TryGetSemanticModel());
var i = 0;
var ms = t.Members.Insert(i, new[] {c}).List();
return t.With(members: ms);
})});
}
if (token.Parent is MemberDeclarationSyntax && (token.Parent.Parent is ClassDeclarationSyntax || token.Parent.Parent is StructDeclarationSyntax)) {
var m = (MemberDeclarationSyntax)token.Parent;
var t = (TypeDeclarationSyntax)m.Parent;
if (!CanInferNonTrivialConstructor(t)) return null;
return new CodeRefactoring(new[] { new ReadyCodeAction("Infer Non-Trivial Constructor Here", document, t, () => {
var c = TryInferNonTrivialConstructor(t, document.TryGetSemanticModel());
var i = t.Members.IndexOf(m);
var ms = t.Members.Insert(i, new[] {c}).List();
return t.With(members: ms);
})});
}
return null;
}
示例5: ReplaceVarByConcreteAction
public ReplaceVarByConcreteAction(ICodeActionEditFactory editFactory, IDocument document, TypeSyntax typeSyntax)
{
this.editFactory = editFactory;
this.document = document;
this.typeSyntax = typeSyntax;
var semanticModel = (SemanticModel)document.GetSemanticModel();
var syntaxTree = (SyntaxTree)document.GetSyntaxTree();
ILocation location = syntaxTree.GetLocation(typeSyntax);
ITypeSymbol variableType = semanticModel.GetSemanticInfo(typeSyntax).Type;
this.typeName = variableType.ToMinimalDisplayString((Location)location, semanticModel);
}
示例6: HandleDocument
private static void HandleDocument(IDocument document)
{
Console.WriteLine(document.DisplayName);
var tree = document.GetSyntaxTree();
var sema = document.GetSemanticModel();
//HandleCompilation(sema.Compilation);
var methods = tree.Root.DescendentNodes().OfType<MethodDeclarationSyntax>();
foreach (var method in methods)
{
HandleMethod(sema, method);
}
}
示例7: GetRefactoring
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
{
var tree = (SyntaxTree)document.GetSyntaxTree(cancellationToken);
var token = tree.GetRoot().FindToken(textSpan.Start);
var p = token.Parent;
var desc = p.TryGetCodeBlockOrAreaDescription();
if (desc == null) return null;
return new CodeRefactoring(new[] { new ReadyCodeAction(
"Lopsided Terminal Ifs -> Early Return Guards in " + desc,
document,
p,
() => p.ReplaceNodes(p.DescendantNodes().OfType<BlockSyntax>(), (e,a) => LopsidedTerminalBranchesToGuardedBranches(a)))});
}
示例8: GetIssues
public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
{
CommonSyntaxTree syntaxTree = document.GetSyntaxTree();
SemanticModel semanticModel = (SemanticModel) document.GetSemanticModel();
AnalysisResult result = _engine.Analyze(syntaxTree, semanticModel);
if (!result.Success)
{
foreach (Issue issue in result.Issues)
{
yield return new CodeIssue(CodeIssueKind.Error, issue.SyntaxNode.Span, issue.Description);
}
}
}
示例9: GetRefactoring
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
{
var assume = Assumptions.All;
var model = document.GetSemanticModel();
var tree = (SyntaxTree)document.GetSyntaxTree(cancellationToken);
var token = tree.GetRoot().FindToken(textSpan.Start);
var desc = token.Parent.TryGetCodeBlockOrAreaDescription();
if (desc == null) return null;
return new CodeRefactoring(new[] { new ReadyCodeAction(
"Remove Unused Operations in " + desc,
document,
token.Parent,
() => token.Parent.ReplaceNodes(token.Parent.DescendantNodes().OfType<IfStatementSyntax>(), (e,a) => a.DropEmptyBranchesIfApplicable(assume, model)))});
}
示例10: GetRefactoring
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
{
var tree = (SyntaxTree)document.GetSyntaxTree(cancellationToken);
var root = tree.GetRoot(cancellationToken);
var token = tree.GetRoot().FindToken(textSpan.Start);
var m = token.Parent as MethodDeclarationSyntax;
if (m == null) return null;
var model = document.TryGetSemanticModel();
if (model == null) return null;
return new CodeRefactoring(new[] { new ReadyCodeAction(
"Remove method and calls to method (including any side-effects in arguments)",
document,
root,
() => document.NewRootForNukeMethodAndAnySideEffectsInArguments(m))});
}
示例11: ClassifyNode
public IEnumerable<SyntaxClassification> ClassifyNode(IDocument document, CommonSyntaxNode syntax, CancellationToken cancellationToken = new CancellationToken())
{
var classList = new List<SyntaxClassification>(2);
var treeRef = ((SyntaxTree) document.GetSyntaxTree(cancellationToken)).GetReference((SyntaxNode)syntax);
var model = document.GetSemanticModel(cancellationToken);
if (GetClassificationForCommandCreation(model, treeRef.GetSyntax() as ObjectCreationExpressionSyntax, cancellationToken))
{
classList.Add(new SyntaxClassification(syntax.Span, invokeClassification));
}
if (GetClassificationForCommandPublication(model, treeRef.GetSyntax() as InvocationExpressionSyntax, cancellationToken))
{
// TODO: create separate classification type?
classList.Add(new SyntaxClassification(syntax.Span, invokeClassification));
}
return classList.AsEnumerable();
}
示例12: GetIssue
protected override CodeIssue GetIssue(
IDocument document,
SyntaxNode node,
CancellationToken cancellationToken)
{
// NOTE(DustinCa): Not supported in REPL for now.
if (document.GetSyntaxTree(cancellationToken).Options.Kind == SourceCodeKind.Interactive)
{
return null;
}
var service = document.GetLanguageService<IGenerateFieldOrPropertyService>();
var result = service.GenerateFieldOrProperty(document, node, cancellationToken);
if (!result.ContainsChanges)
{
return null;
}
return result.GetCodeIssue(cancellationToken);
}
示例13: GetRefactoring
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
{
var syntaxTree = document.GetSyntaxTree(cancellationToken);
var token = syntaxTree.GetRoot(cancellationToken).FindToken(textSpan.Start);
if (token.Parent == null)
{
return null;
}
var propertyDeclaration = token.Parent.FirstAncestorOrSelf<PropertyDeclarationSyntax>();
// Refactor only properties with both a getter and a setter.
if (propertyDeclaration == null || !HasBothAccessors(propertyDeclaration))
{
return null;
}
return new CodeRefactoring(
actions: new[] { new CodeAction(document, propertyDeclaration) },
textSpan: propertyDeclaration.Identifier.Span);
}
示例14: GetRefactoring
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
{
var syntaxTree = document.GetSyntaxTree(cancellationToken);
var token = syntaxTree.Root.FindToken(textSpan.Start);
if (token.Parent == null)
{
return null;
}
var declaration = token.Parent.FirstAncestorOrSelf<VariableDeclaratorSyntax>();
var variable = (VariableDeclaratorSyntax)declaration;
if (declaration == null)
{
return null;
}
var semanticModel = (SemanticModel)document.GetSemanticModel();
var symbol = semanticModel.GetDeclaredSymbol(variable);
var workspace = workspaceDiscoveryService.GetWorkspace(document.GetText().Container);
return new CodeRefactoring(
new[] { new CodeAction0(workspace, renameService, document, symbol) },
variable.Identifier.Span);
}
示例15: GetMethodDeclarationsInDocument
private static IEnumerable<MethodDeclarationSyntax> GetMethodDeclarationsInDocument(IDocument document)
{
return document.GetSyntaxTree()
.Root
.DescendentNodes()
.OfType<MethodDeclarationSyntax>()
.ToList();
}