Skip to content

Commit

Permalink
Correct calculation of caret when a key which triggered a commit *inc…
Browse files Browse the repository at this point in the history
…reases* the length of the transliteration

Resolves: #519

The input method https://github.com/rank-coder/khipro-m17n does this sometimes.
  • Loading branch information
mike-fabian committed Jul 9, 2024
1 parent 7da5c41 commit 28cccef
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions engine/hunspell_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,9 +915,8 @@ def _remove_character_after_cursor(self) -> None:
+self._typed_string[self._typed_string_cursor+1:])
self._update_transliterated_strings()

def get_caret(self) -> int:
'''
Get caret position in preëdit string
def get_caret(self, extra_msymbol: str = '') -> int:
'''Get caret position in preëdit string
The preëdit contains the transliterated string, the caret
position can only be approximated from the cursor position in
Expand Down Expand Up @@ -955,17 +954,32 @@ def get_caret(self) -> int:
the preëdit did not work at all. Now it works fine if
no transliteration is used and works better than nothing
even if transliteration is used.
https://github.com/mike-fabian/ibus-typing-booster/issues/519
A key which triggered a commit might have changed the
transliteration, see
https://bugzilla.redhat.com/show_bug.cgi?id=1353672 and it
might have even changed the length of the transliteration,
even increasing the length is possible. So sometimes we need
to consider an extra msymbol coming from the commit key to
calculate the caret position in the preedit string.
'''
preedit_ime = self._current_imes[0]
typed_string = self._typed_string[:self._typed_string_cursor]
if extra_msymbol and not self._typed_compose_sequence:
typed_string += [extra_msymbol]
transliterated_string_up_to_cursor = (
self._transliterators[preedit_ime].transliterate(
self._typed_string[:self._typed_string_cursor],
ascii_digits=self._ascii_digits))
typed_string, ascii_digits=self._ascii_digits))
if preedit_ime in ['ko-romaja', 'ko-han2']:
transliterated_string_up_to_cursor = unicodedata.normalize(
'NFKD', transliterated_string_up_to_cursor)
transliterated_string_up_to_cursor = unicodedata.normalize(
'NFC', transliterated_string_up_to_cursor)
if (extra_msymbol and not self._typed_compose_sequence
and transliterated_string_up_to_cursor.endswith(extra_msymbol)):
transliterated_string_up_to_cursor = (
transliterated_string_up_to_cursor[:-len(extra_msymbol)])
caret = len(transliterated_string_up_to_cursor)
if self._typed_compose_sequence:
caret += len(
Expand Down Expand Up @@ -7230,7 +7244,7 @@ def _process_key_event(self, key: itb_util.KeyEvent) -> bool:
candidate_was_selected = False
if self.get_lookup_table().cursor_visible:
candidate_was_selected = True
caret_was = self.get_caret()
caret_was = self.get_caret(extra_msymbol=key.msymbol)
if not input_phrase:
input_phrase = commit_string
self._commit_string(commit_string, input_phrase=input_phrase)
Expand Down

0 comments on commit 28cccef

Please sign in to comment.