/// <summary> /// A TextSelection extension method that trims the given target. /// </summary> /// <param name="target"> /// The target to act on. /// </param> /// <param name="topActive"> /// Determines the active text point. /// </param> public static void Trim(this TextSelection target, bool?topActive = null) { if (topActive == null) { topActive = target.IsActiveEndGreater == false; } var text = target.Text.Replace(Environment.NewLine, "\n"); var match = Regex.Match(text, @"\A\s*"); if (match.Success == false) { return; } var len = match.Length; var topPoint = target.TopPoint.AbsoluteCharOffset + len; var bottomPoint = topPoint + text.Trim().Length; if ((bool)topActive) { target.SetSelection(bottomPoint, topPoint); } else { target.SetSelection(topPoint, bottomPoint); } }
/// <summary> /// A TextSelection extension method that selects one or more nodes /// contained by one parent node. /// </summary> /// <param name="selection"> /// The target to act on. /// </param> /// <returns> /// true if it succeeds, otherwise false. /// </returns> public static bool SelectNodes(this TextSelection selection) { logger.Trace("Entered SelectNodes()"); EditorPoints ep = GetEditorPoints(selection); // Find the nodes that are within or that contain the ends of the selection. Tuple <XamlNode, XamlNode> firstAndLast = _rootNode.GetFirstLastNodesBetweenPoints(ep.TopPoint - 1, ep.BottomPoint - 1); // Null is returned if one end of the selection is outside the root node. if (firstAndLast.Item1 == null || firstAndLast.Item2 == null) { // Select the root node. selection.SelectAll(); selection.Trim(); return(true); } // Select the the text for the nodes found above. selection.SetSelection(firstAndLast.Item2.BottomPoint + 1, firstAndLast.Item1.TopPoint + 1); return(true); }
/// <summary> /// A TextSelection extension method that resets the selection. /// </summary> /// <param name="target"> /// The target to act on. /// </param> /// <param name="originalPoints"> /// The original points. /// </param> /// <returns> /// true if it succeeds, otherwise false. /// </returns> public static bool ResetSelection(this TextSelection target, EditorPoints originalPoints) { target.SetSelection(originalPoints.AnchorPoint, originalPoints.ActivePoint); // Usually called when a process fails. This makes it easier to // reply with false while reseting the seleciton. return(false); }
/// <summary> /// A TextSelection extension method that select parent. /// </summary> /// <param name="selection"> /// The target to act on. /// </param> /// <returns> /// true if it succeeds, otherwise false. /// </returns> public static bool SelectParent(this TextSelection selection) { EditorPoints ep = selection.GetEditorPoints(); XamlNode[] nodes = _rootNode.GetSelectedNodes(ep); if (nodes == null || nodes == null) { return(false); } XamlNode parent = nodes[0].Parent; selection.SetSelection(parent.BottomPoint + 1, parent.TopPoint + 1); return(true); }
/// <summary> /// A TextSelection extension method that selects a single node. /// </summary> /// <param name="selection"> /// The target to act on. /// </param> /// <returns> /// true if it succeeds, otherwise false. /// </returns> public static bool SelectNode(this TextSelection selection) { // Works only when the selected text is empty. if (!selection.IsEmpty) { return(false); } // This has to be done here primarily to prime the data // needed by called methods. Othwerwise we could just // reference the offsets directly. EditorPoints ep = GetEditorPoints(selection); XamlNode node = _rootNode.GetNodeWithOffset(ep.ActivePoint - 1); if (node == null) { return(false); } selection.SetSelection(node.BottomPoint + 1, node.TopPoint + 1); return(true); }