2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-06-04 17:31:44 -04:00
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
use super::analysis::source_range_to_lsp_range;
|
2023-11-30 11:23:47 -05:00
|
|
|
use super::config::CodeLensSettings;
|
2021-06-04 17:31:44 -04:00
|
|
|
use super::language_server;
|
2021-09-07 10:39:32 -04:00
|
|
|
use super::text::LineIndex;
|
2021-06-04 17:31:44 -04:00
|
|
|
use super::tsc;
|
2021-09-07 10:39:32 -04:00
|
|
|
use super::tsc::NavigationTree;
|
2021-06-04 17:31:44 -04:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::swc::ast;
|
|
|
|
use deno_ast::swc::visit::Visit;
|
|
|
|
use deno_ast::swc::visit::VisitWith;
|
|
|
|
use deno_ast::ParsedSource;
|
2022-05-20 16:40:55 -04:00
|
|
|
use deno_ast::SourceRange;
|
|
|
|
use deno_ast::SourceRangedForSpanned;
|
2021-06-04 17:31:44 -04:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::resolve_url;
|
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2023-04-12 21:08:01 -04:00
|
|
|
use lazy_regex::lazy_regex;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2021-06-04 17:31:44 -04:00
|
|
|
use regex::Regex;
|
|
|
|
use std::cell::RefCell;
|
2021-06-07 07:38:07 -04:00
|
|
|
use std::collections::HashSet;
|
2021-06-04 17:31:44 -04:00
|
|
|
use std::rc::Rc;
|
2021-10-28 19:56:01 -04:00
|
|
|
use std::sync::Arc;
|
2022-04-03 00:17:30 -04:00
|
|
|
use tower_lsp::lsp_types as lsp;
|
2021-06-04 17:31:44 -04:00
|
|
|
|
2023-04-12 21:08:01 -04:00
|
|
|
static ABSTRACT_MODIFIER: Lazy<Regex> = lazy_regex!(r"\babstract\b");
|
2021-12-18 16:14:42 -05:00
|
|
|
|
2023-04-12 21:08:01 -04:00
|
|
|
static EXPORT_MODIFIER: Lazy<Regex> = lazy_regex!(r"\bexport\b");
|
2021-06-04 17:31:44 -04:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub enum CodeLensSource {
|
|
|
|
#[serde(rename = "implementations")]
|
|
|
|
Implementations,
|
|
|
|
#[serde(rename = "references")]
|
|
|
|
References,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CodeLensData {
|
|
|
|
pub source: CodeLensSource,
|
|
|
|
pub specifier: ModuleSpecifier,
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
struct DenoTestCollector {
|
2021-06-07 07:38:07 -04:00
|
|
|
code_lenses: Vec<lsp::CodeLens>,
|
2021-10-28 19:56:01 -04:00
|
|
|
parsed_source: ParsedSource,
|
2021-06-07 07:38:07 -04:00
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
test_vars: HashSet<String>,
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
impl DenoTestCollector {
|
|
|
|
pub fn new(specifier: ModuleSpecifier, parsed_source: ParsedSource) -> Self {
|
2021-06-07 07:38:07 -04:00
|
|
|
Self {
|
|
|
|
code_lenses: Vec::new(),
|
2021-09-07 10:39:32 -04:00
|
|
|
parsed_source,
|
2021-06-07 07:38:07 -04:00
|
|
|
specifier,
|
|
|
|
test_vars: HashSet::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
fn add_code_lenses<N: AsRef<str>>(&mut self, name: N, range: &SourceRange) {
|
|
|
|
let range =
|
2024-06-05 11:04:16 -04:00
|
|
|
source_range_to_lsp_range(range, self.parsed_source.text_info_lazy());
|
2021-12-20 00:00:38 -05:00
|
|
|
self.add_code_lens(&name, range, "▶\u{fe0e} Run Test", false);
|
|
|
|
self.add_code_lens(&name, range, "Debug", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_code_lens<N: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
name: &N,
|
|
|
|
range: lsp::Range,
|
|
|
|
title: &str,
|
|
|
|
inspect: bool,
|
|
|
|
) {
|
|
|
|
let options = json!({
|
|
|
|
"inspect": inspect,
|
|
|
|
});
|
2021-06-07 07:38:07 -04:00
|
|
|
self.code_lenses.push(lsp::CodeLens {
|
|
|
|
range,
|
|
|
|
command: Some(lsp::Command {
|
2021-12-20 00:00:38 -05:00
|
|
|
title: title.to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2021-12-20 00:00:38 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!(self.specifier),
|
|
|
|
json!(name.as_ref()),
|
|
|
|
options,
|
|
|
|
]),
|
2021-06-07 07:38:07 -04:00
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
fn check_call_expr(&mut self, node: &ast::CallExpr, range: &SourceRange) {
|
2024-01-01 17:22:48 -05:00
|
|
|
if let Some(expr) = node.args.first().map(|es| es.expr.as_ref()) {
|
2021-06-07 07:38:07 -04:00
|
|
|
match expr {
|
|
|
|
ast::Expr::Object(obj_lit) => {
|
|
|
|
for prop in &obj_lit.props {
|
|
|
|
if let ast::PropOrSpread::Prop(prop) = prop {
|
|
|
|
if let ast::Prop::KeyValue(key_value_prop) = prop.as_ref() {
|
2021-12-28 22:00:24 -05:00
|
|
|
if let ast::PropName::Ident(ast::Ident { sym, .. }) =
|
|
|
|
&key_value_prop.key
|
|
|
|
{
|
|
|
|
if sym == "name" {
|
2024-01-05 07:04:33 -05:00
|
|
|
match key_value_prop.value.as_ref() {
|
|
|
|
ast::Expr::Lit(ast::Lit::Str(lit_str)) => {
|
|
|
|
let name = lit_str.value.to_string();
|
|
|
|
self.add_code_lenses(name, range);
|
|
|
|
}
|
|
|
|
ast::Expr::Tpl(tpl) if tpl.quasis.len() == 1 => {
|
|
|
|
let name = tpl.quasis.first().unwrap().raw.to_string();
|
|
|
|
self.add_code_lenses(name, range);
|
|
|
|
}
|
|
|
|
_ => {}
|
2021-06-07 07:38:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 22:00:24 -05:00
|
|
|
ast::Expr::Fn(fn_expr) => {
|
|
|
|
if let Some(ast::Ident { sym, .. }) = fn_expr.ident.as_ref() {
|
|
|
|
let name = sym.to_string();
|
2022-05-20 16:40:55 -04:00
|
|
|
self.add_code_lenses(name, range);
|
2021-12-28 22:00:24 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-07 07:38:07 -04:00
|
|
|
ast::Expr::Lit(ast::Lit::Str(lit_str)) => {
|
|
|
|
let name = lit_str.value.to_string();
|
2022-05-20 16:40:55 -04:00
|
|
|
self.add_code_lenses(name, range);
|
2021-06-07 07:38:07 -04:00
|
|
|
}
|
2024-01-05 07:04:33 -05:00
|
|
|
ast::Expr::Tpl(tpl) if tpl.quasis.len() == 1 => {
|
|
|
|
let name = tpl.quasis.first().unwrap().raw.to_string();
|
|
|
|
self.add_code_lenses(name, range);
|
|
|
|
}
|
2021-06-07 07:38:07 -04:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Move out the code lenses from the collector.
|
|
|
|
fn take(self) -> Vec<lsp::CodeLens> {
|
|
|
|
self.code_lenses
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
impl Visit for DenoTestCollector {
|
2021-12-08 19:12:14 -05:00
|
|
|
fn visit_call_expr(&mut self, node: &ast::CallExpr) {
|
2022-01-13 11:58:00 -05:00
|
|
|
if let ast::Callee::Expr(callee_expr) = &node.callee {
|
2021-06-07 07:38:07 -04:00
|
|
|
match callee_expr.as_ref() {
|
|
|
|
ast::Expr::Ident(ident) => {
|
|
|
|
if self.test_vars.contains(&ident.sym.to_string()) {
|
2022-05-20 16:40:55 -04:00
|
|
|
self.check_call_expr(node, &ident.range());
|
2021-06-07 07:38:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::Expr::Member(member_expr) => {
|
2022-01-13 11:58:00 -05:00
|
|
|
if let ast::MemberProp::Ident(ns_prop_ident) = &member_expr.prop {
|
2024-01-03 11:34:21 -05:00
|
|
|
let mut member_expr = member_expr;
|
|
|
|
let mut ns_prop_ident = ns_prop_ident;
|
|
|
|
let range = ns_prop_ident.range();
|
|
|
|
if matches!(ns_prop_ident.sym.as_str(), "ignore" | "only") {
|
|
|
|
let ast::Expr::Member(member_expr_) = member_expr.obj.as_ref()
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
member_expr = member_expr_;
|
|
|
|
let ast::MemberProp::Ident(ns_prop_ident_) = &member_expr.prop
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
ns_prop_ident = ns_prop_ident_;
|
|
|
|
}
|
2023-10-24 09:37:02 -04:00
|
|
|
if ns_prop_ident.sym == "test" {
|
2022-01-13 11:58:00 -05:00
|
|
|
if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() {
|
2023-10-24 09:37:02 -04:00
|
|
|
if ident.sym == "Deno" {
|
2024-01-03 11:34:21 -05:00
|
|
|
self.check_call_expr(node, &range);
|
2021-06-07 07:38:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:12:14 -05:00
|
|
|
fn visit_var_decl(&mut self, node: &ast::VarDecl) {
|
2021-06-07 07:38:07 -04:00
|
|
|
for decl in &node.decls {
|
|
|
|
if let Some(init) = &decl.init {
|
|
|
|
match init.as_ref() {
|
|
|
|
// Identify destructured assignments of `test` from `Deno`
|
|
|
|
ast::Expr::Ident(ident) => {
|
2023-10-24 09:37:02 -04:00
|
|
|
if ident.sym == "Deno" {
|
2021-06-07 07:38:07 -04:00
|
|
|
if let ast::Pat::Object(object_pat) = &decl.name {
|
|
|
|
for prop in &object_pat.props {
|
|
|
|
match prop {
|
|
|
|
ast::ObjectPatProp::Assign(prop) => {
|
|
|
|
let name = prop.key.sym.to_string();
|
|
|
|
if name == "test" {
|
|
|
|
self.test_vars.insert(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::ObjectPatProp::KeyValue(prop) => {
|
|
|
|
if let ast::PropName::Ident(key_ident) = &prop.key {
|
2023-10-24 09:37:02 -04:00
|
|
|
if key_ident.sym == "test" {
|
2021-06-07 07:38:07 -04:00
|
|
|
if let ast::Pat::Ident(value_ident) =
|
|
|
|
&prop.value.as_ref()
|
|
|
|
{
|
|
|
|
self
|
|
|
|
.test_vars
|
|
|
|
.insert(value_ident.id.sym.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Identify variable assignments where the init is `Deno.test`
|
|
|
|
ast::Expr::Member(member_expr) => {
|
2022-01-13 11:58:00 -05:00
|
|
|
if let ast::Expr::Ident(obj_ident) = member_expr.obj.as_ref() {
|
2023-10-24 09:37:02 -04:00
|
|
|
if obj_ident.sym == "Deno" {
|
2022-01-13 11:58:00 -05:00
|
|
|
if let ast::MemberProp::Ident(prop_ident) = &member_expr.prop {
|
2023-10-24 09:37:02 -04:00
|
|
|
if prop_ident.sym == "test" {
|
2022-01-13 11:58:00 -05:00
|
|
|
if let ast::Pat::Ident(binding_ident) = &decl.name {
|
|
|
|
self.test_vars.insert(binding_ident.id.sym.to_string());
|
2021-06-07 07:38:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 17:31:44 -04:00
|
|
|
async fn resolve_implementation_code_lens(
|
|
|
|
code_lens: lsp::CodeLens,
|
|
|
|
data: CodeLensData,
|
2022-01-19 11:38:40 -05:00
|
|
|
language_server: &language_server::Inner,
|
2021-06-04 17:31:44 -04:00
|
|
|
) -> Result<lsp::CodeLens, AnyError> {
|
2022-04-25 11:23:24 -04:00
|
|
|
let asset_or_doc = language_server.get_asset_or_document(&data.specifier)?;
|
2021-11-12 11:42:04 -05:00
|
|
|
let line_index = asset_or_doc.line_index();
|
2023-05-12 19:07:40 -04:00
|
|
|
let maybe_implementations = language_server
|
|
|
|
.ts_server
|
|
|
|
.get_implementations(
|
|
|
|
language_server.snapshot(),
|
|
|
|
data.specifier.clone(),
|
|
|
|
line_index.offset_tsc(code_lens.range.start)?,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-06-04 17:31:44 -04:00
|
|
|
if let Some(implementations) = maybe_implementations {
|
|
|
|
let mut locations = Vec::new();
|
|
|
|
for implementation in implementations {
|
|
|
|
let implementation_specifier =
|
|
|
|
resolve_url(&implementation.document_span.file_name)?;
|
|
|
|
let implementation_location =
|
2021-10-28 19:56:01 -04:00
|
|
|
implementation.to_location(line_index.clone(), language_server);
|
2021-06-04 17:31:44 -04:00
|
|
|
if !(implementation_specifier == data.specifier
|
|
|
|
&& implementation_location.range.start == code_lens.range.start)
|
|
|
|
{
|
|
|
|
locations.push(implementation_location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let command = if !locations.is_empty() {
|
|
|
|
let title = if locations.len() > 1 {
|
|
|
|
format!("{} implementations", locations.len())
|
|
|
|
} else {
|
|
|
|
"1 implementation".to_string()
|
|
|
|
};
|
|
|
|
lsp::Command {
|
|
|
|
title,
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.showReferences".to_string(),
|
2021-06-04 17:31:44 -04:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!(data.specifier),
|
|
|
|
json!(code_lens.range.start),
|
|
|
|
json!(locations),
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lsp::Command {
|
|
|
|
title: "0 implementations".to_string(),
|
|
|
|
command: "".to_string(),
|
|
|
|
arguments: None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(lsp::CodeLens {
|
|
|
|
range: code_lens.range,
|
|
|
|
command: Some(command),
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let command = Some(lsp::Command {
|
|
|
|
title: "0 implementations".to_string(),
|
|
|
|
command: "".to_string(),
|
|
|
|
arguments: None,
|
|
|
|
});
|
|
|
|
Ok(lsp::CodeLens {
|
|
|
|
range: code_lens.range,
|
|
|
|
command,
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn resolve_references_code_lens(
|
|
|
|
code_lens: lsp::CodeLens,
|
|
|
|
data: CodeLensData,
|
2022-01-19 11:38:40 -05:00
|
|
|
language_server: &language_server::Inner,
|
2021-06-04 17:31:44 -04:00
|
|
|
) -> Result<lsp::CodeLens, AnyError> {
|
2023-03-30 12:15:21 -04:00
|
|
|
fn get_locations(
|
|
|
|
maybe_referenced_symbols: Option<Vec<tsc::ReferencedSymbol>>,
|
|
|
|
language_server: &language_server::Inner,
|
|
|
|
) -> Result<Vec<lsp::Location>, AnyError> {
|
|
|
|
let symbols = match maybe_referenced_symbols {
|
|
|
|
Some(symbols) => symbols,
|
|
|
|
None => return Ok(Vec::new()),
|
|
|
|
};
|
2021-06-04 17:31:44 -04:00
|
|
|
let mut locations = Vec::new();
|
2023-03-30 12:15:21 -04:00
|
|
|
for reference in symbols.iter().flat_map(|s| &s.references) {
|
2021-06-04 17:31:44 -04:00
|
|
|
if reference.is_definition {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let reference_specifier =
|
2023-03-30 12:15:21 -04:00
|
|
|
resolve_url(&reference.entry.document_span.file_name)?;
|
2022-04-25 11:23:24 -04:00
|
|
|
let asset_or_doc =
|
|
|
|
language_server.get_asset_or_document(&reference_specifier)?;
|
2021-11-12 11:42:04 -05:00
|
|
|
locations.push(
|
2022-01-19 17:10:14 -05:00
|
|
|
reference
|
2023-03-30 12:15:21 -04:00
|
|
|
.entry
|
2024-06-17 16:54:23 -04:00
|
|
|
.to_location(asset_or_doc.line_index(), language_server),
|
2021-11-12 11:42:04 -05:00
|
|
|
);
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
2023-03-30 12:15:21 -04:00
|
|
|
Ok(locations)
|
|
|
|
}
|
|
|
|
|
|
|
|
let asset_or_document =
|
|
|
|
language_server.get_asset_or_document(&data.specifier)?;
|
|
|
|
let line_index = asset_or_document.line_index();
|
2023-05-12 19:07:40 -04:00
|
|
|
|
2023-03-30 12:15:21 -04:00
|
|
|
let maybe_referenced_symbols = language_server
|
|
|
|
.ts_server
|
|
|
|
.find_references(
|
2023-05-12 19:07:40 -04:00
|
|
|
language_server.snapshot(),
|
|
|
|
data.specifier.clone(),
|
2023-03-30 12:15:21 -04:00
|
|
|
line_index.offset_tsc(code_lens.range.start)?,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let locations = get_locations(maybe_referenced_symbols, language_server)?;
|
|
|
|
let title = if locations.len() == 1 {
|
|
|
|
"1 reference".to_string()
|
2021-06-04 17:31:44 -04:00
|
|
|
} else {
|
2023-03-30 12:15:21 -04:00
|
|
|
format!("{} references", locations.len())
|
|
|
|
};
|
|
|
|
let command = if locations.is_empty() {
|
|
|
|
lsp::Command {
|
|
|
|
title,
|
|
|
|
command: String::new(),
|
2021-06-04 17:31:44 -04:00
|
|
|
arguments: None,
|
2023-03-30 12:15:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lsp::Command {
|
|
|
|
title,
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.showReferences".to_string(),
|
2023-03-30 12:15:21 -04:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!(data.specifier),
|
|
|
|
json!(code_lens.range.start),
|
|
|
|
json!(locations),
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(lsp::CodeLens {
|
|
|
|
range: code_lens.range,
|
|
|
|
command: Some(command),
|
|
|
|
data: None,
|
|
|
|
})
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub async fn resolve_code_lens(
|
2021-06-04 17:31:44 -04:00
|
|
|
code_lens: lsp::CodeLens,
|
2022-01-19 11:38:40 -05:00
|
|
|
language_server: &language_server::Inner,
|
2021-06-04 17:31:44 -04:00
|
|
|
) -> Result<lsp::CodeLens, AnyError> {
|
|
|
|
let data: CodeLensData =
|
|
|
|
serde_json::from_value(code_lens.data.clone().unwrap())?;
|
|
|
|
match data.source {
|
|
|
|
CodeLensSource::Implementations => {
|
|
|
|
resolve_implementation_code_lens(code_lens, data, language_server).await
|
|
|
|
}
|
|
|
|
CodeLensSource::References => {
|
|
|
|
resolve_references_code_lens(code_lens, data, language_server).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 11:23:47 -05:00
|
|
|
pub fn collect_test(
|
2021-06-07 07:38:07 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-06-05 11:04:16 -04:00
|
|
|
parsed_source: &ParsedSource,
|
2021-06-07 07:38:07 -04:00
|
|
|
) -> Result<Vec<lsp::CodeLens>, AnyError> {
|
2023-11-30 11:23:47 -05:00
|
|
|
let mut collector =
|
|
|
|
DenoTestCollector::new(specifier.clone(), parsed_source.clone());
|
|
|
|
parsed_source.module().visit_with(&mut collector);
|
|
|
|
Ok(collector.take())
|
2021-06-07 07:38:07 -04:00
|
|
|
}
|
|
|
|
|
2021-06-04 17:31:44 -04:00
|
|
|
/// Return tsc navigation tree code lenses.
|
2024-03-11 23:48:00 -04:00
|
|
|
pub fn collect_tsc(
|
2021-06-04 17:31:44 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-11-30 11:23:47 -05:00
|
|
|
code_lens_settings: &CodeLensSettings,
|
2021-10-28 19:56:01 -04:00
|
|
|
line_index: Arc<LineIndex>,
|
2021-09-07 10:39:32 -04:00
|
|
|
navigation_tree: &NavigationTree,
|
2021-06-04 17:31:44 -04:00
|
|
|
) -> Result<Vec<lsp::CodeLens>, AnyError> {
|
|
|
|
let code_lenses = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
navigation_tree.walk(&|i, mp| {
|
|
|
|
let mut code_lenses = code_lenses.borrow_mut();
|
|
|
|
|
|
|
|
// TSC Implementations Code Lens
|
2023-11-30 11:23:47 -05:00
|
|
|
if code_lens_settings.implementations {
|
2021-06-04 17:31:44 -04:00
|
|
|
let source = CodeLensSource::Implementations;
|
|
|
|
match i.kind {
|
|
|
|
tsc::ScriptElementKind::InterfaceElement => {
|
2021-10-28 19:56:01 -04:00
|
|
|
code_lenses.push(i.to_code_lens(
|
|
|
|
line_index.clone(),
|
|
|
|
specifier,
|
|
|
|
&source,
|
|
|
|
));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
tsc::ScriptElementKind::ClassElement
|
|
|
|
| tsc::ScriptElementKind::MemberFunctionElement
|
|
|
|
| tsc::ScriptElementKind::MemberVariableElement
|
|
|
|
| tsc::ScriptElementKind::MemberGetAccessorElement
|
|
|
|
| tsc::ScriptElementKind::MemberSetAccessorElement => {
|
|
|
|
if ABSTRACT_MODIFIER.is_match(&i.kind_modifiers) {
|
2021-10-28 19:56:01 -04:00
|
|
|
code_lenses.push(i.to_code_lens(
|
|
|
|
line_index.clone(),
|
|
|
|
specifier,
|
|
|
|
&source,
|
|
|
|
));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TSC References Code Lens
|
2023-11-30 11:23:47 -05:00
|
|
|
if code_lens_settings.references {
|
2021-06-04 17:31:44 -04:00
|
|
|
let source = CodeLensSource::References;
|
|
|
|
if let Some(parent) = &mp {
|
|
|
|
if parent.kind == tsc::ScriptElementKind::EnumElement {
|
2021-10-28 19:56:01 -04:00
|
|
|
code_lenses.push(i.to_code_lens(
|
|
|
|
line_index.clone(),
|
|
|
|
specifier,
|
|
|
|
&source,
|
|
|
|
));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
match i.kind {
|
|
|
|
tsc::ScriptElementKind::FunctionElement => {
|
2023-11-30 11:23:47 -05:00
|
|
|
if code_lens_settings.references_all_functions {
|
2021-10-28 19:56:01 -04:00
|
|
|
code_lenses.push(i.to_code_lens(
|
|
|
|
line_index.clone(),
|
|
|
|
specifier,
|
|
|
|
&source,
|
|
|
|
));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tsc::ScriptElementKind::ConstElement
|
|
|
|
| tsc::ScriptElementKind::LetElement
|
|
|
|
| tsc::ScriptElementKind::VariableElement => {
|
|
|
|
if EXPORT_MODIFIER.is_match(&i.kind_modifiers) {
|
2021-10-28 19:56:01 -04:00
|
|
|
code_lenses.push(i.to_code_lens(
|
|
|
|
line_index.clone(),
|
|
|
|
specifier,
|
|
|
|
&source,
|
|
|
|
));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tsc::ScriptElementKind::ClassElement => {
|
|
|
|
if i.text != "<class>" {
|
2021-10-28 19:56:01 -04:00
|
|
|
code_lenses.push(i.to_code_lens(
|
|
|
|
line_index.clone(),
|
|
|
|
specifier,
|
|
|
|
&source,
|
|
|
|
));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tsc::ScriptElementKind::InterfaceElement
|
|
|
|
| tsc::ScriptElementKind::TypeElement
|
|
|
|
| tsc::ScriptElementKind::EnumElement => {
|
2021-10-28 19:56:01 -04:00
|
|
|
code_lenses.push(i.to_code_lens(
|
|
|
|
line_index.clone(),
|
|
|
|
specifier,
|
|
|
|
&source,
|
|
|
|
));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
tsc::ScriptElementKind::LocalFunctionElement
|
2024-05-14 17:42:35 -04:00
|
|
|
| tsc::ScriptElementKind::MemberFunctionElement
|
2021-06-04 17:31:44 -04:00
|
|
|
| tsc::ScriptElementKind::MemberGetAccessorElement
|
|
|
|
| tsc::ScriptElementKind::MemberSetAccessorElement
|
|
|
|
| tsc::ScriptElementKind::ConstructorImplementationElement
|
|
|
|
| tsc::ScriptElementKind::MemberVariableElement => {
|
|
|
|
if let Some(parent) = &mp {
|
|
|
|
if parent.spans[0].start != i.spans[0].start {
|
|
|
|
match parent.kind {
|
|
|
|
tsc::ScriptElementKind::ClassElement
|
|
|
|
| tsc::ScriptElementKind::InterfaceElement
|
|
|
|
| tsc::ScriptElementKind::TypeElement => {
|
2021-10-28 19:56:01 -04:00
|
|
|
code_lenses.push(i.to_code_lens(
|
|
|
|
line_index.clone(),
|
|
|
|
specifier,
|
|
|
|
&source,
|
|
|
|
));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(Rc::try_unwrap(code_lenses).unwrap().into_inner())
|
|
|
|
}
|
2021-06-07 07:38:07 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
|
|
|
|
2021-06-07 07:38:07 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deno_test_collector() {
|
|
|
|
let specifier = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let source = r#"
|
2021-06-07 07:38:07 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "test a",
|
|
|
|
fn() {}
|
|
|
|
});
|
|
|
|
|
2021-12-28 22:00:24 -05:00
|
|
|
Deno.test(function useFnName() {});
|
|
|
|
|
2021-06-07 07:38:07 -04:00
|
|
|
Deno.test("test b", function anotherTest() {});
|
2024-01-03 11:34:21 -05:00
|
|
|
|
|
|
|
Deno.test.ignore("test ignore", () => {});
|
|
|
|
|
|
|
|
Deno.test.only("test only", () => {});
|
2024-01-05 07:04:33 -05:00
|
|
|
|
|
|
|
Deno.test(`test template literal name`, () => {});
|
2022-05-20 16:40:55 -04:00
|
|
|
"#;
|
2021-10-28 19:56:01 -04:00
|
|
|
let parsed_module = deno_ast::parse_module(deno_ast::ParseParams {
|
2024-02-08 20:40:26 -05:00
|
|
|
specifier: specifier.clone(),
|
2024-06-05 11:04:16 -04:00
|
|
|
text: source.into(),
|
2021-10-28 19:56:01 -04:00
|
|
|
media_type: MediaType::TypeScript,
|
|
|
|
capture_tokens: true,
|
|
|
|
scope_analysis: true,
|
|
|
|
maybe_syntax: None,
|
|
|
|
})
|
2021-09-07 10:39:32 -04:00
|
|
|
.unwrap();
|
2021-10-28 19:56:01 -04:00
|
|
|
let mut collector =
|
|
|
|
DenoTestCollector::new(specifier, parsed_module.clone());
|
2021-12-08 19:12:14 -05:00
|
|
|
parsed_module.module().visit_with(&mut collector);
|
2021-06-07 07:38:07 -04:00
|
|
|
assert_eq!(
|
|
|
|
collector.take(),
|
|
|
|
vec![
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 1,
|
|
|
|
character: 11
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 1,
|
|
|
|
character: 15
|
|
|
|
}
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "▶\u{fe0e} Run Test".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2021-06-07 07:38:07 -04:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test a"),
|
2021-12-20 00:00:38 -05:00
|
|
|
json!({
|
|
|
|
"inspect": false,
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 1,
|
|
|
|
character: 11
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 1,
|
|
|
|
character: 15
|
|
|
|
}
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "Debug".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2021-12-20 00:00:38 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test a"),
|
|
|
|
json!({
|
|
|
|
"inspect": true,
|
|
|
|
}),
|
2021-06-07 07:38:07 -04:00
|
|
|
])
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 6,
|
|
|
|
character: 11
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 6,
|
|
|
|
character: 15
|
|
|
|
}
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "▶\u{fe0e} Run Test".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2021-06-07 07:38:07 -04:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
2021-12-28 22:00:24 -05:00
|
|
|
json!("useFnName"),
|
2021-12-20 00:00:38 -05:00
|
|
|
json!({
|
|
|
|
"inspect": false,
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 6,
|
|
|
|
character: 11
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 6,
|
|
|
|
character: 15
|
|
|
|
}
|
|
|
|
},
|
2021-12-28 22:00:24 -05:00
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "Debug".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2021-12-28 22:00:24 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("useFnName"),
|
|
|
|
json!({
|
|
|
|
"inspect": true,
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 8,
|
|
|
|
character: 11
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 8,
|
|
|
|
character: 15
|
|
|
|
}
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "▶\u{fe0e} Run Test".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2021-12-28 22:00:24 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test b"),
|
|
|
|
json!({
|
|
|
|
"inspect": false,
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 8,
|
|
|
|
character: 11
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 8,
|
|
|
|
character: 15
|
|
|
|
}
|
|
|
|
},
|
2021-12-20 00:00:38 -05:00
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "Debug".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2021-12-20 00:00:38 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test b"),
|
|
|
|
json!({
|
|
|
|
"inspect": true,
|
|
|
|
}),
|
2021-06-07 07:38:07 -04:00
|
|
|
])
|
|
|
|
}),
|
|
|
|
data: None,
|
2024-01-03 11:34:21 -05:00
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 10,
|
|
|
|
character: 16,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 10,
|
|
|
|
character: 22,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "▶\u{fe0e} Run Test".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2024-01-03 11:34:21 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test ignore"),
|
|
|
|
json!({
|
|
|
|
"inspect": false,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 10,
|
|
|
|
character: 16,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 10,
|
|
|
|
character: 22,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "Debug".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2024-01-03 11:34:21 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test ignore"),
|
|
|
|
json!({
|
|
|
|
"inspect": true,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 12,
|
|
|
|
character: 16,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 12,
|
|
|
|
character: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "▶\u{fe0e} Run Test".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2024-01-03 11:34:21 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test only"),
|
|
|
|
json!({
|
|
|
|
"inspect": false,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 12,
|
|
|
|
character: 16,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 12,
|
|
|
|
character: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "Debug".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2024-01-03 11:34:21 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test only"),
|
|
|
|
json!({
|
|
|
|
"inspect": true,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
2024-01-05 07:04:33 -05:00
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 14,
|
|
|
|
character: 11,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 14,
|
|
|
|
character: 15,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "▶\u{fe0e} Run Test".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2024-01-05 07:04:33 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test template literal name"),
|
|
|
|
json!({
|
|
|
|
"inspect": false,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
lsp::CodeLens {
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 14,
|
|
|
|
character: 11,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 14,
|
|
|
|
character: 15,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "Debug".to_string(),
|
2024-01-28 16:56:45 -05:00
|
|
|
command: "deno.client.test".to_string(),
|
2024-01-05 07:04:33 -05:00
|
|
|
arguments: Some(vec![
|
|
|
|
json!("https://deno.land/x/mod.ts"),
|
|
|
|
json!("test template literal name"),
|
|
|
|
json!({
|
|
|
|
"inspect": true,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
data: None,
|
|
|
|
},
|
2021-06-07 07:38:07 -04:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|