public void SyntaxFacts_Identifier_GetValidIdentifier_ReturnsSame_ForValidIdentifier()
        {
            const string text       = "lorem";
            var          identifier = SyntaxFacts.GetValidIdentifier(text);

            Assert.Equal(text, identifier);
        }
Beispiel #2
0
            private static string BuildColumns(int indent, IEnumerable <TableColumnInstanceSymbol> symbols)
            {
                var indentString = new string(' ', indent);

                var isFirst = true;
                var sb      = new StringBuilder();

                foreach (var symbol in symbols)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        sb.Append(@",");
                        sb.AppendLine();
                        sb.Append(indentString);
                    }

                    var table  = SyntaxFacts.GetValidIdentifier(symbol.TableInstance.Name);
                    var column = SyntaxFacts.GetValidIdentifier(symbol.Column.Name);
                    sb.Append(table);
                    sb.Append(@".");
                    sb.Append(column);
                }

                return(sb.ToString());
            }
Beispiel #3
0
        private static string CreateCondition(TableInstanceSymbol left, IReadOnlyList <ColumnSymbol> leftColumns, TableInstanceSymbol right, IReadOnlyList <ColumnSymbol> rightColumns)
        {
            var sb = new StringBuilder();

            for (var i = 0; i < leftColumns.Count; i++)
            {
                if (sb.Length > 0)
                {
                    sb.Append(@" AND ");
                }

                var leftColumn  = leftColumns[i];
                var rightColumn = rightColumns[i];

                sb.Append(SyntaxFacts.GetValidIdentifier(right.Name));
                sb.Append(@".");
                sb.Append(SyntaxFacts.GetValidIdentifier(rightColumn.Name));
                sb.Append(@" = ");
                sb.Append(SyntaxFacts.GetValidIdentifier(left.Name));
                sb.Append(@".");
                sb.Append(SyntaxFacts.GetValidIdentifier(leftColumn.Name));
            }

            return(sb.ToString());
        }
        private static CompletionItem CreateSymbolCompletionGroup(string name, IEnumerable <Symbol> symbols)
        {
            var multiple = symbols.Skip(1).Any();

            if (!multiple)
            {
                return(CreateSymbolCompletion(symbols.First()));
            }

            var hasNonInvocables = symbols.Any(s => !(s is InvocableSymbol));

            if (!hasNonInvocables)
            {
                return(CreateInvocableCompletionGroup(symbols));
            }

            var displayText   = name;
            var insertionText = SyntaxFacts.GetValidIdentifier(name);

            var sb = new StringBuilder();

            sb.Append(Resources.AmbiguousName);
            foreach (var symbol in symbols)
            {
                sb.AppendLine();
                sb.Append(@"  ");
                sb.Append(symbol);
            }

            var description = sb.ToString();

            return(new CompletionItem(displayText, insertionText, description, Glyph.AmbiguousName));
        }
        public void SyntaxFacts_Identifier_GetValidIdentifier_ReturnsParenthesized_ForSpaces()
        {
            const string text       = "lorem ipsum";
            const string expected   = "[" + text + "]";
            var          identifier = SyntaxFacts.GetValidIdentifier(text);

            Assert.Equal(expected, identifier);
        }
        private static void AppendName(this ICollection <SymbolMarkupToken> markup, SymbolMarkupKind kind, string name)
        {
            var displayName = string.IsNullOrEmpty(name)
                                  ? @"?"
                                  : SyntaxFacts.GetValidIdentifier(name);

            markup.Append(kind, displayName);
        }
        private static CompletionItem CreateSymbolCompletion(Symbol symbol)
        {
            var displayText   = symbol.Name;
            var insertionText = SyntaxFacts.GetValidIdentifier(symbol.Name);
            var description   = SymbolMarkup.ForSymbol(symbol).ToString();
            var glyph         = symbol.GetGlyph();

            return(new CompletionItem(displayText, insertionText, description, glyph, symbol));
        }
        public void SyntaxFacts_Identifier_GetValidIdentifier_ReturnsSame_ForContextualKeywords()
        {
            foreach (var keywordKind in SyntaxFacts.GetContextualKeywordKinds())
            {
                var text       = keywordKind.GetText();
                var identifier = SyntaxFacts.GetValidIdentifier(text);

                Assert.Equal(text, identifier);
            }
        }
        public void SyntaxFacts_Identifier_GetValidIdentifier_ReturnsParenthesized_ForReservedKeywords()
        {
            foreach (var keywordKind in SyntaxFacts.GetReservedKeywordKinds())
            {
                var text       = keywordKind.GetText();
                var expected   = "[" + text + "]";
                var identifier = SyntaxFacts.GetValidIdentifier(text);

                Assert.Equal(expected, identifier);
            }
        }
