Exemplo n.º 1
0
        private void ToggleComment()
        {
            int offset = _editor.CaretOffset;

            if (XParser.IsInsideComment(_editor.Text, offset))
            {
                Uncomment();
            }
            else
            {
                Comment();
            }
        }
Exemplo n.º 2
0
        private void textEditor_TextArea_KeyDown(object sender, KeyEventArgs e)
        {
            var modifiers = Keyboard.Modifiers;

            if (e.Key == Key.D7 &&
                (modifiers & ModifierKeys.Control) == ModifierKeys.Control &&
                (modifiers & ModifierKeys.Alt) == 0
                )
            {
                int offset = _editor.CaretOffset;
                if (XParser.IsInsideComment(_editor.Text, offset))
                {
                    //uncomment
                    int idxClosing = _editor.Text.IndexOf("-->", offset);
                    if (idxClosing >= 0)
                    {
                        _editor.Text = _editor.Text.Remove(idxClosing, 3);
                    }
                    int idxOpening = _editor.Text.LastIndexOf("<!--", offset);
                    Debug.Assert(idxOpening >= 0);
                    if (idxOpening >= 0)
                    {
                        _editor.Text = _editor.Text.Remove(idxOpening, 4);
                    }
                    _editor.CaretOffset = Math.Max(offset - 4, 0);
                }
                else
                {
                    if (_editor.SelectionLength == 0)
                    {
                        //comment whole line
                        DocumentLine line = _editor.Document.GetLineByOffset(offset);
                        _editor.Text = _editor.Text.Insert(line.EndOffset, "-->");
                        _editor.Text = _editor.Text.Insert(line.Offset, "<!--");
                    }
                    else
                    {
                        //comment selection
                        int selStart = _editor.SelectionStart;
                        int selEnd   = _editor.SelectionStart + _editor.SelectionLength;
                        _editor.Text = _editor.Text.Insert(selEnd, "-->");
                        _editor.Text = _editor.Text.Insert(selStart, "<!--");
                    }
                    _editor.CaretOffset = offset + 4;
                }
                e.Handled = true;
            }
        }
