diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-05-20 16:40:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 16:40:55 -0400 |
commit | 1fcecb6789c3f111bc1554766ba9347afcfd02dc (patch) | |
tree | 19cd1b121412b992994b2fe4bea0463793d3986e /cli/lsp | |
parent | e7c894e8f54ebd2d9fd61c97a265906ac54e2068 (diff) |
refactor: upgrade to deno_ast 0.15 (#14680)
Diffstat (limited to 'cli/lsp')
-rw-r--r-- | cli/lsp/analysis.rs | 28 | ||||
-rw-r--r-- | cli/lsp/code_lens.rs | 45 | ||||
-rw-r--r-- | cli/lsp/completions.rs | 15 | ||||
-rw-r--r-- | cli/lsp/diagnostics.rs | 2 | ||||
-rw-r--r-- | cli/lsp/documents.rs | 50 | ||||
-rw-r--r-- | cli/lsp/language_server.rs | 9 | ||||
-rw-r--r-- | cli/lsp/repl.rs | 42 | ||||
-rw-r--r-- | cli/lsp/testing/collectors.rs | 67 | ||||
-rw-r--r-- | cli/lsp/testing/definitions.rs | 33 | ||||
-rw-r--r-- | cli/lsp/testing/execution.rs | 6 | ||||
-rw-r--r-- | cli/lsp/testing/server.rs | 2 | ||||
-rw-r--r-- | cli/lsp/tsc.rs | 8 |
12 files changed, 144 insertions, 163 deletions
diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs index bbce1966b..8e9b4ccaa 100644 --- a/cli/lsp/analysis.rs +++ b/cli/lsp/analysis.rs @@ -9,6 +9,8 @@ use crate::config_file::LintConfig; use crate::tools::lint::create_linter; use crate::tools::lint::get_configured_rules; +use deno_ast::SourceRange; +use deno_ast::SourceRangedForSpanned; use deno_ast::SourceTextInfo; use deno_core::anyhow::anyhow; use deno_core::error::custom_error; @@ -468,8 +470,8 @@ impl CodeActionCollection { // Get the end position of the comment. let line = maybe_parsed_source .unwrap() - .source() - .line_and_column_index(ignore_comment.span.hi()); + .text_info() + .line_and_column_index(ignore_comment.end()); let position = lsp::Position { line: line.line_index as u32, character: line.column_index as u32, @@ -719,6 +721,24 @@ fn prepend_whitespace(content: String, line_content: Option<String>) -> String { } } +pub fn source_range_to_lsp_range( + range: &SourceRange, + source_text_info: &SourceTextInfo, +) -> lsp::Range { + let start = source_text_info.line_and_column_index(range.start); + let end = source_text_info.line_and_column_index(range.end); + lsp::Range { + start: lsp::Position { + line: start.line_index as u32, + character: start.column_index as u32, + }, + end: lsp::Position { + line: end.line_index as u32, + character: end.column_index as u32, + }, + } +} + #[cfg(test)] mod tests { use super::*; @@ -787,12 +807,12 @@ mod tests { start: deno_lint::diagnostic::Position { line_index: 0, column_index: 2, - byte_pos: 23, + byte_index: 23, }, end: deno_lint::diagnostic::Position { line_index: 1, column_index: 0, - byte_pos: 33, + byte_index: 33, }, }; let actual = as_lsp_range(&fixture); diff --git a/cli/lsp/code_lens.rs b/cli/lsp/code_lens.rs index 725051e3c..a4668fd2d 100644 --- a/cli/lsp/code_lens.rs +++ b/cli/lsp/code_lens.rs @@ -1,5 +1,6 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use super::analysis::source_range_to_lsp_range; use super::config::Config; use super::config::WorkspaceSettings; use super::language_server; @@ -8,10 +9,11 @@ use super::tsc; use super::tsc::NavigationTree; use deno_ast::swc::ast; -use deno_ast::swc::common::Span; use deno_ast::swc::visit::Visit; use deno_ast::swc::visit::VisitWith; use deno_ast::ParsedSource; +use deno_ast::SourceRange; +use deno_ast::SourceRangedForSpanned; use deno_core::error::AnyError; use deno_core::resolve_url; use deno_core::serde::Deserialize; @@ -48,21 +50,6 @@ pub struct CodeLensData { pub specifier: ModuleSpecifier, } -fn span_to_range(span: &Span, parsed_source: &ParsedSource) -> lsp::Range { - let start = parsed_source.source().line_and_column_index(span.lo); - let end = parsed_source.source().line_and_column_index(span.hi); - lsp::Range { - start: lsp::Position { - line: start.line_index as u32, - character: start.column_index as u32, - }, - end: lsp::Position { - line: end.line_index as u32, - character: end.column_index as u32, - }, - } -} - struct DenoTestCollector { code_lenses: Vec<lsp::CodeLens>, parsed_source: ParsedSource, @@ -80,8 +67,9 @@ impl DenoTestCollector { } } - fn add_code_lenses<N: AsRef<str>>(&mut self, name: N, span: &Span) { - let range = span_to_range(span, &self.parsed_source); + fn add_code_lenses<N: AsRef<str>>(&mut self, name: N, range: &SourceRange) { + let range = + source_range_to_lsp_range(range, self.parsed_source.text_info()); self.add_code_lens(&name, range, "▶\u{fe0e} Run Test", false); self.add_code_lens(&name, range, "Debug", true); } @@ -111,7 +99,7 @@ impl DenoTestCollector { }); } - fn check_call_expr(&mut self, node: &ast::CallExpr, span: &Span) { + fn check_call_expr(&mut self, node: &ast::CallExpr, range: &SourceRange) { if let Some(expr) = node.args.get(0).map(|es| es.expr.as_ref()) { match expr { ast::Expr::Object(obj_lit) => { @@ -126,7 +114,7 @@ impl DenoTestCollector { key_value_prop.value.as_ref() { let name = lit_str.value.to_string(); - self.add_code_lenses(name, span); + self.add_code_lenses(name, range); } } } @@ -137,12 +125,12 @@ impl DenoTestCollector { ast::Expr::Fn(fn_expr) => { if let Some(ast::Ident { sym, .. }) = fn_expr.ident.as_ref() { let name = sym.to_string(); - self.add_code_lenses(name, span); + self.add_code_lenses(name, range); } } ast::Expr::Lit(ast::Lit::Str(lit_str)) => { let name = lit_str.value.to_string(); - self.add_code_lenses(name, span); + self.add_code_lenses(name, range); } _ => (), } @@ -161,7 +149,7 @@ impl Visit for DenoTestCollector { match callee_expr.as_ref() { ast::Expr::Ident(ident) => { if self.test_vars.contains(&ident.sym.to_string()) { - self.check_call_expr(node, &ident.span); + self.check_call_expr(node, &ident.range()); } } ast::Expr::Member(member_expr) => { @@ -169,7 +157,7 @@ impl Visit for DenoTestCollector { if ns_prop_ident.sym.to_string() == "test" { if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() { if ident.sym.to_string() == "Deno" { - self.check_call_expr(node, &ns_prop_ident.span); + self.check_call_expr(node, &ns_prop_ident.range()); } } } @@ -557,8 +545,7 @@ mod tests { #[test] fn test_deno_test_collector() { let specifier = resolve_url("https://deno.land/x/mod.ts").unwrap(); - let source = Arc::new( - r#" + let source = r#" Deno.test({ name: "test a", fn() {} @@ -567,12 +554,10 @@ mod tests { Deno.test(function useFnName() {}); Deno.test("test b", function anotherTest() {}); - "# - .to_string(), - ); + "#; let parsed_module = deno_ast::parse_module(deno_ast::ParseParams { specifier: specifier.to_string(), - source: SourceTextInfo::new(source), + text_info: SourceTextInfo::new(source.into()), media_type: MediaType::TypeScript, capture_tokens: true, scope_analysis: true, diff --git a/cli/lsp/completions.rs b/cli/lsp/completions.rs index b05d7bfb9..b3e338faf 100644 --- a/cli/lsp/completions.rs +++ b/cli/lsp/completions.rs @@ -97,13 +97,15 @@ fn to_narrow_lsp_range( text_info: &SourceTextInfo, range: &deno_graph::Range, ) -> lsp::Range { - let end_byte_index = text_info.byte_index(LineAndColumnIndex { - line_index: range.end.line, - column_index: range.end.character, - }); + let end_byte_index = text_info + .loc_to_source_pos(LineAndColumnIndex { + line_index: range.end.line, + column_index: range.end.character, + }) + .as_byte_index(text_info.range().start); let text_bytes = text_info.text_str().as_bytes(); let has_trailing_quote = - matches!(text_bytes[end_byte_index.0 as usize - 1], b'"' | b'\''); + matches!(text_bytes[end_byte_index - 1], b'"' | b'\''); lsp::Range { start: lsp::Position { line: range.start.line as u32, @@ -577,7 +579,6 @@ mod tests { use deno_graph::Range; use std::collections::HashMap; use std::path::Path; - use std::sync::Arc; use test_util::TempDir; fn mock_documents( @@ -593,7 +594,7 @@ mod tests { specifier.clone(), *version, language_id.clone(), - Arc::new(source.to_string()), + (*source).into(), ); } let http_cache = HttpCache::new(location); diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index b1ce06dcb..db613c9fd 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -898,7 +898,7 @@ mod tests { specifier.clone(), *version, language_id.clone(), - Arc::new(source.to_string()), + (*source).into(), ); } StateSnapshot { diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index 4fb8c428e..ec1e183ae 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -81,12 +81,12 @@ impl deno_graph::SourceParser for SourceParser { fn parse_module( &self, specifier: &ModuleSpecifier, - source: Arc<String>, + source: Arc<str>, media_type: MediaType, ) -> Result<deno_ast::ParsedSource, deno_ast::Diagnostic> { deno_ast::parse_module(deno_ast::ParseParams { specifier: specifier.to_string(), - source: SourceTextInfo::new(source), + text_info: SourceTextInfo::new(source), media_type, capture_tokens: true, scope_analysis: true, @@ -179,7 +179,7 @@ impl AssetOrDocument { } } - pub fn text(&self) -> Arc<String> { + pub fn text(&self) -> Arc<str> { match self { AssetOrDocument::Asset(a) => a.text(), AssetOrDocument::Document(d) => d.0.text_info.text(), @@ -254,7 +254,7 @@ impl Document { specifier: ModuleSpecifier, fs_version: String, maybe_headers: Option<&HashMap<String, String>>, - content: Arc<String>, + content: Arc<str>, maybe_resolver: Option<&dyn deno_graph::source::Resolver>, ) -> Self { let parser = SourceParser::default(); @@ -293,7 +293,7 @@ impl Document { specifier: ModuleSpecifier, version: i32, language_id: LanguageId, - content: Arc<String>, + content: Arc<str>, maybe_resolver: Option<&dyn deno_graph::source::Resolver>, ) -> Self { let maybe_headers = language_id.as_headers(); @@ -352,7 +352,7 @@ impl Document { index_valid = IndexValid::UpTo(0); } } - let content = Arc::new(content); + let content: Arc<str> = content.into(); let maybe_module = if self .0 .maybe_language_id @@ -413,7 +413,7 @@ impl Document { &self.0.specifier } - pub fn content(&self) -> Arc<String> { + pub fn content(&self) -> Arc<str> { self.0.text_info.text() } @@ -661,12 +661,12 @@ impl FileSystemDocuments { let doc = if specifier.scheme() == "file" { let maybe_charset = Some(text_encoding::detect_charset(&bytes).to_string()); - let content = Arc::new(get_source_from_bytes(bytes, maybe_charset).ok()?); + let content = get_source_from_bytes(bytes, maybe_charset).ok()?; Document::new( specifier.clone(), fs_version, None, - content, + content.into(), maybe_resolver, ) } else { @@ -677,12 +677,12 @@ impl FileSystemDocuments { specifier_metadata.headers.get("content-type").cloned(); let maybe_headers = Some(&specifier_metadata.headers); let (_, maybe_charset) = map_content_type(specifier, maybe_content_type); - let content = Arc::new(get_source_from_bytes(bytes, maybe_charset).ok()?); + let content = get_source_from_bytes(bytes, maybe_charset).ok()?; Document::new( specifier.clone(), fs_version, maybe_headers, - content, + content.into(), maybe_resolver, ) }; @@ -752,7 +752,7 @@ impl Documents { specifier: ModuleSpecifier, version: i32, language_id: LanguageId, - content: Arc<String>, + content: Arc<str>, ) -> Document { let maybe_resolver = self.get_maybe_resolver(); let document = Document::open( @@ -1154,14 +1154,15 @@ mod tests { let temp_dir = TempDir::new(); let (mut documents, _) = setup(&temp_dir); let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap(); - let content = Arc::new( - r#"import * as b from "./b.ts"; + let content = r#"import * as b from "./b.ts"; console.log(b); -"# - .to_string(), +"#; + let document = documents.open( + specifier, + 1, + "javascript".parse().unwrap(), + content.into(), ); - let document = - documents.open(specifier, 1, "javascript".parse().unwrap(), content); assert!(document.is_open()); assert!(document.is_diagnosable()); } @@ -1171,17 +1172,14 @@ console.log(b); let temp_dir = TempDir::new(); let (mut documents, _) = setup(&temp_dir); let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap(); - let content = Arc::new( - r#"import * as b from "./b.ts"; + let content = r#"import * as b from "./b.ts"; console.log(b); -"# - .to_string(), - ); +"#; documents.open( specifier.clone(), 1, "javascript".parse().unwrap(), - content, + content.into(), ); documents .change( @@ -1204,7 +1202,7 @@ console.log(b); ) .unwrap(); assert_eq!( - documents.get(&specifier).unwrap().content().as_str(), + &*documents.get(&specifier).unwrap().content(), r#"import * as b from "./b.ts"; console.log(b, "hello deno"); "# @@ -1227,7 +1225,7 @@ console.log(b, "hello deno"); file_specifier.clone(), 1, LanguageId::TypeScript, - Default::default(), + "".into(), ); // make a clone of the document store and close the document in that one diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 9b82aea27..481e6cadc 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -853,12 +853,11 @@ impl Inner { params.text_document.language_id, params.text_document.uri ); } - let content = Arc::new(params.text_document.text); let document = self.documents.open( specifier.clone(), params.text_document.version, params.text_document.language_id.parse().unwrap(), - content, + params.text_document.text.into(), ); self.performance.measure(mark); @@ -1106,13 +1105,13 @@ impl Inner { Some(Err(err)) => Err(anyhow!("{}", err)), None => { // it's not a js/ts file, so attempt to format its contents - format_file(&file_path, document.content().as_str(), &fmt_options) + format_file(&file_path, &document.content(), &fmt_options) } }; match format_result { Ok(Some(new_text)) => Some(text::get_edits( - document.content().as_str(), + &document.content(), &new_text, document.line_index().as_ref(), )), @@ -1931,7 +1930,7 @@ impl Inner { .map(|span| { span.to_folding_range( asset_or_doc.line_index(), - asset_or_doc.text().as_str().as_bytes(), + asset_or_doc.text().as_bytes(), self.config.client_capabilities.line_folding_only, ) }) diff --git a/cli/lsp/repl.rs b/cli/lsp/repl.rs index d937b0273..5cb747204 100644 --- a/cli/lsp/repl.rs +++ b/cli/lsp/repl.rs @@ -2,8 +2,6 @@ use std::collections::HashMap; -use deno_ast::swc::common::BytePos; -use deno_ast::swc::common::Span; use deno_ast::LineAndColumnIndex; use deno_ast::ModuleSpecifier; use deno_ast::SourceTextInfo; @@ -42,7 +40,7 @@ use super::config::WorkspaceSettings; #[derive(Debug)] pub struct ReplCompletionItem { pub new_text: String, - pub span: Span, + pub range: std::ops::Range<usize>, } pub struct ReplLanguageServer { @@ -113,12 +111,12 @@ impl ReplLanguageServer { position: usize, ) -> Vec<ReplCompletionItem> { self.did_change(line_text).await; - let before_line_len = BytePos(self.document_text.len() as u32); - let position = before_line_len + BytePos(position as u32); let text_info = deno_ast::SourceTextInfo::from_string(format!( "{}{}", self.document_text, self.pending_text )); + let before_line_len = self.document_text.len(); + let position = text_info.range().start + before_line_len + position; let line_and_column = text_info.line_and_column_index(position); let response = self .language_server @@ -158,24 +156,20 @@ impl ReplLanguageServer { item.text_edit.and_then(|edit| match edit { CompletionTextEdit::Edit(edit) => Some(ReplCompletionItem { new_text: edit.new_text, - span: lsp_range_to_span(&text_info, &edit.range), + range: lsp_range_to_std_range(&text_info, &edit.range), }), CompletionTextEdit::InsertAndReplace(_) => None, }) }) .filter(|item| { // filter the results to only exact matches - let text = &text_info.text_str() - [item.span.lo.0 as usize..item.span.hi.0 as usize]; + let text = &text_info.text_str()[item.range.clone()]; item.new_text.starts_with(text) }) .map(|mut item| { // convert back to a line position - item.span = Span::new( - item.span.lo - before_line_len, - item.span.hi - before_line_len, - Default::default(), - ); + item.range.start -= before_line_len; + item.range.end -= before_line_len; item }) .collect() @@ -251,18 +245,24 @@ impl ReplLanguageServer { } } -fn lsp_range_to_span(text_info: &SourceTextInfo, range: &Range) -> Span { - Span::new( - text_info.byte_index(LineAndColumnIndex { +fn lsp_range_to_std_range( + text_info: &SourceTextInfo, + range: &Range, +) -> std::ops::Range<usize> { + let start_index = text_info + .loc_to_source_pos(LineAndColumnIndex { line_index: range.start.line as usize, column_index: range.start.character as usize, - }), - text_info.byte_index(LineAndColumnIndex { + }) + .as_byte_index(text_info.range().start); + let end_index = text_info + .loc_to_source_pos(LineAndColumnIndex { line_index: range.end.line as usize, column_index: range.end.character as usize, - }), - Default::default(), - ) + }) + .as_byte_index(text_info.range().start); + + start_index..end_index } fn get_cwd_uri() -> Result<ModuleSpecifier, AnyError> { diff --git a/cli/lsp/testing/collectors.rs b/cli/lsp/testing/collectors.rs index 33ad3a7ab..537dd5806 100644 --- a/cli/lsp/testing/collectors.rs +++ b/cli/lsp/testing/collectors.rs @@ -3,9 +3,10 @@ use super::definitions::TestDefinition; use deno_ast::swc::ast; -use deno_ast::swc::common::Span; use deno_ast::swc::visit::Visit; use deno_ast::swc::visit::VisitWith; +use deno_ast::SourceRange; +use deno_ast::SourceRangedForSpanned; use deno_core::ModuleSpecifier; use std::collections::HashSet; @@ -254,12 +255,12 @@ impl TestStepCollector { fn add_step<N: AsRef<str>>( &mut self, name: N, - span: &Span, + range: SourceRange, steps: Option<Vec<TestDefinition>>, ) { let step = TestDefinition::new_step( name.as_ref().to_string(), - *span, + range, self.parent.clone(), self.level, steps, @@ -267,11 +268,11 @@ impl TestStepCollector { self.steps.push(step); } - fn check_call_expr(&mut self, node: &ast::CallExpr, span: &Span) { + fn check_call_expr(&mut self, node: &ast::CallExpr, range: SourceRange) { if let Some((name, steps)) = check_call_expr(&self.parent, node, self.level + 1) { - self.add_step(name, span, steps); + self.add_step(name, range, steps); } } @@ -288,7 +289,7 @@ impl Visit for TestStepCollector { // Identify calls to identified variables ast::Expr::Ident(ident) => { if self.vars.contains(&ident.sym.to_string()) { - self.check_call_expr(node, &ident.span); + self.check_call_expr(node, ident.range()); } } // Identify calls to `test.step()` @@ -298,7 +299,7 @@ impl Visit for TestStepCollector { if ns_prop_ident.sym.eq("step") { if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() { if ident.sym == *test_context { - self.check_call_expr(node, &ns_prop_ident.span); + self.check_call_expr(node, ns_prop_ident.range()); } } } @@ -386,23 +387,23 @@ impl TestCollector { fn add_definition<N: AsRef<str>>( &mut self, name: N, - span: &Span, + range: SourceRange, steps: Option<Vec<TestDefinition>>, ) { let definition = TestDefinition::new( &self.specifier, name.as_ref().to_string(), - *span, + range, steps, ); self.definitions.push(definition); } - fn check_call_expr(&mut self, node: &ast::CallExpr, span: &Span) { + fn check_call_expr(&mut self, node: &ast::CallExpr, range: SourceRange) { if let Some((name, steps)) = check_call_expr(self.specifier.as_str(), node, 1) { - self.add_definition(name, span, steps); + self.add_definition(name, range, steps); } } @@ -418,7 +419,7 @@ impl Visit for TestCollector { match callee_expr.as_ref() { ast::Expr::Ident(ident) => { if self.vars.contains(&ident.sym.to_string()) { - self.check_call_expr(node, &ident.span); + self.check_call_expr(node, ident.range()); } } ast::Expr::Member(member_expr) => { @@ -426,7 +427,7 @@ impl Visit for TestCollector { if ns_prop_ident.sym.to_string() == "test" { if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() { if ident.sym.to_string() == "Deno" { - self.check_call_expr(node, &ns_prop_ident.span); + self.check_call_expr(node, ns_prop_ident.range()); } } } @@ -494,24 +495,20 @@ impl Visit for TestCollector { #[cfg(test)] pub mod tests { use super::*; - use deno_ast::swc::common::BytePos; - use deno_ast::swc::common::SyntaxContext; + use deno_ast::StartSourcePos; use deno_core::resolve_url; - use std::sync::Arc; - pub fn new_span(lo: u32, hi: u32, ctxt: u32) -> Span { - Span { - lo: BytePos(lo), - hi: BytePos(hi), - ctxt: SyntaxContext::from_u32(ctxt), - } + pub fn new_range(start: usize, end: usize) -> SourceRange { + SourceRange::new( + StartSourcePos::START_SOURCE_POS + start, + StartSourcePos::START_SOURCE_POS + end, + ) } #[test] fn test_test_collector() { let specifier = resolve_url("file:///a/example.ts").unwrap(); - let source = Arc::new( - r#" + let source = r#" Deno.test({ name: "test a", async fn(t) { @@ -535,13 +532,11 @@ pub mod tests { const t = Deno.test; t("test d", () => {}); - "# - .to_string(), - ); + "#; let parsed_module = deno_ast::parse_module(deno_ast::ParseParams { specifier: specifier.to_string(), - source: deno_ast::SourceTextInfo::new(source), + text_info: deno_ast::SourceTextInfo::new(source.into()), media_type: deno_ast::MediaType::TypeScript, capture_tokens: true, scope_analysis: true, @@ -557,19 +552,19 @@ pub mod tests { id: "cf31850c831233526df427cdfd25b6b84b2af0d6ce5f8ee1d22c465234b46348".to_string(), level: 0, name: "test a".to_string(), - span: new_span(12, 16, 0), + range: new_range(12, 16), steps: Some(vec![ TestDefinition { id: "4c7333a1e47721631224408c467f32751fe34b876cab5ec1f6ac71980ff15ad3".to_string(), level: 1, name: "a step".to_string(), - span: new_span(83, 87, 0), + range: new_range(83, 87), steps: Some(vec![ TestDefinition { id: "abf356f59139b77574089615f896a6f501c010985d95b8a93abeb0069ccb2201".to_string(), level: 2, name: "sub step".to_string(), - span: new_span(132, 136, 3), + range: new_range(132, 136), steps: None, } ]) @@ -580,13 +575,13 @@ pub mod tests { id: "86b4c821900e38fc89f24bceb0e45193608ab3f9d2a6019c7b6a5aceff5d7df2".to_string(), level: 0, name: "useFnName".to_string(), - span: new_span(254, 258, 0), + range: new_range(254, 258), steps: Some(vec![ TestDefinition { id: "67a390d0084ae5fb88f3510c470a72a553581f1d0d5ba5fa89aee7a754f3953a".to_string(), level: 1, name: "step c".to_string(), - span: new_span(313, 314, 4), + range: new_range(313, 314), steps: None, } ]) @@ -595,21 +590,21 @@ pub mod tests { id: "580eda89d7f5e619774c20e13b7d07a8e77c39cba101d60565144d48faa837cb".to_string(), level: 0, name: "test b".to_string(), - span: new_span(358, 362, 0), + range: new_range(358, 362), steps: None, }, TestDefinition { id: "0b7c6bf3cd617018d33a1bf982a08fe088c5bb54fcd5eb9e802e7c137ec1af94".to_string(), level: 0, name: "test c".to_string(), - span: new_span(420, 424, 1), + range: new_range(420, 424), steps: None, }, TestDefinition { id: "69d9fe87f64f5b66cb8b631d4fd2064e8224b8715a049be54276c42189ff8f9f".to_string(), level: 0, name: "test d".to_string(), - span: new_span(480, 481, 1), + range: new_range(480, 481), steps: None, } ] diff --git a/cli/lsp/testing/definitions.rs b/cli/lsp/testing/definitions.rs index aad667959..c810b6a25 100644 --- a/cli/lsp/testing/definitions.rs +++ b/cli/lsp/testing/definitions.rs @@ -3,38 +3,21 @@ use super::lsp_custom; use crate::checksum; +use crate::lsp::analysis::source_range_to_lsp_range; use crate::lsp::client::TestingNotification; -use deno_ast::swc::common::Span; +use deno_ast::SourceRange; use deno_ast::SourceTextInfo; use deno_core::ModuleSpecifier; use std::collections::HashMap; use tower_lsp::lsp_types as lsp; -fn span_to_range( - span: &Span, - source_text_info: &SourceTextInfo, -) -> Option<lsp::Range> { - let start = source_text_info.line_and_column_index(span.lo); - let end = source_text_info.line_and_column_index(span.hi); - Some(lsp::Range { - start: lsp::Position { - line: start.line_index as u32, - character: start.column_index as u32, - }, - end: lsp::Position { - line: end.line_index as u32, - character: end.column_index as u32, - }, - }) -} - #[derive(Debug, Clone, PartialEq)] pub struct TestDefinition { pub id: String, pub level: usize, pub name: String, - pub span: Span, + pub range: SourceRange, pub steps: Option<Vec<TestDefinition>>, } @@ -42,7 +25,7 @@ impl TestDefinition { pub fn new( specifier: &ModuleSpecifier, name: String, - span: Span, + range: SourceRange, steps: Option<Vec<TestDefinition>>, ) -> Self { let id = checksum::gen(&[specifier.as_str().as_bytes(), name.as_bytes()]); @@ -50,14 +33,14 @@ impl TestDefinition { id, level: 0, name, - span, + range, steps, } } pub fn new_step( name: String, - span: Span, + range: SourceRange, parent: String, level: usize, steps: Option<Vec<TestDefinition>>, @@ -71,7 +54,7 @@ impl TestDefinition { id, level, name, - span, + range, steps, } } @@ -89,7 +72,7 @@ impl TestDefinition { .map(|step| step.as_test_data(source_text_info)) .collect() }), - range: span_to_range(&self.span, source_text_info), + range: Some(source_range_to_lsp_range(&self.range, source_text_info)), } } diff --git a/cli/lsp/testing/execution.rs b/cli/lsp/testing/execution.rs index 358f9357b..4f4b9bf1f 100644 --- a/cli/lsp/testing/execution.rs +++ b/cli/lsp/testing/execution.rs @@ -917,7 +917,7 @@ impl test::TestReporter for LspTestReporter { #[cfg(test)] mod tests { use super::*; - use crate::lsp::testing::collectors::tests::new_span; + use crate::lsp::testing::collectors::tests::new_range; #[test] fn test_as_queue_and_filters() { @@ -949,7 +949,7 @@ mod tests { .to_string(), level: 0, name: "test a".to_string(), - span: new_span(420, 424, 1), + range: new_range(420, 424), steps: None, }; let test_def_b = TestDefinition { @@ -957,7 +957,7 @@ mod tests { .to_string(), level: 0, name: "test b".to_string(), - span: new_span(480, 481, 1), + range: new_range(480, 481), steps: None, }; let test_definitions = TestDefinitions { diff --git a/cli/lsp/testing/server.rs b/cli/lsp/testing/server.rs index ab8f99c0e..fbc835cc2 100644 --- a/cli/lsp/testing/server.rs +++ b/cli/lsp/testing/server.rs @@ -117,7 +117,7 @@ impl TestServer { test_definitions.as_notification( specifier, mru.as_ref(), - parsed_source.source(), + parsed_source.text_info(), ), ); } diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 1448c8c96..40efcb99a 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -154,7 +154,7 @@ impl TsServer { #[derive(Debug, Clone)] struct AssetDocumentInner { specifier: ModuleSpecifier, - text: Arc<String>, + text: Arc<str>, line_index: Arc<LineIndex>, maybe_navigation_tree: Option<Arc<NavigationTree>>, } @@ -169,7 +169,7 @@ impl AssetDocument { let text = text.as_ref(); Self(Arc::new(AssetDocumentInner { specifier, - text: Arc::new(text.to_string()), + text: text.into(), line_index: Arc::new(LineIndex::new(text)), maybe_navigation_tree: None, })) @@ -189,7 +189,7 @@ impl AssetDocument { })) } - pub fn text(&self) -> Arc<String> { + pub fn text(&self) -> Arc<str> { self.0.text.clone() } @@ -3114,7 +3114,7 @@ mod tests { specifier.clone(), *version, language_id.clone(), - Arc::new(source.to_string()), + (*source).into(), ); } StateSnapshot { |