コード例 #1
0
        public void ProcessTextEntered(object sender, System.Windows.Input.TextCompositionEventArgs e, ref ICSharpCode.AvalonEdit.CodeCompletion.CompletionWindow completionWindow)
        {
            if (HasThrownException) return; // exit here if intellisense has previous thrown and exception

            try
            {
                if (completionWindow != null)
                {
                    // close the completion window if it has no items
                    if (!completionWindow.CompletionList.ListBox.HasItems)
                    {
                        completionWindow.Close();
                        return;
                    }
                    // close the completion window if the current text is a 100% match for the current item
                    var txt = ((TextArea)sender).Document.GetText(new TextSegment() { StartOffset = completionWindow.StartOffset, EndOffset = completionWindow.EndOffset });
                    var selectedItem = completionWindow.CompletionList.SelectedItem;
                    if (string.Compare(selectedItem.Text, txt, true) == 0 || string.Compare(selectedItem.Content.ToString(), txt, true) == 0) completionWindow.Close();

                    return;
                }

                if (char.IsLetterOrDigit(e.Text[0]) || "\'[".Contains(e.Text[0]))
                {

                    // exit if the completion window is already showing
                    if (completionWindow != null) return;

                    // exit if we are inside a string or comment
                    _daxState = ParseLine();
                    var lineState = _daxState.LineState;
                    if (lineState == LineState.String || _editor.IsInComment()) return;

                    // don't show intellisense if we are in the measure name of a DEFINE block
                    if (DaxLineParser.IsLineMeasureDefinition(GetCurrentLine())) return;

                    // TODO add insights window for Function parameters
                    //InsightWindow insightWindow = new InsightWindow(sender as ICSharpCode.AvalonEdit.Editing.TextArea);

                    completionWindow = new CompletionWindow(sender as ICSharpCode.AvalonEdit.Editing.TextArea);
                    completionWindow.CloseAutomatically = false;

                    completionWindow.CompletionList.BorderThickness = new System.Windows.Thickness(1);

                    if (char.IsLetterOrDigit(e.Text[0]))
                    {
                        // if the window was opened by a letter or digit include it in the match segment
                        //completionWindow.StartOffset -= 1;
                        completionWindow.StartOffset = _daxState.StartOffset;
                        System.Diagnostics.Debug.WriteLine("Setting Completion Offset: {0}", _daxState.StartOffset);
                    }

                    IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;

                    switch (e.Text)
                    {
                        case "[":

                            string tableName = GetPreceedingTableName();
                            if (string.IsNullOrWhiteSpace(tableName))
                            {
                                PopulateCompletionData(data, IntellisenseMetadataTypes.Measures);
                            }
                            else
                            {
                                PopulateCompletionData(data, IntellisenseMetadataTypes.Columns, tableName);
                            }
                            break;
                        case "'":
                            PopulateCompletionData(data, IntellisenseMetadataTypes.Tables);
                            break;
                        default:
                            switch (_daxState.LineState)
                            {
                                case LineState.Column:
                                    PopulateCompletionData(data, IntellisenseMetadataTypes.Columns, _daxState.TableName);
                                    break;
                                case LineState.Table:
                                    PopulateCompletionData(data, IntellisenseMetadataTypes.Tables);
                                    break;
                                case LineState.Measure:
                                    PopulateCompletionData(data, IntellisenseMetadataTypes.Measures);
                                    break;
                                case LineState.Dmv:
                                    PopulateCompletionData(data, IntellisenseMetadataTypes.DMV);
                                    break;
                                default:
                                    PopulateCompletionData(data, IntellisenseMetadataTypes.ALL);
                                    break;
                            }
                            break;
                    }
                    if (data.Count > 0)
                    {
                        //var line = GetCurrentLine();
                        //System.Diagnostics.Debug.Assert(line.Length >= _daxState.EndOffset);
                        var txt = _editor.Document.GetText(new TextSegment() { StartOffset = _daxState.StartOffset, EndOffset = _daxState.EndOffset });
                        //var txt = line.Substring(_daxState.StartOffset,_daxState.EndOffset - _daxState.StartOffset);

                        completionWindow.CompletionList.SelectItem(txt);
                        // only show the completion window if we have valid items to display
                        if (completionWindow.CompletionList.ListBox.HasItems)
                        {
                            completionWindow.Show();
                            completionWindow.Closing += completionWindow_Closing;
                            completionWindow.PreviewKeyUp += completionWindow_PreviewKeyUp;
                            completionWindow.Closed += delegate
                            {
                                _editor.DisposeCompletionWindow();
                            };
                        }
                        else
                        {
                            _editor.DisposeCompletionWindow();
                            completionWindow = null;
                        }
                    }
                    else
                    {
                        completionWindow = null;
                    }
                }
            }
            catch(Exception ex)
            {
                HasThrownException = true;
                Log.Error("{class} {method} {exception} {stacktrace}", "DaxIntellisenseProvider", "ProcessTextEntered", ex.Message, ex.StackTrace);
                Document.OutputError(string.Format("Intellisense Disabled for this window - {0}", ex.Message));
            }
        }