Skip to content

Commit 1e06661

Browse files
committedJan 24, 2012
Mitigate broken tmux layouts where a split has a child split of the same orientation by hoisting the grandchildren up to children and removing the useless intermediate split
1 parent c0ba707 commit 1e06661

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
 

‎TmuxLayoutParser.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
#import <Cocoa/Cocoa.h>
99

1010
// Trees consist of arrays of dictionaries. The dictionaries have these keys defined:
11+
// All nodes:
12+
extern NSString *kLayoutDictNodeType; // Node type from enum LayoutNodeType. NSNumber.
13+
14+
// Intermediate nodes only:
1115
extern NSString *kLayoutDictChildrenKey; // Sub-tree. Returns an array.
16+
17+
// Leaf nodes only:
1218
extern NSString *kLayoutDictWidthKey; // Width of node. String. Use -intValue.
1319
extern NSString *kLayoutDictHeightKey; // Height. String. Use -intValue.
1420
extern NSString *kLayoutDictXOffsetKey; // X position. String. Use -intValue.
1521
extern NSString *kLayoutDictYOffsetKey; // Y position. String. Use -intValue.
16-
extern NSString *kLayoutDictNodeType; // Node type from enum LayoutNodeType. NSNumber.
1722
extern NSString *kLayoutDictWindowPaneKey; // window pane number (leaf nodes only)
1823

1924
// These values are filled in by other classes:

‎TmuxLayoutParser.m

+33-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,38 @@ + (TmuxLayoutParser *)sharedInstance
5555
return instance;
5656
}
5757

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+
5890
- (NSMutableDictionary *)parsedLayoutFromString:(NSString *)layout
5991
{
6092
NSMutableArray *temp = [NSMutableArray array];
@@ -68,7 +100,7 @@ - (NSMutableDictionary *)parsedLayoutFromString:(NSString *)layout
68100
[tree setObject:[NSArray arrayWithObject:oldRoot] forKey:kLayoutDictChildrenKey];
69101
[tree removeObjectForKey:kLayoutDictWindowPaneKey];
70102
}
71-
return tree;
103+
return [self coalescedTree:tree];
72104
} else {
73105
return nil;
74106
}

0 commit comments

Comments
 (0)
Please sign in to comment.