adamontherun / xCodeGenerateDescriptionPlugin

Plugin to automatically generate the description for your class in XCode
103 stars 11 forks source link

Description method is added to top of .h file #21

Open pepejeria opened 9 years ago

pepejeria commented 9 years ago

OS X 10.11 and Xcode 7.01

I followed the instructions but the plugin is not working as expected, it adds the generated method to the top of the .h file:

- (NSString *)description
{
    return [NSString stringWithFormat:@"Measure description:%@\n unit: %@\nvalue: %@\n",[super description], self.unit, self.value];
}
/
//  Measure.h
//  optile
//
//  Created by José Jeria on 24/08/15.
//  Copyright (c) 2015. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Measure : NSObject

@property (strong, nonatomic) NSString *unit;
@property (strong, nonatomic) NSDecimalNumber *value;

@end

The other issue is that the menu option will disappear if Xcode is restarted.

JianboYan commented 8 years ago

I found the bug, and i fix it.

Bug locate in DescriptionGenerator.m, method writeMethodToFileWithDescriptionMethod:

i think the latest OS X changed [NSAPP openFile:]'s implementation, when you call it, it cannt open the source file immediately, so you need wait util next runloop comes.

it's a simple way to fix it-- using dispatch_async:

- (void)writeMethodToFileWithDescriptionMethod:(NSString *)descriptionMethod {
    [DTXcodeUtils openFile:[DTXcodeUtils getDotMFilePathOfCurrentEditFile]];
    dispatch_async(dispatch_get_main_queue(), ^{
        DVTSourceTextView *textView = [DTXcodeUtils currentSourceTextView];
        NSString *textViewText = [textView string];
        NSRange contentRange = [DTXcodeUtils getClassImplementContentRangeWithClassName:self.currentClass mFileText:textViewText];
        NSRange insertRange = [DTXcodeUtils getInsertRangeWithClassImplementContentRange:contentRange];
        [textView scrollRangeToVisible:insertRange];
        NSString *newLinesRemoved = [[textViewText componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];
        NSString *textViewTextSpacesRemoved = [newLinesRemoved stringByReplacingOccurrencesOfString:@" " withString:@""];

        if ([textViewTextSpacesRemoved containsString:@")description"]) {
            NSAlert *alert = [[NSAlert alloc] init];
            [alert setMessageText:@"Description method NOT generated because you already have one. Please delete existing method and try again."];
            [alert runModal];
            return;
        } else {
            [textView insertText:descriptionMethod replacementRange:insertRange];
        }
    });
}