Function format_text

Source
pub fn format_text(options: FormatTextOptions<'_>) -> Result<Option<String>>
Expand description

Formats a file.

Returns the file text or an error when it failed to parse.

ยงExample

use std::path::PathBuf;
use dprint_plugin_typescript::*;
use dprint_plugin_typescript::configuration::*;

// build the configuration once
let config = ConfigurationBuilder::new()
    .line_width(80)
    .prefer_hanging(true)
    .prefer_single_line(false)
    .quote_style(QuoteStyle::PreferSingle)
    .next_control_flow_position(NextControlFlowPosition::SameLine)
    .build();

// now format many files (it is recommended to parallelize this)
let files_to_format = vec![(PathBuf::from("path/to/file.ts"), "const  t  =  5 ;")];
for (file_path, file_text) in files_to_format {
    let result = format_text(FormatTextOptions {
        path: &file_path,
        extension: None,
        text: file_text.into(),
        config: &config,
        external_formatter: None,
    });
    // save result here...
}