def _delete_value(self, root, lines, node): """Delete value and return start possition deleted value""" l1, c1, l2, c2 = node.span.start.line-1, node.span.start.column-1, \ node.span.end.line-1, node.span.end.column-1 l1, c1 = StructureChanger.skip_tar(lines, l1, c1, l2, c2) StructureChanger.delete_structure(lines, l1, c1, l2, c2) return l1, c1
def _copy_tree_from_anchor(self, lines, anchor_node, ref_node, remove_anchor=False): """Copy tree structure from anchor node to node with reference""" l1, c1, l2, c2 = anchor_node.span.start.line-1, anchor_node.span.start.column-1, \ anchor_node.span.end.line-1, anchor_node.span.end.column-1 l1, c1 = StructureChanger.skip_tar(lines, l1, c1, l2, c2) hlpl1, hlpc1, l2, c2 = StructureChanger.add_comments(lines, l1, c1, l2, c2) dl1, dc1, dl2, dc2 = StructureChanger.node_pos(ref_node) intend = re.search(r'^(\s*)(\S.*)$', lines[dl1]) intend = len(intend.group(1)) + 2 add = StructureChanger.copy_structure(lines, l1, c1, l2, c2, intend) while dl1 <= dl2: ref = re.search(r'^(.*\*' + anchor_node.anchor.value + r')(.*)$', lines[dl1]) if ref is not None: anchor = "&" + anchor_node.anchor.value if remove_anchor: anchor = "" lines[dl1] = re.sub(r'\*' + anchor_node.anchor.value + r"\s+", anchor + " ", lines[dl1]) lines[dl1] = re.sub(r'\*' + anchor_node.anchor.value + r"$", anchor, lines[dl1]) if not ref.group(2).isspace() and len(ref.group(2)) > 0: lines.insert(dl1+1, ref.group(2)) lines[dl1] = ref.group(1) StructureChanger.paste_structure(lines, dl1, add, True, True) break dl1 += 1
def _move_key_forward(self, root, lines, action): """Move key forward""" try: parent = re.search(r'^(.*)/([^/]*)$', action['parameters']['path']) node = root.get_node_at_path(action['parameters']['path']) if parent is None: raise TransformationFileFormatError( "Cannot find parent path for path (" + self._get_paths_str(action, 'path') + ")") is_root = False if len(parent.group(1)) == 0: parent_node = root is_root = True else: parent_node = root.get_node_at_path(parent.group(1)) except: return False l1, c1, l2, c2 = StructureChanger.node_pos(node) pl1, pc1, pl2, pc2 = parent_node.span.start.line-1, parent_node.span.start.column-1, \ parent_node.span.end.line-1, parent_node.span.end.column-1 if parent_node.implementation != DataNode.Implementation.mapping: raise TransformationFileFormatError( "Parent of path (" + self._get_paths_str(action, 'path') + ") must be abstract record") if is_root: indentation1 = 0 pl1 = 0 pc1 = 0 else: indentation1 = StructureChanger.get_indentation(lines, pl1) + 2 l1, c1, l2, c2 = StructureChanger.add_comments(lines, l1, c1, l2, c2) add = StructureChanger.copy_structure(lines, l1, c1, l2, c2, indentation1) pl1, pc1 = StructureChanger.skip_tar(lines, pl1, pc1, pl2, pc2) action['parameters']['deep'] = True self._delete_key(root, lines, action) StructureChanger.paste_structure(lines, pl1, add, pc1 != 0) return True