UI: a Python code edit is never completed from the column

While a Python cell's code was edited (F2 or the formula bar), typing at the
end of the code could offer AutoComplete from a text value in the same column,
and Enter then committed that text as the code. The editor no longer offers a
completion during a code edit, and committing a code edit ignores one.
This commit is contained in:
prxnvt
2026-11-01 14:07:15 -05:00
parent cd51fe110d
commit 6c478475e4
3 changed files with 17 additions and 1 deletions
+2 -1
View File
@@ -100,7 +100,8 @@ impl SheetApp {
let Some(ed) = self.editor.take() else { return true };
self.session.mode = gridcraft_engine::Mode::Ready;
let text = match &ed.completion {
Some(full) if full.to_lowercase().starts_with(&ed.text.to_lowercase()) => full.clone(),
// A code edit commits the code as typed, whatever a completion holds.
Some(full) if !ed.code_edit && full.to_lowercase().starts_with(&ed.text.to_lowercase()) => full.clone(),
_ => ed.text.clone(),
};
// F2 then Enter (or a file command) on an untouched draft: nothing to write.
+2
View File
@@ -390,8 +390,10 @@ pub fn editor_widget(app: &mut SheetApp, ui: &mut egui::Ui, id: egui::Id, font:
}
if resp.changed() {
ed.completion = None;
// Python code is never completed from the column's text values.
if app.ui.autocomplete
&& !ed.is_formula()
&& !ed.code_edit
&& !entry_shrank(chars_before, &ed.text)
&& ed.caret == ed.text.chars().count()
&& let Some(sh) = app.session.active().and_then(|d| d.wb.sheet(ed.sheet))
@@ -113,3 +113,16 @@ fn deleting_all_the_code_clears_a_python_cell_with_no_cached_value() {
assert!(h.state().editor.is_none());
assert_eq!(python(&h), None, "the code is removed, not kept as an unchanged empty cell");
}
#[test]
fn a_code_edit_is_never_completed_from_the_column() {
let mut h = harness();
// A2 holds text that starts with A1's code: typing at the end of the code would offer it.
h.state_mut().session.execute("cell.set", json!({"cell": "A2", "input": "1 + 1 and more"})).unwrap();
select_a1(&mut h);
key(&mut h, Key::F2);
text(&mut h, " ");
assert_eq!(h.state().editor.as_ref().and_then(|e| e.completion.clone()), None, "no completion while code is edited");
key(&mut h, Key::Enter);
assert_eq!(python(&h), Some(("1 + 1 ".into(), true)), "the typed code is kept");
}