Exemplo n.º 1
0
        public static void TestAssertingEndText(
            string sampleClassCode,
            TextSpan refactoringSiteTextSpan,
            Action <string> assertion,
            int refactoringNumber = 0)
        {
            TestAsertingRefactorings(
                sampleClassCode,
                refactoringSiteTextSpan,
                (workspace, document, proposedCodeRefactorings) =>
            {
                CodeAction refactoring        = proposedCodeRefactorings.ElementAt(refactoringNumber);
                CodeActionOperation operation = refactoring
                                                .GetOperationsAsync(CancellationToken.None)
                                                .Result
                                                .Single();

                operation.Apply(workspace, CancellationToken.None);

                Document newDocument = workspace.CurrentSolution.GetDocument(document.Id);

                SourceText newText = newDocument.GetTextAsync(CancellationToken.None).Result;

                string text = newText.ToString();

                assertion(text);
            });
        }
Exemplo n.º 2
0
        internal static Project ApplyOperation(this Project project, CodeActionOperation operation)
        {
            var workspace = project.Solution.Workspace;

            operation.Apply(workspace, CancellationToken.None);
            return(workspace.CurrentSolution.GetProject(project.Id) !);
        }
        public InstallPackageDirectlyCodeAction(
            IPackageInstallerService installerService,
            Document document,
            string source,
            string packageName,
            string versionOpt,
            bool includePrerelease,
            bool isLocal
            )
        {
            Title =
                versionOpt == null
                    ? FeaturesResources.Find_and_install_latest_version
                    : isLocal
                        ? string.Format(FeaturesResources.Use_local_version_0, versionOpt)
                        : string.Format(FeaturesResources.Install_version_0, versionOpt);

            _installPackageOperation = new InstallPackageDirectlyCodeActionOperation(
                installerService,
                document,
                source,
                packageName,
                versionOpt,
                includePrerelease,
                isLocal
                );
        }
Exemplo n.º 4
0
        protected override async Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(
            object options,
            CancellationToken cancellationToken
            )
        {
            var operations = SpecializedCollections.EmptyEnumerable <CodeActionOperation>();

            if (
                options is ExtractInterfaceOptionsResult extractInterfaceOptions &&
                !extractInterfaceOptions.IsCancelled
                )
            {
                var extractInterfaceResult = await _extractInterfaceService
                                             .ExtractInterfaceFromAnalyzedTypeAsync(
                    _typeAnalysisResult,
                    extractInterfaceOptions,
                    cancellationToken
                    )
                                             .ConfigureAwait(false);

                if (extractInterfaceResult.Succeeded)
                {
                    operations = new CodeActionOperation[]
                    {
                        new ApplyChangesOperation(extractInterfaceResult.UpdatedSolution),
                        new DocumentNavigationOperation(
                            extractInterfaceResult.NavigationDocumentId,
                            position: 0
                            )
                    };
                }
            }

            return(operations);
        }
            // Возвращаем операцию навигации в определённый документ и определённую позицию
            // как результат нашего CodeAction'а
            protected override Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(
                CancellationToken cancellationToken)
            {
                IEnumerable <CodeActionOperation> operations = new CodeActionOperation[]
                {
                    new DocumentNavigationOperation(_documentId, _position),
                };

                return(Task.FromResult(operations));
            }
Exemplo n.º 6
0
        protected override async Task <IEnumerable <CodeActionOperation> > ComputePreviewOperationsAsync(CancellationToken cancellationToken)
        {
            var addedDocument = CreateTextFixtureDocument(cancellationToken);

            IEnumerable <CodeActionOperation> operations = new CodeActionOperation[]
            {
                new ApplyChangesOperation(addedDocument.Project.Solution),
            };

            return(await Task.FromResult(operations).ConfigureAwait(false));
        }
Exemplo n.º 7
0
        protected override Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(object options, CancellationToken cancellationToken)
        {
            var addedDocument = CreateTextFixtureDocument(cancellationToken);

            IEnumerable <CodeActionOperation> operations = new CodeActionOperation[]
            {
                new ApplyChangesOperation(addedDocument.Project.Solution),
                new OpenDocumentOperation(addedDocument.Id, true)
            };

            return(Task.FromResult(operations));
        }
Exemplo n.º 8
0
        private static void VerifyOperationText([NotNull] CodeActionOperation operation, [NotNull] string expectedCode,
                                                [NotNull] Document document)
        {
            Workspace workspace = document.Project.Solution.Workspace;

            operation.Apply(workspace, CancellationToken.None);

            Document newDocument = workspace.CurrentSolution.GetDocument(document.Id);

            SourceText sourceText = newDocument.GetTextAsync().Result;
            string     text       = sourceText.ToString();

            text.Should().Be(expectedCode);
        }
Exemplo n.º 9
0
        private async Task CodeActionAsync(CodeAction codeAction, Document document, string expectedCode)
        {
            ImmutableArray <CodeActionOperation> operations = codeAction.GetOperationsAsync(CancellationToken.None).Result;

            Assert.Single(operations);

            CodeActionOperation operation = operations.First();
            Workspace           workspace = document.Project.Solution.Workspace;

            operation.Apply(workspace, CancellationToken.None);

            Document newDocument = workspace.CurrentSolution.GetDocument(document.Id);

            SourceText sourceText = await newDocument.GetTextAsync(CancellationToken.None).ConfigureAwait(false);

            string text = sourceText.ToString();

            Assert.Equal(expectedCode, text);
        }
Exemplo n.º 10
0
        protected override Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(object options, CancellationToken cancellationToken)
        {
            IEnumerable <CodeActionOperation> operations = null;

            if (options is ExtractInterfaceOptionsResult extractInterfaceOptions && !extractInterfaceOptions.IsCancelled)
            {
                var extractInterfaceResult = _extractInterfaceService.ExtractInterfaceFromAnalyzedType(_typeAnalysisResult, extractInterfaceOptions, cancellationToken);

                if (extractInterfaceResult.Succeeded)
                {
                    operations = new CodeActionOperation[]
                    {
                        new ApplyChangesOperation(extractInterfaceResult.UpdatedSolution),
                        new DocumentNavigationOperation(extractInterfaceResult.NavigationDocumentId, position: 0)
                    };
                }
            }

            return(Task.FromResult(operations));
        }
        public static void CodeAction([NotNull] CodeAction codeAction, [NotNull] Document document,
                                      [NotNull] string expectedCode)
        {
            Guard.NotNull(codeAction, nameof(codeAction));
            Guard.NotNull(document, nameof(document));
            Guard.NotNull(expectedCode, nameof(expectedCode));

            ImmutableArray <CodeActionOperation> operations =
                codeAction.GetOperationsAsync(CancellationToken.None).Result;

            operations.Should().HaveCount(1);

            CodeActionOperation operation = operations.Single();
            Workspace           workspace = document.Project.Solution.Workspace;

            operation.Apply(workspace, CancellationToken.None);

            Document newDocument = workspace.CurrentSolution.GetDocument(document.Id);

            SourceText sourceText = newDocument.GetTextAsync().Result;
            string     text       = sourceText.ToString();

            text.Should().Be(expectedCode);
        }
 public RoslynCodeActionOperation(CodeActionOperation inner)
 {
     _inner = inner;
 }