// 解析缩进 public int GetFormateIndentation(int offset) { m_indent_root = GetIndentRoot(); if (m_indent_root == null) { return(0); } ABnfElement target = null; // 获取元素 var element = m_indent_root.GetException(offset); if (element != null) { // 获取类型 var node = element as ABnfNodeElement; if (node == null) { node = element.GetParent(); } if (node != null) { target = node; } } int?indent = null; if (target != null) { indent = target.GetReference().GetFormateIndentation(offset, element); } if (!indent.HasValue) { return(0); } return(indent.Value); }
private bool CheckAutoPair(int offset, char left_pair, string right_pair) { if (m_indent_root == null) { var file = new UIABnfFile(m_full_path, m_abnf_ui, m_view.TextBuffer.CurrentSnapshot.GetText()); m_indent_root = m_abnf_ui.Analysis(file); } if (m_indent_root == null) { return(false); } // 获取元素 var element = m_indent_root.GetException(offset - 1); if (element == null) { return(false); } // 如果是错误元素,那么就以目标元素来判定 if (element is ABnfErrorElement) { var error_element = element as ABnfErrorElement; if (error_element.GetTargetElement() is ABnfRegexElement) { return((error_element.GetTargetElement() as ABnfRegexElement).IsMatch(left_pair + right_pair)); } return(false); } // 如果是正则表达式,那么就直接匹配 if (element is ABnfRegexElement) { return((element as ABnfRegexElement).IsMatch(left_pair + right_pair)); } // 获取类型 ABnfNodeElement node = element as ABnfNodeElement; if (node == null) { node = element.GetParent(); } if (node == null) { return(false); } // 查找规则 var rule = m_abnf.GetRule(node.GetNodeType()); foreach (var node_list in rule.node.node_list) { // 如果还有一个子规则,那么就跳过 if (node_list.Count <= 1) { continue; } // 如果找到规则并且和left_pair一致,那么就找到left_pair对应的规则 int index = -1; for (int i = 0; i < node_list.Count; ++i) { var node_token = node_list[i]; if (node_token.value == null) { continue; } if (node_token.value.type == ABnfRuleTokenType.TT_STRING) { if (node_token.value.value.Length == 1 && node_token.value.value[0] == left_pair) { index = i; break; } } } // 如果没有找到就跳过当前规则组 if (index == -1) { continue; } // 从后面往前找,找到与right_pair配对的子规则,如果有,那么就返回true for (int i = node_list.Count - 1; i >= index + 1; --i) { var node_token = node_list[i]; if (node_token.value == null) { continue; } if (node_token.value.type == ABnfRuleTokenType.TT_STRING) { if (node_token.value.value == right_pair) { return(true); } } } } return(false); }