private void SetCompletionData(CodeCompletionResults completionData)
        {
            if (_shell.DebugMode)
            {
                _console.WriteLine(completionData.Contexts.ToString());
            }

            if (!completionData.Contexts.HasFlag(CompletionContext.NaturalLanguage) && (completionData.Contexts != CompletionContext.Unexposed || completionData.Contexts == CompletionContext.Unknown))
            {
                if (IncludeSnippets && !(completionData.Contexts.HasFlag(CompletionContext.ArrowMemberAccess) || completionData.Contexts.HasFlag(CompletionContext.DotMemberAccess)))
                {
                    InsertSnippets(completionData.Completions);
                }

                foreach (var result in completionData.Completions)
                {
                    CompletionDataViewModel currentCompletion = null;

                    currentCompletion = unfilteredCompletions.BinarySearch(c => c.Title, result.Suggestion);

                    if (currentCompletion == null)
                    {
                        unfilteredCompletions.Add(CompletionDataViewModel.Create(result));
                    }
                    else
                    {
                        currentCompletion.Overloads++;
                    }
                }
            }
        }
        private void SetCompletionData(List<CodeCompletionData> completionData)
        {
            unfilteredCompletions.Clear();

            foreach (var result in completionData)
            {
                CompletionDataViewModel currentCompletion = null;

                currentCompletion = unfilteredCompletions.BinarySearch(c => c.Title, result.Suggestion);

                if (currentCompletion == null)
                {
                    unfilteredCompletions.Add(CompletionDataViewModel.Create(result));
                }
                else
                {
                    currentCompletion.Overloads++;
                }
            }
        }
        private void SetCompletionData(CodeCompletionResults completionData)
        {
            unfilteredCompletions.Clear();

            if (completionData.Contexts != CompletionContext.Unexposed)
            {
                foreach (var result in completionData.Completions)
                {
                    CompletionDataViewModel currentCompletion = null;

                    currentCompletion = unfilteredCompletions.BinarySearch(c => c.Title, result.Suggestion);

                    if (currentCompletion == null)
                    {
                        unfilteredCompletions.Add(CompletionDataViewModel.Create(result));
                    }
                    else
                    {
                        currentCompletion.Overloads++;
                    }
                }
            }
        }
