public ITextStructureNavigator CreateTextStructureNavigator(ITextBuffer textBuffer)
        {
            if (textBuffer == null)
            {
                throw new ArgumentNullException("textBuffer");
            }

            ITextStructureNavigator delegateTextStructureNavigator = TextStructureNavigatorSelectorService.CreateTextStructureNavigator(textBuffer, AnyContentType);

            return(new AlloyTextStructureNavigator(textBuffer, delegateTextStructureNavigator));
        }
Пример #2
0
        private string GetCurrentEditorWord()
        {
            IVsTextView view = GetActiveTextView();

            if (view == null)
            {
                return(null);
            }

            try
            {
                IWpfTextView textView = GetTextViewFromVsTextView(view);
                if (textView == null || TextStructureNavigatorSelectorService == null)
                {
                    return(null);
                }
                ITextStructureNavigator textStructureNavigator = TextStructureNavigatorSelectorService.GetTextStructureNavigator(textView.TextBuffer);
                string column = GetCaretWord(textView, textStructureNavigator);

                // Note: GetCaretColumn returns 0-based positions. Guidelines are 1-based
                // positions.
                // However, do not subtract one here since the caret is positioned to the
                // left of
                // the given column and the guidelines are positioned to the right. We
                // want the
                // guideline to line up with the current caret position. e.g. When the
                // caret is
                // at position 1 (zero-based), the status bar says column 2. We want to
                // add a
                // guideline for column 1 since that will place the guideline where the
                // caret is.
                return(column);
            }
            catch (InvalidOperationException)
            {
                return(null);
            }
        }
