Skip to content

Commit cbb0de2

Browse files
committed
Fix TextInfo.moveToCodepointOffset() in PoEdit
1 parent aff0608 commit cbb0de2

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

source/NVDAObjects/window/edit.py

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import (
77
Dict,
88
Optional,
9+
Self,
910
Union,
1011
)
1112

@@ -842,6 +843,20 @@ def updateSelection(self):
842843
self.obj.ITextSelectionObject.start=self._rangeObj.start
843844
self.obj.ITextSelectionObject.end=self._rangeObj.end
844845

846+
def getTextInfoForCodepointMovement(self) -> Self:
847+
# # In PoEdit ITextDocumentTextInfo sometimes cannot access the last character when that character is
848+
# a newline. In this case collapse(True) takes us not to the end of textInfo, but right before
849+
# trailing newline character, which causes adverse side effects in moveToCodepointOffset() function.
850+
# Trimming trailing newline character here to work around.
851+
info = self.copy()
852+
collapsedInfo = info.copy()
853+
collapsedInfo.collapse(end=True)
854+
if collapsedInfo.compareEndPoints(info, "endToEnd") < 0:
855+
import tones
856+
tones.beep(500, 50)
857+
info.setEndPoint(collapsedInfo, "endToEnd")
858+
return info
859+
845860

846861
class EditBase(Window):
847862
""""Base class for Edit and Rich Edit controls, shared by legacy and UIA implementations."""

source/textInfos/__init__.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,10 @@ def getMathMl(self, field):
655655
@raise LookupError: If MathML can't be retrieved for this field.
656656
"""
657657
raise NotImplementedError
658-
658+
659+
def getTextInfoForCodepointMovement(self) -> Self:
660+
return self.copy()
661+
659662
def moveToCodepointOffset(
660663
self,
661664
codepointOffset: int,
@@ -748,15 +751,15 @@ def moveToCodepointOffset(
748751
we reduce the count of characters in order to make sure
749752
the algorithm makes some progress on each iteration.
750753
"""
751-
text = self.text
754+
info = self.getTextInfoForCodepointMovement()
755+
text = info.text
752756
if codepointOffset < 0 or codepointOffset > len(text):
753757
raise ValueError
754758
if codepointOffset == 0 or codepointOffset == len(text):
755-
result = self.copy()
759+
result = info.copy()
756760
result.collapse(end=codepointOffset > 0)
757761
return result
758762

759-
info = self.copy()
760763
# Total codepoint Length represents length in python characters of Current TextInfo we're workoing with.
761764
# We start with self, and then gradually divide and conquer in order to find desired offset.
762765
totalCodepointOffset = len(text)

0 commit comments

Comments
 (0)