Repository: openai/codex
File: codex-rs/tui/src/bottom_pane/chat_composer.rs (Rust)

Code before:
            if pos > idx {
                self.textarea.insert_str(&text[idx..pos]);
            }
            self.textarea.insert_element(ph);
            *count -= 1;
            idx = pos + ph.len();
        }
        if idx < text.len() {
            self.textarea.insert_str(&text[idx..]);
        }

        self.textarea.set_cursor(self.textarea.text().len());
        self.sync_popups();
    }

    pub(crate) fn current_text_with_pending(&self) -> String {
        let mut text = self.textarea.text().to_string();
        for (placeholder, actual) in &self.pending_pastes {
            if text.contains(placeholder) {
                text = text.replace(placeholder, actual);
            }
        }
        text
    }

    fn pending_paste_occurrences(
        text: &str,
        pending_pastes: &[(String, String)],
    ) -> Vec<(usize, String)> {
        let mut unique_placeholders = Vec::new();
        let mut seen = HashSet::new();
        for (placeholder, _) in pending_pastes {
            if seen.insert(placeholder.as_str()) {
                unique_placeholders.push(placeholder.as_str());
            }
        }
        unique_placeholders.sort_by_key(|placeholder| Reverse(placeholder.len()));

        let mut occurrences = Vec::new();
        let mut idx = 0usize;
        while idx < text.len() {
            let slice = &text[idx..];
            let mut matched = None;
            for placeholder in &unique_placeholders {
                if slice.starts_with(placeholder) {
                    matched = Some(*placeholder);
                    break;
                }
            }
            if let Some(placeholder) = matched {
                occurrences.push((idx, placeholder.to_string()));

Review comment (refactor):
Please add a doc comment to capture intent / algorithm notes. Perhaps some inline comments within this / small helper methods with intention revealing names would help. Right now I have to read every line of this method to understand its purpose, and how it solves that.

nit: I prefer to read code top to bottom, as the mental model of calling into code that's defined below is often neater when there are small methods that compose up. can you Move this below `set_text_content_with_pending`?

Code after:
        config: ChatComposerConfig,
    ) -> Self {
        let use_shift_enter_hint = enhanced_keys_supported;

        let mut this = Self {
            textarea: TextArea::new(),
            textarea_state: RefCell::new(TextAreaState::default()),
            active_popup: ActivePopup::None,
            app_event_tx,
            history: ChatComposerHistory::new(),
            quit_shortcut_expires_at: None,
            quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')),
            esc_backtrack_hint: false,
            use_shift_enter_hint,
            dismissed_file_popup_token: None,
            current_file_query: None,
            pending_pastes: Vec::new(),
            large_paste_counters: HashMap::new(),
            has_focus: has_input_focus,
            attached_images: Vec::new(),
            placeholder_text,
            is_task_running: false,
            input_enabled: true,
            input_disabled_placeholder: None,
            paste_burst: PasteBurst::default(),
            disable_paste_burst: false,
            custom_prompts: Vec::new(),
            footer_mode: FooterMode::ComposerEmpty,
            footer_hint_override: None,
            footer_flash: None,
            context_window_percent: None,
            context_window_used_tokens: None,
            skills: None,
            connectors_snapshot: None,
            dismissed_mention_popup_token: None,
            mention_bindings: HashMap::new(),
            recent_submission_mention_bindings: Vec::new(),
            steer_enabled: false,
            collaboration_modes_enabled: false,
            config,
            collaboration_mode_indicator: None,
            connectors_enabled: false,
            personality_command_enabled: false,
            windows_degraded_sandbox_active: false,
            status_line_value: None,
            status_line_enabled: false,
        };
        // Apply configuration via the setter to keep side-effects centralized.
        this.set_disable_paste_burst(disable_paste_burst);
        this
    }