private SyntaxNode GetWriteExpression(
                TExpressionSyntax writeValue,
                bool keepTrivia,
                string?conflictMessage
                )
            {
                if (ShouldWriteToBackingField())
                {
                    var newIdentifierToken = AddConflictAnnotation(
                        Generator.Identifier(_propertyBackingField.Name),
                        conflictMessage
                        );
                    var newIdentifierName = QualifyIfAppropriate(
                        Generator.IdentifierName(newIdentifierToken)
                        );

                    if (keepTrivia)
                    {
                        newIdentifierName = newIdentifierName.WithTriviaFrom(_identifierName);
                    }

                    return(Generator.AssignmentStatement(
                               _expression.ReplaceNode(_identifierName, newIdentifierName),
                               writeValue
                               ));
                }
                else
                {
                    return(GetSetInvocationExpression(writeValue, keepTrivia, conflictMessage));
                }
            }
            public ReferenceReplacer(
                AbstractReplacePropertyWithMethodsService <TIdentifierNameSyntax, TExpressionSyntax, TCrefSyntax, TStatementSyntax, TPropertySyntax> service,
                SemanticModel semanticModel,
                ISyntaxFactsService syntaxFacts,
                ISemanticFactsService semanticFacts,
                SyntaxEditor editor,
                TIdentifierNameSyntax identifierName,
                IPropertySymbol property, IFieldSymbol propertyBackingField,
                string desiredGetMethodName,
                string desiredSetMethodName,
                CancellationToken cancellationToken)
            {
                _service              = service;
                _semanticModel        = semanticModel;
                _syntaxFacts          = syntaxFacts;
                _semanticFacts        = semanticFacts;
                _editor               = editor;
                _identifierName       = identifierName;
                _property             = property;
                _propertyBackingField = propertyBackingField;
                _desiredGetMethodName = desiredGetMethodName;
                _desiredSetMethodName = desiredSetMethodName;
                _cancellationToken    = cancellationToken;

                _expression = _identifierName;
                _cref       = _service.TryGetCrefSyntax(_identifierName);
                if (_syntaxFacts.IsNameOfMemberAccessExpression(_expression))
                {
                    _expression = (TExpressionSyntax)_expression.Parent !;
                }
            }
            private string CreateDisplayText(TExpressionSyntax expression)
            {
                var singleLineExpression = _semanticDocument.Document.GetLanguageService <ISyntaxFactsService>().ConvertToSingleLine(expression);
                var nodeString           = singleLineExpression.ToString();

                return(CreateDisplayText(nodeString));
            }
