Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: upgrade dprint-core to 0.66 #33

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 85 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ tracing = ["dprint-core/tracing"]

[dependencies]
anyhow = "1.0.64"
dprint-core = { version = "0.63.3", features = ["formatting"] }
dprint-core = { version = "0.66.1", features = ["formatting"] }
dprint-core-macros = "0.1.0"
jsonc-parser = { version = "0.23.0" }
serde = { version = "1.0.144", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
Expand Down
47 changes: 25 additions & 22 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::token_finder::TokenFinder;
use dprint_core::formatting::conditions::if_true_or;
use dprint_core::formatting::ir_helpers::SingleLineOptions;
use dprint_core::formatting::*;
use dprint_core_macros::sc;
use jsonc_parser::ast::*;
use jsonc_parser::common::Range;
use jsonc_parser::common::Ranged;
Expand Down Expand Up @@ -81,7 +82,7 @@ fn gen_node_with_inner<'a>(

// generate the node
if has_ignore_comment(&node, context) {
items.push_str(""); // force the current line indentation
items.push_force_current_line_indentation();
items.extend(inner_gen(
ir_helpers::gen_from_raw_string(node.text(context.text)),
context,
Expand Down Expand Up @@ -146,8 +147,8 @@ fn gen_array<'a>(node: &'a Array<'a>, context: &mut Context<'a, '_>) -> PrintIte
items
},
GenSurroundedByTokensOptions {
open_token: "[",
close_token: "]",
open_token: sc!("["),
close_token: sc!("]"),
range: node.range,
first_member: node.elements.first().map(|f| f.range()),
prefer_single_line_when_empty: true,
Expand Down Expand Up @@ -186,8 +187,8 @@ fn gen_object<'a>(obj: &'a Object, context: &mut Context<'a, '_>) -> PrintItems
items
},
GenSurroundedByTokensOptions {
open_token: "{",
close_token: "}",
open_token: sc!("{"),
close_token: sc!("}"),
range: obj.range,
first_member: obj.properties.first().map(|f| &f.range),
prefer_single_line_when_empty: false,
Expand All @@ -199,12 +200,14 @@ fn gen_object<'a>(obj: &'a Object, context: &mut Context<'a, '_>) -> PrintItems
fn gen_object_prop<'a>(node: &'a ObjectProp, context: &mut Context<'a, '_>) -> PrintItems {
let mut items = PrintItems::new();
items.extend(gen_node((&node.name).into(), context));
items.push_str(": ");
items.push_sc(sc!(": "));
items.extend(gen_node((&node.value).into(), context));

items
}

const DOUBLE_QUOTE_SC: &'static StringContainer = sc!("\"");

fn gen_string_lit<'a>(node: &'a StringLit, context: &mut Context<'a, '_>) -> PrintItems {
let text = node.text(context.text);
let is_double_quotes = text.starts_with('"');
Expand All @@ -215,18 +218,18 @@ fn gen_string_lit<'a>(node: &'a StringLit, context: &mut Context<'a, '_>) -> Pri
} else {
text.replace("\\'", "'")
};
items.push_str("\"");
items.push_sc(DOUBLE_QUOTE_SC);
items.push_string(text.replace('"', "\\\""));
items.push_str("\"");
items.push_sc(DOUBLE_QUOTE_SC);
items
}

fn gen_word_lit<'a>(node: &'a WordLit<'a>, _: &mut Context<'a, '_>) -> PrintItems {
// this will be a property name that's not a string literal
let mut items = PrintItems::new();
items.push_str("\"");
items.push_sc(DOUBLE_QUOTE_SC);
items.push_string(node.value.to_string());
items.push_str("\"");
items.push_sc(DOUBLE_QUOTE_SC);
items
}

Expand Down Expand Up @@ -354,8 +357,8 @@ fn gen_comma_separated_value<'a>(
}

struct GenSurroundedByTokensOptions<'a> {
open_token: &'static str,
close_token: &'static str,
open_token: &'static StringContainer,
close_token: &'static StringContainer,
range: Range,
first_member: Option<&'a Range>,
prefer_single_line_when_empty: bool,
Expand All @@ -366,20 +369,20 @@ fn gen_surrounded_by_tokens<'a, 'b>(
opts: GenSurroundedByTokensOptions<'a>,
context: &mut Context<'a, 'b>,
) -> PrintItems {
let open_token_end = opts.range.start + opts.open_token.len();
let close_token_start = opts.range.end - opts.close_token.len();
let open_token_end = opts.range.start + opts.open_token.text.len();
let close_token_start = opts.range.end - opts.close_token.text.len();

// assert the tokens are in the place the caller says they are
#[cfg(debug_assertions)]
context.assert_text(opts.range.start, open_token_end, opts.open_token);
context.assert_text(opts.range.start, open_token_end, opts.open_token.text);
#[cfg(debug_assertions)]
context.assert_text(close_token_start, opts.range.end, opts.close_token);
context.assert_text(close_token_start, opts.range.end, opts.close_token.text);

// generate
let mut items = PrintItems::new();
let open_token_start_line = context.text_info.line_index(opts.range.start);

items.push_str(opts.open_token);
items.push_sc(opts.open_token);
if let Some(first_member) = opts.first_member {
let first_member_start_line = context.text_info.line_index(first_member.start);
if open_token_start_line < first_member_start_line {
Expand Down Expand Up @@ -480,7 +483,7 @@ fn gen_surrounded_by_tokens<'a, 'b>(
}
}

items.push_str(opts.close_token);
items.push_sc(opts.close_token);

return items;

Expand All @@ -497,7 +500,7 @@ fn gen_surrounded_by_tokens<'a, 'b>(
{
if let Some(generated_comment) = gen_comment(first_comment, context) {
items.push_signal(Signal::StartForceNoNewLines);
items.push_str(" ");
items.push_space();
items.extend(generated_comment);
items.push_signal(Signal::FinishForceNoNewLines);
}
Expand Down Expand Up @@ -614,7 +617,7 @@ fn gen_comments_as_trailing<'a: 'b, 'b>(
let mut items = PrintItems::new();

if let Some(Comment::Block(_)) = first_unhandled_comment {
items.push_str(" ");
items.push_space();
}

items.extend(gen_comment_collection(
Expand Down Expand Up @@ -683,10 +686,10 @@ fn gen_comment_based_on_last_node(
}
} else if comment.kind() == CommentKind::Line {
items.push_signal(Signal::StartForceNoNewLines);
items.push_str(" ");
items.push_space();
pushed_ignore_new_lines = true;
} else if last_node.text(context.text).starts_with("/*") {
items.push_str(" ");
items.push_space();
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/wasm_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ impl SyncPluginHandler<Configuration> for JsonPluginHandler {
fn format(
&mut self,
file_path: &Path,
file_text: &str,
file_bytes: Vec<u8>,
config: &Configuration,
_format_with_host: impl FnMut(&Path, String, &ConfigKeyMap) -> FormatResult,
_format_with_host: impl FnMut(&Path, Vec<u8>, &ConfigKeyMap) -> FormatResult,
) -> FormatResult {
super::format_text(file_path, file_text, config)
let file_text = String::from_utf8(file_bytes)?;
super::format_text(file_path, &file_text, config).map(|maybe_text| maybe_text.map(|t| t.into_bytes()))
}
}

Expand Down
Loading