pub(crate) fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSnippet> {
    const FN_MAIN_PREFIX: &str = "fn main() {\n";

    fn enclose_in_main_block(s: &str, config: &Config) -> String {
        let indent = Indent::from_width(config, config.tab_spaces());
        let mut result = String::with_capacity(s.len() * 2);
        result.push_str(FN_MAIN_PREFIX);
        let mut need_indent = true;
        for (kind, line) in LineClasses::new(s) {
            if need_indent {
                result.push_str(&indent.to_string(config));
            }
            result.push_str(&line);
            result.push('\n');
            need_indent = indent_next_line(kind);
        }
        result.push('}');
        result
    }

    // Wrap the given code block with `fn main()` if it does not have one.
    let snippet = enclose_in_main_block(code_snippet, config);
    let mut result = String::with_capacity(snippet.len());
    let mut is_first = true;

    // While formatting the code, ignore the config's newline style setting and always use "\n"
    // instead of "\r\n" for the newline characters. This is ok because the output here is
    // not directly outputted by rustfmt command, but used by the comment formatter's input.
    // We have output-file-wide "\n" ==> "\r\n" conversion process after here if it's necessary.
    let mut config_with_unix_newline = config.clone();
    config_with_unix_newline
        .set()
        .newline_style(NewlineStyle::Unix);
    let mut formatted = format_snippet(&snippet, &config_with_unix_newline)?;
    // Remove wrapping main block
    formatted.unwrap_code_block();

    // Trim "fn main() {" on the first line and "}" on the last line,
    // then unindent the whole code block.
    let block_len = formatted
        .snippet
        .rfind('}')
        .unwrap_or_else(|| formatted.snippet.len());
    let mut is_indented = true;
    let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
    for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
        if !is_first {
            result.push('\n');
        } else {
            is_first = false;
        }
        let trimmed_line = if !is_indented {
            line
        } else if line.len() > config.max_width() {
            // If there are lines that are larger than max width, we cannot tell
            // whether we have succeeded but have some comments or strings that
            // are too long, or we have failed to format code block. We will be
            // conservative and just return `None` in this case.
            return None;
        } else if line.len() > indent_str.len() {
            // Make sure that the line has leading whitespaces.
            if line.starts_with(indent_str.as_ref()) {
                let offset = if config.hard_tabs() {
                    1
                } else {
                    config.tab_spaces()
                };
                &line[offset..]
            } else {
                line
            }
        } else {
            line
        };
        result.push_str(trimmed_line);
        is_indented = indent_next_line(kind);
    }
    Some(FormattedSnippet {
        snippet: result,
        non_formatted_ranges: formatted.non_formatted_ranges,
    })
}
/// Format the given code block. Mainly targeted for code block in comment.
/// The code block may be incomplete (i.e., parser may be unable to parse it).
/// To avoid panic in parser, we wrap the code block with a dummy function.
/// The returned code block does **not** end with newline.
Format the given code block. Mainly targeted for code block in comment.
The code block may be incomplete .
To avoid panic in parser, we wrap the code block with a dummy function.
The returned code block does **not** end with newline.
Format the given code block . Mainly targeted for code block in comment . The code block may be incomplete . To avoid panic in parser we wrap the code block with a dummy function . The returned code block does ** not ** end with newline .
pub(crate) fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSnippet> {
    const FN_MAIN_PREFIX: &str = "fn main() {\n";
    fn enclose_in_main_block(s: &str, config: &Config) -> String {
        let indent = Indent::from_width(config, config.tab_spaces());
        let mut result = String::with_capacity(s.len() * 2);
        result.push_str(FN_MAIN_PREFIX);
        let mut need_indent = true;
        for (kind, line) in LineClasses::new(s) {
            if need_indent {
                result.push_str(&indent.to_string(config));
            }
            result.push_str(&line);
            result.push('\n');
            need_indent = indent_next_line(kind);
        }
        result.push('}');
        result
    }
    // Wrap the given code block with `fn main()` if it does not have one.
    let snippet = enclose_in_main_block(code_snippet, config);
    let mut result = String::with_capacity(snippet.len());
    let mut is_first = true;
    // While formatting the code, ignore the config's newline style setting and always use "\n"
    // instead of "\r\n" for the newline characters. This is ok because the output here is
    // not directly outputted by rustfmt command, but used by the comment formatter's input.
    // We have output-file-wide "\n" ==> "\r\n" conversion process after here if it's necessary.
    let mut config_with_unix_newline = config.clone();
    config_with_unix_newline
        .set()
        .newline_style(NewlineStyle::Unix);
    let mut formatted = format_snippet(&snippet, &config_with_unix_newline)?;
    // Remove wrapping main block
    formatted.unwrap_code_block();
    // Trim "fn main() {" on the first line and "}" on the last line,
    // then unindent the whole code block.
    let block_len = formatted
        .snippet
        .rfind('}')
        .unwrap_or_else(|| formatted.snippet.len());
    let mut is_indented = true;
    let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
    for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
        if !is_first {
            result.push('\n');
        } else {
            is_first = false;
        }
        let trimmed_line = if !is_indented {
            line
        } else if line.len() > config.max_width() {
            // If there are lines that are larger than max width, we cannot tell
            // whether we have succeeded but have some comments or strings that
            // are too long, or we have failed to format code block. We will be
            // conservative and just return `None` in this case.
            return None;
        } else if line.len() > indent_str.len() {
            // Make sure that the line has leading whitespaces.
            if line.starts_with(indent_str.as_ref()) {
                let offset = if config.hard_tabs() {
                    1
                } else {
                    config.tab_spaces()
                };
                &line[offset..]
            } else {
                line
            }
        } else {
            line
        };
        result.push_str(trimmed_line);
        is_indented = indent_next_line(kind);
    }
    Some(FormattedSnippet {
        snippet: result,
        non_formatted_ranges: formatted.non_formatted_ranges,
    })
}
pub ( crate ) fn format_code_block ( code_snippet : & str , config : & Config ) -> Option < FormattedSnippet > { const FN_MAIN_PREFIX : & str = " \n " ; fn enclose_in_main_block ( s : & str , config : & Config ) -> String { let indent = Indent :: from_width ( config , config . tab_spaces ( ) ) ; let mut result = String :: with_capacity ( s . len ( ) * 2 ) ; result . push_str ( FN_MAIN_PREFIX ) ; let mut need_indent = true ; for ( kind , line ) in LineClasses :: new ( s ) { if need_indent { result . push_str ( & indent . to_string ( config ) ) ; } result . push_str ( & line ) ; result . push ( '\n' ) ; need_indent = indent_next_line ( kind ) ; } result . push ( '}' ) ; result } // Wrap the given code block with `fn main()` if it does not have one. let snippet = enclose_in_main_block ( code_snippet , config ) ; let mut result = String :: with_capacity ( snippet . len ( ) ) ; let mut is_first = true ; // While formatting the code, ignore the config's newline style setting and always use "\n" // instead of "\r\n" for the newline characters. This is ok because the output here is // not directly outputted by rustfmt command, but used by the comment formatter's input. // We have output-file-wide "\n" ==> "\r\n" conversion process after here if it's necessary. let mut config_with_unix_newline = config . clone ( ) ; config_with_unix_newline . set ( ) . newline_style ( NewlineStyle :: Unix ) ; let mut formatted = format_snippet ( & snippet , & config_with_unix_newline ) ? ; // Remove wrapping main block formatted . unwrap_code_block ( ) ; // Trim "fn main() {" on the first line and "}" on the last line, // then unindent the whole code block. let block_len = formatted . snippet . rfind ( '}' ) . unwrap_or_else ( | | formatted . snippet . len ( ) ) ; let mut is_indented = true ; let indent_str = Indent :: from_width ( config , config . tab_spaces ( ) ) . to_string ( config ) ; for ( kind , ref line ) in LineClasses :: new ( & formatted . snippet [ FN_MAIN_PREFIX . len ( ) .. block_len ] ) { if ! is_first { result . push ( '\n' ) ; } else { is_first = false ; } let trimmed_line = if ! is_indented { line } else if line . len ( ) > config . max_width ( ) { // If there are lines that are larger than max width, we cannot tell // whether we have succeeded but have some comments or strings that // are too long, or we have failed to format code block. We will be // conservative and just return `None` in this case. return None ; } else if line . len ( ) > indent_str . len ( ) { // Make sure that the line has leading whitespaces. if line . starts_with ( indent_str . as_ref ( ) ) { let offset = if config . hard_tabs ( ) { 1 } else { config . tab_spaces ( ) } ; & line [ offset .. ] } else { line } } else { line } ; result . push_str ( trimmed_line ) ; is_indented = indent_next_line ( kind ) ; } Some ( FormattedSnippet { snippet : result , non_formatted_ranges : formatted . non_formatted_ranges , } ) }
Format the given code block.
Format the given code block .
code_snippet
&str
config
&Config
code_snippet
&str
config
&Config