Make BSSourceView.markers be a set of line numbers, rather than Breakpoint*.
[macgdbp.git] / Source / BreakpointManager.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "BreakpointManager.h"
18
19 #import "AppDelegate.h"
20 #import "PreferenceNames.h"
21
22 @interface BreakpointManager (Private)
23 - (void)updateDisplaysForFile:(NSString*)file;
24 @end
25
26 @implementation BreakpointManager
27
28 @synthesize breakpoints, connection;
29
30 /**
31 * Initializer
32 */
33 - (id)init
34 {
35 if (self = [super init])
36 {
37 if (!breakpoints)
38 {
39 breakpoints = [[NSMutableArray alloc] init];
40 }
41
42 savedBreakpoints = [[[NSUserDefaults standardUserDefaults] arrayForKey:kPrefBreakpoints] mutableCopy];
43 if (savedBreakpoints)
44 {
45 for (NSDictionary* d in savedBreakpoints)
46 {
47 [breakpoints addObject:[[[Breakpoint alloc] initWithDictionary:d] autorelease]];
48 }
49 }
50 else
51 {
52 savedBreakpoints = [NSMutableArray new];
53 }
54 }
55 return self;
56 }
57
58 /**
59 * Returns the shared manager (singleton)
60 */
61 + (BreakpointManager*)sharedManager
62 {
63 static BreakpointManager* manager;
64 if (!manager)
65 {
66 manager = [[BreakpointManager alloc] init];
67 }
68 return manager;
69 }
70
71 /**
72 * Registers a breakpoint at a given line
73 */
74 - (void)addBreakpoint:(Breakpoint*)bp;
75 {
76 if (![breakpoints containsObject:bp])
77 {
78 [breakpoints addObject:bp];
79 [connection addBreakpoint:bp];
80
81 [savedBreakpoints addObject:[bp dictionary]];
82 [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:kPrefBreakpoints];
83
84 [self updateDisplaysForFile:[bp file]];
85 }
86 }
87
88 /**
89 * Removes a breakpoint at a given line/file combination, or nil if nothing was removed
90 */
91 - (Breakpoint*)removeBreakpointAt:(NSUInteger)line inFile:(NSString*)file
92 {
93 for (Breakpoint* b in breakpoints)
94 {
95 if ([b line] == line && [[b file] isEqualToString:file])
96 {
97 // Keep the breakpoint alive after it is removed from the breakpoints
98 // array.
99 [[b retain] autorelease];
100
101 [breakpoints removeObject:b];
102 [connection removeBreakpoint:b];
103
104 [savedBreakpoints removeObject:[b dictionary]];
105 [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:kPrefBreakpoints];
106
107 [self updateDisplaysForFile:file];
108 return b;
109 }
110 }
111 return nil;
112 }
113
114 /**
115 * Returns all the breakpoints for a given file
116 */
117 - (NSSet<NSNumber*>*)breakpointsForFile:(NSString*)file
118 {
119 NSMutableSet<NSNumber*>* matches = [NSMutableSet set];
120 for (Breakpoint* b in breakpoints) {
121 if ([b.file isEqualToString:file]) {
122 [matches addObject:@(b.line)];
123 }
124 }
125
126 return matches;
127 }
128
129 /**
130 * Checks to see if a given file has a breakpoint on a given line
131 */
132 - (BOOL)hasBreakpointAt:(NSUInteger)line inFile:(NSString*)file
133 {
134 return [breakpoints containsObject:[[[Breakpoint alloc] initWithLine:line inFile:file] autorelease]];
135 }
136
137 #pragma mark Private
138
139 /**
140 * This marks BSSourceView needsDisplay, rearranges the objects in the breakpoints controller,
141 * and sets the markers for the BSLineNumberView
142 */
143 - (void)updateDisplaysForFile:(NSString*)file
144 {
145 AppDelegate* appDel = [NSApp delegate];
146 [[[appDel breakpoint] arrayController] rearrangeObjects];
147 [[[appDel breakpoint] sourceView] setNeedsDisplay:YES];
148 [[[appDel breakpoint] sourceView] setMarkers:[self breakpointsForFile:file]];
149 [[[appDel debugger] sourceViewer] setNeedsDisplay:YES];
150 [[[appDel debugger] sourceViewer] setMarkers:[self breakpointsForFile:file]];
151 }
152
153 @end