Ejemplo n.º 1
0
            protected override async Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(
                object options, CancellationToken cancellationToken)
            {
                var result = (PickMembersResult)options;

                if (result.IsCanceled)
                {
                    return(SpecializedCollections.EmptyEnumerable <CodeActionOperation>());
                }

                var addNullChecksOption = result.Options.FirstOrDefault(o => o.Id == AddNullChecksId);

                if (addNullChecksOption != null)
                {
                    // If we presented the 'Add null check' option, then persist whatever value
                    // the user chose.  That way we'll keep that as the default for the next time
                    // the user opens the dialog.
                    var globalOptions = _document.Project.Solution.Services.GetRequiredService <ILegacyGlobalOptionsWorkspaceService>();
                    globalOptions.SetGenerateEqualsAndGetHashCodeFromMembersGenerateOperators(_document.Project.Language, addNullChecksOption.Value);
                }

                var addNullChecks = (addNullChecksOption?.Value ?? false);
                var state         = await State.TryGenerateAsync(
                    _service, _document, _textSpan, _containingType, _desiredAccessibility,
                    result.Members, _fallbackOptions, cancellationToken).ConfigureAwait(false);

                if (state == null)
                {
                    return(SpecializedCollections.EmptyEnumerable <CodeActionOperation>());
                }

                // There was an existing constructor that matched what the user wants to create.
                // Generate it if it's the implicit, no-arg, constructor, otherwise just navigate
                // to the existing constructor
                if (state.MatchingConstructor != null)
                {
                    if (state.MatchingConstructor.IsImplicitlyDeclared)
                    {
                        var codeAction = new FieldDelegatingCodeAction(_service, _document, state, addNullChecks, _fallbackOptions);
                        return(await codeAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false));
                    }

                    var constructorReference = state.MatchingConstructor.DeclaringSyntaxReferences[0];
                    var constructorSyntax    = await constructorReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false);

                    var constructorTree     = constructorSyntax.SyntaxTree;
                    var constructorDocument = _document.Project.Solution.GetRequiredDocument(constructorTree);
                    return(ImmutableArray.Create <CodeActionOperation>(new DocumentNavigationOperation(
                                                                           constructorDocument.Id, constructorSyntax.SpanStart)));
                }
                else
                {
                    var codeAction = state.DelegatedConstructor != null
                        ? new ConstructorDelegatingCodeAction(_service, _document, state, addNullChecks, _fallbackOptions)
                        : (CodeAction) new FieldDelegatingCodeAction(_service, _document, state, addNullChecks, _fallbackOptions);

                    return(await codeAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false));
                }
            }
Ejemplo n.º 2
0
            protected override async Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(
                object options, CancellationToken cancellationToken)
            {
                var result = (PickMembersResult)options;

                if (result.IsCanceled)
                {
                    return(ImmutableArray <CodeActionOperation> .Empty);
                }

                var addNullChecksOption = result.Options.FirstOrDefault(o => o.Id == AddNullChecksId);

                if (addNullChecksOption != null)
                {
                    // If we presented the 'Add null check' option, then persist whatever value
                    // the user chose.  That way we'll keep that as the default for the next time
                    // the user opens the dialog.
                    var workspace = _document.Project.Solution.Workspace;
                    workspace.Options = workspace.Options.WithChangedOption(
                        GenerateConstructorFromMembersOptions.AddNullChecks,
                        _document.Project.Language,
                        addNullChecksOption.Value);
                }

                var addNullChecks = (addNullChecksOption?.Value).GetValueOrDefault();
                var state         = State.TryGenerate(
                    _service, _document, _textSpan, _containingType,
                    result.Members, cancellationToken);

                // There was an existing constructor that matched what the user wants to create.
                // Generate it if it's the implicit, no-arg, constructor, otherwise just navigate
                // to the existing constructor
                if (state.MatchingConstructor != null)
                {
                    if (state.MatchingConstructor.IsImplicitlyDeclared)
                    {
                        var codeAction = new FieldDelegatingCodeAction(_service, _document, state, addNullChecks);
                        return(await codeAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false));
                    }

                    var constructorReference = state.MatchingConstructor.DeclaringSyntaxReferences[0];
                    var constructorSyntax    = await constructorReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false);

                    var constructorTree     = constructorSyntax.SyntaxTree;
                    var constructorDocument = _document.Project.Solution.GetDocument(constructorTree);
                    return(ImmutableArray.Create <CodeActionOperation>(new DocumentNavigationOperation(
                                                                           constructorDocument.Id, constructorSyntax.SpanStart)));
                }
                else
                {
                    var codeAction = state.DelegatedConstructor != null
                        ? new ConstructorDelegatingCodeAction(_service, _document, state, addNullChecks)
                        : (CodeAction) new FieldDelegatingCodeAction(_service, _document, state, addNullChecks);

                    return(await codeAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false));
                }
            }
Ejemplo n.º 3
0
            protected override async Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(
                object options, CancellationToken cancellationToken)
            {
                var result = (PickMembersResult)options;

                if (result.IsCanceled)
                {
                    return(ImmutableArray <CodeActionOperation> .Empty);
                }

                var state = State.TryGenerate(
                    _service, _document, _textSpan, _containingType,
                    result.Members, cancellationToken);

                // There was an existing constructor that matched what the user wants to create.
                // Generate it if it's the implicit, no-arg, constructor, otherwise just navigate
                // to the existing constructor
                if (state.MatchingConstructor != null)
                {
                    if (state.MatchingConstructor.IsImplicitlyDeclared)
                    {
                        var codeAction = new FieldDelegatingCodeAction(_service, _document, state);
                        return(await codeAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false));
                    }

                    var constructorReference = state.MatchingConstructor.DeclaringSyntaxReferences[0];
                    var constructorSyntax    = await constructorReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false);

                    var constructorTree     = constructorSyntax.SyntaxTree;
                    var constructorDocument = _document.Project.Solution.GetDocument(constructorTree);
                    return(ImmutableArray.Create <CodeActionOperation>(new DocumentNavigationOperation(
                                                                           constructorDocument.Id, constructorSyntax.SpanStart)));
                }
                else
                {
                    var codeAction = state.DelegatedConstructor != null
                        ? new ConstructorDelegatingCodeAction(_service, _document, state)
                        : (CodeAction) new FieldDelegatingCodeAction(_service, _document, state);

                    return(await codeAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false));
                }
            }