Skip to content

Commit

Permalink
Detect cycles in badge swifty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
gnachman committed Oct 19, 2018
1 parent 7b6549d commit 52c9868
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
13 changes: 13 additions & 0 deletions sources/PTYSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -3872,6 +3872,19 @@ - (NSString *)badgeFormat {
}

- (void)updateBadgeLabel {
NSMutableArray *badRefs = [NSMutableArray array];
for (iTermVariableReference *ref in _badgeSwiftyString.refs) {
if ([self.variablesScope variableNamed:iTermVariableKeySessionBadge isReferencedBy:ref]) {
[badRefs addObject:ref];
}
}
if (badRefs.count) {
for (iTermVariableReference *ref in badRefs) {
[ref removeAllLinks];
}
[self setSessionSpecificProfileValues:@{ KEY_BADGE_FORMAT: @"[CYCLE DETECTED]" }];
return;
}
[self updateBadgeLabel:[self badgeLabel]];
}

Expand Down
2 changes: 2 additions & 0 deletions sources/iTermSwiftyString.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

NS_ASSUME_NONNULL_BEGIN

@class iTermVariableReference;
@class iTermVariableScope;

// Represents a string with interpolated components like:
Expand All @@ -25,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, copy) id (^source)(NSString *);
@property (nonatomic, readonly, copy) void (^observer)(NSString *);
@property (nullable, nonatomic, readonly) NSString *evaluatedString;
@property (nonatomic, readonly) NSArray<iTermVariableReference *> *refs;

// Variables the string depends on
@property (nonatomic, readonly) NSSet<NSString *> *dependencies;
Expand Down
1 change: 0 additions & 1 deletion sources/iTermSwiftyString.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ @interface iTermSwiftyString()
@implementation iTermSwiftyString {
NSMutableSet<NSString *> *_missingFunctions;
iTermVariableScope *_scope;
NSArray<iTermVariableReference *> *_refs;
BOOL _observing;
}

Expand Down
1 change: 1 addition & 0 deletions sources/iTermVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ typedef NS_OPTIONS(NSUInteger, iTermVariablesSuggestionContext) {
- (void)setValue:(nullable id)value forKey:(NSString *)key NS_UNAVAILABLE;
- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *, id> *)keyedValues NS_UNAVAILABLE;
- (void)addLinksToReference:(iTermVariableReference *)reference;
- (BOOL)variableNamed:(NSString *)name isReferencedBy:(iTermVariableReference *)reference;

@end

Expand Down
23 changes: 23 additions & 0 deletions sources/iTermVariables.m
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,20 @@ - (void)addLinkToReference:(iTermVariableReference *)reference
}
}

- (BOOL)hasLinkToReference:(iTermVariableReference *)reference
path:(NSString *)path {
NSArray<NSString *> *parts = [path componentsSeparatedByString:@"."];
id value = _values[parts.firstObject];
if (!value) {
return NO;
}
iTermVariables *sub = [iTermVariables castFrom:value];
if (sub && parts.count > 1) {
return [sub hasLinkToReference:reference path:[[parts subarrayFromIndex:1] componentsJoinedByString:@"."]];
}
return [_resolvedLinks[path].allObjects containsObject:reference];
}

- (void)removeLinkToReference:(iTermVariableReference *)reference
path:(NSString *)path {
[self removeWeakReferenceFromLinkTable:_resolvedLinks toObject:reference forKey:path];
Expand Down Expand Up @@ -659,6 +673,15 @@ - (iTermVariables *)ownerForKey:(NSString *)key stripped:(out NSString **)stripp
return owner;
}

- (BOOL)variableNamed:(NSString *)name isReferencedBy:(iTermVariableReference *)reference {
NSString *tail;
iTermVariables *variables = [self ownerForKey:name stripped:&tail];
if (!variables) {
return NO;
}
return [variables hasLinkToReference:reference path:tail];
}

- (BOOL)setValuesFromDictionary:(NSDictionary<NSString *, id> *)dict {
// Transform dict from {name: object} to {owner: {stripped_name: object}}
NSMutableDictionary<NSValue *, NSMutableDictionary<NSString *, id> *> *valuesByOwner = [NSMutableDictionary dictionary];
Expand Down

0 comments on commit 52c9868

Please sign in to comment.