public async Task <ImmutableArray <CodeAction> > GenerateDefaultConstructorsAsync(
            Document document,
            TextSpan textSpan,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            bool forRefactoring,
            CancellationToken cancellationToken)
        {
            using (Logger.LogBlock(FunctionId.Refactoring_GenerateMember_GenerateDefaultConstructors, cancellationToken))
            {
                var semanticDocument = await SemanticDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);

                using var _ = ArrayBuilder <CodeAction> .GetInstance(out var result);

                if (textSpan.IsEmpty)
                {
                    var state = State.Generate((TService)this, semanticDocument, textSpan, forRefactoring, cancellationToken);
                    if (state != null)
                    {
                        foreach (var constructor in state.UnimplementedConstructors)
                        {
                            result.Add(new GenerateDefaultConstructorCodeAction(document, state, constructor, fallbackOptions));
                        }

                        if (state.UnimplementedConstructors.Length > 1)
                        {
                            result.Add(new CodeActionAll(document, state, state.UnimplementedConstructors, fallbackOptions));
                        }
                    }
                }

                return(result.ToImmutable());
            }
        }
 public IntentDataProvider(
     string?serializedIntentData,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     _serializedIntentData = serializedIntentData;
     FallbackOptions       = fallbackOptions;
 }
        protected override Task <ImmutableArray <CodeAction> > GetCodeActionsAsync(
            Document document, SyntaxNode node, CodeAndImportGenerationOptionsProvider fallbackOptions, CancellationToken cancellationToken)
        {
            var service = document.GetRequiredLanguageService <IGenerateParameterizedMemberService>();

            return(service.GenerateMethodAsync(document, node, fallbackOptions, cancellationToken));
        }
示例#4
0
 internal GenerateTypeCodeActionWithOption(TService service, Document document, State state, CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     _service         = service;
     _document        = document;
     _state           = state;
     _fallbackOptions = fallbackOptions;
 }
示例#5
0
        public async Task <ImmutableArray <CodeAction> > GenerateTypeAsync(
            Document document,
            SyntaxNode node,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            using (Logger.LogBlock(FunctionId.Refactoring_GenerateType, cancellationToken))
            {
                var semanticDocument = await SemanticDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);

                var state = await State.GenerateAsync((TService)this, semanticDocument, node, cancellationToken).ConfigureAwait(false);

                if (state != null)
                {
                    var actions = GetActions(semanticDocument, node, state, fallbackOptions, cancellationToken);
                    if (actions.Length > 1)
                    {
                        // Wrap the generate type actions into a single top level suggestion
                        // so as to not clutter the list.
                        return(ImmutableArray.Create(CodeAction.Create(
                                                         string.Format(FeaturesResources.Generate_type_0, state.Name),
                                                         actions.AsImmutable(),
                                                         isInlinable: true)));
                    }
                    else
                    {
                        return(actions);
                    }
                }

                return(ImmutableArray <CodeAction> .Empty);
            }
        }
