void completionWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // cancel closing if part way into a table or column name
            var lineState = ParseLine();

            if (SpacePressed && (lineState.LineState == LineState.Column || lineState.LineState == LineState.Table))
            {
                e.Cancel = true;
            }
            var line = GetCurrentLine();

            if (line.EndsWith("("))
            {
                var funcName = DaxLineParser.GetPreceedingWord(line.TrimEnd('('));
                ShowInsight(funcName);
            }
        }
        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, _daxState);
                        }
                        break;

                    case "'":
                        PopulateCompletionData(data, IntellisenseMetadataTypes.Tables);
                        break;

                    default:
                        switch (_daxState.LineState)
                        {
                        case LineState.Column:
                            PopulateCompletionData(data, IntellisenseMetadataTypes.Columns, _daxState);
                            break;

                        case LineState.Table:
                            PopulateCompletionData(data, IntellisenseMetadataTypes.Tables, _daxState);
                            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.DocumentGetText(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)
                        {
                            Log.Verbose("InsightWindow == null : {IsNull}", _editor.InsightWindow == null);
                            if (_editor.InsightWindow != null && _editor.InsightWindow.IsVisible)
                            {
                                Log.Verbose("hiding insight window");
                                _editor.InsightWindow.Visibility = Visibility.Collapsed;
                                //_editor.InsightWindow = null;
                            }

                            Log.Verbose("CW null: {CompletionWindowNull} CW.Vis: {CompletionWindowVisible} IW null: {insightWindowNull} IW.Vis: {InsightWindowVisible}", completionWindow == null, completionWindow.Visibility.ToString(), _editor.InsightWindow == null, completionWindow.Visibility.ToString());

                            completionWindow.Show();
                            completionWindow.Closing      += completionWindow_Closing;
                            completionWindow.PreviewKeyUp += completionWindow_PreviewKeyUp;
                            completionWindow.Closed       += delegate
                            {
                                _editor.DisposeCompletionWindow();
                            };
                        }
                        else
                        {
                            Log.Debug("{class} {method} {message}", "DaxIntellisenseProvider", "ProcessTextEntered", "Closing CompletionWindow as it has no matching items");

                            completionWindow.Close();
                            _editor.DisposeCompletionWindow();
                            completionWindow = null;
                        }
                    }
                    else
                    {
                        _editor.DisposeCompletionWindow();
                        completionWindow = null;
                    }
                }

                if (e.Text[0] == '(')
                {
                    var funcName = DaxLineParser.GetPreceedingWord(GetCurrentLine().TrimEnd('(').Trim()).ToLower();
                    Log.Verbose("Func: {Function}", funcName);
                    ShowInsight(funcName);
                }
            }
            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));
            }
        }