Exemple #4
0
        public async Task OnKeyUp(KeyEventArgs e)
        {
            var isVisible  = intellisenseControl.IsVisible;
            var caretIndex = editor.CaretIndex;

            var caretChar             = '\0';
            var behindCaretChar       = '\0';
            var behindBehindCaretChar = '\0';

            await Dispatcher.UIThread.InvokeTaskAsync(() =>
            {
                if (caretIndex > 1 && caretIndex < editor.TextDocument.TextLength)
                {
                    behindBehindCaretChar = editor.TextDocument.GetCharAt(caretIndex - 2);
                }

                if (caretIndex > 0 && caretIndex < editor.TextDocument.TextLength)
                {
                    caretChar = editor.TextDocument.GetCharAt(caretIndex);
                }

                if (caretIndex > 0 && caretIndex <= editor.TextDocument.TextLength)
                {
                    behindCaretChar = editor.TextDocument.GetCharAt(caretIndex - 1);
                }
            });

            await CompletionAssistantOnKeyUp(behindCaretChar, behindBehindCaretChar);

            if (e.Key == Key.Escape && e.Modifiers == InputModifiers.None)
            {
                await Dispatcher.UIThread.InvokeTaskAsync(() =>
                {
                    if (completionAssistant.IsVisible)
                    {
                        completionAssistant.Close();
                    }
                    else if (intellisenseControl.IsVisible)
                    {
                        Close();
                    }
                });
            }

            if (IsIntellisenseKey(e))
            {
                if (caretIndex <= intellisenseStartedAt)
                {
                    await Dispatcher.UIThread.InvokeTaskAsync(() => { Close(); });

                    return;
                }

                if (IsIntellisenseResetKey(e))
                {
                    isVisible = false; // We dont actually want to hide, so use backing field.
                                       //currentFilter = string.Empty;
                }

                await Dispatcher.UIThread.InvokeTaskAsync(async() => { await CompleteOnKeyUp(); });

                IEnumerable <CompletionDataViewModel> filteredResults = null;

                if (!intellisenseControl.IsVisible && (IsIntellisenseOpenKey(e) || IsIntellisenseResetKey(e)))
                {
                    var caret = new TextLocation();

                    var behindStartChar = '\0';

                    if (behindCaretChar == ':' && behindBehindCaretChar == ':')
                    {
                        intellisenseStartedAt = caretIndex;
                        intellisenseEndsAt    = intellisenseStartedAt;
                        intellisenseOpenedAt  = intellisenseStartedAt;
                    }
                    else if (behindCaretChar == '>' || behindBehindCaretChar == ':' || behindBehindCaretChar == '>')
                    {
                        intellisenseStartedAt = caretIndex - 1;
                        intellisenseEndsAt    = intellisenseStartedAt;
                        intellisenseOpenedAt  = intellisenseStartedAt;
                    }
                    else
                    {
                        await
                        Dispatcher.UIThread.InvokeTaskAsync(
                            () =>
                        {
                            if (caretIndex > 1)
                            {
                                intellisenseOpenedAt = caretIndex - 1;

                                intellisenseStartedAt = TextUtilities.GetNextCaretPosition(editor.TextDocument, caretIndex,
                                                                                           TextUtilities.LogicalDirection.Backward, TextUtilities.CaretPositioningMode.WordStart);
                            }
                            else
                            {
                                intellisenseOpenedAt  = 1;
                                intellisenseStartedAt = 1;
                            }

                            behindStartChar = editor.TextDocument.GetCharAt(intellisenseStartedAt - 1);

                            if ((behindStartChar.IsWhiteSpace() || !behindStartChar.IsSymbolChar()) && (caretChar.IsWhiteSpace() || !caretChar.IsSymbolChar()))
                            {
                                intellisenseEndsAt = intellisenseStartedAt;
                            }
                            else
                            {
                                intellisenseEndsAt = TextUtilities.GetNextCaretPosition(editor.TextDocument, caretIndex,
                                                                                        TextUtilities.LogicalDirection.Forward, TextUtilities.CaretPositioningMode.WordBorder) - 1;
                            }
                        });
                    }

                    if (IsIntellisenseResetKey(e))
                    {
                        intellisenseStartedAt++;
                        intellisenseOpenedAt++;
                    }

                    await Dispatcher.UIThread.InvokeTaskAsync(() =>
                    {
                        currentFilter = editor.TextDocument.GetText(intellisenseStartedAt, caretIndex - intellisenseStartedAt);

                        caret = GetTextLocation(intellisenseStartedAt);
                    });

                    var codeCompletionResults = await intellisenseControl.DoCompletionRequestAsync(caret.Line, caret.Column);

                    unfilteredCompletions.Clear();

                    if (codeCompletionResults != null)
                    {
                        foreach (var result in codeCompletionResults.Completions)
                        {
                            if (result.Suggestion.ToLower().Contains(currentFilter.ToLower()))
                            {
                                CompletionDataViewModel currentCompletion = null;

                                currentCompletion = unfilteredCompletions.BinarySearch(c => c.Title, result.Suggestion);

                                if (currentCompletion == null)
                                {
                                    unfilteredCompletions.Add(CompletionDataViewModel.Create(result));
                                }
                                else
                                {
                                    currentCompletion.Overloads++;
                                }
                            }
                        }
                    }

                    filteredResults = unfilteredCompletions;
                }
                else
                {
                    await Dispatcher.UIThread.InvokeTaskAsync(
                        () =>
                    {
                        if (intellisenseStartedAt != -1 && caretIndex > intellisenseStartedAt)
                        {
                            currentFilter = editor.TextDocument.GetText(intellisenseStartedAt, caretIndex - intellisenseStartedAt);
                        }
                        else
                        {
                            currentFilter = string.Empty;
                        }
                    });

                    filteredResults = unfilteredCompletions.Where(c => c.Title.ToLower().Contains(currentFilter.ToLower()));
                }

                CompletionDataViewModel suggestion = null;

                if (currentFilter != string.Empty)
                {
                    IEnumerable <CompletionDataViewModel> newSelectedCompletions = null;

                    newSelectedCompletions = filteredResults.Where(s => s.Title.StartsWith(currentFilter));
                    // try find exact match case sensitive

                    if (newSelectedCompletions.Count() == 0)
                    {
                        newSelectedCompletions = filteredResults.Where(s => s.Title.ToLower().StartsWith(currentFilter.ToLower()));
                        // try find non-case sensitve match
                    }

                    if (newSelectedCompletions.Count() == 0)
                    {
                        suggestion = noSelectedCompletion;
                    }
                    else
                    {
                        var newSelectedCompletion = newSelectedCompletions.FirstOrDefault();

                        suggestion = newSelectedCompletion;
                    }
                }
                else
                {
                    suggestion = noSelectedCompletion;
                }

                if (filteredResults?.Count() > 0)
                {
                    if (filteredResults?.Count() == 1 && filteredResults.First().Title == currentFilter)
                    {
                        await Dispatcher.UIThread.InvokeTaskAsync(() => { Close(); });
                    }
                    else
                    {
                        var list = filteredResults.ToList();

                        await Dispatcher.UIThread.InvokeTaskAsync(() =>
                        {
                            intellisenseControl.IsVisible = true;
                        });

                        await Dispatcher.UIThread.InvokeTaskAsync(() =>
                        {
                            intellisenseControl.CompletionData = list;

                            intellisenseControl.SelectedCompletion = null;
                            intellisenseControl.SelectedCompletion = suggestion;
                        });
                    }
                }
                else
                {
                    await Dispatcher.UIThread.InvokeTaskAsync(() => { Close(); });
                }
            }
            else if (IsAllowedNonFilterModificationKey(e))
            {
                // do nothing
            }
            else
            {
                if (intellisenseControl.IsVisible && IsCompletionKey(e))
                {
                    e.Handled = true;
                }

                await Dispatcher.UIThread.InvokeTaskAsync(() => { Close(); });
            }
        }