CadQuery / CadQuery/CQ-editor

Autocomplete can replace text from the document start when no period exists

Open
#599 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.2k
Forks
210
PR merge metrics
No merged PRs in 30d

Description

## 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:

```python
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.