Пример #3
0
        public override void AugmentCompletionSession(ICompletionSession session, IList <CompletionSet> completionSets)
        {
            if (session == null || completionSets == null)
            {
                return;
            }

            ITrackingPoint triggerPoint = session.GetTriggerPoint(TextBuffer);

            if (triggerPoint != null)
            {
                IntellisenseController controller     = GetControllerForView(session.TextView);
                CompletionInfo         completionInfo = controller.CompletionInfo;
                ITextSnapshot          snapshot       = triggerPoint.TextBuffer.CurrentSnapshot;
                SnapshotPoint          point          = triggerPoint.GetPoint(snapshot);
                bool extendLeft = false;
                bool extend     = true;

                // labels includes both implicit and explicit labels
                var labels = FindLabelsInScope(point);

                IntellisenseInvocationType invocationType = completionInfo.InvocationType;
                CompletionInfoType         infoType       = completionInfo.InfoType;

                switch (invocationType)
                {
                case IntellisenseInvocationType.Default:
                    extend = infoType == CompletionInfoType.GlobalInfo;
                    break;

                case IntellisenseInvocationType.BackspaceDeleteOrBackTab:
                case IntellisenseInvocationType.IdentifierChar:
                case IntellisenseInvocationType.Sharp:
                case IntellisenseInvocationType.Space:
                case IntellisenseInvocationType.ShowMemberList:
                    break;

                default:
                    extendLeft = true;
                    break;
                }

                TextExtent extentOfWord = default(TextExtent);
                if (extend)
                {
                    ITextBuffer             textBuffer      = TextBuffer;
                    ITextStructureNavigator navigator       = TextStructureNavigatorSelectorService.CreateTextStructureNavigator(textBuffer, textBuffer.ContentType);
                    SnapshotPoint           currentPosition = new SnapshotPoint(snapshot, triggerPoint.GetPosition(snapshot));
                    extentOfWord = navigator.GetExtentOfWord(currentPosition);
                    if (extentOfWord.Span.Start == point)
                    {
                        TextExtent extentOfPreviousWord = navigator.GetExtentOfWord(currentPosition - 1);
                        if (extentOfPreviousWord.IsSignificant && extentOfPreviousWord.Span.End == point && IsCompletionPrefix(extentOfPreviousWord))
                        {
                            extentOfWord = extentOfPreviousWord;
                        }
                        else
                        {
                            extend = false;
                        }
                    }
                }

                if (!extend || !extentOfWord.IsSignificant)
                {
                    SnapshotSpan span = new SnapshotSpan(point, 0);
                    extentOfWord = new TextExtent(span, false);
                }

                if (invocationType == IntellisenseInvocationType.BackspaceDeleteOrBackTab && extentOfWord.Span.Length > 0)
                {
                    string str3 = snapshot.GetText(extentOfWord.Span);
                    if (!string.IsNullOrWhiteSpace(str3))
                    {
                        while (CommitCharacters.IndexOf(str3[0]) > 0)
                        {
                            SnapshotSpan span2 = extentOfWord.Span;
                            SnapshotSpan span3 = new SnapshotSpan(snapshot, span2.Start + 1, span2.Length - 1);
                            extentOfWord = new TextExtent(span3, false);
                            str3         = snapshot.GetText(extentOfWord.Span);
                            if (string.IsNullOrEmpty(str3))
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        SnapshotSpan span4 = new SnapshotSpan(snapshot, extentOfWord.Span.End, 0);
                        extentOfWord = new TextExtent(span4, false);
                        completionInfo.InvocationType = IntellisenseInvocationType.Default;
                    }
                }

                if (completionInfo.InfoType == CompletionInfoType.AutoListMemberInfo && extentOfWord.Span.GetText().StartsWith("$") && labels.Count == 0)
                {
                    session.Dismiss();
                    return;
                }

                ITrackingSpan applicableTo = snapshot.CreateTrackingSpan(extentOfWord.Span, SpanTrackingMode.EdgeInclusive, TrackingFidelityMode.Forward);
                if (extendLeft)
                {
                    SnapshotSpan textSoFarSpan = new SnapshotSpan(snapshot, extentOfWord.Span.Start, triggerPoint.GetPoint(snapshot));
                    string       textSoFar     = textSoFarSpan.GetText();
                    applicableTo = snapshot.CreateTrackingSpan(point.Position - textSoFar.Length, textSoFar.Length, SpanTrackingMode.EdgeInclusive, TrackingFidelityMode.Forward);
                }

                IEnumerable <Completion> context          = GetContextCompletions(triggerPoint.GetPoint(snapshot), (AntlrIntellisenseController)controller, session);
                IEnumerable <Completion> keywords         = GetKeywordCompletions();
                IEnumerable <Completion> snippets         = GetSnippetCompletions();
                IEnumerable <Completion> labelCompletions = GetLabelCompletions(labels);
                //SnapshotSpan? Provider.IntellisenseCache.GetExpressionSpan(triggerPoint.GetPoint(snapshot));

                IEnumerable <Completion> completions        = context.Concat(keywords).Concat(snippets).Concat(labelCompletions);
                IEnumerable <Completion> orderedCompletions = completions.Distinct(CompletionDisplayNameComparer.CurrentCulture).OrderBy(i => i.DisplayText, StringComparer.CurrentCultureIgnoreCase);

                CompletionSet completionSet = new CompletionSet("AntlrCompletions", "Antlr Completions", applicableTo, orderedCompletions, EmptyCompletions);
                completionSets.Add(completionSet);
            }
        }
Пример #4
0
        public override void AugmentCompletionSession(ICompletionSession session, IList <CompletionSet> completionSets)
        {
            if (session == null || completionSets == null)
            {
                return;
            }

            ITrackingPoint triggerPoint = session.GetTriggerPoint(TextBuffer);

            if (triggerPoint != null)
            {
                IntellisenseController controller     = null;
                CompletionInfo         completionInfo = controller.CompletionInfo;
                ITextSnapshot          snapshot       = triggerPoint.TextBuffer.CurrentSnapshot;
                SnapshotPoint          point          = triggerPoint.GetPoint(snapshot);
                bool extendLeft = false;
                bool extend     = true;

                switch (completionInfo.InvocationType)
                {
                case IntellisenseInvocationType.Default:
                    extend = completionInfo.InfoType == CompletionInfoType.GlobalInfo;
                    break;

                case IntellisenseInvocationType.BackspaceDeleteOrBackTab:
                case IntellisenseInvocationType.IdentifierChar:
                case IntellisenseInvocationType.Sharp:
                case IntellisenseInvocationType.Space:
                case IntellisenseInvocationType.ShowMemberList:
                    break;

                default:
                    extendLeft = true;
                    break;
                }

                TextExtent extentOfWord = default(TextExtent);
                if (extend)
                {
                    ITextBuffer             textBuffer      = TextBuffer;
                    ITextStructureNavigator navigator       = TextStructureNavigatorSelectorService.CreateTextStructureNavigator(textBuffer, textBuffer.ContentType);
                    SnapshotPoint           currentPosition = new SnapshotPoint(snapshot, triggerPoint.GetPosition(snapshot));
                    extentOfWord = navigator.GetExtentOfWord(currentPosition);
                    if (extentOfWord.Span.Start == point)
                    {
                        TextExtent extentOfPreviousWord = navigator.GetExtentOfWord(currentPosition - 1);
                        if (extentOfPreviousWord.IsSignificant && extentOfPreviousWord.Span.End == point && IsCompletionPrefix(extentOfPreviousWord))
                        {
                            extentOfWord = extentOfPreviousWord;
                        }
                        else
                        {
                            extend = false;
                        }
                    }
                }

                if (!extend || !extentOfWord.IsSignificant)
                {
                    SnapshotSpan span = new SnapshotSpan(point, 0);
                    extentOfWord = new TextExtent(span, false);
                }

                if (completionInfo.InvocationType == IntellisenseInvocationType.BackspaceDeleteOrBackTab && extentOfWord.Span.Length > 0)
                {
                    string str3 = snapshot.GetText(extentOfWord.Span);
                    if (!string.IsNullOrWhiteSpace(str3))
                    {
                        while (CommitCharacters.IndexOf(str3[0]) > 0)
                        {
                            SnapshotSpan span2 = extentOfWord.Span;
                            SnapshotSpan span3 = new SnapshotSpan(snapshot, span2.Start + 1, span2.Length - 1);
                            extentOfWord = new TextExtent(span3, false);
                            str3         = snapshot.GetText(extentOfWord.Span);
                            if (string.IsNullOrEmpty(str3))
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        SnapshotSpan span4 = new SnapshotSpan(snapshot, extentOfWord.Span.End, 0);
                        extentOfWord = new TextExtent(span4, false);
                        completionInfo.InvocationType = IntellisenseInvocationType.Default;
                    }
                }

                ITrackingSpan applicableTo = snapshot.CreateTrackingSpan(extentOfWord.Span, SpanTrackingMode.EdgeInclusive, TrackingFidelityMode.Forward);
                if (extendLeft)
                {
                    SnapshotSpan textSoFarSpan = new SnapshotSpan(snapshot, extentOfWord.Span.Start, triggerPoint.GetPoint(snapshot));
                    string       textSoFar     = textSoFarSpan.GetText();
                    applicableTo = snapshot.CreateTrackingSpan(point.Position - textSoFar.Length, textSoFar.Length, SpanTrackingMode.EdgeInclusive, TrackingFidelityMode.Forward);
                }

                /* Context Tree
                 *
                 *  - Global
                 *    - Options
                 *    - Tokens
                 *    - AttributeScope
                 *      - ACTION
                 *    - Named Action
                 *      - ACTION
                 *    - Rule
                 *      - Arguments
                 *        - ARG_ACTION
                 *      - AttributeScope
                 *        - ACTION
                 *      - Named Action
                 *        - ACTION
                 *      - Options
                 *      - Alternative
                 *        - Alternative*
                 *        - Rewrite
                 *        - ACTION
                 */

                List <Completion> completions        = new List <Completion>();
                List <Completion> completionBuilders = new List <Completion>();

                List <IntellisenseContext> intellisenseContexts = GetIntellisenseContexts(triggerPoint);
                foreach (var context in intellisenseContexts)
                {
                    context.AugmentCompletionSession(session, completions, completionBuilders);
                }

                string        moniker       = "AntlrCompletions";
                string        displayName   = "ANTLR Completions";
                CompletionSet completionSet = new CompletionSet(moniker, displayName, applicableTo, completions, completionBuilders);
                completionSets.Add(completionSet);
            }
        }