-
Notifications
You must be signed in to change notification settings - Fork 187
Fix replaceTextRange logic for unfocused StyledText with variable line height #2318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix replaceTextRange logic for unfocused StyledText with variable line height #2318
Conversation
|
@HeikoKlare could you please take a look and review? Do you think that the boundary checks do make sense? |
Test Results 539 files - 7 539 suites - 7 35m 19s ⏱️ + 6m 3s For more details on these failures, see this check. Results for commit 49fa012. ± Comparison against base commit bef3ad7. This pull request removes 54 and adds 1 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
747348b to
4b1f261
Compare
BeckerWdf
left a comment
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.
A don't know that code good enough to really be able to judge on this.
|
@HeikoKlare: Do you feel able to review this? |
mickaelistria
left a comment
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 general idea of adding checks to prevent error is fine.
But I find the repetition of a similar check everywhere is making the code less clear. I've put a few suggestions inline, which I believe can make algorithms slightly clearer to understand, refining the loops instead of adding conditions here and there.
| while (lineIndex < lineCount) { | ||
| if (delta <= 0) break; | ||
| delta -= renderer.getCachedLineHeight(lineIndex++); | ||
| if (lineIndex >= 0 && lineIndex < lineCount) { |
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 lineIndex < lineCount doesn't have to be repeated.
Also the condition lineIndex >= 0 can be skipped if we initialize lineIndex to Math.max(0, topIndex)
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.
done with 9ff6acd
| int topIndexHeight = 0; | ||
| if (topIndex >= 0 && topIndex < lineCount) { | ||
| topIndexHeight = renderer.getCachedLineHeight(topIndex); | ||
| } | ||
| topIndexY = -topIndexHeight - delta; |
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.
Wouldn't it be clearer to write it like
if (topIndex >= 0 && topIndex < lineCount) {
topIndexY = -renderer.getCachedLineHeight(topIndex);
}
topIndexY -= delta;
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.
done with 9ff6acd
| return topIndexY + topMargin; | ||
| int height = topIndexY; | ||
| if (lineIndex > topIndex) { | ||
| for (int i = topIndex; i < lineIndex; i++) { |
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.
Can't we use
for (int i = Math.max(topIndex, 0); i < Math.min(lineIndex, lineCount); i++) {
here and get rid of next conditions?
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.
done with 9ff6acd
|
Sorry, I think I am not the best to review here as I am not that deep into the StyledText. Thanks to @mickaelistria for jumping in and reviewing! In case you need a second look from my side, please ping me again. |
|
thanks a lot @mickaelistria for reviewing the code. Do you think that we can merge this change? |
9ff6acd to
8fc5cf8
Compare
mickaelistria
left a comment
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.
(sorry for nitpicking, but I have sad memories of fighting with this code and can't resist the discussion)
I have put some comments inline about possible ways to make code clearer.
| } | ||
| } else { | ||
| for (int i = topIndex - 1; i >= lineIndex; i--) { | ||
| if (i < 0 || i >= lineCount) { |
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 condition could also be merged in the loop, just like above (with the trick that te loop is reversed here)
for (int i - Math.min(topIndex - 1, lineCount); i >= Math.max(0, lineIndex); i--) {
or, if you prefer expressive names
int lastLineToConsider = Math.min(topIndex - 1, lineCount);
int firstLineToConsider = Math.max(0, lineIndex);
for (int i = lastLineToConsider; i >= firstLineToConsider; i--) {
...
which actually isn't so interesting to get in reverse order, it could easily be
for (int i = firstLineToConsider, i <= lastLineToConsider; i++)
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.
done with 4d9d7e6; looks like that you were testing me with int lastLineToConsider = Math.min(topIndex - 1, lineCount); if I understood the algorithm :-) But luckily, the test detected the off-by-one error.
|
Great, thanks! |
done with [95618eb] - could you please check? Did I catch all locations? I hope I did not introduce any error with this change. |
| } | ||
| if (lineIndex == 0 || -delta + renderer.getCachedLineHeight(lineIndex) <= clientAreaHeight - topMargin - bottomMargin) { | ||
| int lineHeight = 0; | ||
| if (lineIndex >= 0 && lineIndex < lineCount) { |
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.
could be lineExists(lineIndex)?
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.
Thanks a lot. Sorry my fault, I was searching for all occurrences of "lineCount" but somehow missed this location. Code updated with [99ae962].
| int lineHeight = renderer.getCachedLineHeight(lineIndex - 1); | ||
| int previousLineIndex = lineIndex - 1; | ||
| int lineHeight = 0; | ||
| if (previousLineIndex >= 0 && previousLineIndex < lineCount) { |
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.
could be lineExists(lineIndex)?
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.
Thanks a lot. Sorry my fault, I was searching for all occurrences of "lineCount" but somehow missed this location. Code updated with [99ae962].
| } else { | ||
| topIndex = lineIndex - 1; | ||
| topIndexY = -renderer.getCachedLineHeight(topIndex) - delta; | ||
| if (topIndex >= 0 && topIndex < lineCount) { |
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.
could be lineExists(lineIndex)?
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.
Thanks a lot. Sorry my fault, I was searching for all occurrences of "lineCount" but somehow missed this location. Code updated with [99ae962].
| lineIndex++; | ||
| } | ||
| int lineHeight = 0; | ||
| if (lineIndex >= 0 && lineIndex < lineCount) { |
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.
could be lineExists(lineIndex)?
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.
Thanks a lot. Sorry my fault, I was searching for all occurrences of "lineCount" but somehow missed this location. Code updated with [99ae962].
|
Thanks, let's merge it now! |
|
@tobiasmelcher Can you please squash those commits as a single one and push --force on your branch to update the PR? |
height - Updated productive code to prevent IllegalArgumentException when replacing text in a StyledText control with variable line height and no focus, by adding boundary checks. - Added a regression test to validate the fix: ensures no exception is thrown when replacing the full text range under these conditions. Fixes: eclipse-platform#2302
99ae962 to
49fa012
Compare
|
test error seems not to be related to this change
|
c0dbf00
into
eclipse-platform:master
|
Thank you! |
|
Thanks @tobiasmelcher for fixing and thanks @mickaelistria for reviewing and merging. |
Fixes: #2302