Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #19 from jasonhotsauce/performance
Browse files Browse the repository at this point in the history
Performance
  • Loading branch information
nek023 committed Mar 15, 2014
2 parents 5079331 + 224cff6 commit dd67b6c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 27 deletions.
6 changes: 6 additions & 0 deletions Lin.xcodeproj/project.pbxproj
Expand Up @@ -58,6 +58,7 @@
AAF0D6B417ECD8EC00B4318B /* LNButtonCell.m in Sources */ = {isa = PBXBuildFile; fileRef = AAF0D6B317ECD8EC00B4318B /* LNButtonCell.m */; };
AAFE3C3F17EECF4A0018E478 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = AAFE3C3E17EECF4A0018E478 /* Localizable.strings */; };
AAFE3C4117EED36C0018E478 /* LNLocalizationCollection.m in Sources */ = {isa = PBXBuildFile; fileRef = AA7D4DB017C74CFE0092DF8A /* LNLocalizationCollection.m */; };
F5859DBD18CABC2A009AF108 /* LNLocalizedStringCollectionOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F5859DBC18CABC2A009AF108 /* LNLocalizedStringCollectionOperation.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -158,6 +159,8 @@
AAF0D6B217ECD8EC00B4318B /* LNButtonCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LNButtonCell.h; sourceTree = "<group>"; };
AAF0D6B317ECD8EC00B4318B /* LNButtonCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LNButtonCell.m; sourceTree = "<group>"; };
AAFE3C3E17EECF4A0018E478 /* Localizable.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = "<group>"; };
F5859DBB18CABC2A009AF108 /* LNLocalizedStringCollectionOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LNLocalizedStringCollectionOperation.h; sourceTree = "<group>"; };
F5859DBC18CABC2A009AF108 /* LNLocalizedStringCollectionOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LNLocalizedStringCollectionOperation.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -268,6 +271,8 @@
children = (
AA2980B217EE2C5300CF46E3 /* LNUserDefaultsManager.h */,
AA2980B317EE2C5300CF46E3 /* LNUserDefaultsManager.m */,
F5859DBB18CABC2A009AF108 /* LNLocalizedStringCollectionOperation.h */,
F5859DBC18CABC2A009AF108 /* LNLocalizedStringCollectionOperation.m */,
);
name = Shared;
sourceTree = "<group>";
Expand Down Expand Up @@ -548,6 +553,7 @@
AA2980B417EE2C5300CF46E3 /* LNUserDefaultsManager.m in Sources */,
AA95FDEF17EB4E77007D33D4 /* LNPopoverWindowController.m in Sources */,
AADEADC017C87392001E0C65 /* MethodSwizzle.m in Sources */,
F5859DBD18CABC2A009AF108 /* LNLocalizedStringCollectionOperation.m in Sources */,
AA470D5B17C3CFEC005CBB7F /* LNDetector.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
21 changes: 21 additions & 0 deletions Lin/LNLocalizedStringCollectionOperation.h
@@ -0,0 +1,21 @@
//
// LNLocalizedStringCollectionOperation.h
// Lin
//
// Created by Wenbin Zhang on 3/7/14.
// Copyright (c) 2014 Tanaka Katsuma. All rights reserved.
//

#import <Foundation/Foundation.h>

@class IDEIndex;

typedef void(^LNLocalizedStringCollectionCompletionBlock)(NSString *workspacePath, NSArray *collections);

@interface LNLocalizedStringCollectionOperation : NSOperation

@property (nonatomic, strong) IDEIndex *index;
@property (nonatomic, copy) LNLocalizedStringCollectionCompletionBlock collectionCompletedBlock;

- (instancetype)initWithIndex:(IDEIndex *)index;
@end
95 changes: 95 additions & 0 deletions Lin/LNLocalizedStringCollectionOperation.m
@@ -0,0 +1,95 @@
//
// LNLocalizedStringCollectionOperation.m
// Lin
//
// Created by Wenbin Zhang on 3/7/14.
// Copyright (c) 2014 Tanaka Katsuma. All rights reserved.
//

#import "LNLocalizedStringCollectionOperation.h"
#import "IDEIndex.h"
#import "IDEWorkspace.h"
#import "IDEIndexCollection.h"
#import "DVTFilePath.h"
#import "LNLocalizationCollection.h"

@interface LNLocalizedStringCollectionOperation ()

@property (nonatomic, strong) NSArray *collections;
@property (nonatomic, strong) NSString *workspaceFilePath;
@end

@implementation LNLocalizedStringCollectionOperation

- (instancetype)initWithIndex:(IDEIndex *)index
{
self = [super init];
if (self) {
self.index = index;
}
return self;
}

- (void)main
{
@autoreleasepool {
if (self.isCancelled) {
return;
}

if (!self.index) {
return;
}
[self processLocalizedString];

if (self.isCancelled) {
return;
}

if (self.collectionCompletedBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.collectionCompletedBlock(self.workspaceFilePath, self.collections);
});
}

[self cleanup];
}
}

- (void)processLocalizedString
{
IDEWorkspace *workspace = [self.index valueForKey:@"_workspace"];
DVTFilePath *representingFilePath = workspace.representingFilePath;
self.workspaceFilePath = representingFilePath.pathString;
NSMutableArray *mutableCollections = [[NSMutableArray alloc] init];

if (self.workspaceFilePath) {
NSString *projectRootPath = [[self.workspaceFilePath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];

// Find .strings files
IDEIndexCollection *indexCollection = [self.index filesContaining:@".strings" anchorStart:NO anchorEnd:NO subsequence:NO ignoreCase:YES cancelWhen:nil];

for (DVTFilePath *filePath in indexCollection) {
if (self.isCancelled) {
return;
}
NSString *pathString = filePath.pathString;

BOOL parseStringsFilesOutsideWorkspaceProject = YES;
if (parseStringsFilesOutsideWorkspaceProject ||
(!parseStringsFilesOutsideWorkspaceProject && [pathString rangeOfString:projectRootPath].location != NSNotFound)) {
// Create localization collection
LNLocalizationCollection *collection = [LNLocalizationCollection localizationCollectionWithContentsOfFile:pathString];
[mutableCollections addObject:collection];
}
}
self.collections = [mutableCollections copy];
}
}

- (void)cleanup
{
self.index = nil;
}

@end
2 changes: 2 additions & 0 deletions Lin/Lin-Info.plist
Expand Up @@ -25,6 +25,8 @@
<key>DVTPlugInCompatibilityUUIDs</key>
<array>
<string>37B30044-3B14-46BA-ABAA-F01000C27B63</string>
<string>640F884E-CE55-4B40-87C0-8869546CAB7A</string>
<string>A2E4D43F-41F4-4FB9-BB94-7177011C9AED</string>
</array>
<key>LSApplicationCategoryType</key>
<string></string>
Expand Down
39 changes: 12 additions & 27 deletions Lin/Lin.m
Expand Up @@ -34,6 +34,7 @@

// Controllers
#import "LNPopoverWindowController.h"
#import "LNLocalizedStringCollectionOperation.h"

static Lin *_sharedPlugin = nil;

Expand All @@ -58,6 +59,7 @@ @interface Lin ()

@property (nonatomic, strong) NSMenuItem *enableMenuItem;
@property (nonatomic, strong) NSMenuItem *showWindowMenuItem;
@property (nonatomic, strong) NSOperationQueue *collectionProcessQueue;

@end

Expand Down Expand Up @@ -137,6 +139,9 @@ - (instancetype)init
if ([[LNUserDefaultsManager sharedManager] isEnabled]) {
[self activate];
}

self.collectionProcessQueue = [[NSOperationQueue alloc] init];
self.collectionProcessQueue.maxConcurrentOperationCount = 1;
}

return self;
Expand Down Expand Up @@ -301,7 +306,7 @@ - (void)editorDocumentDidSave:(NSNotification *)notification
NSString *pathString = filePath.pathString;

// Check whether there are any changes to .strings
NSMutableArray *collections = [self.workspaceLocalizations objectForKey:self.currentWorkspaceFilePath];
NSArray *collections = [self.workspaceLocalizations objectForKey:self.currentWorkspaceFilePath];

for (LNLocalizationCollection *collection in collections) {
if ([collection.filePath isEqualToString:pathString]) {
Expand Down Expand Up @@ -531,38 +536,16 @@ - (void)indexNeedsUpdate:(IDEIndex *)index
NSString *workspaceFilePath = workspaceFile.pathString;

if (workspaceFilePath) {
[self.collectionProcessQueue cancelAllOperations];
[self updateLocalizationsForIndex:index];
}
}

- (void)updateLocalizationsForIndex:(IDEIndex *)index
{
IDEWorkspace *workspace = [index valueForKey:@"_workspace"];
DVTFilePath *representingFilePath = workspace.representingFilePath;
NSString *workspaceFilePath = representingFilePath.pathString;

if (workspaceFilePath) {
NSString *projectRootPath = [[workspaceFilePath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];

// Find .strings files
IDEIndexCollection *indexCollection = [index filesContaining:@".strings" anchorStart:NO anchorEnd:NO subsequence:NO ignoreCase:YES cancelWhen:nil];
NSMutableArray *collections = [NSMutableArray array];

for (DVTFilePath *filePath in indexCollection) {
NSString *pathString = filePath.pathString;

BOOL parseStringsFilesOutsideWorkspaceProject = YES;
if (parseStringsFilesOutsideWorkspaceProject ||
(!parseStringsFilesOutsideWorkspaceProject && [pathString rangeOfString:projectRootPath].location != NSNotFound)) {
// Create localization collection
LNLocalizationCollection *collection = [LNLocalizationCollection localizationCollectionWithContentsOfFile:pathString];
[collections addObject:collection];
}
}

LNLocalizedStringCollectionOperation *processOperation = [[LNLocalizedStringCollectionOperation alloc] initWithIndex:index];
processOperation.collectionCompletedBlock = ^(NSString *workspaceFilePath, NSArray *collections) {
[self.workspaceLocalizations setObject:collections forKey:workspaceFilePath];

// Update popover content if current workspace's files changed
if ([workspaceFilePath isEqualToString:self.currentWorkspaceFilePath]) {
if ([self.popover isShown]) {
LNPopoverContentView *contentView = (LNPopoverContentView *)self.popover.contentViewController.view;
Expand All @@ -572,7 +555,9 @@ - (void)updateLocalizationsForIndex:(IDEIndex *)index
contentView.collections = collections;
}
}
}
};

[self.collectionProcessQueue addOperation:processOperation];
}

- (void)removeLocalizationsForIndex:(IDEIndex *)index
Expand Down

0 comments on commit dd67b6c

Please sign in to comment.