Exemplo n.º 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.containers;
                var includeUsingsOrImports = namespaceContainersAndUsings.usingOrImport;

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

                if (_targetProjectChangeInLanguage == TargetProjectChangeInLanguage.NoChange)
                {
                    enclosingNamespaceGeneratedTypeToAddAndLocation = await _service.GetOrGenerateEnclosingNamespaceSymbolAsync(
                        namedType,
                        containers,
                        generateTypeOptionsResult.ExistingDocument,
                        root,
                        _cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    enclosingNamespaceGeneratedTypeToAddAndLocation = await _targetLanguageService.GetOrGenerateEnclosingNamespaceSymbolAsync(
                        namedType,
                        containers,
                        generateTypeOptionsResult.ExistingDocument,
                        root,
                        _cancellationToken).ConfigureAwait(false);
                }

                var solution      = _semanticDocument.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 = await _service.TryAddUsingsOrImportToDocumentAsync(
                        updatedSolution,
                        generateTypeOptionsResult.ExistingDocument.Id == _semanticDocument.Document.Id?newRoot : null,
                        _semanticDocument.Document,
                        _state.SimpleName,
                        includeUsingsOrImports,
                        _cancellationToken).ConfigureAwait(false);
                }

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

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