Repository: helix-editor/helix
File: helix-term/src/commands.rs (Rust)

Code before:
        };

        let line_num = new_line.saturating_sub(1);

        // Index to insert newlines after, as well as the char width
        // to use to compensate for those inserted newlines.
        let (line_end_index, line_end_offset_width) = if new_line == 0 {
            (0, 0)
        } else {
            (
                line_end_char_index(&doc_text, line_num),
                doc.line_ending.len_chars(),
            )
        };

        let comment_token = {
            let tokens = doc
                .language_config()
                .and_then(|config| config.comment_tokens.as_ref());

            get_comment_token(doc.text(), tokens, cursor_line)
        };
        let new_line_will_be_comment = config.continue_comments && comment_token.is_some();

        let indent = {
            let line = doc_text.line(cursor_line);
            match line.first_non_whitespace_char() {
                Some(pos) if new_line_will_be_comment => line.slice(..pos).to_string(),
                _ => indent::indent_for_newline(
                    doc.language_config(),
                    doc.syntax(),
                    &doc.config.load().indent_heuristic,
                    &doc.indent_style,
                    doc.tab_width(),
                    doc_text,
                    line_num,
                    line_end_index,
                    cursor_line,
                ),
            }
        };

        let indent_len = indent.len();
        let mut text = String::with_capacity(1 + indent_len);
        text.push_str(doc.line_ending.as_str());
        text.push_str(&indent);

        if config.continue_comments {
            if let Some(token) = comment_token {
                text.push_str(token);
                text.push(' ');

Review comment (security):
style nit: we try to avoid using blocks for bindings like this. It's fine for `line` to escape into the outer scope

Code after:

        let line_num = new_line.saturating_sub(1);

        // Index to insert newlines after, as well as the char width
        // to use to compensate for those inserted newlines.
        let (line_end_index, line_end_offset_width) = if new_line == 0 {
            (0, 0)
        } else {
            (
                line_end_char_index(&text, line_num),
                doc.line_ending.len_chars(),
            )
        };

        let continue_comment_token = doc
            .language_config()
            .and_then(|config| config.comment_tokens.as_ref())
            .and_then(|tokens| comment::get_comment_token(text, tokens, cursor_line));

        let line = text.line(cursor_line);
        let indent = match line.first_non_whitespace_char() {
            Some(pos) if continue_comment_token.is_some() => line.slice(..pos).to_string(),
            _ => indent::indent_for_newline(
                doc.language_config(),
                doc.syntax(),
                &doc.config.load().indent_heuristic,
                &doc.indent_style,
                doc.tab_width(),
                text,
                line_num,
                line_end_index,
                cursor_line,
            ),
        };

        let indent_len = indent.len();
        let mut text = String::with_capacity(1 + indent_len);
        text.push_str(doc.line_ending.as_str());
        text.push_str(&indent);

        if let Some(token) = continue_comment_token {
            text.push_str(token);
            text.push(' ');
        }

        let text = text.repeat(count);

        // calculate new selection ranges
        let pos = offs + line_end_index + line_end_offset_width;
        let comment_len = continue_comment_token
            .map(|token| token.len() + 1) // `+ 1` for the extra space added