Beispiel #1
0
        public async System.Threading.Tasks.Task <IEnumerable <CodeAction> > GetRefactoringsAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken)
        {
            if (textSpan.IsEmpty)
            {
                return(null);
            }

            if (String.IsNullOrWhiteSpace(document.GetText().GetSubText(textSpan).ToString()))
            {
                return(null);
            }

            var tree        = (SyntaxTree)document.GetSyntaxTree(cancellationToken);
            var diagnostics = tree.GetDiagnostics(cancellationToken);

            if (diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error || d.IsWarningAsError))
            {
                return(null);
            }

            var linespan = tree.GetLocation(textSpan).GetLineSpan(false);

            if (linespan.EndLinePosition.Line <= linespan.StartLinePosition.Line)
            {
                return(null);                                                              // single line
            }
            var semanticModel = (SemanticModel)document.GetSemanticModel(cancellationToken);
            var sdiag         = semanticModel.GetDiagnostics(cancellationToken);

            if (sdiag == null)
            {
                return(null);
            }
            if (sdiag.Any(d => d.Severity == DiagnosticSeverity.Error || d.IsWarningAsError))
            {
                return(null);
            }

            var methodExtractor = new MethodExtractor(semanticModel, document, textSpan, this.options);
            var newDocument     = methodExtractor.GetRefactoredDocument(cancellationToken);

            if (newDocument == null)
            {
                return(null);
            }
            var action = new ClousotExtractMethodAction(newDocument);

            return(new List <CodeAction> {
                action
            });
        }
        public CodeRefactoring GetRefactoring(Microsoft.CodeAnalysis.Document document, TextSpan textSpan, CancellationToken cancellationToken)
        {
            var tree          = (SyntaxTree)document.GetSyntaxTree(cancellationToken);
            var semanticModel = (SemanticModel)document.GetSemanticModel(cancellationToken);

            var methodExtractor = new MethodExtractor(semanticModel, document, textSpan, this.options);
            var newDocument     = methodExtractor.GetRefactoredDocument(cancellationToken);

            if (newDocument == null)
            {
                return(null);
            }
            var action = new ClousotExtractMethodAction(newDocument);

            return(new CodeRefactoring(new[] { action }));
        }