示例#6
0
        protected override Task <ImmutableArray <CodeAction> > GetCodeActionsAsync(
            Document document, SyntaxNode node, CodeAndImportGenerationOptionsProvider fallbackOptions, CancellationToken cancellationToken)
        {
            var service = document.GetLanguageService <IGenerateConstructorService>();

            return(service.GenerateConstructorAsync(document, node, fallbackOptions, cancellationToken));
        }
        private static async Task <Document> GenerateComparisonOperatorsAsync(
            Document document,
            SyntaxNode typeDeclaration,
            INamedTypeSymbol comparableType,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var containingType = (INamedTypeSymbol)semanticModel.GetRequiredDeclaredSymbol(typeDeclaration, cancellationToken);
            var compareMethod  = TryGetCompareMethodImpl(containingType, comparableType) !;

            var generator = document.GetRequiredLanguageService <SyntaxGenerator>();

            var codeGenService = document.GetRequiredLanguageService <ICodeGenerationService>();
            var operators      = GenerateComparisonOperators(
                generator, semanticModel.Compilation, containingType, comparableType,
                GenerateLeftExpression(generator, comparableType, compareMethod));

            var solutionContext = new CodeGenerationSolutionContext(
                document.Project.Solution,
                new CodeGenerationContext(contextLocation: typeDeclaration.GetLocation()),
                fallbackOptions);

            return(await codeGenService.AddMembersAsync(solutionContext, containingType, operators, cancellationToken).ConfigureAwait(false));
        }
 public CodeActionAll(
     Document document,
     State state,
     IList <IMethodSymbol> constructors,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
     : base(document, state, constructors, FeaturesResources.Generate_all, fallbackOptions)
 {
 }
 public GenerateDefaultConstructorCodeAction(
     Document document,
     State state,
     IMethodSymbol constructor,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
     : base(document, state, new[] { constructor }, GetDisplayText(state, constructor), fallbackOptions)
 {
 }
示例#10
0
 public GenerateEnumMemberCodeAction(
     Document document,
     State state,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     _document        = document;
     _state           = state;
     _fallbackOptions = fallbackOptions;
 }
        public async Task <ImmutableArray <CodeAction> > GenerateVariableAsync(
            Document document,
            SyntaxNode node,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            using (Logger.LogBlock(FunctionId.Refactoring_GenerateMember_GenerateVariable, cancellationToken))
            {
                var semanticDocument = await SemanticDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);

                var state = await State.GenerateAsync((TService)this, semanticDocument, node, cancellationToken).ConfigureAwait(false);

                if (state == null)
                {
                    return(ImmutableArray <CodeAction> .Empty);
                }

                using var _ = ArrayBuilder <CodeAction> .GetInstance(out var actions);

                var canGenerateMember = CodeGenerator.CanAdd(document.Project.Solution, state.TypeToGenerateIn, cancellationToken);

                if (canGenerateMember && state.CanGeneratePropertyOrField())
                {
                    // prefer fields over properties (and vice versa) depending on the casing of the member.
                    // lowercase -> fields.  title case -> properties.
                    var name = state.IdentifierToken.ValueText;
                    if (char.IsUpper(name.ToCharArray().FirstOrDefault()))
                    {
                        await AddPropertyCodeActionsAsync(actions, semanticDocument, state, fallbackOptions, cancellationToken).ConfigureAwait(false);

                        AddFieldCodeActions(actions, semanticDocument, state, fallbackOptions);
                    }
                    else
                    {
                        AddFieldCodeActions(actions, semanticDocument, state, fallbackOptions);
                        await AddPropertyCodeActionsAsync(actions, semanticDocument, state, fallbackOptions, cancellationToken).ConfigureAwait(false);
                    }
                }

                await AddLocalCodeActionsAsync(actions, document, state, fallbackOptions, cancellationToken).ConfigureAwait(false);
                await AddParameterCodeActionsAsync(actions, document, state, fallbackOptions, cancellationToken).ConfigureAwait(false);

                if (actions.Count > 1)
                {
                    // Wrap the generate variable actions into a single top level suggestion
                    // so as to not clutter the list.
                    return(ImmutableArray.Create(CodeAction.Create(
                                                     string.Format(FeaturesResources.Generate_variable_0, state.IdentifierToken.ValueText),
                                                     actions.ToImmutable(),
                                                     isInlinable: true)));
                }

                return(actions.ToImmutable());
            }
        }
            private State(TService service, SemanticDocument document, NamingRule fieldNamingRule, NamingRule propertyNamingRule, NamingRule parameterNamingRule, CodeAndImportGenerationOptionsProvider fallbackOptions)
            {
                _service             = service;
                _document            = document;
                _fieldNamingRule     = fieldNamingRule;
                _propertyNamingRule  = propertyNamingRule;
                _parameterNamingRule = parameterNamingRule;

                ParameterToNewFieldMap    = ImmutableDictionary <string, string> .Empty;
                ParameterToNewPropertyMap = ImmutableDictionary <string, string> .Empty;
                _fallbackOptions          = fallbackOptions;
            }
示例#13
0
 public ConstructorDelegatingCodeAction(
     AbstractGenerateConstructorFromMembersCodeRefactoringProvider service,
     Document document,
     State state,
     bool addNullChecks,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     _service         = service;
     _document        = document;
     _state           = state;
     _addNullChecks   = addNullChecks;
     _fallbackOptions = fallbackOptions;
 }
示例#14
0
 protected AbstractCodeAction(
     Document document,
     State state,
     IList <IMethodSymbol> constructors,
     string title,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     _document        = document;
     _state           = state;
     _constructors    = constructors;
     _title           = title;
     _fallbackOptions = fallbackOptions;
 }
 public GenerateOverridesWithDialogCodeAction(
     GenerateOverridesCodeRefactoringProvider service,
     Document document,
     TextSpan textSpan,
     INamedTypeSymbol containingType,
     ImmutableArray <ISymbol> viableMembers,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     _service         = service;
     _document        = document;
     _containingType  = containingType;
     _viableMembers   = viableMembers;
     _textSpan        = textSpan;
     _fallbackOptions = fallbackOptions;
 }
示例#16
0
 public GenerateParameterizedMemberCodeAction(
     TService service,
     Document document,
     State state,
     CodeAndImportGenerationOptionsProvider fallbackOptions,
     bool isAbstract,
     bool generateProperty)
 {
     _service          = service;
     _document         = document;
     _state            = state;
     _fallbackOptions  = fallbackOptions;
     _isAbstract       = isAbstract;
     _generateProperty = generateProperty;
     _equivalenceKey   = Title;
 }
示例#17
0
 public GenerateTypeCodeAction(
     TService service,
     Document document,
     State state,
     CodeAndImportGenerationOptionsProvider fallbackOptions,
     bool intoNamespace,
     bool inNewFile)
 {
     _service         = service;
     _document        = document;
     _state           = state;
     _fallbackOptions = fallbackOptions;
     _intoNamespace   = intoNamespace;
     _inNewFile       = inNewFile;
     _equivalenceKey  = Title;
 }
示例#18
0
        private ImmutableArray <CodeAction> GetActions(
            SemanticDocument document,
            SyntaxNode node,
            State state,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            using var _ = ArrayBuilder <CodeAction> .GetInstance(out var result);

            var generateNewTypeInDialog = false;

            if (state.NamespaceToGenerateInOpt != null)
            {
                var workspace = document.Project.Solution.Workspace;
                if (workspace == null || workspace.CanApplyChange(ApplyChangesKind.AddDocument))
                {
                    generateNewTypeInDialog = true;
                    result.Add(new GenerateTypeCodeAction((TService)this, document.Document, state, fallbackOptions, intoNamespace: true, inNewFile: true));
                }

                // If they just are generating "Goo" then we want to offer to generate it into the
                // namespace in the same file.  However, if they are generating "SomeNS.Goo", then we
                // only want to allow them to generate if "SomeNS" is the namespace they are
                // currently in.
                var isSimpleName           = state.SimpleName == state.NameOrMemberAccessExpression;
                var generateIntoContaining = IsGeneratingIntoContainingNamespace(document, node, state, cancellationToken);

                if ((isSimpleName || generateIntoContaining) &&
                    CanGenerateIntoContainingNamespace(document, node, cancellationToken))
                {
                    result.Add(new GenerateTypeCodeAction((TService)this, document.Document, state, fallbackOptions, intoNamespace: true, inNewFile: false));
                }
            }

            if (state.TypeToGenerateInOpt != null)
            {
                result.Add(new GenerateTypeCodeAction((TService)this, document.Document, state, fallbackOptions, intoNamespace: false, inNewFile: false));
            }

            if (generateNewTypeInDialog)
            {
                result.Add(new GenerateTypeCodeActionWithOption((TService)this, document.Document, state, fallbackOptions));
            }

            return(result.ToImmutable());
        }
示例#19
0
        public async Task <ImmutableArray <CodeAction> > GenerateDeconstructMethodAsync(
            Document document,
            SyntaxNode leftSide,
            INamedTypeSymbol typeToGenerateIn,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            using (Logger.LogBlock(FunctionId.Refactoring_GenerateMember_GenerateMethod, cancellationToken))
            {
                var semanticDocument = await SemanticDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);

                var state = await State.GenerateDeconstructMethodStateAsync(
                    (TService)this, semanticDocument, leftSide, typeToGenerateIn, cancellationToken).ConfigureAwait(false);

                return(state != null ? await GetActionsAsync(document, state, fallbackOptions, cancellationToken).ConfigureAwait(false) : ImmutableArray <CodeAction> .Empty);
            }
        }
 public Editor(
     TService service,
     SemanticDocument document,
     State state,
     CodeAndImportGenerationOptionsProvider fallbackOptions,
     bool intoNamespace,
     bool inNewFile,
     CancellationToken cancellationToken)
 {
     _service           = service;
     _semanticDocument  = document;
     _state             = state;
     _fallbackOptions   = fallbackOptions;
     _intoNamespace     = intoNamespace;
     _inNewFile         = inNewFile;
     _cancellationToken = cancellationToken;
 }
 public GenerateVariableCodeAction(
     SemanticDocument document,
     State state,
     bool generateProperty,
     bool isReadonly,
     bool isConstant,
     RefKind refKind,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     _semanticDocument = document;
     _state            = state;
     _generateProperty = generateProperty;
     _isReadonly       = isReadonly;
     _isConstant       = isConstant;
     _refKind          = refKind;
     _equivalenceKey   = Title;
     _fallbackOptions  = fallbackOptions;
 }
