mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 20:29:11 +00:00
perf(lsp): optimize formatting minified files (#20829)
This commit is contained in:
parent
2167a52d69
commit
35f028daf2
2 changed files with 26 additions and 16 deletions
|
@ -1651,11 +1651,11 @@ impl Inner {
|
||||||
}
|
}
|
||||||
|
|
||||||
// spawn a blocking task to allow doing other work while this is occurring
|
// 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 fmt_options = self.fmt_options.options.clone();
|
||||||
let document = document.clone();
|
let document = document.clone();
|
||||||
move || {
|
move || {
|
||||||
match document.maybe_parsed_source() {
|
let format_result = match document.maybe_parsed_source() {
|
||||||
Some(Ok(parsed_source)) => {
|
Some(Ok(parsed_source)) => {
|
||||||
format_parsed_source(&parsed_source, &fmt_options)
|
format_parsed_source(&parsed_source, &fmt_options)
|
||||||
}
|
}
|
||||||
|
@ -1672,13 +1672,8 @@ impl Inner {
|
||||||
// it's not a js/ts file, so attempt to format its contents
|
// it's not a js/ts file, so attempt to format its contents
|
||||||
format_file(&file_path, &document.content(), &fmt_options)
|
format_file(&file_path, &document.content(), &fmt_options)
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
match format_result {
|
||||||
})
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let text_edits = match format_result {
|
|
||||||
Ok(Some(new_text)) => Some(text::get_edits(
|
Ok(Some(new_text)) => Some(text::get_edits(
|
||||||
&document.content(),
|
&document.content(),
|
||||||
&new_text,
|
&new_text,
|
||||||
|
@ -1686,11 +1681,14 @@ impl Inner {
|
||||||
)),
|
)),
|
||||||
Ok(None) => Some(Vec::new()),
|
Ok(None) => Some(Vec::new()),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
// TODO(lucacasonato): handle error properly
|
|
||||||
lsp_warn!("Format error: {:#}", err);
|
lsp_warn!("Format error: {:#}", err);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
self.performance.measure(mark);
|
self.performance.measure(mark);
|
||||||
if let Some(text_edits) = text_edits {
|
if let Some(text_edits) = text_edits {
|
||||||
|
|
|
@ -210,6 +210,18 @@ pub fn get_edits(a: &str, b: &str, line_index: &LineIndex) -> Vec<TextEdit> {
|
||||||
if a == b {
|
if a == b {
|
||||||
return vec![];
|
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 chunks = diff(a, b);
|
||||||
let mut text_edits = Vec::<TextEdit>::new();
|
let mut text_edits = Vec::<TextEdit>::new();
|
||||||
let mut iter = chunks.iter().peekable();
|
let mut iter = chunks.iter().peekable();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue