Fix cloze numbering in Instant Note when text already contains clozes#20360
Fix cloze numbering in Instant Note when text already contains clozes#20360Alok-Silswal wants to merge 2 commits intoankidroid:mainfrom
Conversation
criticalAY
left a comment
There was a problem hiding this comment.
The seems LLM generated, comments are still there
| /** Sync cloze counter with actual text. | ||
| When shared text already contains {{cX::}}, we must detect existing indices and start from max + 1. | ||
| Also update intClozeList so undo/reset logic works correctly. */ |
There was a problem hiding this comment.
Doc can be improved
" Make cloze counter and intClozeList consistent with actual text. "
How about this one?
| val existingNumbers = | ||
| clozePattern | ||
| .findAll(text) | ||
| .mapNotNull { it.groups[2]?.value?.toIntOrNull() } | ||
| .toList() | ||
|
|
||
| // Sync the list of indices | ||
| intClozeList.clear() | ||
| intClozeList.addAll(existingNumbers) | ||
|
|
||
| // Set the next cloze number to max + 1 | ||
| _currentClozeNumber.value = (existingNumbers.maxOrNull() ?: 0) + 1 |
There was a problem hiding this comment.
We can do without intermediate list allocation
There was a problem hiding this comment.
Understood! We will allocate directly then.
intClozeList.clear()
var max = 0
clozePattern.findAll(text).forEach {
val value = it.groups[2]?.value?.toIntOrNull()
if (value != null) {
intClozeList.add(value)
if (value > max) max = value
}
}
_currentClozeNumber.value = max + 1
Is this approach correct?
I had over-explained the details. Simplified it now. |
Alok-Silswal
left a comment
There was a problem hiding this comment.
Improved the doc and removed intermediate list allocation.
Can I confirm you've read our AI policy: https://github.com/ankidroid/Anki-Android/blob/main/AI_POLICY.md? The code you provided was AI-generated |
I have read it now. Code logic was mine (POV: I do DSA regularly), but I used AI for implementation and for polishing PR description also in the first one(whose words were also mine). Now I understand that up till 3 PR we cannot use AI in any form. This makes this PR ineligible. Shall I rework this PR or close it to submit a new one? |
|
I checked other PRs with same issue and seems like this will not work. |
Purpose / Description
Instant Note was not handling cloze numbering properly when the shared text already had clozes like
{{c1::}},{{c2::}}, etc.This PR fixes it.
Fixes
Approach
The fix ensures that the cloze counter is derived from the actual text instead of assuming it always starts from
1.Mode switching was simplified so that changing modes does not change the counter by itself.
Flow Summary:
During Selection:
How Has This Been Tested?
Verified that existing unit tests pass (
InstantEditorViewModelTest.kt).Tested on my phone (Android 14, Xiaomi / HyperOS) using a debug build.
Used the following two texts for testing:
Two screen recording attached as evidence.
Checklist
Please, go through these checks before submitting the PR.
Cloze_fix_evidence_1.mp4
Cloze_fix_evidence_2.mp4