Skip to content
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

Fix TextInfo.moveToCodepointOffset() in PoEdit #16484

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions source/NVDAObjects/window/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import (
Dict,
Optional,
Self,
Union,
)

Expand Down Expand Up @@ -842,6 +843,19 @@ def updateSelection(self):
self.obj.ITextSelectionObject.start=self._rangeObj.start
self.obj.ITextSelectionObject.end=self._rangeObj.end

def getTextInfoForCodepointMovement(self) -> Self:
# In PoEdit ITextDocumentTextInfo sometimes cannot access the last character when that character is
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also reproducible in Wordpad, so seems to be ITextRange related in general. Could you please update this comment accordingly?


# a newline. In this case collapse(True) takes us not to the end of textInfo, but right before
# trailing newline character, which causes adverse side effects in moveToCodepointOffset() function.
# Trimming trailing newline character here to work around.
info = self.copy()
collapsedInfo = info.copy()
collapsedInfo.collapse(end=True)
if collapsedInfo.compareEndPoints(info, "endToEnd") < 0:
info.setEndPoint(collapsedInfo, "endToEnd")
return info


class EditBase(Window):
""""Base class for Edit and Rich Edit controls, shared by legacy and UIA implementations."""
Expand Down
11 changes: 7 additions & 4 deletions source/textInfos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,10 @@ def getMathMl(self, field):
@raise LookupError: If MathML can't be retrieved for this field.
"""
raise NotImplementedError


def getTextInfoForCodepointMovement(self) -> Self:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a private method:

Suggested change
def getTextInfoForCodepointMovement(self) -> Self:
def _getTextInfoForCodepointMovement(self) -> Self:

return self.copy()

def moveToCodepointOffset(
self,
codepointOffset: int,
Expand Down Expand Up @@ -748,15 +751,15 @@ def moveToCodepointOffset(
we reduce the count of characters in order to make sure
the algorithm makes some progress on each iteration.
"""
text = self.text
info = self.getTextInfoForCodepointMovement()
text = info.text
if codepointOffset < 0 or codepointOffset > len(text):
raise ValueError
if codepointOffset == 0 or codepointOffset == len(text):
result = self.copy()
result = info.copy()
result.collapse(end=codepointOffset > 0)
return result

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