diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-07-20 09:28:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-20 15:28:40 +0200 |
commit | 1ee6218e488b277eeb5b80f87dbea97ebaf5dcb2 (patch) | |
tree | 1f92ee990e9616933b45af46aaeb37b661845e9e /cli | |
parent | 0c3bbf7acdeacdd1c36b4b847679d85ca19962dc (diff) |
perf(lsp): format in a blocking task (#19883)
Diffstat (limited to 'cli')
-rw-r--r-- | cli/lsp/language_server.rs | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 0f1fc2460..6dead0fcf 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -1638,24 +1638,33 @@ impl Inner { return Ok(None); } - let format_result = match document.maybe_parsed_source() { - Some(Ok(parsed_source)) => { - format_parsed_source(&parsed_source, &self.fmt_options.options) - } - Some(Err(err)) => Err(anyhow!("{}", err)), - None => { - // the file path is only used to determine what formatter should - // be used to format the file, so give the filepath an extension - // that matches what the user selected as the language - let file_path = document - .maybe_language_id() - .and_then(|id| id.as_extension()) - .map(|ext| file_path.with_extension(ext)) - .unwrap_or(file_path); - // it's not a js/ts file, so attempt to format its contents - format_file(&file_path, &document.content(), &self.fmt_options.options) + // spawn a blocking task to allow doing other work while this is occurring + let format_result = deno_core::task::spawn_blocking({ + let fmt_options = self.fmt_options.options.clone(); + let document = document.clone(); + move || { + match document.maybe_parsed_source() { + Some(Ok(parsed_source)) => { + format_parsed_source(&parsed_source, &fmt_options) + } + Some(Err(err)) => Err(anyhow!("{}", err)), + None => { + // the file path is only used to determine what formatter should + // be used to format the file, so give the filepath an extension + // that matches what the user selected as the language + let file_path = document + .maybe_language_id() + .and_then(|id| id.as_extension()) + .map(|ext| file_path.with_extension(ext)) + .unwrap_or(file_path); + // it's not a js/ts file, so attempt to format its contents + format_file(&file_path, &document.content(), &fmt_options) + } + } } - }; + }) + .await + .unwrap(); let text_edits = match format_result { Ok(Some(new_text)) => Some(text::get_edits( |