Summary
insert_completion() uses text_before_cursor.rfind('.') as a cursor position without handling the -1 result. Autocomplete can be triggered for a bare identifier, so a missing period is a reachable input. The code then moves the cursor to position 0 and can replace text from the start of the document instead of replacing the current token.
Code path
cq_editor/widgets/editor.py:464-478: rfind('.'), cursor positioning, selection, and insertion.
cq_editor/widgets/editor.py:362: autocomplete is triggered for partial identifiers, not only expressions containing a period.
Steps to reproduce
The source-level boundary is:
text_before_cursor = "import os\nim"
last_period_index = text_before_cursor.rfind(".")
assert last_period_index == -1
assert last_period_index + 1 == 0
In the current implementation, that value is passed to cursor.setPosition() before the completion text is selected and inserted.
Expected behavior
Completion should replace only the current identifier or use an explicit no-period branch. Text before the current token must remain unchanged.
Actual behavior
When no period exists before the cursor, rfind() returns -1; setPosition(0) moves the cursor to the document start and the following selection can delete or replace the first word.
Existing coverage
The checked issue and PR history did not contain an exact report or fix for this cursor calculation.
Suggested fix
Handle the no-period case explicitly, or compute the replacement range from the current token rather than treating -1 + 1 as a valid cursor position.
Suggested tests
- Bare identifier with no period.
- Period at the first character.
- Consecutive periods.
- Existing dotted completion path remains unchanged.
Submitted with Codex.
Summary
insert_completion()usestext_before_cursor.rfind('.')as a cursor position without handling the-1result. Autocomplete can be triggered for a bare identifier, so a missing period is a reachable input. The code then moves the cursor to position0and can replace text from the start of the document instead of replacing the current token.Code path
cq_editor/widgets/editor.py:464-478:rfind('.'), cursor positioning, selection, and insertion.cq_editor/widgets/editor.py:362: autocomplete is triggered for partial identifiers, not only expressions containing a period.Steps to reproduce
The source-level boundary is:
In the current implementation, that value is passed to
cursor.setPosition()before the completion text is selected and inserted.Expected behavior
Completion should replace only the current identifier or use an explicit no-period branch. Text before the current token must remain unchanged.
Actual behavior
When no period exists before the cursor,
rfind()returns-1;setPosition(0)moves the cursor to the document start and the following selection can delete or replace the first word.Existing coverage
The checked issue and PR history did not contain an exact report or fix for this cursor calculation.
Suggested fix
Handle the no-period case explicitly, or compute the replacement range from the current token rather than treating
-1 + 1as a valid cursor position.Suggested tests
Submitted with Codex.