Пример #1
0
        internal string EnsureUniqueParameterName(
            string baseName,
            ISymbol containingSymbol,
            SemanticModel semanticModel,
            bool isCaseSensitive = true,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (containingSymbol == null)
            {
                throw new ArgumentNullException(nameof(containingSymbol));
            }

            if (semanticModel == null)
            {
                throw new ArgumentNullException(nameof(semanticModel));
            }

            if (containingSymbol.Kind == SymbolKind.Method)
            {
                var methodSymbol = (IMethodSymbol)containingSymbol;

                containingSymbol = methodSymbol.PartialImplementationPart ?? methodSymbol;
            }

            SyntaxNode containingNode = containingSymbol.GetSyntax(cancellationToken);

            ImmutableArray <ISymbol> symbols = semanticModel
                                               .GetDeclaredSymbols(containingNode, excludeAnonymousTypeProperty: true, cancellationToken: cancellationToken)
                                               .AddRange(semanticModel.LookupSymbols(containingNode.SpanStart));

            return(EnsureUniqueName(baseName, symbols, isCaseSensitive));
        }
Пример #2
0
        private static async Task <Document> RefactorAsync(
            Document document,
            ForEachStatementSyntax forEachStatement,
            IEnumerable <ISymbol> deconstructSymbols,
            ISymbol identifierSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            int         position              = forEachStatement.SpanStart;
            ITypeSymbol elementType           = semanticModel.GetForEachStatementInfo(forEachStatement).ElementType;
            SyntaxNode  enclosingSymbolSyntax = semanticModel.GetEnclosingSymbolSyntax(position, cancellationToken);

            ImmutableArray <ISymbol> declaredSymbols = semanticModel.GetDeclaredSymbols(enclosingSymbolSyntax, excludeAnonymousTypeProperty: true, cancellationToken);

            ImmutableArray <ISymbol> symbols = declaredSymbols
                                               .Concat(semanticModel.LookupSymbols(position))
                                               .Distinct()
                                               .Except(deconstructSymbols)
                                               .ToImmutableArray();

            Dictionary <string, string> newNames = deconstructSymbols
                                                   .Select(parameter =>
            {
                string name    = StringUtility.FirstCharToLower(parameter.Name);
                string newName = NameGenerator.Default.EnsureUniqueName(name, symbols);

                return(name: parameter.Name, newName);
            })
                                                   .ToDictionary(f => f.name, f => f.newName);

            var rewriter = new DeconstructForeachVariableRewriter(identifierSymbol, newNames, semanticModel, cancellationToken);

            var newStatement = (StatementSyntax)rewriter.Visit(forEachStatement.Statement);

            DeclarationExpressionSyntax variableExpression = DeclarationExpression(
                CSharpFactory.VarType().WithTriviaFrom(forEachStatement.Type),
                ParenthesizedVariableDesignation(
                    deconstructSymbols.Select(parameter =>
            {
                return(SingleVariableDesignation(
                           Identifier(SyntaxTriviaList.Empty, newNames[parameter.Name], SyntaxTriviaList.Empty)));
            })
                    .ToSeparatedSyntaxList <VariableDesignationSyntax>())
                .WithTriviaFrom(forEachStatement.Identifier))
                                                             .WithFormatterAnnotation();

            ForEachVariableStatementSyntax forEachVariableStatement = ForEachVariableStatement(
                forEachStatement.AttributeLists,
                forEachStatement.AwaitKeyword,
                forEachStatement.ForEachKeyword,
                forEachStatement.OpenParenToken,
                variableExpression,
                forEachStatement.InKeyword,
                forEachStatement.Expression,
                forEachStatement.CloseParenToken,
                newStatement);

            return(await document.ReplaceNodeAsync(forEachStatement, forEachVariableStatement, cancellationToken).ConfigureAwait(false));
        }
Пример #3
0
        protected override IEnumerable <CompletionItem> GetItems(SemanticModel semanticModel, int position, ConditionedJoinedTableReferenceSyntax node)
        {
            if (node.OnKeyword.IsMissing || position < node.OnKeyword.Span.End)
            {
                return(Enumerable.Empty <CompletionItem>());
            }

            var leftInstances  = semanticModel.GetDeclaredSymbols(node.Left).ToImmutableArray();
            var rightInstances = semanticModel.GetDeclaredSymbols(node.Right).ToImmutableArray();
            var relations      = semanticModel.Compilation.DataContext.Relations;

            return(from left in leftInstances
                   from right in rightInstances
                   from relation in relations
                   where IsApplicable(relation, left.Table, right.Table)
                   select GetCompletionItem(relation, left, right));
        }