0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

perf(lsp): optimize formatting minified files (#20829)

This commit is contained in:
Nayeem Rahman 2023-10-09 04:39:52 +01:00 committed by GitHub
parent 2167a52d69
commit 35f028daf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 16 deletions

View file

@ -1651,11 +1651,11 @@ impl Inner {
}
// spawn a blocking task to allow doing other work while this is occurring
let format_result = deno_core::unsync::spawn_blocking({
let text_edits = deno_core::unsync::spawn_blocking({
let fmt_options = self.fmt_options.options.clone();
let document = document.clone();
move || {
match document.maybe_parsed_source() {
let format_result = match document.maybe_parsed_source() {
Some(Ok(parsed_source)) => {
format_parsed_source(&parsed_source, &fmt_options)
}
@ -1672,26 +1672,24 @@ impl Inner {
// it's not a js/ts file, so attempt to format its contents
format_file(&file_path, &document.content(), &fmt_options)
}
};
match format_result {
Ok(Some(new_text)) => Some(text::get_edits(
&document.content(),
&new_text,
document.line_index().as_ref(),
)),
Ok(None) => Some(Vec::new()),
Err(err) => {
lsp_warn!("Format error: {:#}", err);
None
}
}
}
})
.await
.unwrap();
let text_edits = match format_result {
Ok(Some(new_text)) => Some(text::get_edits(
&document.content(),
&new_text,
document.line_index().as_ref(),
)),
Ok(None) => Some(Vec::new()),
Err(err) => {
// TODO(lucacasonato): handle error properly
lsp_warn!("Format error: {:#}", err);
None
}
};
self.performance.measure(mark);
if let Some(text_edits) = text_edits {
if text_edits.is_empty() {

View file

@ -210,6 +210,18 @@ pub fn get_edits(a: &str, b: &str, line_index: &LineIndex) -> Vec<TextEdit> {
if a == b {
return vec![];
}
// Heuristic to detect things like minified files. `diff()` is expensive.
if b.chars().filter(|c| *c == '\n').count()
> line_index.utf8_offsets.len() * 3
{
return vec![TextEdit {
range: lsp::Range {
start: lsp::Position::new(0, 0),
end: line_index.position_utf16(TextSize::from(a.len() as u32)),
},
new_text: b.to_string(),
}];
}
let chunks = diff(a, b);
let mut text_edits = Vec::<TextEdit>::new();
let mut iter = chunks.iter().peekable();