Beispiel #10
0
        private static string GetColumnReference(int index, QueryColumnInstanceSymbol column)
        {
            if (!string.IsNullOrEmpty(column.Name))
            {
                return(SyntaxFacts.GetValidIdentifier(column.Name));
            }

            var ordinal = index + 1;

            return(ordinal.ToString(CultureInfo.InvariantCulture));
        }
Beispiel #11
0
 protected override IEnumerable <CodeIssue> GetIssues(SemanticModel semanticModel, OrderedQuerySyntax node)
 {
     return(from orderByColumn in node.Columns
            let selector = orderByColumn.ColumnSelector as LiteralExpressionSyntax
                           where selector != null && selector.Value is int
                           let column = semanticModel.GetSymbol(orderByColumn)
                                        where column != null && !string.IsNullOrEmpty(column.Name)
                                        let namedReference = SyntaxFacts.GetValidIdentifier(column.Name)
                                                             let action = new[] { new ReplaceOrdingalByNamedReferenceCodeAction(selector, namedReference) }
            select new CodeIssue(CodeIssueKind.Warning, selector.Span, action));
 }
        public override IAggregatable CreateAggregatable(Type argumentType)
        {
            // Create an expression to determine the type of inputType + inputType

            var addDataContext = DataContext.Empty.AddVariables(new VariableSymbol(@"Left", argumentType),
                                                                new VariableSymbol(@"Right", argumentType));

            var addExpression = Expression <object> .Create(addDataContext, @"Left + Right");

            Type sumType;

            try
            {
                sumType = addExpression.Resolve();
            }
            catch (CompilationException)
            {
                return(null);
            }

            // In some cases, the result of the adding both arguments might be different from
            // arguments type, e.g. adding to bytes will result in an int.
            //
            // In order to get the correct answer, let's compose a new expression.

            var leftVariable   = new VariableSymbol(@"Left", argumentType);
            var rightVariable  = new VariableSymbol(@"Right", argumentType);
            var sumDataContext = DataContext.Empty.AddVariables(leftVariable, rightVariable);
            var sumExpression  = Expression <object> .Create(sumDataContext, @"Left + Right");

            try
            {
                var resultType = sumExpression.Resolve();
                if (resultType != sumType)
                {
                    return(null);
                }
            }
            catch (CompilationException)
            {
                return(null);
            }

            // Conversion from inputType to sumType

            var conversionInputVariable = new VariableSymbol(@"Input", argumentType);
            var convertionDataContext   = DataContext.Empty.AddVariables(conversionInputVariable);
            var conversionExpression    = Expression <object> .Create(convertionDataContext, $"CAST(@Input AS {SyntaxFacts.GetValidIdentifier(sumType.Name)})");

            try
            {
                conversionExpression.Resolve();
            }
            catch (CompilationException)
            {
                return(null);
            }

            return(new SumAggregatable(sumExpression, leftVariable, rightVariable, conversionExpression, conversionInputVariable));
        }
Beispiel #13
0
            protected override void GetChanges(TextChangeSet changeSet)
            {
                var name = SyntaxFacts.GetValidIdentifier(_symbol.TableInstance.Name);

                changeSet.InsertText(_node.Span.Start, name + @".");
            }