@@ -87,7 +87,6 @@ - (void)addObject:(id)object forKey:(id)key {
87
87
NSUInteger keyIndex = [self .keys indexOfObject: key];
88
88
if (keyIndex == NSNotFound ) {
89
89
[self .keys addObject: key];
90
- keyIndex = [self .keys indexOfObject: key];
91
90
[self .items addObject: object];
92
91
} else {
93
92
[self .items replaceObjectAtIndex: keyIndex withObject: object];
@@ -396,6 +395,7 @@ - (void)_clearText
396
395
397
396
// /////////////////////////////////////////////////////////////////////////////////
398
397
#pragma mark - MCDVTTextStorage
398
+
399
399
static IMP OriginalFixAttributesInRangeIMP = nil ;
400
400
401
401
static void *kLastAttributeKey ;
@@ -406,14 +406,22 @@ - (void)fixAttributesInRange:(NSRange)range;
406
406
@interface NSObject (DVTTextStorage)
407
407
- (void )setLastAttribute : (NSDictionary *)attribute ;
408
408
- (NSDictionary *)lastAttribute ;
409
+ - (void )setConsoleStorage : (BOOL )consoleStorage ;
410
+ - (BOOL )consoleStorage ;
409
411
- (void )updateAttributes : (NSMutableDictionary *)attrs withANSIESCString : (NSString *)ansiEscString ;
410
412
@end
411
413
412
414
@implementation MCDVTTextStorage
413
415
414
416
- (void )fixAttributesInRange : (NSRange )range
415
417
{
416
- OriginalFixAttributesInRangeIMP (self, _cmd, range);
418
+ // To ignore those text storages which are not for console.
419
+ if (!self.consoleStorage ) {
420
+ return ;
421
+ }
422
+
423
+ // Workaround: Comment it out in case of EXC_BAD_ACCESS.
424
+ // OriginalFixAttributesInRangeIMP(self, _cmd, range);
417
425
418
426
__block NSRange lastRange = NSMakeRange (range.location , 0 );
419
427
NSMutableDictionary *attrs = [NSMutableDictionary dictionary ];
@@ -463,6 +471,16 @@ - (NSDictionary *)lastAttribute
463
471
return objc_getAssociatedObject (self, &kLastAttributeKey );
464
472
}
465
473
474
+ - (void )setConsoleStorage : (BOOL )consoleStorage
475
+ {
476
+ objc_setAssociatedObject (self, @selector (consoleStorage ), @(consoleStorage), OBJC_ASSOCIATION_RETAIN_NONATOMIC );
477
+ }
478
+
479
+ - (BOOL )consoleStorage
480
+ {
481
+ return [objc_getAssociatedObject (self , @selector (consoleStorage )) boolValue ];
482
+ }
483
+
466
484
- (void )updateAttributes : (NSMutableDictionary *)attrs withANSIESCString : (NSString *)ansiEscString
467
485
{
468
486
NSArray *attrComponents = [ansiEscString componentsSeparatedByString: @" ;" ];
@@ -754,6 +772,11 @@ - (BOOL)addCustomViews
754
772
if (!consoleTextView) {
755
773
return NO ;
756
774
}
775
+
776
+ MCDVTTextStorage *textStorage = [consoleTextView valueForKey: @" textStorage" ];
777
+ if ([textStorage respondsToSelector: @selector (setConsoleStorage: )]) {
778
+ [textStorage setConsoleStorage: YES ];
779
+ }
757
780
758
781
contentView = [self getParantViewByClassName: @" DVTControllerContentView" andView: consoleTextView];
759
782
NSView *scopeBarView = [self getViewByClassName: @" DVTScopeBarView" andContainerView: contentView];
@@ -895,7 +918,6 @@ void hookIDEConsoleItem()
895
918
void hookDVTTextStorage ()
896
919
{
897
920
Class DVTTextStorage = NSClassFromString (@" DVTTextStorage" );
898
-
899
921
Method fixAttributesInRange = class_getInstanceMethod (DVTTextStorage, @selector (fixAttributesInRange: ));
900
922
OriginalFixAttributesInRangeIMP = method_getImplementation (fixAttributesInRange);
901
923
IMP newFixAttributesInRangeIMP = class_getMethodImplementation ([MCDVTTextStorage class ], @selector (fixAttributesInRange: ));
0 commit comments