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

wire up CancellationToken

This commit is contained in:
Bartek Iwańczuk 2025-01-03 23:52:51 +01:00
parent ebb6b60012
commit 7b3cdf1322
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
5 changed files with 49 additions and 9 deletions

View file

@ -36,6 +36,7 @@ use node_resolver::ResolutionMode;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use text_lines::LineAndColumnIndex; use text_lines::LineAndColumnIndex;
use tokio_util::sync::CancellationToken;
use tower_lsp::lsp_types as lsp; use tower_lsp::lsp_types as lsp;
use tower_lsp::lsp_types::Position; use tower_lsp::lsp_types::Position;
use tower_lsp::lsp_types::Range; use tower_lsp::lsp_types::Range;
@ -184,8 +185,9 @@ fn as_lsp_range(
pub fn get_lint_references( pub fn get_lint_references(
parsed_source: &deno_ast::ParsedSource, parsed_source: &deno_ast::ParsedSource,
linter: &CliLinter, linter: &CliLinter,
token: CancellationToken,
) -> Result<Vec<Reference>, AnyError> { ) -> Result<Vec<Reference>, AnyError> {
let lint_diagnostics = linter.lint_with_ast(parsed_source); let lint_diagnostics = linter.lint_with_ast(parsed_source, token);
Ok( Ok(
lint_diagnostics lint_diagnostics

View file

@ -851,6 +851,7 @@ fn generate_lint_diagnostics(
&document, &document,
&lint_config, &lint_config,
&linter, &linter,
token.clone(),
), ),
}, },
}); });
@ -862,6 +863,7 @@ fn generate_document_lint_diagnostics(
document: &Document, document: &Document,
lint_config: &LintConfig, lint_config: &LintConfig,
linter: &CliLinter, linter: &CliLinter,
token: CancellationToken,
) -> Vec<lsp::Diagnostic> { ) -> Vec<lsp::Diagnostic> {
if !lint_config.files.matches_specifier(document.specifier()) { if !lint_config.files.matches_specifier(document.specifier()) {
return Vec::new(); return Vec::new();
@ -869,7 +871,7 @@ fn generate_document_lint_diagnostics(
match document.maybe_parsed_source() { match document.maybe_parsed_source() {
Some(Ok(parsed_source)) => { Some(Ok(parsed_source)) => {
if let Ok(references) = if let Ok(references) =
analysis::get_lint_references(parsed_source, linter) analysis::get_lint_references(parsed_source, linter, token)
{ {
references references
.into_iter() .into_iter()

View file

@ -51,6 +51,14 @@ pub struct LintPluginContainer {
} }
impl LintPluginContainer { impl LintPluginContainer {
pub fn set_cancellation_token(
&mut self,
maybe_token: Option<CancellationToken>,
) {
let token = maybe_token.unwrap_or_default();
self.token = token;
}
pub fn set_info_for_file( pub fn set_info_for_file(
&mut self, &mut self,
specifier: ModuleSpecifier, specifier: ModuleSpecifier,

View file

@ -5,6 +5,7 @@ use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use ::tokio_util::sync::CancellationToken;
use deno_ast::MediaType; use deno_ast::MediaType;
use deno_ast::ModuleSpecifier; use deno_ast::ModuleSpecifier;
use deno_ast::ParsedSource; use deno_ast::ParsedSource;
@ -96,6 +97,7 @@ impl CliLinter {
pub fn lint_with_ast( pub fn lint_with_ast(
&self, &self,
parsed_source: &ParsedSource, parsed_source: &ParsedSource,
token: CancellationToken,
) -> Vec<LintDiagnostic> { ) -> Vec<LintDiagnostic> {
// TODO(bartlomieju): surface error is running plugin fails // TODO(bartlomieju): surface error is running plugin fails
let external_linter: Option<ExternalLinterCb> = let external_linter: Option<ExternalLinterCb> =
@ -103,7 +105,12 @@ impl CliLinter {
Some(Arc::new(move |parsed_source: ParsedSource| { Some(Arc::new(move |parsed_source: ParsedSource| {
// TODO: clean this up // TODO: clean this up
let file_path = parsed_source.specifier().to_file_path().unwrap(); let file_path = parsed_source.specifier().to_file_path().unwrap();
run_plugins(plugin_runner.clone(), parsed_source, file_path) run_plugins(
plugin_runner.clone(),
parsed_source,
file_path,
Some(token.clone()),
)
})) }))
} else { } else {
None None
@ -137,7 +144,7 @@ impl CliLinter {
Some(Arc::new(move |parsed_source: ParsedSource| { Some(Arc::new(move |parsed_source: ParsedSource| {
// TODO: clean this up // TODO: clean this up
let file_path = parsed_source.specifier().to_file_path().unwrap(); let file_path = parsed_source.specifier().to_file_path().unwrap();
run_plugins(plugin_runner.clone(), parsed_source, file_path) run_plugins(plugin_runner.clone(), parsed_source, file_path, None)
})) }))
} else { } else {
None None
@ -315,6 +322,7 @@ fn run_plugins(
plugin_runner: Arc<Mutex<PluginHostProxy>>, plugin_runner: Arc<Mutex<PluginHostProxy>>,
parsed_source: ParsedSource, parsed_source: ParsedSource,
file_path: PathBuf, file_path: PathBuf,
maybe_token: Option<CancellationToken>,
) -> Result<ExternalLinterResult, AnyError> { ) -> Result<ExternalLinterResult, AnyError> {
let source_text_info = parsed_source.text_info_lazy().clone(); let source_text_info = parsed_source.text_info_lazy().clone();
let plugin_info = plugin_runner.lock().get_plugin_rules(); let plugin_info = plugin_runner.lock().get_plugin_rules();
@ -329,6 +337,7 @@ fn run_plugins(
&file_path, &file_path,
serialized_ast, serialized_ast,
source_text_info, source_text_info,
maybe_token,
) )
.await .await
} }

View file

@ -5,6 +5,7 @@ use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use ::tokio_util::sync::CancellationToken;
use deno_ast::ModuleSpecifier; use deno_ast::ModuleSpecifier;
use deno_ast::ParsedSource; use deno_ast::ParsedSource;
use deno_ast::SourceTextInfo; use deno_ast::SourceTextInfo;
@ -34,11 +35,13 @@ use crate::tools::lint::serialize_ast_to_buffer;
#[derive(Debug)] #[derive(Debug)]
pub enum PluginHostRequest { pub enum PluginHostRequest {
// TODO: write to structs
LoadPlugins(Vec<ModuleSpecifier>, Option<Vec<String>>), LoadPlugins(Vec<ModuleSpecifier>, Option<Vec<String>>),
Run(Vec<u8>, PathBuf, SourceTextInfo), Run(Vec<u8>, PathBuf, SourceTextInfo, Option<CancellationToken>),
} }
pub enum PluginHostResponse { pub enum PluginHostResponse {
// TODO: write to structs
LoadPlugin(Result<Vec<PluginInfo>, AnyError>), LoadPlugin(Result<Vec<PluginInfo>, AnyError>),
Run(Result<Vec<LintDiagnostic>, AnyError>), Run(Result<Vec<LintDiagnostic>, AnyError>),
} }
@ -276,10 +279,20 @@ impl PluginHost {
let r = self.load_plugins(specifiers, exclude).await; let r = self.load_plugins(specifiers, exclude).await;
let _ = self.tx.send(PluginHostResponse::LoadPlugin(r)).await; let _ = self.tx.send(PluginHostResponse::LoadPlugin(r)).await;
} }
PluginHostRequest::Run(serialized_ast, specifier, source_text_info) => { PluginHostRequest::Run(
serialized_ast,
specifier,
source_text_info,
maybe_token,
) => {
let start = std::time::Instant::now(); let start = std::time::Instant::now();
let r = match self let r = match self
.run_plugins(&specifier, serialized_ast, source_text_info) .run_plugins(
&specifier,
serialized_ast,
source_text_info,
maybe_token,
)
.await .await
{ {
Ok(()) => Ok(self.take_diagnostics()), Ok(()) => Ok(self.take_diagnostics()),
@ -309,14 +322,17 @@ impl PluginHost {
specifier: &Path, specifier: &Path,
serialized_ast: Vec<u8>, serialized_ast: Vec<u8>,
source_text_info: SourceTextInfo, source_text_info: SourceTextInfo,
maybe_token: Option<CancellationToken>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
{ {
let state = self.worker.js_runtime.op_state(); let state = self.worker.js_runtime.op_state();
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
state.borrow_mut::<LintPluginContainer>().set_info_for_file( let container = state.borrow_mut::<LintPluginContainer>();
container.set_info_for_file(
ModuleSpecifier::from_file_path(specifier).unwrap(), ModuleSpecifier::from_file_path(specifier).unwrap(),
source_text_info, source_text_info,
); );
container.set_cancellation_token(maybe_token);
} }
let (file_name_v8, ast_uint8arr_v8) = { let (file_name_v8, ast_uint8arr_v8) = {
@ -452,6 +468,7 @@ impl PluginHostProxy {
specifier: &Path, specifier: &Path,
serialized_ast: Vec<u8>, serialized_ast: Vec<u8>,
source_text_info: SourceTextInfo, source_text_info: SourceTextInfo,
maybe_token: Option<CancellationToken>,
) -> Result<Vec<LintDiagnostic>, AnyError> { ) -> Result<Vec<LintDiagnostic>, AnyError> {
self self
.tx .tx
@ -459,6 +476,7 @@ impl PluginHostProxy {
serialized_ast, serialized_ast,
specifier.to_path_buf(), specifier.to_path_buf(),
source_text_info, source_text_info,
maybe_token,
)) ))
.await?; .await?;
let mut rx = self.rx.lock().await; let mut rx = self.rx.lock().await;
@ -498,9 +516,10 @@ pub async fn run_rules_for_ast(
specifier: &Path, specifier: &Path,
serialized_ast: Vec<u8>, serialized_ast: Vec<u8>,
source_text_info: SourceTextInfo, source_text_info: SourceTextInfo,
maybe_token: Option<CancellationToken>,
) -> Result<Vec<LintDiagnostic>, AnyError> { ) -> Result<Vec<LintDiagnostic>, AnyError> {
let d = host_proxy let d = host_proxy
.run_rules(specifier, serialized_ast, source_text_info) .run_rules(specifier, serialized_ast, source_text_info, maybe_token)
.await?; .await?;
Ok(d) Ok(d)
} }