-
Notifications
You must be signed in to change notification settings - Fork 176
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
base: master
Are you sure you want to change the base?
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? |
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
747348b
to
4b1f261
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.
A don't know that code good enough to really be able to judge on this.
@HeikoKlare: Do you feel able to review this? |
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)
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;
@@ -3877,10 +3903,16 @@ public int getLinePixel(int lineIndex) { | |||
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?
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. |
Fixes: #2302