1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use super::configuration::Configuration;

use anyhow::Result;
use dprint_core::configuration::resolve_new_line_kind;
use sqlformat::FormatOptions;
use sqlformat::Indent;
use sqlformat::QueryParams;
use std::path::Path;

pub fn format_text(_file_path: &Path, text: &str, config: &Configuration) -> Result<Option<String>> {
  let input_text = text;
  let text = sqlformat::format(
    text,
    &QueryParams::None,
    FormatOptions {
      indent: if config.use_tabs {
        Indent::Tabs
      } else {
        Indent::Spaces(config.indent_width)
      },
      uppercase: config.uppercase,
      lines_between_queries: config.lines_between_queries,
    },
  );

  // ensure ends with newline
  let text = if !text.ends_with('\n') {
    let mut text = text;
    text.push('\n');
    text
  } else {
    text
  };

  // newline
  let text = if resolve_new_line_kind(&text, config.new_line_kind) == "\n" {
    text.replace("\r\n", "\n")
  } else {
    // lazy
    text.replace("\r\n", "\n").replace("\n", "\r\n")
  };

  if text == input_text {
    Ok(None)
  } else {
    Ok(Some(text))
  }
}