当前位置: 首页>>代码示例>>C#>>正文


C# IDocument.GetSyntaxTree方法代码示例

本文整理汇总了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
            );
        }
开发者ID:DamianReeves,项目名称:Rhema,代码行数:27,代码来源:CodeRefactoringProvider.cs

示例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);
        }
开发者ID:yonder-innovation,项目名称:Roslyn-Code-Refactoring,代码行数:32,代码来源:CodeRefactoringProvider.cs

示例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") };
            //}
        }
开发者ID:jjrdk,项目名称:CqrsMessagingTools,代码行数:34,代码来源:CodeIssueProvider.cs

示例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;
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:30,代码来源:InferNonTrivialConstructor.cs

示例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);
        }
开发者ID:jorik041,项目名称:Roslyn,代码行数:14,代码来源:ReplaceVarByConcreteAction.cs

示例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);
            }
        }
开发者ID:halllo,项目名称:MTSS12,代码行数:14,代码来源:SolutionHandler.cs

示例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)))});
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:15,代码来源:LopsidedTerminalIfsToEarlyReturnGuards.cs

示例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);
                }
            }
        }
开发者ID:flashcurd,项目名称:ThreadSafetyAnnotations,代码行数:15,代码来源:CodeIssueProvider.cs

示例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)))});
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:16,代码来源:RemoveUnusedOperations.cs

示例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))});
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:16,代码来源:RemoveMethodAndInvocations.cs

示例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();
        }
开发者ID:jjrdk,项目名称:CqrsMessagingTools,代码行数:18,代码来源:MessageSendSyntaxClassifier.cs

示例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);
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:20,代码来源:GenerateVariableCodeIssueProvider.cs

示例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);
        }
开发者ID:2j2e,项目名称:presentations,代码行数:21,代码来源:CodeRefactoringProvider.cs

示例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);
    }
开发者ID:dchung89,项目名称:rfactor,代码行数:24,代码来源:RenameOption.cs

示例15: GetMethodDeclarationsInDocument

 private static IEnumerable<MethodDeclarationSyntax> GetMethodDeclarationsInDocument(IDocument document)
 {
     return document.GetSyntaxTree()
                    .Root
                    .DescendentNodes()
                    .OfType<MethodDeclarationSyntax>()
                    .ToList();
 }
开发者ID:mikkoj,项目名称:SeekUnusedMethods,代码行数:8,代码来源:Program.cs


注:本文中的IDocument.GetSyntaxTree方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。