Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tobiasmelcher
Copy link
Contributor

  • 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: #2302

@tobiasmelcher
Copy link
Contributor Author

@HeikoKlare could you please take a look and review? Do you think that the boundary checks do make sense?

Copy link
Contributor

github-actions bot commented Jul 16, 2025

Test Results

   546 files  ±0     546 suites  ±0   28m 53s ⏱️ - 2m 8s
 4 408 tests +1   4 391 ✅ +1   17 💤 ±0  0 ❌ ±0 
16 717 runs  +4  16 590 ✅ +4  127 💤 ±0  0 ❌ ±0 

Results for commit 4b1f261. ± Comparison against base commit 95b46b6.

♻️ This comment has been updated with latest results.

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
@tobiasmelcher tobiasmelcher force-pushed the styled_text_index_out_of_bounds branch from 747348b to 4b1f261 Compare July 16, 2025 15:26
Copy link
Contributor

@BeckerWdf BeckerWdf left a 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.

@BeckerWdf
Copy link
Contributor

@HeikoKlare: Do you feel able to review this?

Copy link
Contributor

@mickaelistria mickaelistria left a 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) {
Copy link
Contributor

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)

Comment on lines +1100 to +1104
int topIndexHeight = 0;
if (topIndex >= 0 && topIndex < lineCount) {
topIndexHeight = renderer.getCachedLineHeight(topIndex);
}
topIndexY = -topIndexHeight - delta;
Copy link
Contributor

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++) {
Copy link
Contributor

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?

@HeikoKlare
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression for programmatic deletion of text from non-focused StyledText with non-fixed line-height
5 participants