コード例 #1
0
            private async Task <IEnumerable <CodeActionOperation> > GetGenerateIntoExistingDocumentAsync(
                INamedTypeSymbol namedType,
                Project triggeringProject,
                GenerateTypeOptionsResult generateTypeOptionsResult,
                bool isDialog)
            {
                var root = await generateTypeOptionsResult.ExistingDocument.GetSyntaxRootAsync(_cancellationToken).ConfigureAwait(false);

                var folders = generateTypeOptionsResult.ExistingDocument.Folders;

                var namespaceContainersAndUsings = GetNamespaceContainersAndAddUsingsOrImport(isDialog, new List <string>(folders), generateTypeOptionsResult.AreFoldersValidIdentifiers, generateTypeOptionsResult.Project, triggeringProject);

                var containers             = namespaceContainersAndUsings.Item1;
                var includeUsingsOrImports = namespaceContainersAndUsings.Item2;

                Tuple <INamespaceSymbol, INamespaceOrTypeSymbol, Location> enclosingNamespaceGeneratedTypeToAddAndLocation = null;

                if (_targetProjectChangeInLanguage == TargetProjectChangeInLanguage.NoChange)
                {
                    enclosingNamespaceGeneratedTypeToAddAndLocation = _service.GetOrGenerateEnclosingNamespaceSymbol(
                        namedType,
                        containers,
                        generateTypeOptionsResult.ExistingDocument,
                        root,
                        _cancellationToken).WaitAndGetResult(_cancellationToken);
                }
                else
                {
                    enclosingNamespaceGeneratedTypeToAddAndLocation = _targetLanguageService.GetOrGenerateEnclosingNamespaceSymbol(
                        namedType,
                        containers,
                        generateTypeOptionsResult.ExistingDocument,
                        root,
                        _cancellationToken).WaitAndGetResult(_cancellationToken);
                }

                var solution      = _document.Project.Solution;
                var codeGenResult = await CodeGenerator.AddNamespaceOrTypeDeclarationAsync(
                    solution,
                    enclosingNamespaceGeneratedTypeToAddAndLocation.Item1,
                    enclosingNamespaceGeneratedTypeToAddAndLocation.Item2,
                    new CodeGenerationOptions(afterThisLocation : enclosingNamespaceGeneratedTypeToAddAndLocation.Item3),
                    _cancellationToken)
                                    .ConfigureAwait(false);

                var newRoot = await codeGenResult.GetSyntaxRootAsync(_cancellationToken).ConfigureAwait(false);

                var updatedSolution = solution.WithDocumentSyntaxRoot(generateTypeOptionsResult.ExistingDocument.Id, newRoot, PreservationMode.PreserveIdentity);

                // Update the Generating Document with a using if required
                if (includeUsingsOrImports != null)
                {
                    updatedSolution = _service.TryAddUsingsOrImportToDocument(
                        updatedSolution,
                        generateTypeOptionsResult.ExistingDocument.Id == _document.Document.Id ? newRoot : null,
                        _document.Document,
                        _state.SimpleName,
                        includeUsingsOrImports,
                        _cancellationToken);
                }

                updatedSolution = AddProjectReference(generateTypeOptionsResult.Project, triggeringProject, updatedSolution);

                return(new CodeActionOperation[] { new ApplyChangesOperation(updatedSolution) });
            }