Repository: TabbyML/tabby
File: crates/tabby/src/services/completion.rs (Rust)

Code before:

            let snippets = self
                .build_snippets(
                    &language,
                    segments,
                    allowed_code_repository,
                    request.disable_retrieval_augmented_code_completion(),
                )
                .await;
            let prompt = self
                .prompt_builder
                .build(&language, segments.clone(), &snippets);
            if use_crlf {
                (prompt.replace("\r\n", "\n"), Some(segments), snippets)
            } else {
                (prompt, Some(segments), snippets)
            }
        } else {
            return Err(CompletionError::EmptyPrompt);
        };

        let mut text = self.engine.generate(&prompt, options).await;
        if use_crlf {
            let re = Regex::new(r"([^\r])\n").unwrap(); // Match \n that is preceded by anything except \r
            text = re.replace_all(&text, "$1\r\n").to_string() // Replace with captured character and \r\n
        }

        self.logger.log(
            request.user.clone(),
            Event::Completion {
                completion_id: completion_id.clone(),
                language,
                prompt: prompt.clone(),
                segments: segments.cloned().map(|x| x.into()),
                choices: vec![api::event::Choice {
                    index: 0,
                    text: text.clone(),
                }],
                user_agent: user_agent.map(|x| x.to_owned()),
            },
        );

        let debug_data = request
            .debug_options
            .as_ref()
            .map(|debug_options| DebugData {
                snippets: debug_options.return_snippets.then_some(snippets),
                prompt: debug_options.return_prompt.then_some(prompt),
            });

        Ok(CompletionResponse::new(

Review comment (refactor):
extract a function `override_text(text, use_crlf)`.

Please also document a bit why this regex is needed for replacement (instead of just `\n` -> `\r\n`)

Code after:
                    allowed_code_repository,
                    request.disable_retrieval_augmented_code_completion(),
                )
                .await;
            let prompt = self
                .prompt_builder
                .build(&language, segments.clone(), &snippets);

            (override_prompt(prompt, use_crlf), Some(segments), snippets)
        } else {
            return Err(CompletionError::EmptyPrompt);
        };

        let generated_text =
            override_generated_text(self.engine.generate(&prompt, options).await, use_crlf);

        self.logger.log(
            request.user.clone(),
            Event::Completion {
                completion_id: completion_id.clone(),
                language,
                prompt: prompt.clone(),
                segments: segments.cloned().map(|x| x.into()),
                choices: vec![api::event::Choice {
                    index: 0,
                    text: generated_text.clone(),
                }],
                user_agent: user_agent.map(|x| x.to_owned()),
            },
        );

        let debug_data = request
            .debug_options
            .as_ref()
            .map(|debug_options| DebugData {
                snippets: debug_options.return_snippets.then_some(snippets),
                prompt: debug_options.return_prompt.then_some(prompt),
            });

        Ok(CompletionResponse::new(
            completion_id,
            vec![Choice::new(generated_text)],
            debug_data,
        ))
    }
}

fn contains_crlf(segments: &Segments) -> bool {
    if segments.prefix.contains("\r\n") {
        return true;
    }