Skip to content

Commit

Permalink
Simplify fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
matias-gonz committed Oct 1, 2024
1 parent 3de9104 commit 6dabb8d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 55 deletions.
52 changes: 2 additions & 50 deletions zk_toolbox/crates/zk_supervisor/src/commands/sql_fmt.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use std::mem::take;

use anyhow::{bail, Context, Result};
use anyhow::{bail, Result};
use common::spinner::Spinner;
use sqruff_lib::{api::simple::get_simple_config, core::linter::core::Linter};
use xshell::Shell;

use super::lint_utils::{get_unignored_files, IgnoredData, Target};
use crate::messages::{
msg_file_is_not_formatted, MSG_FAILED_TO_DETERMINE_BASE_INDENT,
MSG_FAILED_TO_FIND_END_OF_REGULAR_STRING_QUERY,
MSG_FAILED_TO_FIND_START_OF_REGULAR_STRING_QUERY, MSG_RUNNING_SQL_FMT_SPINNER,
};
use crate::messages::{msg_file_is_not_formatted, MSG_RUNNING_SQL_FMT_SPINNER};

fn format_query(query: &str) -> anyhow::Result<String> {
let exclude_rules = vec!["LT12".to_string()]; // avoid adding newline before `$` character
Expand Down Expand Up @@ -68,44 +64,6 @@ fn format_rust_string_query(query: &str, is_raw: bool) -> anyhow::Result<String>
Ok(add_indent(&reconstructed_rust_string, base_indent))
}

fn format_one_line_query(line: &str) -> anyhow::Result<String> {
let is_raw_string = line.contains("sqlx::query!(r");

let query_start = if is_raw_string {
line.find(r#"r#""#)
.context(MSG_FAILED_TO_FIND_START_OF_REGULAR_STRING_QUERY)?
} else {
line.find('"')
.context(MSG_FAILED_TO_FIND_START_OF_REGULAR_STRING_QUERY)?
};

let base_indent = line
.find(|c: char| !c.is_whitespace())
.context(MSG_FAILED_TO_DETERMINE_BASE_INDENT)?
+ 4;

let prefix = &line[..query_start];

let query_end = if is_raw_string {
line.find(r#""#)
.context(MSG_FAILED_TO_FIND_END_OF_REGULAR_STRING_QUERY)?
+ 2
} else {
line[1..]
.find('"')
.context(MSG_FAILED_TO_FIND_END_OF_REGULAR_STRING_QUERY)?
+ 3
};

let suffix = &line[query_end..];
let query = &line[query_start..query_end];

let mut formatted_query = format_rust_string_query(query, is_raw_string)?;
formatted_query = add_indent(&formatted_query, base_indent);

Ok(format!("{}\n{}\n{}", prefix, formatted_query, suffix))
}

fn fmt_file(shell: &Shell, file_path: &str, check: bool) -> Result<()> {
let content = shell.read_file(file_path)?;
let mut modified_file = String::new();
Expand All @@ -116,12 +74,6 @@ fn fmt_file(shell: &Shell, file_path: &str, check: bool) -> Result<()> {
let mut built_query = String::new();

for line in content.lines() {
if line.contains("sqlx::query!(\"") || line.contains("sqlx::query!(r") {
modified_file.push_str(&format_one_line_query(line)?);
modified_file.push('\n');
continue;
}

if line.ends_with("sqlx::query!(") {
lines_to_query = Some(1);
is_raw_string = false;
Expand Down
5 changes: 0 additions & 5 deletions zk_toolbox/crates/zk_supervisor/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,6 @@ pub(super) const MSG_RUNNING_CONTRACTS_LINTER_SPINNER: &str = "Running contracts
pub(super) const MSG_RUNNING_CONTRACTS_FMT_SPINNER: &str = "Running prettier for contracts..";

pub(super) const MSG_RUNNING_SQL_FMT_SPINNER: &str = "Running SQL formatter..";
pub(super) const MSG_FAILED_TO_FIND_END_OF_REGULAR_STRING_QUERY: &str =
"Failed to find the end of the regular string query";
pub(super) const MSG_FAILED_TO_FIND_START_OF_REGULAR_STRING_QUERY: &str =
"Failed to find the start of the regular string query";
pub(super) const MSG_FAILED_TO_DETERMINE_BASE_INDENT: &str = "Failed to determine base indent";

pub(super) fn msg_file_is_not_formatted(file: &str) -> String {
format!("File {} is not formatted", file)
Expand Down

0 comments on commit 6dabb8d

Please sign in to comment.