Esempio n. 1
0
        private bool IsExtentTokenExpected(SnapshotSpan range, TextExtent extent, out ITrackingSpan trackSpan, out PSharpParser parser)
        {
            string extentText  = extent.Span.GetText();
            var    extentToken = string.IsNullOrWhiteSpace(extentText) ? null : new PSharpLexer().Tokenize(extentText).FirstOrDefault();

            if (extentToken == null)
            {
                trackSpan = null;
                parser    = null;
                return(false);
            }

            // TODO: Minimize the re-parse span for speed.
            var snapshot = extent.Span.Snapshot;

            trackSpan = range.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
            var preSpan = new SnapshotSpan(snapshot, new Span(snapshot.GetLineFromLineNumber(0).Start,
                                                              trackSpan.GetStartPoint(snapshot).Position));

            var tokens = new PSharpLexer().Tokenize(preSpan.GetText());

            parser = new PSharpParser(ParsingOptions.CreateForVsLanguageService());
            try
            {
                parser.ParseTokens(tokens);
            }
            catch (ParsingException)
            {
                // Parsing exception is expected
            }
            return(this.IsExpectedTokenType(extentToken, parser.GetExpectedTokenTypes()));
        }
Esempio n. 2
0
        private IEnumerable <SuggestedActionSet> GetSuggestedActions(TextExtent textExtent, ITrackingSpan trackingSpan, PSharpParser parser)
        {
            var span = textExtent.Span;
            var word = span.Snapshot.TextBuffer.CurrentSnapshot.GetText().Substring(span.Start.Position, span.End.Position - span.Start.Position);

            // Start with prefix search
            var suggestions = CompletionSource.RefineAvailableKeywords(parser.GetExpectedTokenTypes(), word, usePrefix: false).ToList();

            foreach (var suggestion in suggestions)
            {
                yield return(new SuggestedActionSet(new ISuggestedAction[] { new ErrorFixSuggestedAction(word, suggestion, trackingSpan) }));
            }
        }
Esempio n. 3
0
        public IEnumerable <SuggestedActionSet> GetSuggestedActions(ISuggestedActionCategorySet requestedActionCategories,
                                                                    SnapshotSpan range, CancellationToken cancellationToken)
        {
            return(Enumerable.Empty <SuggestedActionSet>());

            TextExtent extent;

            if (!this.TryGetWordUnderCaret(out extent) || !extent.IsSignificant)
            {
                return(Enumerable.Empty <SuggestedActionSet>());
            }

            var extentToken = new PSharpLexer().Tokenize(extent.Span.GetText()).FirstOrDefault();

            if (extentToken == null)
            {
                return(Enumerable.Empty <SuggestedActionSet>());
            }

            var snapshot  = extent.Span.Snapshot;
            var trackSpan = range.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
            var preSpan   = new SnapshotSpan(snapshot, new Span(snapshot.GetLineFromLineNumber(0).Start,
                                                                trackSpan.GetStartPoint(snapshot).Position));

            var tokens = new PSharpLexer().Tokenize(preSpan.GetText());
            var parser = new PSharpParser(ParsingOptions.CreateDefault());

            parser.ParseTokens(tokens);

            var expected = parser.GetExpectedTokenTypes();

            if (this.IsExpectedTokenType(extentToken, expected))
            {
                return(Enumerable.Empty <SuggestedActionSet>());
            }

            var errorFixAction = new ErrorFixSuggestedAction(trackSpan);

            return(new SuggestedActionSet[] { new SuggestedActionSet(new ISuggestedAction[] { errorFixAction }) });
        }
Esempio n. 4
0
        void ICompletionSource.AugmentCompletionSession(ICompletionSession session, IList <CompletionSet> completionSets)
        {
            return; // TODO short-circuited for now

            var keywords = Keywords.Get();

            var snapshot  = this.Buffer.CurrentSnapshot;
            var trackSpan = this.FindTokenSpanAtPosition(session.GetTriggerPoint(this.Buffer), session);
            var preSpan   = new SnapshotSpan(snapshot, new Span(snapshot.GetLineFromLineNumber(0).Start,
                                                                trackSpan.GetStartPoint(snapshot).Position));

            var tokens = new PSharpLexer().Tokenize(preSpan.GetText());
            var parser = new PSharpParser(ParsingOptions.CreateDefault());

            parser.ParseTokens(tokens);
            this.RefineAvailableKeywords(parser.GetExpectedTokenTypes(), keywords);

            this.CompletionList = new List <Completion>();

            foreach (var keyword in keywords)
            {
                this.CompletionList.Add(new Completion(keyword.Key, keyword.Key,
                                                       keyword.Value.Item1, null, null));
            }

            if (keywords.Count == 0)
            {
                return;
            }

            completionSets.Add(new CompletionSet(
                                   "Tokens",
                                   "Tokens",
                                   trackSpan,
                                   this.CompletionList,
                                   null));
        }
Esempio n. 5
0
        public Task <bool> HasSuggestedActionsAsync(ISuggestedActionCategorySet requestedActionCategories,
                                                    SnapshotSpan range, CancellationToken cancellationToken)
        {
            return(Task.Factory.StartNew(() =>
            {
                return false;

                TextExtent extent;
                if (!this.TryGetWordUnderCaret(out extent))
                {
                    return false;
                }

                var extentToken = new PSharpLexer().Tokenize(extent.Span.GetText()).FirstOrDefault();
                if (extentToken == null)
                {
                    return false;
                }

                var snapshot = extent.Span.Snapshot;
                var trackSpan = range.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
                var preSpan = new SnapshotSpan(snapshot, new Span(snapshot.GetLineFromLineNumber(0).Start,
                                                                  trackSpan.GetStartPoint(snapshot).Position));

                var tokens = new PSharpLexer().Tokenize(preSpan.GetText());
                var parser = new PSharpParser(ParsingOptions.CreateDefault());
                parser.ParseTokens(tokens);

                var expected = parser.GetExpectedTokenTypes();
                if (this.IsExpectedTokenType(extentToken, expected))
                {
                    return false;
                }

                return extent.IsSignificant;
            }));
        }
Esempio n. 6
0
        private IEnumerable <TokenType> GetAvailableKeywords(int precedingLength)
        {
            // Parse preceding words for context to filter suggestions. TODO: Minimize the re-parse span for speed.
            var precedingString = precedingLength <= 0 ? string.Empty : this.Buffer.CurrentSnapshot.GetText().Substring(0, precedingLength).Trim();

            if (precedingString.Length > 0)
            {
                var tokens = new PSharpLexer().Tokenize(precedingString);
                if (tokens.Count > 0)
                {
                    var parser = new PSharpParser(ParsingOptions.CreateForVsLanguageService());
                    try
                    {
                        parser.ParseTokens(tokens);
                    }
                    catch (ParsingException)
                    {
                        // We expect a parsing exception as we are probably in the middle of a word
                    }
                    return(parser.GetExpectedTokenTypes());
                }
            }
            return(new List <TokenType>());
        }