示例#22
0
 public GenerateConstructorWithDialogCodeAction(
     AbstractGenerateConstructorFromMembersCodeRefactoringProvider service,
     Document document,
     TextSpan textSpan,
     INamedTypeSymbol containingType,
     Accessibility?desiredAccessibility,
     ImmutableArray <ISymbol> viableMembers,
     ImmutableArray <PickMembersOption> pickMembersOptions,
     CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     _service              = service;
     _document             = document;
     _textSpan             = textSpan;
     _containingType       = containingType;
     _desiredAccessibility = desiredAccessibility;
     ViableMembers         = viableMembers;
     PickMembersOptions    = pickMembersOptions;
     _fallbackOptions      = fallbackOptions;
 }
示例#23
0
        public async Task <ImmutableArray <CodeAction> > GenerateConversionAsync(
            Document document,
            SyntaxNode node,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            using (Logger.LogBlock(FunctionId.Refactoring_GenerateMember_GenerateMethod, cancellationToken))
            {
                var semanticDocument = await SemanticDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);

                var state = await State.GenerateConversionStateAsync((TService)this, semanticDocument, node, cancellationToken).ConfigureAwait(false);

                if (state == null)
                {
                    return(ImmutableArray <CodeAction> .Empty);
                }

                return(await GetActionsAsync(document, state, fallbackOptions, cancellationToken).ConfigureAwait(false));
            }
        }
            public Editor(
                TService service,
                SemanticDocument document,
                State state,
                CodeAndImportGenerationOptionsProvider fallbackOptions,
                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;
                _fallbackOptions           = fallbackOptions;
                _fromDialog                = fromDialog;
                _generateTypeOptionsResult = generateTypeOptionsResult;
                _cancellationToken         = cancellationToken;
            }
            public static async Task <State?> GenerateAsync(
                TService service,
                SemanticDocument document,
                SyntaxNode node,
                CodeAndImportGenerationOptionsProvider fallbackOptions,
                CancellationToken cancellationToken)
            {
                var fieldNamingRule = await document.Document.GetApplicableNamingRuleAsync(SymbolKind.Field, Accessibility.Private, fallbackOptions, cancellationToken).ConfigureAwait(false);

                var propertyNamingRule = await document.Document.GetApplicableNamingRuleAsync(SymbolKind.Property, Accessibility.Public, fallbackOptions, cancellationToken).ConfigureAwait(false);

                var parameterNamingRule = await document.Document.GetApplicableNamingRuleAsync(SymbolKind.Parameter, Accessibility.NotApplicable, fallbackOptions, cancellationToken).ConfigureAwait(false);

                var state = new State(service, document, fieldNamingRule, propertyNamingRule, parameterNamingRule, fallbackOptions);

                if (!await state.TryInitializeAsync(node, cancellationToken).ConfigureAwait(false))
                {
                    return(null);
                }

                return(state);
            }
