fix(type): count normalized insert text caret (#3074) (#3138)

This commit is contained in:
Sir Starch authored and GitHub committed 2026-10-11 16:31:51 -04:00
1 parent c234865b17
commit b66ebb84d2
2 files changed
+13 -2

No files matched your search

+1 -1
View File
@@ -57,7 +57,7 @@ fn color(v: &Value) -> Option<Color> {
}
/// Normalises line breaks to `\n`.
fn norm_text(s: &str) -> String {
pub(crate) fn norm_text(s: &str) -> String {
s.replace("\r\n", "\n").replace('\r', "\n")
}
+12 -1
View File
@@ -16,7 +16,7 @@ use photocraft_text::spell::Dictionary;
use serde_json::{Value, json};
use crate::commands::{CommandSpec, layer_param};
use crate::type_cmds::{refresh, replace_text};
use crate::type_cmds::{norm_text, refresh, replace_text};
use crate::{EngineError, Result, Session};
fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
@@ -196,6 +196,7 @@ fn insert_text(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "type.insertText";
let id = layer_param(s, p)?;
let new = p.get("text").and_then(Value::as_str).filter(|t| !t.is_empty()).ok_or_else(|| bad(cmd, "missing `text`"))?.to_string();
let new = norm_text(&new);
let doc = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
let t = text(&doc, id).ok_or_else(|| bad(cmd, format!("layer {} is not a type layer", id.0)))?;
let n = t.chars().count();
@@ -324,4 +325,14 @@ mod tests {
assert_eq!(text_of(&s, id), "€b!");
assert!(s.execute("type.insertText", json!({"text": ""})).is_err());
}
#[test]
fn insert_crlf_reports_caret_in_normalized_text() {
let (mut s, id) = session("");
let inserted = s.execute("type.insertText", json!({"layer": id, "text": "x\r\ny"})).unwrap();
assert_eq!(inserted["caret"], 3);
assert_eq!(s.execute("type.info", json!({"layer": id})).unwrap()["text"], "x\ny");
assert_eq!(s.execute("type.caret", json!({"layer": id, "index": inserted["caret"]})).unwrap()["index"], inserted["caret"]);
}
}