private DaxLineState ParseLine()
        {
            //Log.Debug("{class} {method} {message}", "DaxIntellisenseProvider", "ParseLine", "start");
            string line    = GetCurrentLine();
            int    pos     = _editor.CaretOffset > 0 ? _editor.CaretOffset - 1 : 0;
            var    loc     = _editor.DocumentGetLocation(pos);
            var    docLine = _editor.DocumentGetLineByOffset(pos);

            Log.Debug("{class} {method} {line} col:{column} off:{offset}", "DaxIntellisenseProvider", "ParseLine", line, loc.Column, docLine.Offset);
            return(DaxLineParser.ParseLine(line, loc.Column, docLine.Offset));
        }
        private string GetPreceedingTableName()
        {
            string tableName = "";

            try
            {
                string line = GetCurrentLine();
                tableName = DaxLineParser.GetPreceedingTableName(line);
            }
            catch (Exception ex)
            {
                Log.Error("{class} {method} {error}", "DaxIntellisenseProvider", "GetPreceedingTableName", ex.Message);
            }
            return(tableName);
        }
        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);
            }
        }
Beispiel #4
0
        private LinePosition GetPreceedingWordSegment(IDocument document, ISegment completionSegment)
        {
            string line = "";

            int pos = completionSegment.EndOffset - 1;
            var loc = document.GetLocation(pos);

            Log.Debug("{class} {method} pos:{position}", "DaxCompletionData", "GetPreceedingWordSegment", pos);
            var docLine = document.GetLineByOffset(pos);

            //line = textArea.Document.GetText(docLine.Offset, loc.Column);
            line = document.GetText(docLine.Offset, docLine.Length);

            Log.Verbose("{class} {method} {message}", "DaxCompletionData", "GetPreceedingWordSegment", "line: " + line);
            var daxState = DaxLineParser.ParseLine(line, loc.Column, 0);

            //TODO - look ahead to see if we have a table/column/function end character that we should replace upto
            return(DaxLineParser.GetPreceedingWordSegment(docLine.Offset, loc.Column, line, daxState));
        }
        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));
            }
        }