private bool CanIntroduceVariable(
                bool isSpanEmpty,
                CancellationToken cancellationToken)
            {
                if (!_service.CanIntroduceVariableFor(this.Expression))
                {
                    return(false);
                }

                if (isSpanEmpty && this.Expression is TNameSyntax)
                {
                    // to extract a name, you must have a selection (this avoids making the refactoring too noisy)
                    return(false);
                }

                if (this.Expression is TTypeSyntax && !(this.Expression is TNameSyntax))
                {
                    // name syntax can introduce variables, but not other type syntaxes
                    return(false);
                }

                // Even though we're creating a variable, we still ask if we can be replaced with an
                // RValue and not an LValue.  This is because introduction of a local adds a *new* LValue
                // location, and we want to ensure that any writes will still happen to the *original*
                // LValue location.  i.e. if you have: "a[1] = b" then you don't want to change that to
                // "var c = a[1]; c = b", as that write is no longer happening into the right LValue.
                //
                // In essence, this says "i can be replaced with an expression as long as I'm not being
                // written to".
                var semanticFacts = this.Document.Project.LanguageServices.GetService <ISemanticFactsService>();

                return(semanticFacts.CanReplaceWithRValue(this.Document.SemanticModel, this.Expression, cancellationToken));
            }
Beispiel #2
0
            private bool CanIntroduceVariable(
                CancellationToken cancellationToken)
            {
                // Don't generate a variable for an expression that's the only expression in a
                // statement.  Otherwise we'll end up with something like "v;" which is not
                // legal in C#.
                if (!_service.CanIntroduceVariableFor(this.Expression))
                {
                    return(false);
                }

                if (this.Expression is TTypeSyntax)
                {
                    return(false);
                }

                // Even though we're creating a variable, we still ask if we can be replaced with an
                // RValue and not an LValue.  This is because introduction of a local adds a *new* LValue
                // location, and we want to ensure that any writes will still happen to the *original*
                // LValue location.  i.e. if you have: "a[1] = b" then you don't want to change that to
                // "var c = a[1]; c = b", as that write is no longer happening into the right LValue.
                //
                // In essense, this says "i can be replaced with an expression as long as i'm not being
                // written to".
                var semanticFacts = this.Document.Project.LanguageServices.GetService <ISemanticFactsService>();

                return(semanticFacts.CanReplaceWithRValue(this.Document.SemanticModel, this.Expression, cancellationToken));
            }