public override void KeyDown(KeyEventArgs args)
        {
            char?key = this.keyTypeConverter.ConvertToChar(args.Key);

            // Check if the parethesis key was pressed
            if (key.HasValue && key.Value == '(')
            {
                // Create a regex line data
                RegexLineData regexLineData = RegexLineData.CreateFromCaretPosition(this.view);

                // Check if the line contains a new Regex statement
                if (IsValid(regexLineData))
                {
                    this.view.TextBuffer.Insert(regexLineData.CaretPosition, key.ToString());
                    // Show the regex editor
                    RegexEditorResult editorResult = this.regexEditorService.ShowEditor();
                    // Insert the edited pattern in the line
                    if (editorResult.Result.HasValue && editorResult.Result.Value)
                    {
                        this.view.TextBuffer.Insert(regexLineData.CaretPosition + 1, string.Format("@\"{0}\"", editorResult.Pattern));
                    }

                    args.Handled = true;
                }
            }
        }
        public override void PreprocessMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            // Check if ctrl+click
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {
                // Create a regex line data
                RegexLineData regexLineData = RegexLineData.CreateFromCaretPosition(this.view);

                // Check if the line contains a new Regex statement
                if (IsValid(regexLineData))
                {
                    // Get the regular expression parameter of the Regex ctor
                    SnapshotSpan?expression = regexLineData.Expression;

                    if (expression.HasValue)
                    {
                        // Check if the click was made over the regular expression parameter.
                        if (regexLineData.CaretPosition >= expression.Value.Start.Position && regexLineData.CaretPosition <= expression.Value.End.Position)
                        {
                            RegexEditorResult editorResult = this.regexEditorService.ShowEditor(regexLineData.Expression.Value.GetText());
                            if (editorResult.Result.HasValue && editorResult.Result.Value)
                            {
                                this.view.TextBuffer.Replace(regexLineData.Expression.Value.Span, editorResult.Pattern);
                            }

                            e.Handled = true;
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Creates the regex line for the current position in the view.
        /// </summary>
        /// <param name="view"></param>
        /// <param name="line"></param>
        /// <returns></returns>
        internal static RegexLineData CreateFromTextViewLine(ITextView view, ITextViewLine line)
        {
            RegexLineData data = new RegexLineData();

            data.View = view;

            // TODO: RC1
            //data.Line = line.SnapshotLine;
            data.Line = line.Snapshot.GetLineFromPosition(line.Start.Position);

            return(data);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates the regex line for the current position in the view.
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        internal static RegexLineData CreateFromCaretPosition(IWpfTextView view)
        {
            RegexLineData data = new RegexLineData();

            data.View = view;

            data.CaretPoint = view.Caret.Position.Point.GetPoint(
                view.TextBuffer,
                PositionAffinity.Predecessor);

            data.Line = view.TextSnapshot.GetLineFromPosition(data.CaretPosition);

            return(data);
        }
        public override void PreprocessMouseMove(MouseEventArgs e)
        {
            // Look for regex lines to show the tooltip/hand cursor
            foreach (ITextViewLine line in this.view.TextViewLines)
            {
                RegexLineData regexLineData = RegexLineData.CreateFromTextViewLine(this.view, line);

                // Check if the regex line contains a new Regex statement
                if (IsValid(regexLineData))
                {
                    // Get the pattern parameter
                    SnapshotSpan?expression = regexLineData.Expression;

                    if (expression.HasValue)
                    {
                        // Get the marker geometry of the pattern parameter
                        Geometry expressionGeometry = view.TextViewLines.GetMarkerGeometry(expression.Value);
                        if (expressionGeometry != null && expressionGeometry.Bounds.Contains(GetPointRelativeToView(e)))
                        {
                            e.Handled = true;

                            if (!IsToolTipShown)
                            {
                                IsToolTipShown = true;
                                IToolTipProvider toolTipProvider = this.toolTipProviderFactory.GetToolTipProvider(this.view);

                                // Show the tooltip
                                toolTipProvider.ShowToolTip(expression.Value.Snapshot.CreateTrackingSpan(
                                                                expression.Value.Span,
                                                                SpanTrackingMode.EdgeExclusive), "Ctrl+Click to open the regex editor",
                                                            PopupStyles.DismissOnMouseLeaveText);
                            }
                            // If the ctrl key is pressed change the cursor to hand
                            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
                            {
                                e.MouseDevice.OverrideCursor = Cursors.Hand;
                            }
                            return;
                        }
                    }
                }
            }

            IsToolTipShown = false;
            e.MouseDevice.OverrideCursor = null;
        }
 /// <summary>
 /// Returns true if the regex line ends with a "new Regex" statement (for C# or VB)
 /// </summary>
 /// <param name="regexLineData"></param>
 /// <returns></returns>
 private bool IsValid(RegexLineData regexLineData)
 {
     return(NewRegexStatementForCSharpOrVisualBasic.IsMatch(regexLineData.Text.TrimEnd()));
 }