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


C# IDocument.TryGetSemanticModel方法代码示例

本文整理汇总了C#中IDocument.TryGetSemanticModel方法的典型用法代码示例。如果您正苦于以下问题:C# IDocument.TryGetSemanticModel方法的具体用法?C# IDocument.TryGetSemanticModel怎么用?C# IDocument.TryGetSemanticModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IDocument的用法示例。


在下文中一共展示了IDocument.TryGetSemanticModel方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: 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

示例2: CheckRenamePreconditions

 /* Check the preconditions of rename refactoring, return true if can be finished succesfully. */
 public static bool CheckRenamePreconditions(IDocument document, int start, int length)
 {
     var service = ServiceArchive.getInstance().RenameService;
     ISemanticModel model;
     document.TryGetSemanticModel(out model);
     return false;
 }
开发者ID:nkcsgexi,项目名称:GhostFactor2,代码行数:8,代码来源:RoslynUtil.cs

示例3: 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

示例4: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var assume = Assumptions.All;
            var c = (StatementSyntax)node;
            if (!(c.Parent is BlockSyntax)) {
                if (c is EmptyStatementSyntax) return null;
                if (c is BlockSyntax && c.Statements().None()) return null;
            }

            if (c.HasSideEffects(document.TryGetSemanticModel(), assume) != false) return null;
            return new[] { new CodeIssue(CodeIssue.Severity.Warning, c.Span, "Statement without any effect", new[] { new ReadyCodeAction(
                "Remove Unnecessary Statement",
                document,
                c,
                () => c.Parent is BlockSyntax ? null : Syntax.EmptyStatement())})};
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:16,代码来源:UselessStatement.cs

示例5: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var model = document.TryGetSemanticModel();
            if (model == null) return null;
            if (model.GetDiagnostics().Any(e => e.Info.Severity == DiagnosticSeverity.Error)) return null;
            if (document.Project.Documents.Any(e => e.GetSyntaxTree() == null || e.GetSemanticModel() == null)) return null;

            var fieldNode = (FieldDeclarationSyntax)node;
            if (fieldNode.IsReadOnly()) return null;

            var classDecl = fieldNode.Ancestors().OfType<ClassDeclarationSyntax>().FirstOrDefault();
            if (classDecl == null) return null;
            var constructors = classDecl.Members.OfType<ConstructorDeclarationSyntax>().ToRet();

            var modsWithReadOnly = fieldNode.Modifiers.Append(SyntaxKind.ReadOnlyKeyword.AsToken()).AsTokenList();

            var scopes = fieldNode.IsPrivate()
                       ? new[] {Tuple.Create((CommonSyntaxNode)classDecl, model)}
                       : document.Project.Documents.Select(e => Tuple.Create(e.GetSyntaxTree().GetRoot(), e.GetSemanticModel()));

            var unmutatedVars = fieldNode.Declaration.Variables.Where(v => {
                var field = model.GetDeclaredSymbol(v);
                if (scopes.Any(c => c.Item1.DescendantNodes(e => !constructors.Contains(e)).OfType<ExpressionSyntax>().Any(e => SurfaceWritesTo(e, c.Item2, field)))) return false;
                return true;
            }).ToArray();

            if (unmutatedVars.Length == 0) return null;
            if (unmutatedVars.Length == fieldNode.Declaration.Variables.Count) {
                var r = new ReadyCodeAction("Make readonly", document, fieldNode, () => fieldNode.WithModifiers(modsWithReadOnly));
                var desc = unmutatedVars.Length == 1 ? "Mutable field is never modified." : "Mutable fields are never modified.";
                return r.CodeIssues1(CodeIssue.Severity.Warning, fieldNode.Declaration.Type.Span, desc);
            }

            return unmutatedVars.Select(v => {
                var singleReadOnly = fieldNode.WithModifiers(modsWithReadOnly)
                                              .WithDeclaration(fieldNode.Declaration.WithVariables(v.SepList1()));
                var rest = fieldNode
                           .WithLeadingTrivia()
                           .WithTrailingTrivia(Syntax.Whitespace(Environment.NewLine))
                           .WithDeclaration(fieldNode.Declaration.WithVariables(fieldNode.Declaration.Variables.Without(v)));
                var newClassDecl = classDecl.With(members: classDecl.Members.WithItemReplacedByMany(fieldNode, new[] { singleReadOnly, rest }));
                var action = new ReadyCodeAction("Split readonly", document, classDecl, () => newClassDecl);
                return new CodeIssue(CodeIssue.Severity.Warning, v.Identifier.Span, "Mutable field is never modified.", new[] { action });
            }).ToArray();
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:45,代码来源:UnnecessaryMutability.cs

示例6: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var forLoop = (ForEachStatementSyntax)node;
            var model = document.TryGetSemanticModel();
            if (model == null) return null;

            // list created just before loop starts?
            var initStatement = forLoop.TryGetPrevStatement();
            var lhs = initStatement.TryGetLHSExpOfAssignmentOrInit() as IdentifierNameSyntax;
            if (lhs == null) return null;
            var target = lhs.Identifier;
            var rhs = initStatement.TryGetRHSOfAssignmentOrInit() as ObjectCreationExpressionSyntax;
            if (rhs == null) return null;
            var cr = model.GetTypeInfo(rhs).Type;
            if (cr.AllInterfaces.All(e => e.Name != "IList")) return null;

            // loop adds things directly into the list?
            var adderStatement = forLoop.Statement.Statements().SingleOrDefaultAllowMany() as ExpressionStatementSyntax;
            if (adderStatement == null) return null;
            var adderInvoke = adderStatement.Expression as InvocationExpressionSyntax;
            if (adderInvoke == null) return null;
            var adderAccess = adderInvoke.Expression as MemberAccessExpressionSyntax;
            if (adderAccess == null) return null;
            var adderTarget = adderAccess.Expression as IdentifierNameSyntax;
            if (adderTarget == null) return null;
            if (adderTarget.PlainName != target.ValueText) return null;
            if (adderAccess.Name.PlainName != "Add") return null;
            if (adderInvoke.ArgumentList.Arguments.Count != 1) return null;
            var adderExp = adderInvoke.ArgumentList.Arguments.Single().Expression as SimpleNameSyntax;
            if (adderExp == null) return null;
            if (adderExp.PlainName != forLoop.Identifier.ValueText) return null;

            var linqed = forLoop.Expression.Accessing("ToList").Invoking();
            var replacedInit = initStatement.TryWithNewRightHandSideOfAssignmentOrSingleInit(linqed);

            var action = new ReadyCodeAction(
                "Select into list",
                document,
                new[] { initStatement, forLoop },
                (e, a) => e == initStatement ? replacedInit : a.Dropped());
            return action.CodeIssues1(
                CodeIssue.Severity.Warning,
                forLoop.ForEachKeyword.Span,
                "Initializing a list with a 'for each' loop.");
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:45,代码来源:ForEachToList.cs


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