public static string GetParent(TextArea sender) { // 获取光标位置前面的至多30个字符 var currentCursorPosition = sender.Caret.Position; var selection = new RectangleSelection(sender, new TextViewPosition(currentCursorPosition.Line, currentCursorPosition.Column - 30 >= 0 ? currentCursorPosition.Column - 30 : 0), currentCursorPosition); // 正则获取Parent var parentMatch = Regex.Match(selection.GetText(), @"(?<=([^\w{}]|^))[^.]*?(?:\[.*?\])?\.(?=\w*$)", RegexOptions.RightToLeft); if (parentMatch.Success) { return(parentMatch.Value); } else { return(""); } }
public static string GetToken(TextArea sender) { // 获取光标位置前面的至多30个字符 var po = sender.Caret.Position; var selection = new RectangleSelection( sender, new TextViewPosition(po.Line, po.Column - 30 >= 0 ? po.Column - 30 : 0), po); // 正则获取Parent var tokenMatch = Regex.Match(selection.GetText(), @"(?<=([^\w]|^))\w*$", RegexOptions.RightToLeft); if (tokenMatch.Success) { return(tokenMatch.Value); } else { return(""); } }