public Editor(
     TService service,
     SemanticDocument document,
     State state,
     bool fromDialog,
     GenerateTypeOptionsResult generateTypeOptionsResult,
     CancellationToken cancellationToken)
 {
     _service    = service;
     _document   = document;
     _state      = state;
     _fromDialog = fromDialog;
     _generateTypeOptionsResult = generateTypeOptionsResult;
     _cancellationToken         = cancellationToken;
 }
            private void AddFieldDelegatingConstructor(
                IList <TArgumentSyntax> argumentList, IList <ISymbol> members, GenerateTypeOptionsResult options = null)
            {
                var factory            = _document.Project.LanguageServices.GetService <SyntaxGenerator>();
                var syntaxFactsService = _document.Project.LanguageServices.GetService <ISyntaxFactsService>();

                var availableTypeParameters = _service.GetAvailableTypeParameters(_state, _document.SemanticModel, _intoNamespace, _cancellationToken);
                var parameterTypes          = GetArgumentTypes(argumentList);
                var parameterNames          = _service.GenerateParameterNames(_document.SemanticModel, argumentList);
                var parameters = new List <IParameterSymbol>();

                var parameterToExistingFieldMap = new Dictionary <string, ISymbol>();
                var parameterToNewFieldMap      = new Dictionary <string, string>();

                var syntaxFacts = _document.Project.LanguageServices.GetService <ISyntaxFactsService>();

                for (var i = 0; i < parameterNames.Count; i++)
                {
                    var refKind = syntaxFacts.GetRefKindOfArgument(argumentList[i]);

                    var parameterName = parameterNames[i];
                    var parameterType = parameterTypes[i];
                    parameterType = parameterType.RemoveUnavailableTypeParameters(
                        _document.SemanticModel.Compilation, availableTypeParameters);

                    if (!TryFindMatchingField(parameterName, parameterType, parameterToExistingFieldMap, caseSensitive: true))
                    {
                        if (!TryFindMatchingField(parameterName, parameterType, parameterToExistingFieldMap, caseSensitive: false))
                        {
                            parameterToNewFieldMap[parameterName] = parameterName;
                        }
                    }

                    parameters.Add(CodeGenerationSymbolFactory.CreateParameterSymbol(
                                       attributes: null,
                                       refKind: refKind,
                                       isParams: false,
                                       type: parameterType,
                                       name: parameterName));
                }

                // Empty Constructor for Struct is not allowed
                if (!(parameters.Count == 0 && options != null && (options.TypeKind == TypeKind.Struct || options.TypeKind == TypeKind.Structure)))
                {
                    members.AddRange(factory.CreateFieldDelegatingConstructor(
                                         DetermineName(), null, parameters, parameterToExistingFieldMap, parameterToNewFieldMap, _cancellationToken));
                }
            }
Exemple #3
0
            public Editor(
                TService service,
                SemanticDocument document,
                State state,
                bool fromDialog,
                GenerateTypeOptionsResult generateTypeOptionsResult,
                CancellationToken cancellationToken)
            {
                // the document comes from the same snapshot as the project
                Contract.ThrowIfFalse(document.Project.Solution == generateTypeOptionsResult.Project.Solution);

                _service                   = service;
                _semanticDocument          = document;
                _state                     = state;
                _fromDialog                = fromDialog;
                _generateTypeOptionsResult = generateTypeOptionsResult;
                _cancellationToken         = cancellationToken;
            }
Exemple #4
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;

                (INamespaceSymbol, INamespaceOrTypeSymbol, Location)enclosingNamespaceGeneratedTypeToAddAndLocation;
                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) });
            }