예제 #4
0
            public ReferenceReplacer(
                AbstractReplacePropertyWithMethodsService <TIdentifierNameSyntax, TExpressionSyntax, TStatementSyntax> service,
                SemanticModel semanticModel,
                ISyntaxFactsService syntaxFacts,
                ISemanticFactsService semanticFacts,
                SyntaxEditor editor, SyntaxToken nameToken,
                IPropertySymbol property, IFieldSymbol propertyBackingField,
                string desiredGetMethodName,
                string desiredSetMethodName,
                CancellationToken cancellationToken)
            {
                _service              = service;
                _semanticModel        = semanticModel;
                _syntaxFacts          = syntaxFacts;
                _semanticFacts        = semanticFacts;
                _editor               = editor;
                _nameToken            = nameToken;
                _property             = property;
                _propertyBackingField = propertyBackingField;
                _desiredGetMethodName = desiredGetMethodName;
                _desiredSetMethodName = desiredSetMethodName;
                _cancellationToken    = cancellationToken;

                _identifierName = (TIdentifierNameSyntax)nameToken.Parent;
                _expression     = _identifierName;
                if (_syntaxFacts.IsMemberAccessExpressionName(_expression))
                {
                    _expression = _expression.Parent as TExpressionSyntax;
                }
            }
                static bool IsInitializerOfConstant(SemanticDocument document, TExpressionSyntax expression)
                {
                    var syntaxFacts = document.Document.GetRequiredLanguageService <ISyntaxFactsService>();

                    var current = expression;

                    while (syntaxFacts.IsParenthesizedExpression(current.Parent))
                    {
                        current = (TExpressionSyntax)current.Parent;
                    }

                    if (!syntaxFacts.IsEqualsValueClause(current.Parent))
                    {
                        return(false);
                    }

                    var equalsValue = current.Parent;

                    if (!syntaxFacts.IsVariableDeclarator(equalsValue.Parent))
                    {
                        return(false);
                    }

                    var declaration = equalsValue.AncestorsAndSelf().FirstOrDefault(n => syntaxFacts.IsLocalDeclarationStatement(n) || syntaxFacts.IsFieldDeclaration(n));

                    if (declaration == null)
                    {
                        return(false);
                    }

                    var generator = SyntaxGenerator.GetGenerator(document.Document);

                    return(generator.GetModifiers(declaration).IsConst);
                }
 private SyntaxNode GetSetInvocationExpression(
     TExpressionSyntax writeValue, bool keepTrivia, string?conflictMessage)
 {
     return(GetInvocationExpression(_desiredSetMethodName,
                                    argument: Generator.Argument(writeValue),
                                    keepTrivia: keepTrivia,
                                    conflictMessage: conflictMessage));
 }
            protected IEnumerable <ITypeSymbol> GetTypes(TExpressionSyntax expression, bool objectAsDefault = false)
            {
                if (_seenExpressionGetType.Add(expression))
                {
                    return(GetTypes_DoNotCallDirectly(expression, objectAsDefault));
                }

                return(SpecializedCollections.EmptyEnumerable <ITypeSymbol>());
            }
 public InlineMethodContext(
     ImmutableArray <TStatementSyntax> statementsToInsertBeforeInvocationOfCallee,
     TExpressionSyntax inlineExpression,
     bool containsAwaitExpression)
 {
     StatementsToInsertBeforeInvocationOfCallee = statementsToInsertBeforeInvocationOfCallee;
     InlineExpression        = inlineExpression;
     ContainsAwaitExpression = containsAwaitExpression;
 }
