示例#1
0
        public void TokenStreamLineBreakTest()
        {
            var tokens = new[]
            {
                new TestToken(TestTokenType.Token1, TextRange.FromBounds(0, 1)),
                new TestToken(TestTokenType.Token2, TextRange.FromBounds(3, 4)),
                new TestToken(TestTokenType.Token3, TextRange.FromBounds(6, 8)),
                new TestToken(TestTokenType.Token1, TextRange.FromBounds(12, 20)),
                new TestToken(TestTokenType.Token2, TextRange.FromBounds(20, 21)),
            };

            var           ts           = CreateTokenStream(tokens);
            ITextProvider textProvider = new TextStream("1  2  11  \r\n12345678x");

            ts.IsLineBreakAfter(textProvider, ts.Position).Should().BeFalse();

            ts.Advance(2);
            ts.IsLineBreakAfter(textProvider, ts.Position).Should().BeTrue();

            ts.Advance(-1);
            ts.IsLineBreakAfter(textProvider, ts.Position).Should().BeFalse();

            ts.MoveToNextLine(textProvider);
            ts.CurrentToken.Should().HaveType(TestTokenType.Token1);

            string s = textProvider.GetText(ts.CurrentToken);

            s.Should().Be("12345678");
        }
示例#2
0
        internal static bool IsPackageListCompletion(IEditorBuffer editorBuffer, int position)
        {
            var snapshot     = editorBuffer.CurrentSnapshot;
            var line         = snapshot.GetLineFromPosition(position);
            var lineText     = line.GetText();
            var linePosition = position - line.Start;

            // We should be either at library(| or inside library(|)
            // or over package name like in library(ba|se)

            // Go left and right looking for
            var           tokenizer    = new RTokenizer();
            ITextProvider textProvider = new TextStream(lineText);
            var           c            = tokenizer.Tokenize(textProvider, 0, textProvider.Length);
            var           tokens       = new TokenStream <RToken>(c, RToken.EndOfStreamToken);

            while (!tokens.IsEndOfStream())
            {
                if (tokens.CurrentToken.Start >= linePosition)
                {
                    break;
                }

                if (tokens.CurrentToken.TokenType == RTokenType.Identifier)
                {
                    var identifier = textProvider.GetText(tokens.CurrentToken);
                    if (identifier == "library" || identifier == "require")
                    {
                        tokens.MoveToNextToken();

                        if (tokens.CurrentToken.TokenType == RTokenType.OpenBrace)
                        {
                            var openBrace = tokens.CurrentToken;
                            while (!tokens.IsEndOfStream())
                            {
                                if (tokens.CurrentToken.TokenType == RTokenType.CloseBrace)
                                {
                                    if (linePosition >= openBrace.End && linePosition <= tokens.CurrentToken.Start)
                                    {
                                        return(true);
                                    }
                                    return(false);
                                }
                                else if (tokens.NextToken.TokenType == RTokenType.EndOfStream)
                                {
                                    return(true);
                                }
                                tokens.MoveToNextToken();
                            }
                        }
                    }
                }
                tokens.MoveToNextToken();
            }
            return(false);
        }
示例#3
0
        public static IReadOnlyList <ISignatureInfo> ParseSignatures(string usageContent)
        {
            // RD signature text may contain \dots sequence  which denotes ellipsis.
            // R parser does not know  about it and hence we will replace \dots by ...
            // Also, signatures may contain S3 method info like
            // '\method{as.matrix}{data.frame}(x, rownames.force = NA, \dots)'
            // which we need to filter out since they are irrelevant to intellisense.

            var signatures = new List <ISignatureInfo>();

            usageContent = usageContent.Replace(@"\dots", "...");

            var tokenizer    = new RTokenizer(separateComments: true);
            var collection   = tokenizer.Tokenize(usageContent);
            var textProvider = new TextStream(usageContent);
            var tokens       = new TokenStream <RToken>(collection, RToken.EndOfStreamToken);

            var parseContext = new ParseContext(textProvider,
                                                TextRange.FromBounds(tokens.CurrentToken.Start, textProvider.Length),
                                                tokens, tokenizer.CommentTokens);

            while (!tokens.IsEndOfStream())
            {
                // Filter out '\method{...}{}(signature)
                if (tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace)
                {
                    // Check if { is preceded by \method
                }

                if (tokens.CurrentToken.TokenType != RTokenType.Identifier)
                {
                    break;
                }

                var functionName = textProvider.GetText(tokens.CurrentToken);
                tokens.MoveToNextToken();

                var info = ParseSignature(functionName, parseContext);
                if (info != null)
                {
                    signatures.Add(info);
                }
            }

            return(signatures);
        }