Exemplo n.º 3
0
        private bool completeOnCtrlSpace()
        {
            if (XParser.IsInsideComment(_editor.Text, _editor.CaretOffset))
            {
                IList <ICompletionData> data = new List <ICompletionData>();
                data.Add(new MyCompletionData("-->"));
                showCompletion(data);
                return(true);
            }

            if (XParser.IsInsideEmptyElement(_editor.Text, _editor.CaretOffset))
            {
                //replace <test/> --> <test></test>

                IList <ICompletionData> data = new List <ICompletionData>();

                data.Add(new ActionCompletionData("Expand empty tag", null, (textArea, completionSegment, eventArgs) =>
                {
                    int newCursor;
                    int startReplace;
                    int lengthReplace;
                    string replaceWith;
                    XActor.ExpandEmptyTag(_editor.Text, _editor.CaretOffset, out newCursor, out startReplace,
                                          out lengthReplace, out replaceWith);

                    textArea.Document.Replace(startReplace, lengthReplace, replaceWith);
                    _editor.CaretOffset = newCursor;
                }));

                showCompletion(data);
                return(true);
            }

            {
                bool result = checkAttributeCompletion();
                if (result)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 4
0
        private void textEditor_TextArea_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right &&
                (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control &&
                (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
            {
                string xml    = _editor.Document.Text;
                int    offset = _editor.CaretOffset;

                try
                {
                    if (XParser.IsClosingElement(xml, offset))
                    {
                        int i = xml.IndexOf('>', offset);
                        if (i > 0)
                        {
                            _editor.CaretOffset = i + 1;
                        }
                    }
                    else if (XParser.IsInsideElementDeclaration(xml, offset))
                    {
                        if (XParser.IsInsideAttributeKey(xml, offset))
                        {
                            int i = xml.IndexOf('=', offset);
                            if (i > 0)
                            {
                                _editor.CaretOffset = i + 1;
                            }
                        }
                        else if (XParser.IsInsideAttributeValue(xml, offset))
                        {
                            int i = xml.IndexOf('>', offset);
                            if (i > 0)
                            {
                                _editor.CaretOffset = i + 1;
                            }
                        }
                        else
                        {
                            //element name
                            int i = xml.IndexOf('>', offset);
                            if (i > 0)
                            {
                                _editor.CaretOffset = i + 1;
                            }
                        }
                    }
                    else if (XParser.IsInsideComment(xml, offset))
                    {
                        int i = xml.IndexOf("-->", offset);
                        if (i > 0)
                        {
                            _editor.CaretOffset = i + 4;
                        }
                    }
                    else
                    {
                        //text, goto next element
                        int i = xml.IndexOf('<', offset);
                        if (i > 0)
                        {
                            if (i == offset)
                            {
                                i = xml.IndexOf('>', offset);
                            }
                            _editor.CaretOffset = i + 1;
                        }
                    }
                }
                catch
                {
                    Debug.Fail("");
                }

                e.Handled = true;
            }
        }
Exemplo n.º 5
0
        private bool checkAttributeCompletion()
        {
            if (_schemaParser == null)
            {
                return(false);
            }

            int offset = _editor.CaretOffset;

            if (XParser.IsInsideElementDeclaration(_editor.Text, offset - 1))
            {
                string element = XParser.GetElementAtCursorFuzzy(_editor.Text, offset - 1);

                IList <IXsdNode> all  = _schemaParser.GetAllNodes();
                IXsdNode         node = getNodeWithName(all, element);
                if (node != null)
                {
                    ICollection <XsdAttribute> attrs = getAttributeNames(node);
                    if (attrs != null && attrs.Count > 0)
                    {
                        IList <ICompletionData> data = new List <ICompletionData>();
                        foreach (XsdAttribute attr in attrs)
                        {
                            string desc = null;
                            if (attr.Annotation != null && attr.Annotation.Count > 0)
                            {
                                StringBuilder description = new StringBuilder();
                                foreach (string ann in attr.Annotation)
                                {
                                    description.AppendLine(ann);
                                }

                                desc = description.ToString();
                            }

                            data.Add(new ActionCompletionData(attr.Name, desc, (area, segment, args) =>
                            {
                                int idxOfSpace        = area.Document.Text.IndexOf(' ', offset - 1);
                                int idxOfElementClose = area.Document.Text.IndexOf('>', offset - 1);

                                int insertAt = Math.Min(idxOfSpace, idxOfElementClose);

                                if (insertAt < 0)
                                {
                                    insertAt = offset;
                                }
                                else
                                {
                                    if (idxOfElementClose == insertAt)
                                    {
                                        area.Document.Insert(insertAt, " ");
                                    }
                                    insertAt = insertAt + 1;
                                }

                                area.Document.Insert(insertAt, attr.Name + "=\"");
                                _editor.CaretOffset = insertAt + attr.Name.Length + 2;
                            }));
                        }
                        showCompletion(data);
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 6
0
        private bool completeBasedOnTextEntered(TextCompositionEventArgs e)
        {
            try
            {
                switch (e.Text)
                {
                case ">":
                {
                    //auto-insert closing element
                    int    offset = _editor.CaretOffset;
                    string s      = XParser.GetElementAtCursor(_editor.Text, offset - 1);
                    if (!string.IsNullOrWhiteSpace(s) && "!--" != s)
                    {
                        if (!XParser.IsClosingElement(_editor.Text, offset - 1, s))
                        {
                            string endElement    = "</" + s + ">";
                            var    rightOfCursor = _editor.Text.Substring(offset, Math.Max(0, Math.Min(endElement.Length + 50, _editor.Text.Length) - offset - 1)).TrimStart();
                            if (!rightOfCursor.StartsWith(endElement))
                            {
                                _editor.TextArea.Document.Insert(offset, endElement);
                                _editor.CaretOffset = offset;
                                return(true);
                            }
                        }
                    }
                    break;
                }

                case "/":
                {
                    //insert name of closing element
                    int offset = _editor.CaretOffset;
                    if (offset > 1 && _editor.Text[offset - 2] == '<')
                    {
                        //expand to closing tag
                        string s = XParser.GetParentElementAtCursor(_editor.Text, offset - 1);
                        if (!string.IsNullOrEmpty(s))
                        {
                            showCompletion(new List <ICompletionData>
                                {
                                    new MyCompletionData(s + ">")
                                });
                            return(true);
                        }
                    }
                    if (_editor.Text.Length > offset + 2 && _editor.Text[offset] == '>')
                    {
                        //remove closing tag if exist
                        string s = XParser.GetElementAtCursor(_editor.Text, offset - 1);
                        if (!string.IsNullOrWhiteSpace(s))
                        {
                            //search closing end tag. Element must be empty (whitespace allowed)
                            //"<hallo>  </hallo>" --> enter '/' --> "<hallo/>  "
                            string expectedEndTag = "</" + s + ">";
                            for (int i = offset + 1; i < _editor.Text.Length - expectedEndTag.Length + 1; i++)
                            {
                                if (!char.IsWhiteSpace(_editor.Text[i]))
                                {
                                    if (_editor.Text.Substring(i, expectedEndTag.Length) == expectedEndTag)
                                    {
                                        //remove already existing endTag
                                        _editor.Document.Remove(i, expectedEndTag.Length);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    break;
                }

                case "!":
                {
                    int offset = _editor.CaretOffset;
                    if (offset > 1 && _editor.Text[offset - 2] == '<')
                    {
                        _editor.TextArea.Document.Insert(offset, "-- ");
                        _editor.CaretOffset = offset + 2;
                        return(true);
                    }
                    break;
                }

                case "\"":
                {
                    //auto-insert closing apostroph
                    int offset         = _editor.CaretOffset;
                    int countApostroph = 0;
                    for (int i = offset; i >= 0; i--)
                    {
                        char charAtCursor = _editor.Text[i];
                        if (charAtCursor == '\"')
                        {
                            countApostroph++;
                        }
                        else if (charAtCursor == '<')
                        {
                            break;
                        }
                    }

                    bool oddLeft = (countApostroph % 2 == 1);

                    for (int i = offset; i < _editor.Text.Length; i++)
                    {
                        char charAtCursor = _editor.Text[i];
                        if (charAtCursor == '\"')
                        {
                            countApostroph++;
                        }
                        else if (charAtCursor == '>')
                        {
                            break;
                        }
                    }

                    bool oddRight = (countApostroph % 2 == 1);

                    if (oddLeft && oddRight)
                    {
                        _editor.TextArea.Document.Insert(offset, "\"");
                        _editor.CaretOffset = offset;
                        return(true);
                    }
                    break;
                }

                case " ":
                {
                    //when user writes an element/attribute, completion shall pop up on whitespace
                    bool result = checkAttributeCompletion();
                    if (result)
                    {
                        return(true);
                    }
                    break;
                }

                case "<":
                {
                    if (_schemaParser == null)
                    {
                        return(false);
                    }
                    int    offset = _editor.CaretOffset;
                    string parent = XParser.GetParentElementAtCursor(_editor.Text, offset);

                    XsdElement[] names;
                    if (parent == "")
                    {
                        IXsdNode root = _schemaParser.GetVirtualRoot();
                        names = getChildNames(root);
                    }
                    else
                    {
                        IList <IXsdNode> all  = _schemaParser.GetAllNodes();
                        IXsdNode         node = getNodeWithName(all, parent);
                        if (node != null)
                        {
                            names = getChildNames(node);
                        }
                        else
                        {
                            names = null;
                        }
                    }

                    if (names != null && names.Length > 0)
                    {
                        IList <ICompletionData> data = new List <ICompletionData>();
                        foreach (XsdElement name in names)
                        {
                            if (name.Annotation != null && name.Annotation.Count > 0)
                            {
                                StringBuilder sb = new StringBuilder();
                                foreach (string ann in name.Annotation)
                                {
                                    sb.AppendLine(ann);
                                }
                                data.Add(new MyCompletionData(name.Name, sb.ToString()));
                            }
                            else
                            {
                                data.Add(new MyCompletionData(name.Name));
                            }
                        }
                        showCompletion(data);
                        return(true);
                    }

                    break;
                }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
            return(false);
        }