示例#26
0
        protected static async Task <Document> AddPropertyAsync(
            Document document,
            Solution destinationSolution,
            IFieldSymbol field,
            IPropertySymbol property,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            var codeGenerationService = document.GetLanguageService <ICodeGenerationService>();

            var fieldDeclaration = field.DeclaringSyntaxReferences.First();

            var context = new CodeGenerationSolutionContext(
                destinationSolution,
                new CodeGenerationContext(
                    contextLocation: fieldDeclaration.SyntaxTree.GetLocation(fieldDeclaration.Span)),
                fallbackOptions);

            var destination = field.ContainingType;

            return(await codeGenerationService.AddPropertyAsync(
                       context, destination, property, cancellationToken).ConfigureAwait(false));
        }
 protected abstract Task <ImmutableArray <CodeAction> > GetCodeActionsAsync(Document document, SyntaxNode node, CodeAndImportGenerationOptionsProvider fallbackOptions, CancellationToken cancellationToken);
示例#28
0
 protected abstract Task <SyntaxNode> RewriteFieldNameAndAccessibilityAsync(string originalFieldName, bool makePrivate, Document document, SyntaxAnnotation declarationAnnotation, CodeAndImportGenerationOptionsProvider fallbackOptions, CancellationToken cancellationToken);
        private static async Task AddPropertyCodeActionsAsync(
            ArrayBuilder <CodeAction> result, SemanticDocument document, State state, CodeAndImportGenerationOptionsProvider fallbackOptions, CancellationToken cancellationToken)
        {
            if (state.IsInOutContext)
            {
                return;
            }

            if (state.IsConstant)
            {
                return;
            }

            if (state.TypeToGenerateIn.TypeKind == TypeKind.Interface && state.IsStatic)
            {
                return;
            }

            // Don't generate properties with a `_` prefix unless that's what the user really wants as their naming style.
            if (await NameIsHighlyUnlikelyToWarrantSymbolAsync(
                    document.Document, state, SymbolKind.Property, state.DetermineMaximalAccessibility(), fallbackOptions, cancellationToken).ConfigureAwait(false))
            {
                return;
            }

            var isOnlyReadAndIsInInterface = state.TypeToGenerateIn.TypeKind == TypeKind.Interface && !state.IsWrittenTo;

            if (isOnlyReadAndIsInInterface || state.IsInConstructor)
            {
                result.Add(new GenerateVariableCodeAction(
                               document, state, generateProperty: true, isReadonly: true, isConstant: false, refKind: GetRefKindFromContext(state), fallbackOptions));
            }

            GenerateWritableProperty(result, document, state, fallbackOptions);
        }
 private static void GenerateWriteableField(ArrayBuilder <CodeAction> result, SemanticDocument document, State state, CodeAndImportGenerationOptionsProvider fallbackOptions)
 {
     result.Add(new GenerateVariableCodeAction(
                    document, state, generateProperty: false, isReadonly: false, isConstant: false, refKind: RefKind.None, fallbackOptions));
 }