From 5238965534d03bcabfd2f75be2e9fdf87a0ea5cf Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Tue, 12 Jul 2022 23:47:03 -0700 Subject: [PATCH] [Playlist Insert] Add a special case for filtered Insertions which occur when the playlist is filtered can try to add past the end of the playlist. Let's try to dodge that. Signed-off-by: Christopher Snowhill --- Playlist/PlaylistController.m | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Playlist/PlaylistController.m b/Playlist/PlaylistController.m index bb6fa0b8a..bb03f0d8c 100644 --- a/Playlist/PlaylistController.m +++ b/Playlist/PlaylistController.m @@ -847,6 +847,23 @@ static void *playlistControllerContext = &playlistControllerContext; } - (void)insertObjects:(NSArray *)objects atArrangedObjectIndexes:(NSIndexSet *)indexes { + // Special case, somehow someone is undoing while a search filter is in place + __block NSUInteger index = NSNotFound; + [indexes enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) { + if(range.location < index) { + index = range.location; + } + }]; + NSUInteger count = [[self content] count]; + if(index > count) { + // Ah, oops, bodge fix + __block NSMutableIndexSet *replacementIndexes = [[NSMutableIndexSet alloc] init]; + [indexes enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) { + [replacementIndexes addIndexesInRange:NSMakeRange(range.location - index + count, range.length)]; + }]; + indexes = replacementIndexes; + } + [[[self undoManager] prepareWithInvocationTarget:self] removeObjectsAtIndexes:[self disarrangeIndexes:indexes]]; NSString *actionName =