From 326d3fdc8e63d37cabbd4937d754263da7d6c7e0 Mon Sep 17 00:00:00 2001 From: Jens Schwarzer Date: Sat, 10 May 2014 09:03:33 +0200 Subject: [PATCH 1/9] Simplify reloadLocalizations method and add comments in regexp --- Lin/LNLocalizationCollection.m | 65 ++++++++-------------------------- 1 file changed, 15 insertions(+), 50 deletions(-) diff --git a/Lin/LNLocalizationCollection.m b/Lin/LNLocalizationCollection.m index 2c786d5..dc8aaa8 100644 --- a/Lin/LNLocalizationCollection.m +++ b/Lin/LNLocalizationCollection.m @@ -93,59 +93,24 @@ - (void)reloadLocalizations if (contents) { NSMutableSet *localizations = [NSMutableSet set]; - // Parse - __block NSInteger lineOffset = 0; - __block NSString *key; - __block NSString *value; - __block NSRange entityRange; - __block NSRange keyRange; - __block NSRange valueRange; - - NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"(\"(\\S+.*\\S+)\"|(\\S+.*\\S+))\\s*=\\s*\"(.*)\";$" + NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern: + @"(?#key )(?:\"(.*)\"|(\\S+))" + @"(?#equals)\\s*=\\s*" + @"(?#value )\"(.*)\";" options:0 - error:NULL]; + error:nil]; - [contents enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { - key = nil; - value = nil; - keyRange = NSMakeRange(NSNotFound, 0); - valueRange = NSMakeRange(NSNotFound, 0); - - NSTextCheckingResult *result = [regularExpression firstMatchInString:line - options:0 - range:NSMakeRange(0, line.length)]; - - if (result.range.location != NSNotFound && result.numberOfRanges == 5) { - entityRange = [result rangeAtIndex:0]; - entityRange.location += lineOffset; - - keyRange = [result rangeAtIndex:2]; - if (keyRange.location == NSNotFound) keyRange = [result rangeAtIndex:3]; - - valueRange = [result rangeAtIndex:4]; - - key = [line substringWithRange:keyRange]; - value = [line substringWithRange:valueRange]; - - keyRange.location += lineOffset; - valueRange.location += lineOffset; - } - - // Create localization - if (key != nil && value != nil) { - LNLocalization *localization = [LNLocalization localizationWithKey:key - value:value - entityRange:entityRange - keyRange:keyRange - valueRange:valueRange - collection:self]; - - [localizations addObject:localization]; - } + [regularExpression enumerateMatchesInString:contents options:0 range:NSMakeRange(0, [contents length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { + NSRange entityRange = [result rangeAtIndex:0]; + NSRange keyRange = [result rangeAtIndex:1].location != NSNotFound ? [result rangeAtIndex:1] : [result rangeAtIndex:2]; + NSRange valueRange = [result rangeAtIndex:3]; - // Move offset - NSRange lineRange = [contents lineRangeForRange:NSMakeRange(lineOffset, 0)]; - lineOffset += lineRange.length; + [localizations addObject:[LNLocalization localizationWithKey:[contents substringWithRange:keyRange] + value:[contents substringWithRange:valueRange] + entityRange:entityRange + keyRange:keyRange + valueRange:valueRange + collection:self]]; }]; self.localizations = localizations; From bac2666643610add76131a5a016aa2bd0af7f272 Mon Sep 17 00:00:00 2001 From: Jens Schwarzer Date: Tue, 13 May 2014 17:37:45 +0200 Subject: [PATCH 2/9] Strictly extended regexp to include a comment and then show the comment in LNPopoverContentView --- Lin/LNLocalization.h | 6 +++-- Lin/LNLocalization.m | 14 ++++++++---- Lin/LNLocalizationCollection.m | 42 +++++++++++++++++++++++++++++----- Lin/LNPopoverContentView.m | 7 ++++++ Lin/LNPopoverContentView.xib | 40 +++++++++++++++++++++----------- LinTests/LNLocalizationTests.m | 2 ++ 6 files changed, 86 insertions(+), 25 deletions(-) diff --git a/Lin/LNLocalization.h b/Lin/LNLocalization.h index 147dd85..488a57a 100644 --- a/Lin/LNLocalization.h +++ b/Lin/LNLocalization.h @@ -14,15 +14,17 @@ @property (nonatomic, copy, readonly) NSString *key; @property (nonatomic, copy, readonly) NSString *value; +@property (nonatomic, copy, readonly) NSString *comment; @property (nonatomic, assign, readonly) NSRange entityRange; @property (nonatomic, assign, readonly) NSRange keyRange; @property (nonatomic, assign, readonly) NSRange valueRange; +@property (nonatomic, assign, readonly) NSRange commentRange; @property (nonatomic, weak, readonly) LNLocalizationCollection *collection; -+ (instancetype)localizationWithKey:(NSString *)key value:(NSString *)value entityRange:(NSRange)entityRange keyRange:(NSRange)keyRange valueRange:(NSRange)valueRange collection:(LNLocalizationCollection *)collection; ++ (instancetype)localizationWithKey:(NSString *)key value:(NSString *)value comment:(NSString *)comment entityRange:(NSRange)entityRange keyRange:(NSRange)keyRange valueRange:(NSRange)valueRange commentRange:(NSRange)commentRange collection:(LNLocalizationCollection *)collection; -- (instancetype)initWithKey:(NSString *)key value:(NSString *)value entityRange:(NSRange)entityRange keyRange:(NSRange)keyRange valueRange:(NSRange)valueRange collection:(LNLocalizationCollection *)collection; +- (instancetype)initWithKey:(NSString *)key value:(NSString *)value comment:(NSString *)comment entityRange:(NSRange)entityRange keyRange:(NSRange)keyRange valueRange:(NSRange)valueRange commentRange:(NSRange)commentRange collection:(LNLocalizationCollection *)collection; @end diff --git a/Lin/LNLocalization.m b/Lin/LNLocalization.m index 411f0cf..5431896 100644 --- a/Lin/LNLocalization.m +++ b/Lin/LNLocalization.m @@ -12,10 +12,12 @@ @interface LNLocalization () @property (nonatomic, copy, readwrite) NSString *key; @property (nonatomic, copy, readwrite) NSString *value; +@property (nonatomic, copy, readwrite) NSString *comment; @property (nonatomic, assign, readwrite) NSRange entityRange; @property (nonatomic, assign, readwrite) NSRange keyRange; @property (nonatomic, assign, readwrite) NSRange valueRange; +@property (nonatomic, assign, readwrite) NSRange commentRange; @property (nonatomic, weak, readwrite) LNLocalizationCollection *collection; @@ -23,22 +25,24 @@ @interface LNLocalization () @implementation LNLocalization -+ (instancetype)localizationWithKey:(NSString *)key value:(NSString *)value entityRange:(NSRange)entityRange keyRange:(NSRange)keyRange valueRange:(NSRange)valueRange collection:(LNLocalizationCollection *)collection ++ (instancetype)localizationWithKey:(NSString *)key value:(NSString *)value comment:(NSString *)comment entityRange:(NSRange)entityRange keyRange:(NSRange)keyRange valueRange:(NSRange)valueRange commentRange:(NSRange)commentRange collection:(LNLocalizationCollection *)collection { - return [[self alloc] initWithKey:key value:value entityRange:(NSRange)entityRange keyRange:keyRange valueRange:valueRange collection:collection]; + return [[self alloc] initWithKey:key value:value comment:comment entityRange:(NSRange)entityRange keyRange:keyRange valueRange:valueRange commentRange:commentRange collection:collection]; } -- (instancetype)initWithKey:(NSString *)key value:(NSString *)value entityRange:(NSRange)entityRange keyRange:(NSRange)keyRange valueRange:(NSRange)valueRange collection:(LNLocalizationCollection *)collection +- (instancetype)initWithKey:(NSString *)key value:(NSString *)value comment:(NSString *)comment entityRange:(NSRange)entityRange keyRange:(NSRange)keyRange valueRange:(NSRange)valueRange commentRange:(NSRange)commentRange collection:(LNLocalizationCollection *)collection { self = [super init]; if (self) { self.key = key; self.value = value; + self.comment = comment; self.entityRange = entityRange; self.keyRange = keyRange; self.valueRange = valueRange; + self.commentRange = commentRange; self.collection = collection; } @@ -66,14 +70,16 @@ - (NSUInteger)hash - (NSString *)description { return [NSString stringWithFormat: - @"<%@: %p; key = %@; value = %@; entityRange = %@; keyRange = %@; valueRange = %@; collection = %p>", + @"<%@: %p; key = %@; value = %@; comment = %@; entityRange = %@; keyRange = %@; valueRange = %@; commentRange = %@; collection = %p>", NSStringFromClass([self class]), self, self.key, self.value, + self.comment, NSStringFromRange(self.entityRange), NSStringFromRange(self.keyRange), NSStringFromRange(self.valueRange), + NSStringFromRange(self.commentRange), self.collection ]; } diff --git a/Lin/LNLocalizationCollection.m b/Lin/LNLocalizationCollection.m index dc8aaa8..2221f1c 100644 --- a/Lin/LNLocalizationCollection.m +++ b/Lin/LNLocalizationCollection.m @@ -93,23 +93,44 @@ - (void)reloadLocalizations if (contents) { NSMutableSet *localizations = [NSMutableSet set]; + // to keep it simple for now comments are only accepted if + // * the comment is C-style + // * the comment is on the line above the key-value + // * the comment is followed by no whitespace except one new line + // * the key-value line is not prefixed with any whitespace + // this can be changed later on when time allows :) + // example found below: + // v - no whitespace here except newline + // /* Comment */ + // @"key" = @"value"; + // ^ - no whitespace here + NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern: - @"(?#key )(?:\"(.*)\"|(\\S+))" - @"(?#equals)\\s*=\\s*" - @"(?#value )\"(.*)\";" + @"(?#comment)(?:/\\*(.*)\\*/\n)?" + @"(?#key )(?:\"(.*)\"|(\\S+))" + @"(?#equals )\\s*=\\s*" + @"(?#value )\"(.*)\";" options:0 error:nil]; [regularExpression enumerateMatchesInString:contents options:0 range:NSMakeRange(0, [contents length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { - NSRange entityRange = [result rangeAtIndex:0]; - NSRange keyRange = [result rangeAtIndex:1].location != NSNotFound ? [result rangeAtIndex:1] : [result rangeAtIndex:2]; - NSRange valueRange = [result rangeAtIndex:3]; + NSString *comment = nil; + + NSRange entityRange = [result rangeAtIndex:0]; + NSRange commentRange = [result rangeAtIndex:1]; + NSRange keyRange = [result rangeAtIndex:2].location != NSNotFound ? [result rangeAtIndex:2] : [result rangeAtIndex:3]; + NSRange valueRange = [result rangeAtIndex:4]; + + if (commentRange.location != NSNotFound) + comment = [contents substringWithRange:commentRange]; [localizations addObject:[LNLocalization localizationWithKey:[contents substringWithRange:keyRange] value:[contents substringWithRange:valueRange] + comment:comment entityRange:entityRange keyRange:keyRange valueRange:valueRange + commentRange:commentRange collection:self]]; }]; @@ -129,6 +150,10 @@ - (void)addLocalization:(LNLocalization *)localization contents = [contents stringByAppendingString:@"\n"]; } + // Add comment + if (localization.comment) + contents = [contents stringByAppendingFormat:@"/*%@*/\n", localization.comment]; + contents = [contents stringByAppendingFormat:@"\"%@\" = \"%@\";\n", localization.key, localization.value]; // Override @@ -171,6 +196,11 @@ - (void)replaceLocalization:(LNLocalization *)localization withLocalization:(LNL // Replace NSString *newEntity = [NSString stringWithFormat:@"\"%@\" = \"%@\";", newLocalization.key, newLocalization.value]; + + // Add comment + if (localization.comment) + newEntity = [NSString stringWithFormat:@"/*%@*/\n%@", localization.comment, newEntity]; + contents = [contents stringByReplacingCharactersInRange:localization.entityRange withString:newEntity]; // Override diff --git a/Lin/LNPopoverContentView.m b/Lin/LNPopoverContentView.m index 65f6129..7e6e4e8 100644 --- a/Lin/LNPopoverContentView.m +++ b/Lin/LNPopoverContentView.m @@ -121,9 +121,11 @@ - (void)textDidEndEditing:(NSNotification *)notification LNLocalization *newLocalization = [LNLocalization localizationWithKey:key value:value + comment:nil entityRange:localization.entityRange keyRange:localization.keyRange valueRange:localization.valueRange + commentRange:NSMakeRange(NSNotFound, 0) collection:localization.collection]; // Replace in file @@ -187,9 +189,11 @@ - (IBAction)addLocalization:(id)sender LNLocalization *localization = [LNLocalization localizationWithKey:key value:value + comment:nil entityRange:NSMakeRange(NSNotFound, 0) keyRange:NSMakeRange(NSNotFound, 0) valueRange:NSMakeRange(NSNotFound, 0) + commentRange:NSMakeRange(NSNotFound, 0) collection:collection]; // Add localization to file @@ -309,6 +313,9 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum else if ([identifier isEqualToString:@"value"]) { return localization.value; } + else if ([identifier isEqualToString:@"comment"]) { + return localization.comment; + } return nil; } diff --git a/Lin/LNPopoverContentView.xib b/Lin/LNPopoverContentView.xib index aeaa319..7cc8b84 100644 --- a/Lin/LNPopoverContentView.xib +++ b/Lin/LNPopoverContentView.xib @@ -1,8 +1,8 @@ - + - + @@ -12,19 +12,19 @@ - - + + - + - + - + @@ -87,6 +87,20 @@ + + + + + + + + + + + + + + @@ -97,7 +111,7 @@ - + - + - + @@ -153,7 +167,7 @@ - -