0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-06 11:18:57 -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 regex::Regex;
use text_lines::LineAndColumnIndex;
use tokio_util::sync::CancellationToken;
use tower_lsp::lsp_types as lsp;
use tower_lsp::lsp_types::Position;
use tower_lsp::lsp_types::Range;
@ -184,8 +185,9 @@ fn as_lsp_range(
pub fn get_lint_references(
parsed_source: &deno_ast::ParsedSource,
linter: &CliLinter,
token: CancellationToken,
) -> Result<Vec<Reference>, AnyError> {
let lint_diagnostics = linter.lint_with_ast(parsed_source);
let lint_diagnostics = linter.lint_with_ast(parsed_source, token);
Ok(
lint_diagnostics

View file

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

View file

@ -51,6 +51,14 @@ pub struct 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(
&mut self,
specifier: ModuleSpecifier,

View file

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

View file

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