コード例 #1
0
ファイル: CodeEditor.cs プロジェクト: guojianbin/dotnetpad
        private async Task ShowCompletionAsync(char?triggerChar)
        {
            completionCancellation.Cancel();

            if (WorkspaceService == null || DocumentFile == null)
            {
                return;
            }

            completionCancellation = new CancellationTokenSource();
            var cancellationToken = completionCancellation.Token;

            try
            {
                if (completionWindow == null && (triggerChar == null || triggerChar == '.' || IsAllowedLanguageLetter(triggerChar.Value)))
                {
                    var position = CaretOffset;
                    var word     = GetWord(position);

                    var document          = WorkspaceService.GetDocument(DocumentFile);
                    var completionService = CompletionService.GetService(document);

                    var completionList = await Task.Run(async() =>
                                                        await completionService.GetCompletionsAsync(document, position, cancellationToken: cancellationToken), cancellationToken);

                    if (completionList == null)
                    {
                        return;
                    }

                    cancellationToken.ThrowIfCancellationRequested();

                    using (new PerformanceTrace("CompletionWindow.Show", DocumentFile))
                    {
                        completionWindow = new CompletionWindow(TextArea)
                        {
                            WindowStyle        = WindowStyle.None,
                            AllowsTransparency = true
                        };
                        completionWindow.MaxWidth  = completionWindow.Width = 340;
                        completionWindow.MaxHeight = completionWindow.Height = 206;
                        foreach (var completionItem in completionList.Items)
                        {
                            completionWindow.CompletionList.CompletionData.Add(new CodeCompletionData(completionItem.DisplayText,
                                                                                                      () => GetDescriptionAsync(completionService, document, completionItem), completionItem.Tags));
                        }

                        if (triggerChar == null || IsAllowedLanguageLetter(triggerChar.Value))
                        {
                            completionWindow.StartOffset = word.Item1;
                            completionWindow.CompletionList.SelectItem(word.Item2);
                        }
                        completionWindow.Show();
                        completionWindow.Closed += (s2, e2) => completionWindow = null;
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
        }