Skip to content

Commit

Permalink
Add a method to MGSBreakpointDelegate for updating breakpoints in res…
Browse files Browse the repository at this point in the history
…ponse to user edits.
  • Loading branch information
shysaur committed Mar 6, 2016
1 parent 69e7043 commit d3b6b57
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
11 changes: 11 additions & 0 deletions MGSBreakpointDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,16 @@
* @param line The one-based line number which the user clicked on. */
- (void)toggleBreakpointForFragaria:(MGSFragariaView *)sender onLine:(NSUInteger)line;

/** Tells the delegate that some lines were added or deleted from the text,
* to allow fixing any breakpoints that lie on these lines.
* @discussion Your implementation of this method should call
* -reloadBreakpointData on sender if any line number had to be
* fixed (it is not automatic).
* @param newRange The affected range of one-based line numbers.
* @param delta How many lines were added (if positive) or deleted (if
* negative) during the edit.
* @param sender The MGSFragaria instance which sent the message. */
- (void)fixBreakpointsOfAddedLines:(NSInteger)delta inLineRange:(NSRange)newRange ofFragaria:(MGSFragariaView *)sender;


@end
29 changes: 29 additions & 0 deletions MGSLineNumberView.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ @implementation MGSLineNumberView
NSSize _markerImagesSize;

NSDictionary *_breakpointData;
NSUInteger _lastLineCount;
}


Expand Down Expand Up @@ -167,6 +168,7 @@ - (void)layoutManagerWillChangeTextStorage
- (void)layoutManagerDidChangeTextStorage
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
_lastLineCount = self.clientView.textStorage.mgs_lineCount;
[nc addObserver:self selector:@selector(textStorageDidProcessEditing:)
name:NSTextStorageDidProcessEditingNotification object:self.clientView.textStorage];
}
Expand Down Expand Up @@ -202,6 +204,33 @@ - (void)setBreakpointDelegate:(id<MGSBreakpointDelegate>)breakpointDelegate

- (void)textStorageDidProcessEditing:(NSNotification *)notification
{
id <MGSBreakpointDelegate> bd;
NSTextStorage *ts;
NSRange ecr, elr;
NSInteger charDelta, lineDelta;

bd = self.breakpointDelegate;
if ([bd respondsToSelector:
@selector(fixBreakpointsOfAddedLines:inLineRange:ofFragaria:)]) {
ts = self.clientView.textStorage;
charDelta = ts.changeInLength;
if (charDelta && self.lineCount != _lastLineCount) {
lineDelta = self.lineCount - _lastLineCount;

ecr = ts.editedRange;
elr.location = [ts mgs_rowOfCharacter:ecr.location];
if (ecr.length)
elr.length = [ts mgs_rowOfCharacter:NSMaxRange(ecr)] - elr.location;
else
elr.length = 0;
elr.location++;
elr.length++;

[bd fixBreakpointsOfAddedLines:lineDelta inLineRange:elr ofFragaria:_fragaria];
}
}
_lastLineCount = self.lineCount;

[self setNeedsDisplay:YES];
}

Expand Down
4 changes: 1 addition & 3 deletions NSTextStorage+Fragaria.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ - (void)textStorageWillProcessEditing:(NSNotification *)notification
lastEditedRange = ts.editedRange;
lastChangeInLength = ts.changeInLength;
} else {
/* Merging edited ranges is difficult, so we give up. Also, it is
* unreliable because I saw this notification fire twice for
* the same edit. */
/* Merging edited ranges is difficult, so we give up. */
lineCountGuess = NSNotFound;
}
}
Expand Down

0 comments on commit d3b6b57

Please sign in to comment.