summaryrefslogtreecommitdiff
path: root/cli/lsp/text.rs
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2023-10-09 04:39:52 +0100
committerGitHub <noreply@github.com>2023-10-09 04:39:52 +0100
commit35f028daf27bb40e86829e7b7cc19aa72a62c0a0 (patch)
tree6ab02d98771646b87197a40aa927c680158e7bab /cli/lsp/text.rs
parent2167a52d693e79c661f91d29164f59111f3bc3c9 (diff)
perf(lsp): optimize formatting minified files (#20829)
Diffstat (limited to 'cli/lsp/text.rs')
-rw-r--r--cli/lsp/text.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/cli/lsp/text.rs b/cli/lsp/text.rs
index 26df170ce..8013edd52 100644
--- a/cli/lsp/text.rs
+++ b/cli/lsp/text.rs
@@ -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();