-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Conversation
@frederoni, thanks for your PR! By analyzing this pull request, we identified @1ec5, @boundsj and @friedbunny to be potential reviewers. |
6dd6fae
to
81a728e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some off-by-one errors remain because we need to account for the reversal in some special cases.
macosapp's Layers sidebar should continue to list the layers top to bottom, but I can take care of that in a separate PR.
@@ -293,7 +293,7 @@ - (void)insertObject:(MGLStyleLayer *)styleLayer inLayersAtIndex:(NSUInteger)ind | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cases above need to be adjusted. If you pass in an index equal to the number of layers, an exception should be raised. If you pass in an index of 0, the layer should be inserted below the bottommost layer, not above the topmost layer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you pass in an index of 0 and the number of layers is 0? The layer should be inserted and no exception should be raised. -[NSMutableArray insertObject:atIndex:]
allows index=size but + 1 is out of bounds. The second argument still holds but the first should be >
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you: #7484 (comment). Unfortunately it's this comment that stuck around. 😁
@@ -308,7 +308,7 @@ - (void)removeObjectFromLayersAtIndex:(NSUInteger)index | |||
[NSException raise:NSRangeException |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's valid to remove the last (topmost) layer in the style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These off-by-one errors are confusing but it's still possible to remove the topmost layer.
Perhaps a greater than or equal to operator would make it easier to reason about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you pass in the index
of the last layer in the array, wouldn’t index
be equal to layers.size() - 1
, triggering this exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be equal but it wouldn't trigger because the operator is >
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh, I’m blind! 😆 OK, I remember intentionally using > … - 1
here to parallel the subtraction below. Now that that line below has been simplified, let’s use >=
instead.
Some exception messages still need to be adjusted: for example, this message should say “cannot be placed below”, not above. I think we need to double-check all three insertion methods to make sure they insert before or after the sibling layer as appropriate. |
Feel free to implement that in this PR if it makes it easier. |
@@ -282,18 +282,19 @@ - (void)insertObject:(MGLStyleLayer *)styleLayer inLayersAtIndex:(NSUInteger)ind | |||
styleLayer]; | |||
} | |||
auto layers = self.mapView.mbglMap->getLayers(); | |||
if (index > layers.size()) { | |||
if (index >= layers.size()) { | |||
[NSException raise:NSRangeException | |||
format:@"Cannot insert style layer at out-of-bounds index %lu.", (unsigned long)index]; | |||
} else if (index == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a special case for when index == layers.size()
, not for when index
is 0, and it should continue to insert below nil
. The else
case below will crash for index == layers.size()
because layers.at(layers.size())
is out of bounds, even if you make the change below to say layers.at(index)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
index == layers.size()
gets caught by the case above, which I think is incorrect: you can append a single object to NSMutableArray by passing its count
into -insertObject:atIndex:
; an exception only gets raised if the index is count + 1
or higher.
} catch (const std::runtime_error & err) { | ||
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; | ||
} | ||
} else { | ||
try { | ||
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(layers.size() - index)]; | ||
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index + 1)]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inserting a layer at an index should put it below (before) the layer currently at that index. Therefore, this should be layers.at(index)
.
@@ -448,7 +449,7 @@ - (void)insertLayer:(MGLStyleLayer *)layer aboveLayer:(MGLStyleLayer *)sibling { | |||
if (index + 1 > layers.size()) { | |||
[NSException raise:NSInvalidArgumentException | |||
format: | |||
@"A style layer cannot be placed above %@ in the style. " | |||
@"A style layer cannot be placed below %@ in the style. " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I misread the diff when I suggested rewording this to say “below” instead of “above” in #7484 (comment). It should indeed say “above”, because that’s what this method is all about.
f2e6c08
to
d885d64
Compare
@@ -293,7 +293,7 @@ - (void)insertObject:(MGLStyleLayer *)styleLayer inLayersAtIndex:(NSUInteger)ind | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you: #7484 (comment). Unfortunately it's this comment that stuck around. 😁
d885d64
to
c8149bf
Compare
Fixed #7474 Reverse MGLStyle.layers.