Skip to content

Commit

Permalink
Allow MGSBreakpointDelegate to return breakpoints in an NSIndexSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
shysaur committed Mar 6, 2016
1 parent 71043c1 commit 69e7043
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Fragaria.xcodeproj/project.pbxproj
Expand Up @@ -62,6 +62,8 @@
01F0D7111C120FC1000A08B6 /* Fragaria.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = AB69B62B118B745700903D1D /* Fragaria.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
01F0D7151C126957000A08B6 /* MASPreferencesWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = D0718B821AB553A20073493A /* MASPreferencesWindowController.m */; };
01F0D7171C126E04000A08B6 /* MASPreferencesWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = D0718B801AB553A20073493A /* MASPreferencesWindow.xib */; };
01F336901C8C962100B5F395 /* NSSet+Fragaria.h in Headers */ = {isa = PBXBuildFile; fileRef = 01F3368E1C8C962100B5F395 /* NSSet+Fragaria.h */; };
01F336911C8C962100B5F395 /* NSSet+Fragaria.m in Sources */ = {isa = PBXBuildFile; fileRef = 01F3368F1C8C962100B5F395 /* NSSet+Fragaria.m */; };
01FBC3831C1EADDF00837F04 /* MGSStringColumnTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 01FBC3821C1EADDF00837F04 /* MGSStringColumnTests.m */; };
01FBC7EC1B0679520008EAF8 /* MGSMutableSubstring.m in Sources */ = {isa = PBXBuildFile; fileRef = 01FBC7EA1B0679520008EAF8 /* MGSMutableSubstring.m */; };
01FC487F1C1C7EC600F4227E /* Fragaria.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = AB69B62B118B745700903D1D /* Fragaria.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -301,6 +303,8 @@
01F0D7001C120F7D000A08B6 /* Document.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Document.swift; sourceTree = "<group>"; };
01F0D70A1C120F7D000A08B6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
01F0D7161C1269A2000A08B6 /* Fragaria-Swift-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Fragaria-Swift-Bridging-Header.h"; sourceTree = "<group>"; };
01F3368E1C8C962100B5F395 /* NSSet+Fragaria.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSSet+Fragaria.h"; sourceTree = "<group>"; };
01F3368F1C8C962100B5F395 /* NSSet+Fragaria.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSSet+Fragaria.m"; sourceTree = "<group>"; };
01FBC3821C1EADDF00837F04 /* MGSStringColumnTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGSStringColumnTests.m; sourceTree = "<group>"; };
01FBC7E91B0679520008EAF8 /* MGSMutableSubstring.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGSMutableSubstring.h; sourceTree = "<group>"; };
01FBC7EA1B0679520008EAF8 /* MGSMutableSubstring.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGSMutableSubstring.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -695,6 +699,8 @@
014865681B19036900BE1DC4 /* NSObject+Fragaria.m */,
01FBC7E91B0679520008EAF8 /* MGSMutableSubstring.h */,
01FBC7EA1B0679520008EAF8 /* MGSMutableSubstring.m */,
01F3368E1C8C962100B5F395 /* NSSet+Fragaria.h */,
01F3368F1C8C962100B5F395 /* NSSet+Fragaria.m */,
);
name = Miscellaneous;
sourceTree = "<group>";
Expand Down Expand Up @@ -913,6 +919,7 @@
files = (
D0718B491AB54EA70073493A /* MGSPrefsColourPropertiesViewController.h in Headers */,
D0718B441AB54E8C0073493A /* MGSPrefsEditorPropertiesViewController.h in Headers */,
01F336901C8C962100B5F395 /* NSSet+Fragaria.h in Headers */,
019F56D51A9D3511009F9DCD /* SMLTextView+MGSDragging.h in Headers */,
019B80191A9E77330007D234 /* MGSFragariaTextViewDelegate.h in Headers */,
AB69B754118B830700903D1D /* Fragaria.h in Headers */,
Expand Down Expand Up @@ -1248,6 +1255,7 @@
D07717B11AC25A8800B23867 /* MGSBoolToGlobalHintTransformer.m in Sources */,
017FD58B1A7A992700B305FB /* MGSLineNumberView.m in Sources */,
AB69B68F118B78D400903D1D /* SMLTextView.m in Sources */,
01F336911C8C962100B5F395 /* NSSet+Fragaria.m in Sources */,
D06653D31AC00D3D00ACE8B0 /* MGSColourToPlainTextTransformer.m in Sources */,
AB69B6DA118B7F2A00903D1D /* SMLLayoutManager.m in Sources */,
AB3971CB118C1F8A00AEF388 /* MGSSyntaxController.m in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions MGSBreakpointDelegate.h
Expand Up @@ -35,9 +35,9 @@

/** Returns a set of one-based line numbers containing breakpoints.
* @param sender The MGSFragaria instance which sent the message.
* @return A set of NSNumber indicating the line numbers that have
* breakpoints. */
- (NSSet *)breakpointsForFragaria:(MGSFragariaView *)sender;
* @return An NSSet of NSNumbers, or an NSIndexSet, indicating the line
* numbers that have breakpoints. */
- (id)breakpointsForFragaria:(MGSFragariaView *)sender;

/** Returns the color for the breakpoint marker to be shown at the specified
* line.
Expand Down
14 changes: 13 additions & 1 deletion MGSLineNumberView.m
Expand Up @@ -39,6 +39,7 @@
#import "MGSLineNumberView.h"
#import "MGSBreakpointDelegate.h"
#import "NSTextStorage+Fragaria.h"
#import "NSSet+Fragaria.h"


#define RULER_MARGIN 5.0
Expand Down Expand Up @@ -710,6 +711,7 @@ - (void)reloadBreakpointData
{
NSMutableDictionary *data;
NSSet *linesWithBreakpoints;
id tmp;
NSNumber *line;

if (!_breakpointDelegate) {
Expand All @@ -725,7 +727,17 @@ - (void)reloadBreakpointData
data = [NSMutableDictionary dictionary];

if ([_breakpointDelegate respondsToSelector:@selector(breakpointsForFragaria:)]) {
linesWithBreakpoints = [_breakpointDelegate breakpointsForFragaria:self.fragaria];
tmp = [_breakpointDelegate breakpointsForFragaria:self.fragaria];
if ([tmp isKindOfClass:[NSIndexSet class]]) {
linesWithBreakpoints = [[NSSet alloc] mgs_initWithIndexSet:tmp];
} else if ([tmp isKindOfClass:[NSSet class]]) {
linesWithBreakpoints = tmp;
} else {
[NSException raise:@"MGSBrokenBreakpointDelegate" format:@"The "
"breakpoint delegate %@ of %@ returned an object which is not an "
"NSSet or an NSIndexSet, from the -breakpointsForFragaria: method.",
_breakpointDelegate, self];
}
} else {
[NSException raise:@"MGSBrokenBreakpointDelegate" format:@"The breakpoint "
"delegate %@ of %@ does not implement at least one of the following "
Expand Down
22 changes: 22 additions & 0 deletions NSSet+Fragaria.h
@@ -0,0 +1,22 @@
//
// NSSet+Fragaria.h
// Fragaria
//
// Created by Daniele Cattaneo on 06/03/16.
//
/// @cond PRIVATE

#import <Foundation/Foundation.h>


/** This private category adds an utility initializer to NSSet. */

@interface NSSet (Fragaria)


/** Returns an NSSet of those NSNumbers that are indexes of the specified
* index set.
* @param is An index set. */
- (NSSet *)mgs_initWithIndexSet:(NSIndexSet *)is;

@end
147 changes: 147 additions & 0 deletions NSSet+Fragaria.m
@@ -0,0 +1,147 @@
//
// NSSet+Fragaria.m
// Fragaria
//
// Created by Daniele Cattaneo on 06/03/16.
//
//

#import "NSSet+Fragaria.h"


@interface MGSSetBackedByIndexSet: NSSet

- (instancetype)initWithIndexSet:(NSIndexSet *)is;

- (NSUInteger)count;
- (id)member:(id)obj;
- (NSEnumerator *)objectEnumerator;

@end


@interface MGSSetBackedByIndexSetEnumerator : NSEnumerator

- (instancetype)initWithSet:(MGSSetBackedByIndexSet *)s;

- (NSArray *)allObjects;
- (id)nextObject;

@end


@implementation MGSSetBackedByIndexSet
{
NSIndexSet *_backingStore;
}


- (instancetype)initWithIndexSet:(NSIndexSet *)is
{
self = [super init];
_backingStore = [is copy];
return self;
}


- (NSUInteger)count
{
return [_backingStore count];
}


- (id)member:(id)obj
{
if (![obj isKindOfClass:[NSNumber class]])
return nil;

/* Lowercase objCTypes correspond to signed values. */
if (islower([obj objCType][0])) {
/* Bail out if negative */
if ([obj compare:@(0)] == NSOrderedAscending)
return nil;
}

if ([_backingStore containsIndex:[obj unsignedIntegerValue]])
return obj;
return nil;
}


- (NSEnumerator *)objectEnumerator
{
return [[MGSSetBackedByIndexSetEnumerator alloc] initWithSet:self];
}


- (NSIndexSet *)_indexSet
{
return _backingStore;
}


@end


@implementation MGSSetBackedByIndexSetEnumerator
{
MGSSetBackedByIndexSet *_set;
NSUInteger _nextIndex;
}


- (instancetype)initWithSet:(MGSSetBackedByIndexSet *)s
{
self = [super init];
_set = s;
_nextIndex = 0;
return self;
}


- (NSArray *)allObjects
{
NSRange range;
__block NSMutableArray *a;
NSIndexSet *is = _set._indexSet;

a = [NSMutableArray array];
if (is.lastIndex != NSNotFound && is.lastIndex >= _nextIndex) {
range = NSMakeRange(_nextIndex, (is.lastIndex+1) - _nextIndex);
[is enumerateIndexesInRange:range options:0
usingBlock:^(NSUInteger i, BOOL *stop) {
[a addObject:@(i)];
}];
}
_set = nil;
return [a copy];
}


- (id)nextObject
{
NSUInteger idx;

idx = [_set._indexSet indexGreaterThanOrEqualToIndex:_nextIndex];
if (idx == NSNotFound) {
_set = nil;
return nil;
}
_nextIndex = idx+1;
return @(idx);
}


@end


@implementation NSSet (Fragaria)


- (NSSet *)mgs_initWithIndexSet:(NSIndexSet *)is
{
return [[MGSSetBackedByIndexSet alloc] initWithIndexSet:is];
}


@end

0 comments on commit 69e7043

Please sign in to comment.