From 5b1cb0479b42072760f1d07a7f70bb749d8b174d Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:38:17 -0500 Subject: [PATCH 1/5] feat(desktop-gpui): correct transcript words in place --- apps/desktop-gpui/locales/de.toml | 3 +- apps/desktop-gpui/locales/en.toml | 3 +- apps/desktop-gpui/locales/es.toml | 3 +- apps/desktop-gpui/locales/fr.toml | 3 +- apps/desktop-gpui/locales/hi.toml | 3 +- apps/desktop-gpui/locales/ja.toml | 3 +- apps/desktop-gpui/locales/zh-CN.toml | 3 +- apps/desktop-gpui/src/transcript.rs | 40 +- apps/desktop-gpui/src/transcript/model.rs | 52 ++- apps/desktop-gpui/src/transcript/tests.rs | 428 ++++++++++++++++++ apps/desktop-gpui/src/transcript/view.rs | 295 +++++++++++- apps/desktop-gpui/src/transcript/word_edit.rs | 137 ++++++ 12 files changed, 949 insertions(+), 24 deletions(-) create mode 100644 apps/desktop-gpui/src/transcript/word_edit.rs diff --git a/apps/desktop-gpui/locales/de.toml b/apps/desktop-gpui/locales/de.toml index c96d36f22a2..96af9c11b37 100644 --- a/apps/desktop-gpui/locales/de.toml +++ b/apps/desktop-gpui/locales/de.toml @@ -2912,9 +2912,10 @@ cleanup = "Aufräumen" cleanup_tooltip = "Füllwörter entfernen und Pausen kürzen" delete_matches = "Alle löschen" delete_matches_tooltip = "Jeden Treffer aus dem Video schneiden" +edit_hint = "Eingabetaste zum Speichern, Esc zum Abbrechen, Tab für das nächste Wort" empty_description = "Transkribieren Sie diese Aufnahme auf diesem Computer und bearbeiten Sie dann das Video über seinen Text." empty_title = "Noch kein Transkript" -hint = "Klicken Sie auf ein Wort, um dorthin zu springen. Wählen Sie Wörter aus und drücken Sie die Löschtaste, um sie aus dem Video zu schneiden." +hint = "Klicken Sie auf ein Wort, um dorthin zu springen, oder doppelklicken Sie, um es zu korrigieren. Wählen Sie Wörter aus und drücken Sie die Löschtaste, um sie aus dem Video zu schneiden." next_match = "Nächster Treffer" no_audio = "Diese Aufnahme hat keinen Ton zum Transkribieren." no_speech = "Keine Sprache in dieser Aufnahme gefunden" diff --git a/apps/desktop-gpui/locales/en.toml b/apps/desktop-gpui/locales/en.toml index 29f268aa5c6..2d342342f29 100644 --- a/apps/desktop-gpui/locales/en.toml +++ b/apps/desktop-gpui/locales/en.toml @@ -2914,9 +2914,10 @@ cleanup = "Clean up" cleanup_tooltip = "Remove filler words and shorten pauses" delete_matches = "Delete all" delete_matches_tooltip = "Cut every match from the video" +edit_hint = "Enter to save, Esc to cancel, Tab for the next word" empty_description = "Transcribe this recording on this computer, then edit the video by editing its text." empty_title = "No transcript yet" -hint = "Click a word to jump to it. Select words and press Delete to cut them from the video." +hint = "Click a word to jump to it, double-click to fix it. Select words and press Delete to cut them from the video." next_match = "Next match" no_audio = "This recording has no audio to transcribe." no_speech = "No speech found in this recording" diff --git a/apps/desktop-gpui/locales/es.toml b/apps/desktop-gpui/locales/es.toml index 27fbf306d17..3f9b7919b37 100644 --- a/apps/desktop-gpui/locales/es.toml +++ b/apps/desktop-gpui/locales/es.toml @@ -2912,9 +2912,10 @@ cleanup = "Limpiar" cleanup_tooltip = "Quitar muletillas y acortar pausas" delete_matches = "Eliminar todo" delete_matches_tooltip = "Cortar del vídeo cada coincidencia" +edit_hint = "Intro para guardar, Esc para cancelar, Tab para la siguiente palabra" empty_description = "Transcribe esta grabación en este ordenador y luego edita el vídeo editando su texto." empty_title = "Aún no hay transcripción" -hint = "Haz clic en una palabra para ir a ella. Selecciona palabras y pulsa la tecla de borrar para cortarlas del vídeo." +hint = "Haz clic en una palabra para ir a ella o doble clic para corregirla. Selecciona palabras y pulsa la tecla de borrar para cortarlas del vídeo." next_match = "Siguiente coincidencia" no_audio = "Esta grabación no tiene audio que transcribir." no_speech = "No se encontró voz en esta grabación" diff --git a/apps/desktop-gpui/locales/fr.toml b/apps/desktop-gpui/locales/fr.toml index 87b46c59460..0b645331b85 100644 --- a/apps/desktop-gpui/locales/fr.toml +++ b/apps/desktop-gpui/locales/fr.toml @@ -2912,9 +2912,10 @@ cleanup = "Nettoyer" cleanup_tooltip = "Supprimer les mots de remplissage et raccourcir les pauses" delete_matches = "Tout supprimer" delete_matches_tooltip = "Couper chaque résultat de la vidéo" +edit_hint = "Entrée pour enregistrer, Échap pour annuler, Tab pour le mot suivant" empty_description = "Transcrivez cet enregistrement sur cet ordinateur, puis modifiez la vidéo en modifiant son texte." empty_title = "Pas encore de transcription" -hint = "Cliquez sur un mot pour y aller. Sélectionnez des mots et appuyez sur la touche Supprimer pour les couper de la vidéo." +hint = "Cliquez sur un mot pour y aller, double-cliquez pour le corriger. Sélectionnez des mots et appuyez sur la touche Supprimer pour les couper de la vidéo." next_match = "Résultat suivant" no_audio = "Cet enregistrement n'a pas d'audio à transcrire." no_speech = "Aucune parole trouvée dans cet enregistrement" diff --git a/apps/desktop-gpui/locales/hi.toml b/apps/desktop-gpui/locales/hi.toml index 319ed6675bb..d3b4a8a963c 100644 --- a/apps/desktop-gpui/locales/hi.toml +++ b/apps/desktop-gpui/locales/hi.toml @@ -2912,9 +2912,10 @@ cleanup = "साफ़ करें" cleanup_tooltip = "फ़िलर शब्द हटाएँ और ठहराव छोटे करें" delete_matches = "सभी हटाएँ" delete_matches_tooltip = "हर मिलान को वीडियो से काटें" +edit_hint = "सेव करने के लिए Enter, रद्द करने के लिए Esc, अगले शब्द के लिए Tab" empty_description = "इस कंप्यूटर पर इस रिकॉर्डिंग को ट्रांसक्राइब करें, फिर उसका टेक्स्ट बदलकर वीडियो एडिट करें।" empty_title = "अभी कोई ट्रांसक्रिप्ट नहीं" -hint = "किसी शब्द पर जाने के लिए उस पर क्लिक करें। शब्द चुनें और डिलीट कुंजी दबाएँ, वे वीडियो से कट जाएँगे।" +hint = "किसी शब्द पर जाने के लिए उस पर क्लिक करें, उसे ठीक करने के लिए डबल-क्लिक करें। शब्द चुनें और डिलीट कुंजी दबाएँ, वे वीडियो से कट जाएँगे।" next_match = "अगला मिलान" no_audio = "इस रिकॉर्डिंग में ट्रांसक्राइब करने के लिए कोई ऑडियो नहीं है।" no_speech = "इस रिकॉर्डिंग में कोई बोली नहीं मिली" diff --git a/apps/desktop-gpui/locales/ja.toml b/apps/desktop-gpui/locales/ja.toml index d007debfd05..b53fac1acc2 100644 --- a/apps/desktop-gpui/locales/ja.toml +++ b/apps/desktop-gpui/locales/ja.toml @@ -2912,9 +2912,10 @@ cleanup = "クリーンアップ" cleanup_tooltip = "フィラーを削除し、間を短くします" delete_matches = "すべて削除" delete_matches_tooltip = "一致した箇所をすべて動画からカットします" +edit_hint = "Enterで保存、Escでキャンセル、Tabで次の単語へ" empty_description = "このコンピュータで録画を文字起こしすると、テキストを編集して動画を編集できます。" empty_title = "文字起こしはまだありません" -hint = "単語をクリックするとその位置に移動します。単語を選択して削除キーを押すと、動画からカットされます。" +hint = "単語をクリックするとその位置に移動し、ダブルクリックすると修正できます。単語を選択して削除キーを押すと、動画からカットされます。" next_match = "次の一致" no_audio = "この録画には文字起こしできる音声がありません。" no_speech = "この録画に発話は見つかりませんでした" diff --git a/apps/desktop-gpui/locales/zh-CN.toml b/apps/desktop-gpui/locales/zh-CN.toml index 922d4ee4a5d..7637e06e258 100644 --- a/apps/desktop-gpui/locales/zh-CN.toml +++ b/apps/desktop-gpui/locales/zh-CN.toml @@ -2912,9 +2912,10 @@ cleanup = "清理" cleanup_tooltip = "移除填充词并缩短停顿" delete_matches = "全部删除" delete_matches_tooltip = "从视频中剪掉所有匹配项" +edit_hint = "按 Enter 保存,按 Esc 取消,按 Tab 编辑下一个单词" empty_description = "在这台电脑上转写此录制内容,然后通过编辑文字来编辑视频。" empty_title = "还没有转写内容" -hint = "点击单词即可跳转。选中单词后按删除键,即可从视频中剪掉。" +hint = "点击单词即可跳转,双击即可修改。选中单词后按删除键,即可从视频中剪掉。" next_match = "下一个匹配项" no_audio = "此录制内容没有可转写的音频。" no_speech = "此录制内容中未发现语音" diff --git a/apps/desktop-gpui/src/transcript.rs b/apps/desktop-gpui/src/transcript.rs index 64c0bbd7ebc..1cf811e8e07 100644 --- a/apps/desktop-gpui/src/transcript.rs +++ b/apps/desktop-gpui/src/transcript.rs @@ -15,8 +15,9 @@ pub(crate) mod model; #[cfg(test)] mod tests; mod view; +pub(crate) mod word_edit; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::collections::{HashMap, HashSet}; use std::ops::Range; use std::rc::Rc; @@ -24,7 +25,7 @@ use std::sync::Arc; use std::time::Instant; use cap_project::disfluency::DisfluencyKind; -use gpui::{FocusHandle, ListAlignment, ListState, TextLayout, px}; +use gpui::{FocusHandle, ListAlignment, ListOffset, ListState, Pixels, Point, TextLayout, px}; use self::energy::EnergyEnvelope; use self::model::{TranscriptDoc, TranscriptEdl}; @@ -61,6 +62,23 @@ impl WordSelection { } } +/// The word being corrected in place, found again by its text and start if +/// the transcript is rebuilt while the field is open. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct WordEditor { + pub word: usize, + pub text: String, + pub start: f64, +} + +/// Where the edited word sits inside its paragraph's text, measured when the +/// paragraph paints so the field lands exactly over it. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct WordAnchor { + pub origin: Point, + pub width: Pixels, +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum DragUnit { Word, @@ -165,6 +183,12 @@ pub(crate) struct TranscriptState { pub selection: Option, pub caret: Option, pub drag: Option, + pub editing: Option, + pub edit_input: Option>, + pub edit_anchor: Rc>>, + /// The press that started this click restored a cut, so its double click + /// must not also open the word for editing. + pub restored_on_press: bool, pub active: Option, pub hovered_cut: Option, pub user_scrolled: Option, @@ -190,6 +214,10 @@ impl TranscriptState { selection: None, caret: None, drag: None, + editing: None, + edit_input: None, + edit_anchor: Rc::new(Cell::new(None)), + restored_on_press: false, active: None, hovered_cut: None, user_scrolled: None, @@ -216,6 +244,7 @@ impl TranscriptState { let Some(captions) = project.captions.as_ref().filter(|_| has_words(project)) else { let changed = self.doc.is_some(); self.doc = None; + self.editing = None; self.edl = None; self.list.reset(0); self.selection = None; @@ -231,7 +260,14 @@ impl TranscriptState { .is_none_or(|doc| doc.signature != signature) { let doc = TranscriptDoc::build(captions); + let scroll = self.doc.is_some().then(|| self.list.logical_scroll_top()); self.list.reset(doc.paragraphs.len()); + if let Some(scroll) = scroll.filter(|_| !doc.paragraphs.is_empty()) { + self.list.scroll_to(ListOffset { + item_ix: scroll.item_ix.min(doc.paragraphs.len() - 1), + offset_in_item: scroll.offset_in_item, + }); + } self.layouts.borrow_mut().clear(); self.selection = None; self.caret = None; diff --git a/apps/desktop-gpui/src/transcript/model.rs b/apps/desktop-gpui/src/transcript/model.rs index eb15f4e4c9f..6285009bd16 100644 --- a/apps/desktop-gpui/src/transcript/model.rs +++ b/apps/desktop-gpui/src/transcript/model.rs @@ -112,23 +112,46 @@ pub fn captions_signature(captions: Option<&CaptionsData>) -> u64 { } pub fn flatten_words(captions: &CaptionsData) -> Vec { + flatten(captions, None) +} + +/// Where each transcript word came from: the `(segment, word)` indices of the +/// caption words folded into it, its spoken word first. +pub fn word_sources(captions: &CaptionsData) -> (Vec, Vec>) { + let mut sources = Vec::new(); + let words = flatten(captions, Some(&mut sources)); + (words, sources) +} + +fn flatten( + captions: &CaptionsData, + mut sources: Option<&mut Vec>>, +) -> Vec { let mut words: Vec = Vec::new(); - let mut raw: Vec<(&str, f64, f64)> = captions + let mut raw: Vec<(&str, f64, f64, (usize, usize))> = captions .segments .iter() - .flat_map(|segment| segment.words.iter()) - .filter_map(|word| { + .enumerate() + .flat_map(|(segment_index, segment)| { + segment + .words + .iter() + .enumerate() + .map(move |(word_index, word)| (word, (segment_index, word_index))) + }) + .filter_map(|(word, source)| { let text = word.text.trim(); let (start, end) = (f64::from(word.start), f64::from(word.end)); (!text.is_empty() && start.is_finite() && end.is_finite()).then_some(( text, start, end.max(start), + source, )) }) .collect(); raw.sort_by(|a, b| a.1.total_cmp(&b.1)); - for (text, start, end) in raw { + for (text, start, end, source) in raw { let spoken = !cap_project::disfluency::normalize_word(text).is_empty(); match words.last_mut() { Some(previous) if !spoken || attaches_to_previous(text) => { @@ -136,13 +159,24 @@ pub fn flatten_words(captions: &CaptionsData) -> Vec { if spoken { previous.end = previous.end.max(end); } + if let Some(last) = sources + .as_mut() + .and_then(|sources| sources.last_mut()) + { + last.push(source); + } } _ if !spoken => {} - _ => words.push(Word { - text: text.to_string(), - start, - end, - }), + _ => { + words.push(Word { + text: text.to_string(), + start, + end, + }); + if let Some(sources) = sources.as_mut() { + sources.push(vec![source]); + } + } } } words diff --git a/apps/desktop-gpui/src/transcript/tests.rs b/apps/desktop-gpui/src/transcript/tests.rs index 4ee9fd63629..b6029e40195 100644 --- a/apps/desktop-gpui/src/transcript/tests.rs +++ b/apps/desktop-gpui/src/transcript/tests.rs @@ -2164,3 +2164,431 @@ fn a_cut_stays_put_when_the_music_split_by_it_was_given_a_fade_since() { assert_eq!(music[1].fade_in, 0.3); assert!(close(music[0].end, music[1].start)); } + +fn edit_word(project: &mut ProjectConfiguration, word: usize, text: &str) -> bool { + super::word_edit::replace_word(project.captions.as_mut().unwrap(), word, text) +} + +fn doc_texts(project: &ProjectConfiguration) -> Vec { + flatten_words(project.captions.as_ref().unwrap()) + .into_iter() + .map(|word| word.text) + .collect() +} + +fn caption_texts(project: &ProjectConfiguration) -> Vec { + project + .captions + .as_ref() + .unwrap() + .segments + .iter() + .map(|segment| segment.text.clone()) + .collect() +} + +fn rendered_words(project: &mut ProjectConfiguration, durations: &[f64]) -> Vec { + cap_project::synchronize_captions(project, durations); + timeline(project) + .caption_segments + .iter() + .flat_map(|segment| &segment.words) + .map(|word| word.text.clone()) + .collect() +} + +#[test] +fn correcting_a_word_keeps_its_timing_and_reaches_the_rendered_captions() { + let durations = [5.0]; + let words = spoken(&["One", "two.", "Three", "four", "five"]); + let mut project = project(&borrowed(&words), &durations); + let before = flatten_words(project.captions.as_ref().unwrap()); + + assert!(edit_word(&mut project, 2, "Tree")); + + let after = flatten_words(project.captions.as_ref().unwrap()); + assert_eq!(doc_texts(&project), ["One", "two.", "Tree", "four", "five"]); + assert_eq!(after.len(), before.len()); + for (before, after) in before.iter().zip(&after) { + assert!(close(before.start, after.start) && close(before.end, after.end)); + } + assert_eq!(caption_texts(&project), ["One two. Tree four five"]); + assert_eq!( + project.captions.as_ref().unwrap().segments[0].id, + "segment-0" + ); + assert_eq!( + rendered_words(&mut project, &durations), + ["One", "two.", "Tree", "four", "five"] + ); + assert!(!edit_word(&mut project, 2, "Tree")); + assert!(!edit_word(&mut project, 2, " Tree ")); + assert!(!edit_word(&mut project, 9, "nine")); +} + +#[test] +fn a_word_with_attached_punctuation_is_replaced_whole_and_keeps_its_span() { + let durations = [5.0]; + let mut project = project( + &[ + ("hello", 1.0, 1.3), + (",", 1.3, 1.3), + ("world", 1.5, 1.8), + (".", 1.8, 1.8), + ("it", 2.0, 2.1), + ("'s", 2.1, 2.3), + ], + &durations, + ); + assert_eq!(doc_texts(&project), ["hello,", "world.", "it's"]); + + assert!(edit_word(&mut project, 0, "Hi!")); + assert_eq!(doc_texts(&project), ["Hi!", "world.", "it's"]); + let words = flatten_words(project.captions.as_ref().unwrap()); + assert!(close(words[0].start, 1.0) && close(words[0].end, f64::from(1.3f32))); + + assert!(edit_word(&mut project, 0, "Hi ,")); + assert_eq!(doc_texts(&project), ["Hi,", "world.", "it's"]); + assert_eq!(caption_texts(&project), ["Hi, world. it's"]); + let words = flatten_words(project.captions.as_ref().unwrap()); + assert!(close(words[0].end, f64::from(1.3f32))); + + assert!(edit_word(&mut project, 2, "it is")); + let caption_words = &project.captions.as_ref().unwrap().segments[0].words; + let split: Vec<(&str, f32, f32)> = caption_words[caption_words.len() - 2..] + .iter() + .map(|word| (word.text.as_str(), word.start, word.end)) + .collect(); + assert_eq!(split, [("it", 2.0, 2.1), ("is", 2.1, 2.3)]); +} + +#[test] +fn splitting_a_word_shares_its_span_and_cut_words_stay_cut() { + let durations = [5.0]; + let words = spoken(&["One", "two.", "Three", "four", "five"]); + let mut project = project(&borrowed(&words), &durations); + cut_words(&mut project, &durations, &[(4, 5)]); + let cut = bounds(&project); + assert_eq!(kept(&project, &durations), [true, true, true, true, false]); + + assert!(edit_word(&mut project, 4, "fi ve")); + assert_eq!( + doc_texts(&project), + ["One", "two.", "Three", "four", "fi", "ve"] + ); + assert_eq!( + kept(&project, &durations), + [true, true, true, true, false, false] + ); + assert_eq!(bounds(&project), cut); + let words = flatten_words(project.captions.as_ref().unwrap()); + assert!(close(words[4].start, 3.0) && close(words[5].end, f64::from(3.3f32))); + assert!(close(words[4].end, words[5].start)); + + assert!(edit_word(&mut project, 2, "Tree")); + assert_eq!( + kept(&project, &durations), + [true, true, true, true, false, false] + ); + assert_eq!(bounds(&project), cut); + assert_eq!( + rendered_words(&mut project, &durations), + ["One", "two.", "Tree", "four"] + ); +} + +#[test] +fn a_corrected_cut_word_comes_back_with_its_new_text_when_restored() { + let durations = [5.0]; + let words = spoken(&["One", "two.", "Three", "four", "five"]); + let mut project = project(&borrowed(&words), &durations); + let uncut = bounds(&project); + cut_words(&mut project, &durations, &[(2, 3)]); + assert!(edit_word(&mut project, 2, "Tree")); + assert_eq!(kept(&project, &durations), [true, true, false, true, true]); + assert_eq!( + rendered_words(&mut project, &durations), + ["One", "two.", "four", "five"] + ); + + let doc = TranscriptDoc::build(project.captions.as_ref().unwrap()); + assert_eq!( + restore_words(timeline_mut(&mut project), &durations, &[], &doc, 2..3), + Ok(1) + ); + assert_eq!(bounds(&project), uncut); + assert_eq!( + rendered_words(&mut project, &durations), + ["One", "two.", "Tree", "four", "five"] + ); +} + +#[test] +fn clearing_a_word_drops_it_from_the_captions_but_not_the_video() { + let durations = [10.0]; + let words = spoken(&["a", "b", "c", "d", "e", "f", "g"]); + let mut project = project(&borrowed(&words), &durations); + let uncut = bounds(&project); + assert_eq!(project.captions.as_ref().unwrap().segments.len(), 2); + + assert!(edit_word(&mut project, 6, " ")); + assert_eq!(doc_texts(&project), ["a", "b", "c", "d", "e", "f"]); + assert_eq!(project.captions.as_ref().unwrap().segments.len(), 1); + assert!(edit_word(&mut project, 0, "")); + assert_eq!(caption_texts(&project), ["b c d e f"]); + assert_eq!(bounds(&project), uncut); +} + +#[test] +fn punctuation_in_the_next_caption_is_replaced_with_its_word() { + let mut data = captions(&[("hello", 1.0, 1.3)]); + let mut tail = captions(&[(",", 1.3, 1.3), ("world", 1.5, 1.8)]).segments; + tail[0].id = "segment-1".to_string(); + data.segments.extend(tail); + let mut project = project(&[], &[5.0]); + project.captions = Some(data); + assert_eq!(doc_texts(&project), ["hello,", "world"]); + + assert!(edit_word(&mut project, 0, "Hey")); + assert_eq!(doc_texts(&project), ["Hey", "world"]); + assert_eq!(caption_texts(&project), ["Hey", "world"]); + + assert!(edit_word(&mut project, 0, "")); + assert_eq!(caption_texts(&project), ["world"]); + assert_eq!( + project.captions.as_ref().unwrap().segments[0].id, + "segment-1" + ); +} + +#[test] +fn right_to_left_cjk_and_emoji_corrections_keep_their_text() { + let durations = [5.0]; + let words = spoken(&["One", "two", "Three", "four", "five"]); + let mut project = project(&borrowed(&words), &durations); + + assert!(edit_word(&mut project, 0, "שלום")); + assert!(edit_word(&mut project, 1, "世界、")); + assert!(edit_word(&mut project, 2, "great 👍")); + assert_eq!( + doc_texts(&project), + ["שלום", "世界、", "great👍", "four", "five"] + ); + assert_eq!(caption_texts(&project), ["שלום 世界、 great 👍 four five"]); + + assert!(edit_word(&mut project, 3, "🎉")); + assert_eq!(doc_texts(&project), ["שלום", "世界、", "great👍🎉", "five"]); + assert_eq!(caption_texts(&project), ["שלום 世界、 great 👍 🎉 five"]); + assert_eq!( + rendered_words(&mut project, &durations), + ["שלום", "世界、", "great", "👍", "🎉", "five"] + ); +} + +#[test] +fn an_edited_word_is_found_again_after_its_transcript_moves() { + let words = flatten_words(&captions(&FIRST_PASS)); + let refined = flatten_words(&captions(&REFINED)); + assert_eq!(super::word_edit::locate(&words, 4, "works", 1.6), Some(4)); + assert_eq!(super::word_edit::locate(&refined, 4, "works", 1.6), Some(4)); + assert_eq!(super::word_edit::locate(&refined, 2, "works", 1.6), Some(4)); + assert_eq!(super::word_edit::locate(&refined, 4, "works", 9.0), None); + assert_eq!(super::word_edit::locate(&refined, 40, "gone", 1.6), None); +} + +#[test] +fn a_correction_survives_a_refinement_and_the_word_cut_still_follows() { + let durations = [5.16]; + let uncut = project(&FIRST_PASS, &durations); + let mut project = uncut.clone(); + cut_words(&mut project, &durations, &[(3, 4)]); + assert!(edit_word(&mut project, 4, "worked")); + cap_project::synchronize_captions(&mut project, &durations); + let input = uncut.captions.as_ref().unwrap().segments.clone(); + let refined = crate::transcription::RefinedCaptions::new(&input, captions(&REFINED).segments); + let mut landing = super::RefinementLanding::new(&refined, &durations, &[], None); + + assert_eq!(landing.land(&mut project), Some(1)); + + assert!(!plays(&project, &SPOKEN_UH), "{:?}", bounds(&project)); + assert_eq!( + rendered_words(&mut project, &durations), + ["feels", "the", "same,", "worked", "very", "similar,"] + ); +} + +#[test] +fn correcting_a_word_in_a_two_hour_transcript_is_fast() { + let mut words = Vec::with_capacity(20_000); + let mut time = 0.5; + for index in 0..20_000 { + words.push((format!("w{index},"), time, time + 0.25)); + time += 0.36; + } + let durations = [time + 1.0]; + let mut project = project(&borrowed(&words), &durations); + + let started = std::time::Instant::now(); + assert!(edit_word(&mut project, 19_990, "fixed")); + let elapsed = started.elapsed(); + + let limit = std::time::Duration::from_millis(if cfg!(debug_assertions) { 600 } else { 40 }); + println!("20k words: one correction {elapsed:?}"); + assert!(elapsed < limit, "{elapsed:?}"); + let texts = doc_texts(&project); + assert_eq!(texts.len(), 20_000); + assert_eq!(texts[19_990], "fixed"); + assert_eq!(texts[19_991], "w19991,"); +} + +mod word_edit_ui { + use gpui::{AppContext, Context, Entity, IntoElement, Render, Window, div}; + + use crate::editor_window::EditorWindow; + use crate::transcript::WordSelection; + + struct EditView { + editor: Entity, + _project: tempfile::TempDir, + } + + impl Render for EditView { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + div() + } + } + + fn fixture( + cx: &mut gpui::TestAppContext, + ) -> (Entity, &mut gpui::VisualTestContext) { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().to_path_buf(); + let (view, cx) = cx.add_window_view(move |window, cx| { + let editor = cx.new(|cx| EditorWindow::new(path, window, cx)); + editor.update(cx, |editor, cx| { + let words = super::spoken(&["One", "two.", "Three", "four", "five"]); + editor.project = super::project(&super::borrowed(&words), &[5.0]); + editor.clip_display_durations = vec![5.0]; + editor.history.record(&editor.project); + editor.transcript.open = true; + editor.transcript.dirty = true; + editor.transcript_refresh(); + window.focus(&editor.transcript.focus, cx); + }); + EditView { + editor, + _project: directory, + } + }); + (view.read_with(cx, |view, _| view.editor.clone()), cx) + } + + fn texts(editor: &EditorWindow) -> Vec { + super::doc_texts(&editor.project) + } + + fn type_text(editor: &EditorWindow, text: &str, cx: &mut Context) { + let input = editor.transcript.edit_input.clone().unwrap(); + input.update(cx, |input, cx| input.set_text(text.to_string(), cx)); + } + + #[gpui::test] + fn tab_saves_the_correction_and_opens_the_next_word(cx: &mut gpui::TestAppContext) { + let (editor, cx) = fixture(cx); + editor.update_in(cx, |editor, window, cx| { + editor.transcript_begin_word_edit(2, window, cx); + let input = editor.transcript.edit_input.clone().unwrap(); + assert_eq!(input.read(cx).text(), "Three"); + assert!(input.read(cx).focus_handle().is_focused(window)); + assert_eq!(editor.transcript.selection, Some(WordSelection::single(2))); + + type_text(editor, "Tree", cx); + editor.transcript_step_word_edit(true, window, cx); + assert_eq!(texts(editor), ["One", "two.", "Tree", "four", "five"]); + assert_eq!(editor.transcript.editing.as_ref().map(|e| e.word), Some(3)); + assert_eq!(input.read(cx).text(), "four"); + assert!(input.read(cx).focus_handle().is_focused(window)); + + type_text(editor, "for", cx); + editor.transcript_step_word_edit(false, window, cx); + assert_eq!(texts(editor), ["One", "two.", "Tree", "for", "five"]); + assert_eq!(editor.transcript.editing.as_ref().map(|e| e.word), Some(2)); + + editor.transcript_commit_word_edit(window, cx); + assert!(editor.transcript.editing.is_none()); + assert_eq!(texts(editor), ["One", "two.", "Tree", "for", "five"]); + }); + } + + #[gpui::test] + fn tab_skips_cut_words_and_stops_after_the_last(cx: &mut gpui::TestAppContext) { + let (editor, cx) = fixture(cx); + editor.update_in(cx, |editor, window, cx| { + super::cut_words(&mut editor.project, &[5.0], &[(3, 4)]); + editor.transcript.dirty = true; + editor.transcript_refresh(); + editor.transcript_begin_word_edit(2, window, cx); + editor.transcript_step_word_edit(true, window, cx); + assert_eq!(editor.transcript.editing.as_ref().map(|e| e.word), Some(4)); + editor.transcript_step_word_edit(true, window, cx); + assert!(editor.transcript.editing.is_none()); + assert!(editor.transcript.focus.is_focused(window)); + }); + } + + #[gpui::test] + fn escape_discards_and_a_split_word_lands_on_the_next_word(cx: &mut gpui::TestAppContext) { + let (editor, cx) = fixture(cx); + editor.update_in(cx, |editor, window, cx| { + editor.transcript_begin_word_edit(0, window, cx); + type_text(editor, "Uno", cx); + }); + let input = editor.read_with(cx, |editor, _| { + editor.transcript.edit_input.clone().unwrap() + }); + input.update_in(cx, |_, _, cx| cx.emit(crate::ui::TextInputEvent::Cancelled)); + editor.update_in(cx, |editor, window, cx| { + assert!(editor.transcript.editing.is_none()); + assert_eq!(texts(editor), ["One", "two.", "Three", "four", "five"]); + + editor.transcript_begin_word_edit(0, window, cx); + type_text(editor, "On e", cx); + editor.transcript_step_word_edit(true, window, cx); + assert_eq!(texts(editor), ["On", "e", "two.", "Three", "four", "five"]); + assert_eq!(editor.transcript.editing.as_ref().map(|e| e.word), Some(2)); + assert_eq!( + editor + .transcript + .edit_input + .as_ref() + .unwrap() + .read(cx) + .text(), + "two." + ); + }); + } + + #[gpui::test] + fn a_correction_is_one_undo_step(cx: &mut gpui::TestAppContext) { + let (editor, cx) = fixture(cx); + editor.update_in(cx, |editor, window, cx| { + editor.transcript_begin_word_edit(2, window, cx); + type_text(editor, "Tree", cx); + editor.transcript_commit_word_edit(window, cx); + // `project_changed` records nothing until a recording loads. + editor.history.record(&editor.project); + assert_eq!(texts(editor), ["One", "two.", "Tree", "four", "five"]); + + editor.undo(window, cx); + assert_eq!(texts(editor), ["One", "two.", "Three", "four", "five"]); + editor.transcript_refresh(); + assert_eq!( + editor.transcript.doc.as_ref().unwrap().words[2].text, + "Three" + ); + editor.redo(window, cx); + assert_eq!(texts(editor), ["One", "two.", "Tree", "four", "five"]); + }); + } +} diff --git a/apps/desktop-gpui/src/transcript/view.rs b/apps/desktop-gpui/src/transcript/view.rs index 0d2009a2317..31a53932fef 100644 --- a/apps/desktop-gpui/src/transcript/view.rs +++ b/apps/desktop-gpui/src/transcript/view.rs @@ -1,4 +1,4 @@ -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::collections::{HashMap, HashSet}; use std::ops::Range; use std::rc::Rc; @@ -16,9 +16,10 @@ use gpui::{ use super::cuts::{self, CutReport, RestoreBlocker}; use super::model::{TranscriptDoc, TranscriptEdl}; +use super::word_edit; use super::{ CleanupTab, DragUnit, FOLLOW_PAUSE, PAUSE_KEEP_RANGE, PAUSE_LONGER_RANGE, SearchState, - WordDrag, WordSelection, category_index, + WordAnchor, WordDrag, WordEditor, WordSelection, category_index, }; use crate::editor_window::EditorWindow; use crate::i18n::tr; @@ -174,8 +175,24 @@ struct ParagraphPainter { current_match: Option>, ignored: Arc>, palette: Palette, + theme: Theme, editor: WeakEntity, layouts: Rc>>, + editing: Option<(usize, gpui::Entity)>, + edit_anchor: Rc>>, +} + +fn word_anchor(layout: &TextLayout, span: Range) -> Option { + let bounds = layout.bounds(); + let start = layout.position_for_index(span.start)?; + let width = layout + .position_for_index(span.end) + .filter(|end| end.y == start.y && end.x > start.x) + .map_or(px(EDIT_MIN_WIDTH), |end| end.x - start.x); + Some(WordAnchor { + origin: start - bounds.origin, + width, + }) } impl ParagraphPainter { @@ -286,6 +303,17 @@ impl ParagraphPainter { let layouts = self.layouts.clone(); let paint_layout = layout.clone(); let playing = self.active_paragraph == Some(index); + let editing = self + .editing + .as_ref() + .filter(|(word, _)| paragraph.words.contains(word)) + .and_then(|(word, input)| Some((paragraph.span(*word)?, input.clone()))); + let measure_span = editing.as_ref().map(|(span, _)| span.clone()); + let edit_anchor = self.edit_anchor.clone(); + let overlay = editing.and_then(|(_, input)| { + let anchor = self.edit_anchor.get()?; + Some(self.word_field(anchor, input)) + }); div() .id(("transcript-paragraph", index)) .w_full() @@ -327,7 +355,14 @@ impl ParagraphPainter { .child(text) .child( gpui::canvas( - move |_, _, _| { + move |_, window, _| { + if let Some(span) = measure_span { + let anchor = word_anchor(&paint_layout, span); + if anchor != edit_anchor.get() { + edit_anchor.set(anchor); + window.refresh(); + } + } layouts.borrow_mut().insert(index, paint_layout); }, |_, _, _, _| {}, @@ -335,6 +370,7 @@ impl ParagraphPainter { .absolute() .size_0(), ) + .children(overlay) .on_mouse_down(MouseButton::Left, move |event, window, cx| { let byte = byte_at(&down_layout, event.position); editor @@ -356,6 +392,67 @@ impl ParagraphPainter { } } +impl ParagraphPainter { + /// The correction field, laid over the word it edits: the same type size + /// and baseline as the paragraph, on the panel's own surface so the + /// original spelling does not show through. + fn word_field( + &self, + anchor: WordAnchor, + input: gpui::Entity, + ) -> AnyElement { + let theme = &self.theme; + let palette = self.palette; + let editor = self.editor.clone(); + div() + .id("transcript-word-edit") + .absolute() + .left(anchor.origin.x - px(EDIT_PAD_X + 1.)) + .top(anchor.origin.y) + .h(px(LINE_HEIGHT)) + .min_w(anchor.width + px(2. * (EDIT_PAD_X + 1.))) + .px(px(EDIT_PAD_X)) + .flex() + .flex_row() + .items_center() + .rounded(px(5.)) + .bg(Hsla::from(theme.editor.card)) + .border_1() + .border_color(palette.accent) + .shadow(theme.editor.pop_shadow()) + .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) + .on_key_down(move |event: &KeyDownEvent, window, cx| { + let Some(direction) = ui::keyboard_nav::direction(&event.keystroke) else { + return; + }; + if ui::text_input::text_input_is_composing(window, cx) { + return; + } + cx.stop_propagation(); + window.prevent_default(); + let forward = direction == ui::keyboard_nav::Direction::Next; + editor + .update(cx, |this, cx| { + this.transcript_step_word_edit(forward, window, cx); + }) + .ok(); + }) + .child( + ui::TextInput::bare(theme, "transcript-word-input", &input) + .fit_content() + .padding_x(px(0.)) + .text_size(px(TEXT_SIZE)) + .line_height(px(LINE_HEIGHT)) + .text_color(palette.text_1) + .selection_color(palette.selection), + ) + .into_any_element() + } +} + +const EDIT_PAD_X: f32 = 3.; +const EDIT_MIN_WIDTH: f32 = 24.; + const ROW_HEIGHT: f32 = 30.; const MAX_VISIBLE_ROWS: usize = 8; @@ -725,21 +822,37 @@ impl EditorWindow { let Some(word) = doc.paragraphs.get(paragraph).map(|p| p.word_at_byte(byte)) else { return; }; - window.focus(&self.transcript.focus, cx); cx.stop_propagation(); + if self.transcript.editing.is_some() { + self.transcript_commit_word_edit(window, cx); + window.focus(&self.transcript.focus, cx); + return; + } + window.focus(&self.transcript.focus, cx); self.set_selection(None, cx); + if event.click_count == 1 { + self.transcript.restored_on_press = false; + } if !edl.is_kept(word) && !event.modifiers.shift && event.click_count == 1 { if let Some(run) = edl.cut_run_of(word) { let words = edl.cut_runs[run].clone(); self.transcript_restore(words, window, cx); + self.transcript.restored_on_press = true; + } + return; + } + + if event.click_count == 2 && !event.modifiers.shift { + if !std::mem::take(&mut self.transcript.restored_on_press) { + self.transcript_begin_word_edit(word, window, cx); } return; } let unit = match event.click_count { - 2 => DragUnit::Sentence, - count if count >= 3 => DragUnit::Paragraph, + 3 => DragUnit::Sentence, + count if count >= 4 => DragUnit::Paragraph, _ => DragUnit::Word, }; let origin = unit_bounds(&doc, word, unit); @@ -1208,6 +1321,165 @@ impl EditorWindow { self.invalidate_transcript(cx); } + fn transcript_word_input( + &mut self, + window: &mut Window, + cx: &mut Context, + ) -> gpui::Entity { + if let Some(input) = &self.transcript.edit_input { + return input.clone(); + } + let input = cx.new(|cx| ui::TextInputState::single_line(window, cx)); + let subscription = cx.subscribe_in( + &input, + window, + |this: &mut Self, _, event: &ui::TextInputEvent, window, cx| match event { + ui::TextInputEvent::Confirmed => { + this.transcript_commit_word_edit(window, cx); + window.focus(&this.transcript.focus, cx); + } + ui::TextInputEvent::Cancelled => { + if this.transcript.editing.take().is_some() { + this.invalidate_transcript(cx); + } + window.focus(&this.transcript.focus, cx); + } + ui::TextInputEvent::Blurred => { + this.transcript_commit_word_edit(window, cx); + } + ui::TextInputEvent::Changed => {} + }, + ); + self.push_text_subscription(subscription); + self.transcript.edit_input = Some(input.clone()); + input + } + + /// Opens `word` for correcting in place, its text selected. + pub(super) fn transcript_begin_word_edit( + &mut self, + word: usize, + window: &mut Window, + cx: &mut Context, + ) { + let Some(doc) = self.transcript.doc.clone() else { + return; + }; + let Some(current) = doc.words.get(word) else { + return; + }; + let input = self.transcript_word_input(window, cx); + let text = current.text.clone(); + self.transcript.editing = Some(WordEditor { + word, + text: text.clone(), + start: current.start, + }); + let paragraph = doc.paragraph_of(word); + let anchor = self + .transcript + .layouts + .borrow() + .get(¶graph) + .zip(doc.paragraphs[paragraph].span(word)) + .and_then(|(layout, span)| word_anchor(layout, span)); + self.transcript.edit_anchor.set(anchor); + input.update(cx, |input, cx| { + input.set_text(text, cx); + input.focus_and_select_all(window, cx); + }); + self.transcript.selection = Some(WordSelection::single(word)); + self.transcript.caret = None; + self.transcript.drag = None; + self.transcript_reveal(word, false); + self.invalidate_transcript(cx); + } + + /// Writes the open correction into the captions as one undo step and + /// closes the field. Returns the word it landed on, found again if the + /// transcript was rebuilt while the field was open. + pub(super) fn transcript_commit_word_edit( + &mut self, + window: &mut Window, + cx: &mut Context, + ) -> Option { + let editing = self.transcript.editing.take()?; + self.invalidate_transcript(cx); + let text = self + .transcript + .edit_input + .as_ref()? + .read(cx) + .text() + .to_string(); + self.transcript_refresh(); + let doc = self.transcript.doc.clone()?; + let word = word_edit::locate(&doc.words, editing.word, &editing.text, editing.start)?; + let mut changed = false; + self.edit_timeline_project( + |project| { + changed = project + .captions + .as_mut() + .is_some_and(|captions| word_edit::replace_word(captions, word, &text)); + changed + }, + window, + cx, + ); + if changed { + self.transcript.dirty = true; + self.transcript_refresh(); + tracing::info!( + word, + words = self + .transcript + .doc + .as_ref() + .map_or(0, |doc| doc.words.len()), + "transcript word edited" + ); + let count = self + .transcript + .doc + .as_ref() + .map_or(0, |doc| doc.words.len()); + self.transcript.selection = (word < count).then_some(WordSelection::single(word)); + self.transcript_sync_active(false, cx); + } + Some(word) + } + + /// Tab and Shift-Tab in the correction field: keep the edit and move on + /// to the next or previous word that plays. + pub(super) fn transcript_step_word_edit( + &mut self, + forward: bool, + window: &mut Window, + cx: &mut Context, + ) { + let (Some((doc, edl)), Some(editing)) = + (self.transcript_doc(), self.transcript.editing.clone()) + else { + return; + }; + let target = if forward { + (editing.word + 1..doc.words.len()).find(|word| edl.is_kept(*word)) + } else { + (0..editing.word.min(doc.words.len())).rfind(|word| edl.is_kept(*word)) + } + .map(|word| (word, doc.words[word].text.clone(), doc.words[word].start)); + self.transcript_commit_word_edit(window, cx); + let next = target.and_then(|(hint, text, start)| { + let doc = self.transcript.doc.clone()?; + word_edit::locate(&doc.words, hint, &text, start) + }); + match next { + Some(word) => self.transcript_begin_word_edit(word, window, cx), + None => window.focus(&self.transcript.focus, cx), + } + } + pub(super) fn transcript_open_search(&mut self, window: &mut Window, cx: &mut Context) { if let Some(search) = &self.transcript.search { let input = search.input.clone(); @@ -1482,8 +1754,16 @@ impl EditorWindow { .and_then(|search| search.matches.get(search.current).cloned()), ignored: Arc::new(self.transcript.cleanup.ignored.clone()), palette, + theme, editor: editor.clone(), layouts: self.transcript.layouts.clone(), + editing: self + .transcript + .editing + .as_ref() + .zip(self.transcript.edit_input.clone()) + .map(|(editing, input)| (editing.word, input)), + edit_anchor: self.transcript.edit_anchor.clone(), }; let scroll_editor = editor.clone(); self.transcript.list.set_scroll_handler(move |_, _, cx| { @@ -2062,6 +2342,9 @@ impl EditorWindow { let doc = self.transcript.doc.as_ref(); let edl = self.transcript.edl.as_ref(); let message: SharedString = match (doc, edl) { + (Some(doc), Some(_)) if !doc.is_empty() && self.transcript.editing.is_some() => { + tr::editor::transcript::edit_hint().into() + } (Some(doc), Some(edl)) if !doc.is_empty() => { if let Some(run) = self .transcript diff --git a/apps/desktop-gpui/src/transcript/word_edit.rs b/apps/desktop-gpui/src/transcript/word_edit.rs new file mode 100644 index 00000000000..228834608fc --- /dev/null +++ b/apps/desktop-gpui/src/transcript/word_edit.rs @@ -0,0 +1,137 @@ +use cap_project::caption_words::caption_text_from_words; +use cap_project::disfluency::normalize_word; +use cap_project::{CaptionWord, CaptionsData}; + +use super::model::{Word, word_sources}; + +/// How far a word may have moved, by a refinement landing while it was being +/// edited, and still be found again by its text. +const LOCATE_WINDOW: f64 = 1.0; + +fn is_spoken(text: &str) -> bool { + !normalize_word(text).is_empty() +} + +/// Rewrites transcript word `word` as `text` in the source-time captions. +/// +/// The word keeps the time it is spoken at, so it stays cut or kept exactly as +/// before and the captions, the export and later word cuts all see the new +/// spelling at the same moment. A replacement with as many spoken words as the +/// original keeps each one's own timing; any other count shares the word's +/// span evenly. Empty text drops the word from the captions only: the video is +/// never cut by an edit. `false` when nothing changed. +pub fn replace_word(captions: &mut CaptionsData, word: usize, text: &str) -> bool { + let (words, sources) = word_sources(captions); + let (Some(current), Some(members)) = (words.get(word), sources.get(word)) else { + return false; + }; + let tokens: Vec<&str> = text.split_whitespace().collect(); + if tokens.join(" ") == current.text { + return false; + } + let spoken: Vec<(f32, f32)> = members + .iter() + .map(|&(segment, index)| &captions.segments[segment].words[index]) + .filter(|word| is_spoken(&word.text)) + .map(|word| (word.start, word.end.max(word.start))) + .collect(); + let Some(&(start, _)) = spoken.first() else { + return false; + }; + let end = spoken.iter().fold(start, |end, span| end.max(span.1)); + let replacements = retime(&tokens, &spoken, start, end); + + let mut removing = members.clone(); + removing.sort_unstable(); + let (head_segment, head_index) = members[0]; + let insert_at = head_index + - removing + .iter() + .filter(|&&(segment, index)| segment == head_segment && index < head_index) + .count(); + for &(segment, index) in removing.iter().rev() { + captions.segments[segment].words.remove(index); + } + captions.segments[head_segment] + .words + .splice(insert_at..insert_at, replacements); + + let mut touched: Vec = removing.iter().map(|(segment, _)| *segment).collect(); + touched.dedup(); + for &segment in &touched { + let segment = &mut captions.segments[segment]; + segment.text = caption_text_from_words(&segment.words); + } + for &segment in touched.iter().rev() { + if captions.segments[segment].words.is_empty() { + captions.segments.remove(segment); + } + } + true +} + +/// Times each token. Punctuation and other unspoken tokens share the span of +/// the spoken token they sit beside, so the transcript folds them back into +/// it and its span is unchanged. +fn retime(tokens: &[&str], spoken: &[(f32, f32)], start: f32, end: f32) -> Vec { + let spoken_tokens = tokens.iter().filter(|token| is_spoken(token)).count(); + let spans: Vec<(f32, f32)> = if spoken_tokens == spoken.len() { + spoken.to_vec() + } else { + let parts = spoken_tokens.max(1); + let (from, until) = (f64::from(start), f64::from(end)); + let step = (until - from) / parts as f64; + (0..parts) + .map(|part| { + let part_end = if part + 1 == parts { + end + } else { + (from + step * (part + 1) as f64) as f32 + }; + ((from + step * part as f64) as f32, part_end) + }) + .collect() + }; + let mut unit = 0; + let mut seen_spoken = false; + tokens + .iter() + .map(|token| { + if is_spoken(token) { + if seen_spoken { + unit += 1; + } + seen_spoken = true; + } + let (start, end) = spans[unit.min(spans.len() - 1)]; + CaptionWord { + text: (*token).to_string(), + start, + end, + } + }) + .collect() +} + +/// Finds a word again after the transcript was rebuilt under it: at `hint` +/// when nothing moved, otherwise the nearest word with the same text. +pub fn locate(words: &[Word], hint: usize, text: &str, start: f64) -> Option { + if words + .get(hint) + .is_some_and(|word| word.text == text && (word.start - start).abs() < 1e-6) + { + return Some(hint); + } + let from = words.partition_point(|word| word.start < start - LOCATE_WINDOW); + words[from..] + .iter() + .take_while(|word| word.start <= start + LOCATE_WINDOW) + .enumerate() + .filter(|(_, word)| word.text == text) + .min_by(|a, b| { + (a.1.start - start) + .abs() + .total_cmp(&(b.1.start - start).abs()) + }) + .map(|(offset, _)| from + offset) +} From c084b240ab1ab48706cfd40dfb2e2a32c1a41611 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:46:59 -0500 Subject: [PATCH 2/5] fix(desktop-gpui): let corrected words follow refined timings --- .../src/i18n/editor_shell_surface_tests.rs | 6 ++- apps/desktop-gpui/src/transcription.rs | 27 +++++++++++++- apps/desktop-gpui/src/transcription/refine.rs | 37 ++++++++++++++++++- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs index 05e50b7b5a8..fbb2420e299 100644 --- a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs @@ -1120,9 +1120,13 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { "the the, I I", ), (tr::editor::transcript::fillers::title(), "Filler words"), + ( + tr::editor::transcript::edit_hint(), + "Enter to save, Esc to cancel, Tab for the next word", + ), ( tr::editor::transcript::hint(), - "Click a word to jump to it. Select words and press Delete to cut them from the video.", + "Click a word to jump to it, double-click to fix it. Select words and press Delete to cut them from the video.", ), (tr::editor::transcript::next_match(), "Next match"), ( diff --git a/apps/desktop-gpui/src/transcription.rs b/apps/desktop-gpui/src/transcription.rs index 7c88c907293..b650027b472 100644 --- a/apps/desktop-gpui/src/transcription.rs +++ b/apps/desktop-gpui/src/transcription.rs @@ -3531,10 +3531,35 @@ mod tests { .collect(); assert_eq!( words, - vec![("so", 1.05, 1.3), ("um", 1.5, 1.8), ("yeah", 2.0, 3.0)] + vec![("so", 1.05, 1.3), ("um", 1.5, 1.8), ("yeah", 2.1, 2.6)] ); } + #[test] + fn a_refinement_leaves_a_word_retimed_by_hand_where_it_was_put() { + let mut project = cut_project(); + let input = project.captions.as_ref().unwrap().segments.clone(); + let word = &mut project.captions.as_mut().unwrap().segments[0].words[2]; + (word.text, word.start, word.end) = ("yeah".to_string(), 2.2, 2.9); + let refined = RefinedCaptions::new( + &input, + vec![caption( + "segment-0", + 0.6, + 2.6, + &[("so", 1.05, 1.3), ("um", 1.5, 1.8), ("yes", 2.1, 2.6)], + )], + ); + + assert!(apply_refined_captions(&mut project, &refined, &[10.0])); + + let last = project.captions.as_ref().unwrap().segments[0] + .words + .last() + .map(|word| (word.text.clone(), word.start, word.end)); + assert_eq!(last, Some(("yeah".to_string(), 2.2, 2.9))); + } + #[test] fn fillers_count_as_english_only_when_the_language_allows_it() { let words = [ diff --git a/apps/desktop-gpui/src/transcription/refine.rs b/apps/desktop-gpui/src/transcription/refine.rs index 68aeab35746..ee38cc3dda2 100644 --- a/apps/desktop-gpui/src/transcription/refine.rs +++ b/apps/desktop-gpui/src/transcription/refine.rs @@ -135,16 +135,49 @@ pub struct RefinedCaptions { /// Each input word's refined times, for captions edited while the /// refinement ran. retimed: HashMap, + /// The same keyed by time alone, so a word whose spelling was corrected + /// while the refinement ran still moves. Times two input words share are + /// left out. + retimed_by_time: HashMap<(u32, u32), (f32, f32)>, } impl RefinedCaptions { pub fn new(input: &[CaptionSegment], segments: Vec) -> Self { + let retimed = match_retimes(input, &segments); Self { input_signature: words_signature(input), - retimed: match_retimes(input, &segments), + retimed_by_time: retimes_by_time(input, &retimed), + retimed, segments, } } + + fn retime_of(&self, word: &CaptionWord) -> Option<(f32, f32)> { + self.retimed + .get(&word_key(word)) + .or_else(|| { + self.retimed_by_time + .get(&(word.start.to_bits(), word.end.to_bits())) + }) + .copied() + } +} + +fn retimes_by_time( + input: &[CaptionSegment], + retimed: &HashMap, +) -> HashMap<(u32, u32), (f32, f32)> { + let mut uses: HashMap<(u32, u32), usize> = HashMap::new(); + for word in input.iter().flat_map(|segment| &segment.words) { + *uses + .entry((word.start.to_bits(), word.end.to_bits())) + .or_default() += 1; + } + retimed + .iter() + .map(|((_, start, end), times)| ((*start, *end), *times)) + .filter(|(time, _)| uses.get(time) == Some(&1)) + .collect() } /// Pairs each input word with the refined word that has its text, in order. @@ -194,7 +227,7 @@ pub fn apply_refined_captions( for segment in &mut captions.segments { let mut moved = false; for word in &mut segment.words { - if let Some(&(start, end)) = refined.retimed.get(&word_key(word)) + if let Some((start, end)) = refined.retime_of(word) && (word.start, word.end) != (start, end) { (word.start, word.end) = (start, end); From ba6768d4272b642d9f508cfff9d98e4795f34fb7 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:47:39 -0500 Subject: [PATCH 3/5] test(desktop-gpui): run the word edit window tests where test windows exist --- apps/desktop-gpui/src/transcript/tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/desktop-gpui/src/transcript/tests.rs b/apps/desktop-gpui/src/transcript/tests.rs index b6029e40195..db7595a4957 100644 --- a/apps/desktop-gpui/src/transcript/tests.rs +++ b/apps/desktop-gpui/src/transcript/tests.rs @@ -2441,6 +2441,7 @@ fn correcting_a_word_in_a_two_hour_transcript_is_fast() { assert_eq!(texts[19_991], "w19991,"); } +#[cfg(target_os = "linux")] mod word_edit_ui { use gpui::{AppContext, Context, Entity, IntoElement, Render, Window, div}; From 7c1dcc93ce68aa9a34f761b1a089d2a07bda9e00 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:40:37 -0500 Subject: [PATCH 4/5] fix(desktop-gpui): keep the word field on its word and its history to itself --- apps/desktop-gpui/src/transcript.rs | 11 +++- apps/desktop-gpui/src/transcript/model.rs | 5 +- apps/desktop-gpui/src/transcript/tests.rs | 54 ++++++++++++++++ apps/desktop-gpui/src/transcript/view.rs | 62 ++++++++++++++++--- apps/desktop-gpui/src/transcript/word_edit.rs | 4 -- apps/desktop-gpui/src/transcription.rs | 47 ++++++++++++++ apps/desktop-gpui/src/transcription/refine.rs | 55 +++++++++------- apps/desktop-gpui/src/ui/text_input.rs | 6 ++ 8 files changed, 202 insertions(+), 42 deletions(-) diff --git a/apps/desktop-gpui/src/transcript.rs b/apps/desktop-gpui/src/transcript.rs index 1cf811e8e07..6fbda2a0369 100644 --- a/apps/desktop-gpui/src/transcript.rs +++ b/apps/desktop-gpui/src/transcript.rs @@ -62,8 +62,6 @@ impl WordSelection { } } -/// The word being corrected in place, found again by its text and start if -/// the transcript is rebuilt while the field is open. #[derive(Clone, Debug, PartialEq)] pub(crate) struct WordEditor { pub word: usize, @@ -260,6 +258,15 @@ impl TranscriptState { .is_none_or(|doc| doc.signature != signature) { let doc = TranscriptDoc::build(captions); + if let Some(editing) = self.editing.as_mut() { + match word_edit::locate(&doc.words, editing.word, &editing.text, editing.start) { + Some(word) => { + editing.word = word; + editing.start = doc.words[word].start; + } + None => self.editing = None, + } + } let scroll = self.doc.is_some().then(|| self.list.logical_scroll_top()); self.list.reset(doc.paragraphs.len()); if let Some(scroll) = scroll.filter(|_| !doc.paragraphs.is_empty()) { diff --git a/apps/desktop-gpui/src/transcript/model.rs b/apps/desktop-gpui/src/transcript/model.rs index 6285009bd16..c9bf79a1a1d 100644 --- a/apps/desktop-gpui/src/transcript/model.rs +++ b/apps/desktop-gpui/src/transcript/model.rs @@ -159,10 +159,7 @@ fn flatten( if spoken { previous.end = previous.end.max(end); } - if let Some(last) = sources - .as_mut() - .and_then(|sources| sources.last_mut()) - { + if let Some(last) = sources.as_mut().and_then(|sources| sources.last_mut()) { last.push(source); } } diff --git a/apps/desktop-gpui/src/transcript/tests.rs b/apps/desktop-gpui/src/transcript/tests.rs index db7595a4957..0e9bb380fff 100644 --- a/apps/desktop-gpui/src/transcript/tests.rs +++ b/apps/desktop-gpui/src/transcript/tests.rs @@ -2570,6 +2570,60 @@ mod word_edit_ui { }); } + #[gpui::test] + fn the_open_field_follows_its_word_when_the_transcript_is_rebuilt( + cx: &mut gpui::TestAppContext, + ) { + let (editor, cx) = fixture(cx); + editor.update_in(cx, |editor, window, cx| { + editor.transcript_begin_word_edit(2, window, cx); + let segment = &mut editor.project.captions.as_mut().unwrap().segments[0]; + segment.words.insert( + 0, + cap_project::CaptionWord { + text: "Hey".into(), + start: 0.2, + end: 0.5, + }, + ); + editor.transcript.dirty = true; + editor.transcript_refresh(); + assert_eq!(editor.transcript.editing.as_ref().map(|e| e.word), Some(3)); + + editor.transcript_step_word_edit(true, window, cx); + assert_eq!(editor.transcript.editing.as_ref().map(|e| e.word), Some(4)); + assert_eq!( + editor + .transcript + .edit_input + .as_ref() + .unwrap() + .read(cx) + .text(), + "four" + ); + }); + } + + #[gpui::test] + fn a_triple_click_on_the_field_still_selects_the_sentence(cx: &mut gpui::TestAppContext) { + let (editor, cx) = fixture(cx); + editor.update_in(cx, |editor, window, cx| { + editor.transcript_begin_word_edit(3, window, cx); + assert!(editor.transcript_select_from_field(3, window, cx)); + assert!(editor.transcript.editing.is_none()); + assert_eq!( + editor.transcript.selection.map(WordSelection::range), + Some(2..5) + ); + + editor.transcript_begin_word_edit(3, window, cx); + type_text(editor, "for", cx); + assert!(!editor.transcript_select_from_field(3, window, cx)); + assert_eq!(editor.transcript.editing.as_ref().map(|e| e.word), Some(3)); + }); + } + #[gpui::test] fn a_correction_is_one_undo_step(cx: &mut gpui::TestAppContext) { let (editor, cx) = fixture(cx); diff --git a/apps/desktop-gpui/src/transcript/view.rs b/apps/desktop-gpui/src/transcript/view.rs index 31a53932fef..b680fa43fc3 100644 --- a/apps/desktop-gpui/src/transcript/view.rs +++ b/apps/desktop-gpui/src/transcript/view.rs @@ -393,9 +393,6 @@ impl ParagraphPainter { } impl ParagraphPainter { - /// The correction field, laid over the word it edits: the same type size - /// and baseline as the paragraph, on the panel's own surface so the - /// original spelling does not show through. fn word_field( &self, anchor: WordAnchor, @@ -420,6 +417,22 @@ impl ParagraphPainter { .border_1() .border_color(palette.accent) .shadow(theme.editor.pop_shadow()) + .capture_any_mouse_down({ + let editor = editor.clone(); + move |event, window, cx| { + if event.button != MouseButton::Left || event.click_count < 3 { + return; + } + let selected = editor + .update(cx, |this, cx| { + this.transcript_select_from_field(event.click_count, window, cx) + }) + .unwrap_or(false); + if selected { + cx.stop_propagation(); + } + } + }) .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) .on_key_down(move |event: &KeyDownEvent, window, cx| { let Some(direction) = ui::keyboard_nav::direction(&event.keystroke) else { @@ -1355,7 +1368,6 @@ impl EditorWindow { input } - /// Opens `word` for correcting in place, its text selected. pub(super) fn transcript_begin_word_edit( &mut self, word: usize, @@ -1386,6 +1398,7 @@ impl EditorWindow { self.transcript.edit_anchor.set(anchor); input.update(cx, |input, cx| { input.set_text(text, cx); + input.clear_history(); input.focus_and_select_all(window, cx); }); self.transcript.selection = Some(WordSelection::single(word)); @@ -1395,9 +1408,6 @@ impl EditorWindow { self.invalidate_transcript(cx); } - /// Writes the open correction into the captions as one undo step and - /// closes the field. Returns the word it landed on, found again if the - /// transcript was rebuilt while the field was open. pub(super) fn transcript_commit_word_edit( &mut self, window: &mut Window, @@ -1450,8 +1460,42 @@ impl EditorWindow { Some(word) } - /// Tab and Shift-Tab in the correction field: keep the edit and move on - /// to the next or previous word that plays. + /// The second click of a triple-click opens the field under the pointer, + /// so the third lands on it. While nothing has been typed it still means + /// sentence (or, with a fourth, paragraph) selection. + pub(super) fn transcript_select_from_field( + &mut self, + click_count: usize, + window: &mut Window, + cx: &mut Context, + ) -> bool { + let (Some(doc), Some(editing), Some(input)) = ( + self.transcript.doc.clone(), + self.transcript.editing.clone(), + self.transcript.edit_input.clone(), + ) else { + return false; + }; + if input.read(cx).text() != editing.text || editing.word >= doc.words.len() { + return false; + } + self.transcript.editing = None; + window.focus(&self.transcript.focus, cx); + let unit = if click_count >= 4 { + DragUnit::Paragraph + } else { + DragUnit::Sentence + }; + let origin = unit_bounds(&doc, editing.word, unit); + self.transcript.selection = Some(WordSelection { + anchor: origin.0, + focus: origin.1, + }); + self.transcript.drag = Some(WordDrag { unit, origin }); + self.invalidate_transcript(cx); + true + } + pub(super) fn transcript_step_word_edit( &mut self, forward: bool, diff --git a/apps/desktop-gpui/src/transcript/word_edit.rs b/apps/desktop-gpui/src/transcript/word_edit.rs index 228834608fc..6d47876684e 100644 --- a/apps/desktop-gpui/src/transcript/word_edit.rs +++ b/apps/desktop-gpui/src/transcript/word_edit.rs @@ -4,8 +4,6 @@ use cap_project::{CaptionWord, CaptionsData}; use super::model::{Word, word_sources}; -/// How far a word may have moved, by a refinement landing while it was being -/// edited, and still be found again by its text. const LOCATE_WINDOW: f64 = 1.0; fn is_spoken(text: &str) -> bool { @@ -113,8 +111,6 @@ fn retime(tokens: &[&str], spoken: &[(f32, f32)], start: f32, end: f32) -> Vec Option { if words .get(hint) diff --git a/apps/desktop-gpui/src/transcription.rs b/apps/desktop-gpui/src/transcription.rs index b650027b472..005e300b592 100644 --- a/apps/desktop-gpui/src/transcription.rs +++ b/apps/desktop-gpui/src/transcription.rs @@ -3535,6 +3535,53 @@ mod tests { ); } + #[test] + fn a_refinement_moves_caption_words_corrected_into_one() { + let input = vec![caption( + "segment-0", + 1.0, + 2.3, + &[("so", 1.0, 1.4), ("it", 2.0, 2.1), ("'s", 2.1, 2.3)], + )]; + let mut project = ProjectConfiguration { + timeline: Some(timeline(serde_json::json!({ + "segments": [{ "timescale": 1.0, "start": 0.0, "end": 10.0 }], + "zoomSegments": [] + }))), + captions: Some(CaptionsData { + segments: input.clone(), + source_timed: true, + ..Default::default() + }), + ..Default::default() + }; + let words = &mut project.captions.as_mut().unwrap().segments[0].words; + words.truncate(1); + words.push(CaptionWord { + text: "its".into(), + start: 2.0, + end: 2.3, + }); + let refined = RefinedCaptions::new( + &input, + vec![caption( + "segment-0", + 1.05, + 2.25, + &[("so", 1.05, 1.3), ("it", 2.05, 2.15), ("'s", 2.15, 2.25)], + )], + ); + + assert!(apply_refined_captions(&mut project, &refined, &[10.0])); + + let words: Vec<(&str, f32, f32)> = project.captions.as_ref().unwrap().segments[0] + .words + .iter() + .map(|word| (word.text.as_str(), word.start, word.end)) + .collect(); + assert_eq!(words, [("so", 1.05, 1.3), ("its", 2.05, 2.25)]); + } + #[test] fn a_refinement_leaves_a_word_retimed_by_hand_where_it_was_put() { let mut project = cut_project(); diff --git a/apps/desktop-gpui/src/transcription/refine.rs b/apps/desktop-gpui/src/transcription/refine.rs index ee38cc3dda2..3e012b0b9af 100644 --- a/apps/desktop-gpui/src/transcription/refine.rs +++ b/apps/desktop-gpui/src/transcription/refine.rs @@ -135,49 +135,58 @@ pub struct RefinedCaptions { /// Each input word's refined times, for captions edited while the /// refinement ran. retimed: HashMap, - /// The same keyed by time alone, so a word whose spelling was corrected - /// while the refinement ran still moves. Times two input words share are - /// left out. - retimed_by_time: HashMap<(u32, u32), (f32, f32)>, + /// Refined starts and ends keyed by the input word boundary they replace, + /// so a word corrected while the refinement ran, or several caption words + /// corrected into one, still moves when it starts and ends on input word + /// boundaries. A boundary two input words share is left out. + refined_starts: HashMap, + refined_ends: HashMap, } impl RefinedCaptions { pub fn new(input: &[CaptionSegment], segments: Vec) -> Self { let retimed = match_retimes(input, &segments); + let (refined_starts, refined_ends) = refined_boundaries(input, &retimed); Self { input_signature: words_signature(input), - retimed_by_time: retimes_by_time(input, &retimed), retimed, + refined_starts, + refined_ends, segments, } } fn retime_of(&self, word: &CaptionWord) -> Option<(f32, f32)> { - self.retimed - .get(&word_key(word)) - .or_else(|| { - self.retimed_by_time - .get(&(word.start.to_bits(), word.end.to_bits())) - }) - .copied() + if let Some(times) = self.retimed.get(&word_key(word)) { + return Some(*times); + } + let start = *self.refined_starts.get(&word.start.to_bits())?; + let end = *self.refined_ends.get(&word.end.to_bits())?; + (start <= end).then_some((start, end)) } } -fn retimes_by_time( +fn refined_boundaries( input: &[CaptionSegment], retimed: &HashMap, -) -> HashMap<(u32, u32), (f32, f32)> { - let mut uses: HashMap<(u32, u32), usize> = HashMap::new(); +) -> (HashMap, HashMap) { + let mut starts: HashMap = HashMap::new(); + let mut ends: HashMap = HashMap::new(); for word in input.iter().flat_map(|segment| &segment.words) { - *uses - .entry((word.start.to_bits(), word.end.to_bits())) - .or_default() += 1; + *starts.entry(word.start.to_bits()).or_default() += 1; + *ends.entry(word.end.to_bits()).or_default() += 1; } - retimed - .iter() - .map(|((_, start, end), times)| ((*start, *end), *times)) - .filter(|(time, _)| uses.get(time) == Some(&1)) - .collect() + let mut refined_starts = HashMap::new(); + let mut refined_ends = HashMap::new(); + for ((_, start, end), (refined_start, refined_end)) in retimed { + if starts.get(start) == Some(&1) { + refined_starts.insert(*start, *refined_start); + } + if ends.get(end) == Some(&1) { + refined_ends.insert(*end, *refined_end); + } + } + (refined_starts, refined_ends) } /// Pairs each input word with the refined word that has its text, in order. diff --git a/apps/desktop-gpui/src/ui/text_input.rs b/apps/desktop-gpui/src/ui/text_input.rs index dfbe702caca..3515c70ca47 100644 --- a/apps/desktop-gpui/src/ui/text_input.rs +++ b/apps/desktop-gpui/src/ui/text_input.rs @@ -662,6 +662,12 @@ impl TextInputState { cx.notify(); } + pub fn clear_history(&mut self) { + self.undo_stack.clear(); + self.redo_stack.clear(); + self.last_group = None; + } + pub fn select_all_text(&mut self, cx: &mut Context) { self.selected_range = 0..self.content.len(); self.selection_reversed = false; From cd5bdfec0bb764a1441dc1b1f4761a51642d7e7c Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:10:20 +0000 Subject: [PATCH 5/5] fix(desktop-gpui): open the word field on the line a wrapped word starts --- apps/desktop-gpui/src/transcript/tests.rs | 58 +++++++++++++++++++++++ apps/desktop-gpui/src/transcript/view.rs | 26 +++++++--- 2 files changed, 78 insertions(+), 6 deletions(-) diff --git a/apps/desktop-gpui/src/transcript/tests.rs b/apps/desktop-gpui/src/transcript/tests.rs index 0e9bb380fff..393766c4dc2 100644 --- a/apps/desktop-gpui/src/transcript/tests.rs +++ b/apps/desktop-gpui/src/transcript/tests.rs @@ -2646,4 +2646,62 @@ mod word_edit_ui { assert_eq!(texts(editor), ["One", "two.", "Tree", "four", "five"]); }); } + + /// Lays `text` out 60px wide in the test text system (6px per character, + /// 20px lines) and returns each span's field anchor as (x, y, width). + fn anchors( + cx: &mut gpui::TestAppContext, + text: &'static str, + spans: &[std::ops::Range], + ) -> Vec<(f32, f32, f32)> { + use gpui::{ParentElement, Styled, StyledText, point, px, size}; + + let cx = cx.add_empty_window(); + let styled = StyledText::new(text); + let layout = styled.layout().clone(); + cx.draw(point(px(0.), px(0.)), size(px(60.), px(200.)), |_, _| { + div() + .w(px(60.)) + .text_size(px(10.)) + .line_height(px(20.)) + .child(styled) + }); + spans + .iter() + .map(|span| { + let anchor = crate::transcript::view::word_anchor(&layout, span.clone()).unwrap(); + ( + f32::from(anchor.origin.x), + f32::from(anchor.origin.y), + f32::from(anchor.width), + ) + }) + .collect() + } + + #[gpui::test] + fn the_field_opens_on_the_line_a_wrapped_word_starts(cx: &mut gpui::TestAppContext) { + // "one two " fills the first line, so "three" opens the second. + assert_eq!( + anchors(cx, "one two three four", &[0..3, 4..7, 8..13, 14..18]), + [ + (0., 0., 18.), + (24., 0., 18.), + (0., 20., 30.), + (36., 20., 24.) + ] + ); + } + + #[gpui::test] + fn a_word_longer_than_the_line_gets_the_rest_of_its_first_line(cx: &mut gpui::TestAppContext) { + assert_eq!( + anchors(cx, "ab cdefghijklmnop", &[0..2, 3..17]), + [(0., 0., 12.), (0., 20., 60.)] + ); + assert_eq!( + anchors(cx, "abcdefghijklmnop qr", &[0..16]), + [(0., 0., 60.)] + ); + } } diff --git a/apps/desktop-gpui/src/transcript/view.rs b/apps/desktop-gpui/src/transcript/view.rs index b680fa43fc3..2335e4a2def 100644 --- a/apps/desktop-gpui/src/transcript/view.rs +++ b/apps/desktop-gpui/src/transcript/view.rs @@ -182,13 +182,27 @@ struct ParagraphPainter { edit_anchor: Rc>>, } -fn word_anchor(layout: &TextLayout, span: Range) -> Option { +pub(super) fn word_anchor(layout: &TextLayout, span: Range) -> Option { let bounds = layout.bounds(); - let start = layout.position_for_index(span.start)?; - let width = layout - .position_for_index(span.end) - .filter(|end| end.y == start.y && end.x > start.x) - .map_or(px(EDIT_MIN_WIDTH), |end| end.x - start.x); + let mut start = layout.position_for_index(span.start)?; + let first = layout.text().get(span.clone())?.chars().next()?; + // gpui places an index that sits on a soft wrap at the end of the line + // above, so a word that opens a wrapped line starts at the left edge of + // the line its first character is on. + let after_first = layout.position_for_index(span.start + first.len_utf8())?; + if after_first.y > start.y { + start = Point { + x: bounds.origin.x, + y: after_first.y, + }; + } + let width = match layout.position_for_index(span.end) { + Some(end) if end.y == start.y && end.x > start.x => end.x - start.x, + // A word longer than the line wraps itself: the field covers the + // rest of the line it starts on. + Some(end) if end.y > start.y => (bounds.right() - start.x).max(px(EDIT_MIN_WIDTH)), + _ => px(EDIT_MIN_WIDTH), + }; Some(WordAnchor { origin: start - bounds.origin, width,