コード例 #1
0
        /// <summary>
        ///     A TextSelection extension method that queries if 'selection' is node
        ///     selected.
        /// </summary>
        /// <param name="selection">
        ///     The target to act on.
        /// </param>
        /// <param name="name">
        ///     (optional) the name.
        /// </param>
        /// <returns>
        ///     true if node selected, otherwise false.
        /// </returns>
        public static bool IsNodeSelected(this TextSelection selection, string name = "")
        {
            EditorPoints ep = GetEditorPoints(selection);

            if (ep.IsInvalid)
            {
                return(false);
            }
            ep.RestoreSelectedText(selection);
            Tuple <XamlNode, XamlNode> firstAndLast = _rootNode.GetFirstLastNodesBetweenPoints(ep.TopPoint - 1,
                                                                                               ep.BottomPoint - 1);

            if (firstAndLast.Item1 != firstAndLast.Item2)
            {
                return(false);
            }
            return(name == string.Empty ||
                   Regex.IsMatch(selection.Text, string.Format(@"<(?<StartTagName>{0})(?=[\s/>])", name)));
        }
コード例 #2
0
        private static EditorPoints GetEditorPoints(TextSelection sel, bool handleExceptions = true)
        {
            EditorPoints ep = sel.GetEditorPoints();

            try
            {
                if (_documentText == ep.DocumentText)
                {
                    return(ep);
                }

                _documentText = ep.DocumentText;
                _rootNode     = new XamlNode(_documentText);
                return(ep);
            }
            catch
            {
                ep.RestoreSelectedText(sel);
                return(EditorPoints.GetInvalidEditorPoints());
            }
        }
コード例 #3
0
        /// <summary>
        ///     A TextSelection extension method that contract selection.
        /// </summary>
        /// <param name="selection">
        ///     The target to act on.
        /// </param>
        /// <returns>
        ///     .
        /// </returns>
        public static NarrowSelectionResult ContractSelection(this TextSelection selection)
        {
            if (selection.IsEmpty)
            {
                return(NarrowSelectionResult.SelectionIsEmpty);
            }

            //! If the selection begins or ends other than at the beginning and
            //! ending of a node, narrow the selection to include only the node.
            //
            //! If the selection begins and ends at the ends of a node, narrow
            //! the selection by removing the node tags from the selection.
            //
            //! If neither of the above, report a failure.

            // Get the original end points of the selection
            EditorPoints origEndPoints = GetEditorPoints(selection);

            // Get the first and last selected nodes.
            Tuple <XamlNode, XamlNode> firstAndLast = _rootNode.GetFirstLastNodesBetweenPoints(
                origEndPoints.TopPoint - 1,
                origEndPoints.BottomPoint
                - 1);

            //+ Case: No nodes are returned
            //  Means that the selection nodes were not within one parent
            if (firstAndLast == null)
            {
                return(NarrowSelectionResult.InconsistentSelectionEnds);
            }

            //+ Case: one node is returned.
            if (firstAndLast.Item1 == firstAndLast.Item2)
            {
                if (firstAndLast.Item1.IsNodeWithin(origEndPoints.TopPoint - 1, origEndPoints.BottomPoint - 1) &&
                    !firstAndLast.Item1.IsSelfClosing)
                {
                    EditorPoints contentEndPoints = firstAndLast.Item1.GetContentEndPoints();
                    contentEndPoints.RestoreSelectedText(selection);
                }
                else
                {
                    selection.MoveToAbsoluteOffset(selection.ActivePoint.AbsoluteCharOffset);
                }
                return(NarrowSelectionResult.Success);
            }

            //+ Case: multiple nodes are selected within one parent.

            // if we have more than one node, we narrow the selection by removing
            // the node farthest away from the anchor point from the selection.

            int top;
            int bottom;

            if (selection.IsActiveEndGreater)
            {
                // remove the top node from the selection.
                // +1 to point beyond the last character.
                // +1 to change from 0- to 1-based offset.
                top    = firstAndLast.Item1.BottomPoint + 2;
                bottom = firstAndLast.Item2.BottomPoint + 2;
            }
            else
            {
                // remove the bottom node from the selection.
                // +1 to change from 0- to 1-based offset.
                top    = firstAndLast.Item1.TopPoint + 1;
                bottom = firstAndLast.Item2.TopPoint + 1;
            }
            if (selection.IsActiveEndGreater)
            {
                selection.MoveToAbsoluteOffset(top);
                selection.MoveToAbsoluteOffset(bottom, true);
            }
            else
            {
                selection.MoveToAbsoluteOffset(bottom);
                selection.MoveToAbsoluteOffset(top, true);
            }
            selection.Trim();
            return(NarrowSelectionResult.Success);
        }