예제 #9
0
 static bool IsExpressionConstant(
     SemanticDocument document,
     TExpressionSyntax expression,
     TService service,
     CancellationToken cancellationToken
     )
 {
     if (
         document.SemanticModel.GetConstantValue(expression, cancellationToken) is
         { HasValue : true, Value : var value }
예제 #10
0
 internal IntroduceVariableAllOccurrenceCodeAction(
     TService service,
     SemanticDocument document,
     TExpressionSyntax expression,
     bool allOccurrences,
     bool isConstant,
     bool isLocal,
     bool isQueryLocal)
     : base(service, document, expression, allOccurrences, isConstant, isLocal, isQueryLocal)
 {
 }
예제 #11
0
            public static ImmutableHashSet <ISymbol> GetAllSymbols(
                SemanticModel semanticModel,
                TExpressionSyntax methodDeclarationSyntax,
                CancellationToken cancellationToken)
            {
                var visitor   = new LocalVariableDeclarationVisitor(cancellationToken);
                var operation = semanticModel.GetOperation(methodDeclarationSyntax, cancellationToken);

                visitor.Visit(operation);

                return(visitor._allSymbols.ToImmutableHashSet());
            }
            public IEnumerable <ITypeSymbol> InferTypes(TExpressionSyntax expression, bool filterUnusable = true)
            {
                if (expression != null)
                {
                    if (_seenExpressionInferType.Add(expression))
                    {
                        var types = InferTypesWorker_DoNotCallDirectly(expression);
                        return(Filter(types, filterUnusable));
                    }
                }

                return(SpecializedCollections.EmptyEnumerable <ITypeSymbol>());
            }
예제 #13
0
            public ImmutableArray <TypeInferenceInfo> InferTypes(TExpressionSyntax expression, bool filterUnusable = true)
            {
                if (expression != null)
                {
                    if (_seenExpressionInferType.Add(expression))
                    {
                        var types = InferTypesWorker_DoNotCallDirectly(expression);
                        return(Filter(types, filterUnusable));
                    }
                }

                return(ImmutableArray <TypeInferenceInfo> .Empty);
            }
            private string CreateDisplayText(TExpressionSyntax expression)
            {
                var singleLineExpression = _document.Project.LanguageServices.GetService <ISyntaxFactsService>().ConvertToSingleLine(expression);
                var nodeString           = singleLineExpression.ToString();

                // prevent the display string from being too long
                const int MaxLength = 40;

                if (nodeString.Length > MaxLength)
                {
                    nodeString = nodeString.Substring(0, MaxLength) + "...";
                }

                return(CreateDisplayText(nodeString));
            }
예제 #15
0
            private string CreateDisplayText(TExpressionSyntax expression)
            {
                var singleLineExpression = this.document.Project.LanguageServices.GetService <ISyntaxFactsService>().ConvertToSingleLine(expression);
                var nodeString           = singleLineExpression.ToFullString().Trim();

                // prevent the display string from spanning multiple lines
                nodeString = newlinePattern.Replace(nodeString, " ");

                // prevent the display string from being too long
                const int MaxLength = 40;

                if (nodeString.Length > MaxLength)
                {
                    nodeString = nodeString.Substring(0, MaxLength) + "...";
                }

                return(CreateDisplayText(nodeString));
            }
 internal AbstractIntroduceVariableCodeAction(
     TService service,
     SemanticDocument document,
     TExpressionSyntax expression,
     bool allOccurrences,
     bool isConstant,
     bool isLocal,
     bool isQueryLocal)
 {
     _service        = service;
     _document       = document;
     _expression     = expression;
     _allOccurrences = allOccurrences;
     _isConstant     = isConstant;
     _isLocal        = isLocal;
     _isQueryLocal   = isQueryLocal;
     _title          = CreateDisplayText(expression);
 }
예제 #17
0
            private string CreateDisplayText(TExpressionSyntax expression)
            {
                var singleLineExpression = expression.ConvertToSingleLine();
                var nodeString           = singleLineExpression.ToFullString().Trim();

                // prevent the display string from spanning multiple lines
                nodeString = s_newlinePattern.Replace(nodeString, " ");

                // prevent the display string from being too long
                const int MaxLength = 40;

                if (nodeString.Length > MaxLength)
                {
                    nodeString = nodeString.Substring(0, MaxLength) + "...";
                }

                return(CreateDisplayText(nodeString));
            }
예제 #18
0
            private bool DetermineNamespaceOrTypeToGenerateIn(
                TService service,
                SemanticModel semanticModel,
                TExpressionSyntax leftSide,
                CancellationToken cancellationToken)
            {
                var leftSideInfo = semanticModel.GetSymbolInfo(leftSide, cancellationToken);

                if (leftSideInfo.Symbol != null)
                {
                    var symbol = leftSideInfo.Symbol;

                    if (symbol is INamespaceSymbol)
                    {
                        this.NamespaceToGenerateInOpt = symbol.ToNameDisplayString();
                        return(true);
                    }
                    else if (symbol is INamedTypeSymbol)
                    {
                        // TODO: Code coverage
                        this.TypeToGenerateInOpt = (INamedTypeSymbol)symbol.OriginalDefinition;
                        return(true);
                    }

                    // We bound to something other than a namespace or named type.  Can't generate a
                    // type inside this.
                    return(false);
                }
                else
                {
                    // If it's a dotted name, then perhaps it's a namespace.  i.e. the user wrote
                    // "new Foo.Bar.Baz()".  In this case we want to generate a namespace for
                    // "Foo.Bar".
                    IList <string> nameParts;
                    if (service.TryGetNameParts(leftSide, out nameParts))
                    {
                        this.NamespaceToGenerateInOpt = string.Join(".", nameParts);
                        return(true);
                    }
                }

                return(false);
            }
 public IntroduceParameterDocumentRewriter(AbstractIntroduceParameterService <TExpressionSyntax, TInvocationExpressionSyntax, TObjectCreationExpressionSyntax, TIdentifierNameSyntax> service,
                                           Document originalDocument,
                                           TExpressionSyntax expression,
                                           IMethodSymbol methodSymbol,
                                           SyntaxNode containingMethod,
                                           IntroduceParameterCodeActionKind selectedCodeAction,
                                           bool allOccurrences)
 {
     _service          = service;
     _originalDocument = originalDocument;
     _generator        = SyntaxGenerator.GetGenerator(originalDocument);
     _syntaxFacts      = originalDocument.GetRequiredLanguageService <ISyntaxFactsService>();
     _semanticFacts    = originalDocument.GetRequiredLanguageService <ISemanticFactsService>();
     _expression       = expression;
     _methodSymbol     = methodSymbol;
     _containerMethod  = containingMethod;
     _actionKind       = selectedCodeAction;
     _allOccurrences   = allOccurrences;
 }
예제 #20
0
 protected abstract SyntaxNode GetExpression(TExpressionSyntax expression);
예제 #21
0
 protected abstract IEnumerable <TypeInferenceInfo> GetTypes_DoNotCallDirectly(TExpressionSyntax expression, bool objectAsDefault);
예제 #22
0
            private bool TryInitializeIdentifierName(
                TService service,
                SemanticDocument semanticDocument,
                TSimpleNameSyntax identifierName,
                CancellationToken cancellationToken)
            {
                this.SimpleName = identifierName;
                if (!service.TryInitializeIdentifierNameState(semanticDocument, identifierName, cancellationToken,
                                                              out var identifierToken, out var simpleNameOrMemberAccessExpression))
                {
                    return(false);
                }

                this.IdentifierToken = identifierToken;
                this.SimpleNameOrMemberAccessExpression = simpleNameOrMemberAccessExpression;

                var semanticModel = semanticDocument.SemanticModel;
                var semanticFacts = semanticDocument.Document.GetLanguageService <ISemanticFactsService>();
                var syntaxFacts   = semanticDocument.Document.GetLanguageService <ISyntaxFactsService>();

                if (semanticFacts.IsWrittenTo(semanticModel, this.SimpleNameOrMemberAccessExpression, cancellationToken) ||
                    syntaxFacts.IsInNamespaceOrTypeContext(this.SimpleNameOrMemberAccessExpression))
                {
                    return(false);
                }

                // Now, try to bind the invocation and see if it succeeds or not.  if it succeeds and
                // binds uniquely, then we don't need to offer this quick fix.
                cancellationToken.ThrowIfCancellationRequested();
                var containingType = semanticModel.GetEnclosingNamedType(identifierToken.SpanStart, cancellationToken);

                if (containingType == null)
                {
                    return(false);
                }

                var semanticInfo = semanticModel.GetSymbolInfo(this.SimpleNameOrMemberAccessExpression, cancellationToken);

                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                if (semanticInfo.Symbol != null)
                {
                    return(false);
                }
                // Either we found no matches, or this was ambiguous. Either way, we might be able
                // to generate a method here.  Determine where the user wants to generate the method
                // into, and if it's valid then proceed.
                if (!service.TryDetermineTypeToGenerateIn(
                        semanticDocument, containingType, simpleNameOrMemberAccessExpression, cancellationToken,
                        out var typeToGenerateIn, out var isStatic))
                {
                    return(false);
                }

                if (!isStatic)
                {
                    return(false);
                }

                this.TypeToGenerateIn = typeToGenerateIn;
                return(true);
            }
예제 #23
0
 public Relational(BinaryOperatorKind operatorKind, TExpressionSyntax value)
 {
     OperatorKind = operatorKind;
     Value        = value;
 }
예제 #24
0
 public Argument(RefKind refKind, string name, TExpressionSyntax expression)
 {
     RefKind    = refKind;
     Name       = name ?? "";
     Expression = expression;
 }
예제 #25
0
 public Range(TExpressionSyntax lowerBound, TExpressionSyntax higherBound)
 {
     LowerBound  = lowerBound;
     HigherBound = higherBound;
 }
 protected abstract IEnumerable <ITypeSymbol> GetTypes_DoNotCallDirectly(TExpressionSyntax expression, bool objectAsDefault);
 protected abstract IEnumerable <ITypeSymbol> InferTypesWorker_DoNotCallDirectly(TExpressionSyntax expression);
예제 #28
0
 public Constant(TExpressionSyntax expression)
 => ExpressionSyntax = expression;
예제 #29
0
 protected abstract IEnumerable <TypeInferenceInfo> InferTypesWorker_DoNotCallDirectly(TExpressionSyntax expression);