@@ -55,6 +55,38 @@ + (TmuxLayoutParser *)sharedInstance
55
55
return instance;
56
56
}
57
57
58
+ // When a child has the same node type as its parent, add its children to
59
+ // the parent's children.
60
+ - (NSMutableDictionary *)coalescedTree : (NSMutableDictionary *)node
61
+ {
62
+ int nodeType = [[node objectForKey: kLayoutDictNodeType ] intValue ];
63
+ if (nodeType == kVSplitLayoutNode || nodeType == kHSplitLayoutNode ) {
64
+ NSSize size;
65
+ size.width = [[node objectForKey: kLayoutDictWidthKey ] intValue ];
66
+ size.height = [[node objectForKey: kLayoutDictHeightKey ] intValue ];
67
+ NSArray *children = [node objectForKey: kLayoutDictChildrenKey ];
68
+ NSMutableArray *coalescedChildren = [NSMutableArray array ];
69
+ for (NSMutableDictionary *child in children) {
70
+ // Recursively apply this algorithm to the child (does nothing if it's a leaf node)
71
+ NSMutableDictionary *coalescedChild = [self coalescedTree: child];
72
+
73
+ if ([[coalescedChild objectForKey: kLayoutDictNodeType ] intValue ] == nodeType) {
74
+ // The child is a split of the same orientation as this node.
75
+ // Iterate over its children (this node's grandchildren) and hoist each up to
76
+ // be a child of this node.
77
+ [coalescedChildren addObjectsFromArray: [coalescedChild objectForKey: kLayoutDictChildrenKey ]];
78
+ } else {
79
+ // The child is not a splitter of the same orientation, so just add it.
80
+ [coalescedChildren addObject: coalescedChild];
81
+ }
82
+ }
83
+ // Done coalescing children--if at all--and replace our existing children
84
+ // with the new ones.
85
+ [node setObject: coalescedChildren forKey: kLayoutDictChildrenKey ];
86
+ }
87
+ return node;
88
+ }
89
+
58
90
- (NSMutableDictionary *)parsedLayoutFromString : (NSString *)layout
59
91
{
60
92
NSMutableArray *temp = [NSMutableArray array ];
@@ -68,7 +100,7 @@ - (NSMutableDictionary *)parsedLayoutFromString:(NSString *)layout
68
100
[tree setObject: [NSArray arrayWithObject: oldRoot] forKey: kLayoutDictChildrenKey ];
69
101
[tree removeObjectForKey: kLayoutDictWindowPaneKey ];
70
102
}
71
- return tree;
103
+ return [ self coalescedTree: tree] ;
72
104
} else {
73
105
return nil ;
74
106
}
0 commit comments