Skip to content

Fix text editor search crash and stale highlighting after document edits - #4801

Open
jester-sys wants to merge 1 commit into
TeamAmaze:release/4.0from
jester-sys:fix/4792-text-editor-search
Open

jester-sys wants to merge 1 commit into
TeamAmaze:release/4.0from
jester-sys:fix/4792-text-editor-search

Conversation

@jester-sys

Copy link
Copy Markdown

Description

This PR fixes the text editor crash and incorrect search highlighting that occur when the document is modified while a search is active.

Fixes #4792.

Root Cause

Search result indices were not invalidated when the document content changed. As a result, the previous and next navigation buttons continued using outdated character offsets.

When the document was shortened, these invalid offsets were passed to setSpan(), causing an IndexOutOfBoundsException and loss of unsaved changes.

Same-length text replacements could also result in incorrect highlighting.

Changes Made

  • Invalidate existing search results when the document content or search query changes.
  • Cancel previous asynchronous search tasks.
  • Clear stale search indices and highlighting spans.
  • Recompute search results using the latest document content.
  • Introduce a search generation counter to prevent outdated asynchronous callbacks from updating the UI.
  • Prevent search callbacks from updating a destroyed activity.
  • Disable navigation buttons until fresh search results are available.
  • Validate character offsets before applying highlighting spans.
  • Preserve existing document modification and saving logic.

Before Fix

The application crashes when navigating search results after shortening the document.

Crash Log

FATAL EXCEPTION: main
Process: com.amaze.filemanager.debug

java.lang.IndexOutOfBoundsException:
setSpan (26 ... 31) ends beyond length 8

at android.text.SpannableStringBuilder.checkRange
at android.text.SpannableStringBuilder.setSpan
at androidx.emoji2.text.SpannableBuilder.setSpan
at TextEditorActivity.colorSearchResult(TextEditorActivity.java:594)
at TextEditorActivity.unhighlightCurrentSearchResult(TextEditorActivity.java:572)
at TextEditorActivity.onClick(TextEditorActivity.java:555)

Before Fix Video

🎥 Watch Video Demonstration
Before.Crash.Video-2.mp4

After Fix

🎥 Watch Video Demonstration
After_View-2.mp4
  • The reported crash no longer occurs during the tested reproduction steps.
  • Search results are automatically recomputed after document edits.
  • Invalid highlights are removed.
  • Remaining valid matches continue to be highlighted.
  • Previous and next search navigation works correctly.
  • Unsaved document changes remain intact.

Logcat Verification

No recurrence of the reported IndexOutOfBoundsException or related fatal exception was observed in the post-fix log capture.

[Attach after-fix crash log here]

After Fix Video

Manual Testing

  • Reproduced the original crash before implementing the fix.
  • Verified that the reported crash no longer occurs.
  • Verified same-length text replacement.
  • Verified highlighting of remaining valid matches.
  • Verified previous and next search navigation.
  • Verified rapid document editing.
  • Verified preservation of unsaved changes.

Testing Environment

  • Device: Android Emulator
  • Android SDK: API 37
  • Build: Debug
  • Package: com.amaze.filemanager.debug

Scope

The changes are limited to TextEditorActivity.java. Existing file-saving and document modification tracking logic remains unchanged.

All listed manual regression tests passed.

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.

Text editor: search arrows crash or highlight the wrong text after editing

1 participant