Skip to content

Commit

Permalink
optimize matching (#58)
Browse files Browse the repository at this point in the history
* optimize_reserved_matching

* refactor if/else to match
  • Loading branch information
dkuku authored Nov 4, 2024
1 parent e4217f2 commit 3c00871
Show file tree
Hide file tree
Showing 2 changed files with 469 additions and 384 deletions.
86 changes: 52 additions & 34 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,58 @@ pub(crate) fn format(
continue;
}

if token.kind == TokenKind::Whitespace {
// ignore (we do our own whitespace formatting)
} else if token.kind == TokenKind::LineComment {
formatter.format_line_comment(token, &mut formatted_query);
} else if token.kind == TokenKind::BlockComment {
formatter.format_block_comment(token, &mut formatted_query);
} else if token.kind == TokenKind::ReservedTopLevel {
formatter.format_top_level_reserved_word(token, &mut formatted_query);
formatter.previous_reserved_word = Some(token);
} else if token.kind == TokenKind::ReservedTopLevelNoIndent {
formatter.format_top_level_reserved_word_no_indent(token, &mut formatted_query);
formatter.previous_reserved_word = Some(token);
} else if token.kind == TokenKind::ReservedNewline {
formatter.format_newline_reserved_word(token, &mut formatted_query);
formatter.previous_reserved_word = Some(token);
} else if token.kind == TokenKind::Reserved {
formatter.format_with_spaces(token, &mut formatted_query);
formatter.previous_reserved_word = Some(token);
} else if token.kind == TokenKind::OpenParen {
formatter.format_opening_parentheses(token, &mut formatted_query);
} else if token.kind == TokenKind::CloseParen {
formatter.format_closing_parentheses(token, &mut formatted_query);
} else if token.kind == TokenKind::Placeholder {
formatter.format_placeholder(token, &mut formatted_query);
} else if token.value == "," {
formatter.format_comma(token, &mut formatted_query);
} else if token.value == ":" {
formatter.format_with_space_after(token, &mut formatted_query);
} else if token.value == "." {
formatter.format_without_spaces(token, &mut formatted_query);
} else if token.value == ";" {
formatter.format_query_separator(token, &mut formatted_query);
} else {
formatter.format_with_spaces(token, &mut formatted_query);
match token.kind {
TokenKind::Whitespace => {
// ignore (we do our own whitespace formatting)
}
TokenKind::LineComment => {
formatter.format_line_comment(token, &mut formatted_query);
}
TokenKind::BlockComment => {
formatter.format_block_comment(token, &mut formatted_query);
}
TokenKind::ReservedTopLevel => {
formatter.format_top_level_reserved_word(token, &mut formatted_query);
formatter.previous_reserved_word = Some(token);
}
TokenKind::ReservedTopLevelNoIndent => {
formatter.format_top_level_reserved_word_no_indent(token, &mut formatted_query);
formatter.previous_reserved_word = Some(token);
}
TokenKind::ReservedNewline => {
formatter.format_newline_reserved_word(token, &mut formatted_query);
formatter.previous_reserved_word = Some(token);
}
TokenKind::Reserved => {
formatter.format_with_spaces(token, &mut formatted_query);
formatter.previous_reserved_word = Some(token);
}
TokenKind::OpenParen => {
formatter.format_opening_parentheses(token, &mut formatted_query);
}
TokenKind::CloseParen => {
formatter.format_closing_parentheses(token, &mut formatted_query);
}
TokenKind::Placeholder => {
formatter.format_placeholder(token, &mut formatted_query);
}
_ => match token.value {
"," => {
formatter.format_comma(token, &mut formatted_query);
}
":" => {
formatter.format_with_space_after(token, &mut formatted_query);
}
"." => {
formatter.format_without_spaces(token, &mut formatted_query);
}
";" => {
formatter.format_query_separator(token, &mut formatted_query);
}
_ => {
formatter.format_with_spaces(token, &mut formatted_query);
}
},
}
}
formatted_query.trim().to_string()
Expand Down
Loading

0 comments on commit 3c00871

Please sign in to comment.