2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-01-22 05:03:16 -05:00
|
|
|
|
2022-02-01 21:04:26 -05:00
|
|
|
use super::cache::calculate_fs_version;
|
2021-01-22 05:03:16 -05:00
|
|
|
use super::text::LineIndex;
|
2021-06-04 17:31:44 -04:00
|
|
|
use super::tsc;
|
2021-11-12 11:42:04 -05:00
|
|
|
use super::tsc::AssetDocument;
|
2021-01-22 05:03:16 -05:00
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::ConfigFile;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::cache::CachedUrlMetadata;
|
|
|
|
use crate::cache::HttpCache;
|
2021-10-28 19:56:01 -04:00
|
|
|
use crate::file_fetcher::get_source_from_bytes;
|
|
|
|
use crate::file_fetcher::map_content_type;
|
|
|
|
use crate::file_fetcher::SUPPORTED_SCHEMES;
|
2022-10-21 11:20:18 -04:00
|
|
|
use crate::node;
|
|
|
|
use crate::node::node_resolve_npm_reference;
|
|
|
|
use crate::node::NodeResolution;
|
|
|
|
use crate::npm::NpmPackageReference;
|
|
|
|
use crate::npm::NpmPackageReq;
|
|
|
|
use crate::npm::NpmPackageResolver;
|
2022-11-02 10:47:02 -04:00
|
|
|
use crate::resolver::CliResolver;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::path::specifier_to_file_path;
|
|
|
|
use crate::util::text_encoding;
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2022-08-22 12:14:59 -04:00
|
|
|
use deno_ast::ParsedSource;
|
2021-10-28 19:56:01 -04:00
|
|
|
use deno_ast::SourceTextInfo;
|
2021-01-22 05:03:16 -05:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-10-20 13:23:21 -04:00
|
|
|
use deno_core::futures::future;
|
2021-10-28 19:56:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
|
|
|
use deno_core::url;
|
2021-01-22 05:03:16 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2022-08-09 17:27:22 -04:00
|
|
|
use deno_graph::GraphImport;
|
2022-01-31 17:33:57 -05:00
|
|
|
use deno_graph::Resolved;
|
2022-11-30 18:07:32 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2021-11-08 20:26:39 -05:00
|
|
|
use std::collections::BTreeMap;
|
2021-01-22 05:03:16 -05:00
|
|
|
use std::collections::HashMap;
|
2021-06-03 07:13:53 -04:00
|
|
|
use std::collections::HashSet;
|
2022-11-29 19:32:18 -05:00
|
|
|
use std::collections::VecDeque;
|
2021-10-28 19:56:01 -04:00
|
|
|
use std::fs;
|
2021-01-22 05:03:16 -05:00
|
|
|
use std::ops::Range;
|
2021-10-28 19:56:01 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2021-06-02 06:29:58 -04:00
|
|
|
use std::str::FromStr;
|
2021-09-07 10:39:32 -04:00
|
|
|
use std::sync::Arc;
|
2022-04-03 00:17:30 -04:00
|
|
|
use tower_lsp::lsp_types as lsp;
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
static JS_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
|
|
|
([(
|
|
|
|
"content-type".to_string(),
|
|
|
|
"application/javascript".to_string(),
|
|
|
|
)])
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
static JSX_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
|
|
|
([("content-type".to_string(), "text/jsx".to_string())])
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
static TS_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
|
|
|
([(
|
|
|
|
"content-type".to_string(),
|
|
|
|
"application/typescript".to_string(),
|
|
|
|
)])
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
static TSX_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
|
|
|
([("content-type".to_string(), "text/tsx".to_string())])
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
});
|
2021-06-02 06:29:58 -04:00
|
|
|
|
2022-12-20 15:19:35 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub enum LanguageId {
|
2021-06-02 06:29:58 -04:00
|
|
|
JavaScript,
|
|
|
|
Jsx,
|
|
|
|
TypeScript,
|
|
|
|
Tsx,
|
|
|
|
Json,
|
|
|
|
JsonC,
|
|
|
|
Markdown,
|
2021-08-18 23:19:12 -04:00
|
|
|
Unknown,
|
2021-06-02 06:29:58 -04:00
|
|
|
}
|
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
impl LanguageId {
|
2022-12-20 15:19:35 -05:00
|
|
|
pub fn as_media_type(&self) -> MediaType {
|
|
|
|
match self {
|
|
|
|
LanguageId::JavaScript => MediaType::JavaScript,
|
|
|
|
LanguageId::Jsx => MediaType::Jsx,
|
|
|
|
LanguageId::TypeScript => MediaType::TypeScript,
|
|
|
|
LanguageId::Tsx => MediaType::Tsx,
|
|
|
|
LanguageId::Json => MediaType::Json,
|
|
|
|
LanguageId::JsonC => MediaType::Json,
|
|
|
|
LanguageId::Markdown | LanguageId::Unknown => MediaType::Unknown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_extension(&self) -> Option<&'static str> {
|
|
|
|
match self {
|
|
|
|
LanguageId::JavaScript => Some("js"),
|
|
|
|
LanguageId::Jsx => Some("jsx"),
|
|
|
|
LanguageId::TypeScript => Some("ts"),
|
|
|
|
LanguageId::Tsx => Some("tsx"),
|
|
|
|
LanguageId::Json => Some("json"),
|
|
|
|
LanguageId::JsonC => Some("jsonc"),
|
|
|
|
LanguageId::Markdown => Some("md"),
|
|
|
|
LanguageId::Unknown => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
fn as_headers(&self) -> Option<&HashMap<String, String>> {
|
|
|
|
match self {
|
|
|
|
Self::JavaScript => Some(&JS_HEADERS),
|
|
|
|
Self::Jsx => Some(&JSX_HEADERS),
|
|
|
|
Self::TypeScript => Some(&TS_HEADERS),
|
|
|
|
Self::Tsx => Some(&TSX_HEADERS),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_diagnosable(&self) -> bool {
|
|
|
|
matches!(
|
|
|
|
self,
|
|
|
|
Self::JavaScript | Self::Jsx | Self::TypeScript | Self::Tsx
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 06:29:58 -04:00
|
|
|
impl FromStr for LanguageId {
|
|
|
|
type Err = AnyError;
|
|
|
|
|
2021-08-18 23:19:12 -04:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2021-06-02 06:29:58 -04:00
|
|
|
match s {
|
|
|
|
"javascript" => Ok(Self::JavaScript),
|
2021-10-28 19:56:01 -04:00
|
|
|
"javascriptreact" | "jsx" => Ok(Self::Jsx),
|
2021-06-02 06:29:58 -04:00
|
|
|
"typescript" => Ok(Self::TypeScript),
|
2021-10-28 19:56:01 -04:00
|
|
|
"typescriptreact" | "tsx" => Ok(Self::Tsx),
|
2021-06-02 06:29:58 -04:00
|
|
|
"json" => Ok(Self::Json),
|
|
|
|
"jsonc" => Ok(Self::JsonC),
|
|
|
|
"markdown" => Ok(Self::Markdown),
|
2021-08-18 23:19:12 -04:00
|
|
|
_ => Ok(Self::Unknown),
|
2021-06-02 06:29:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
enum IndexValid {
|
|
|
|
All,
|
|
|
|
UpTo(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexValid {
|
|
|
|
fn covers(&self, line: u32) -> bool {
|
|
|
|
match *self {
|
|
|
|
IndexValid::UpTo(to) => to > line,
|
|
|
|
IndexValid::All => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2022-03-02 16:06:38 -05:00
|
|
|
pub enum AssetOrDocument {
|
2021-11-12 11:42:04 -05:00
|
|
|
Document(Document),
|
|
|
|
Asset(AssetDocument),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssetOrDocument {
|
2022-03-02 16:06:38 -05:00
|
|
|
pub fn specifier(&self) -> &ModuleSpecifier {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(asset) => asset.specifier(),
|
|
|
|
AssetOrDocument::Document(doc) => doc.specifier(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn document(&self) -> Option<&Document> {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(_) => None,
|
|
|
|
AssetOrDocument::Document(doc) => Some(doc),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
pub fn text(&self) -> Arc<str> {
|
2021-11-12 11:42:04 -05:00
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(a) => a.text(),
|
|
|
|
AssetOrDocument::Document(d) => d.0.text_info.text(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn line_index(&self) -> Arc<LineIndex> {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(a) => a.line_index(),
|
|
|
|
AssetOrDocument::Document(d) => d.line_index(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_navigation_tree(&self) -> Option<Arc<tsc::NavigationTree>> {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(a) => a.maybe_navigation_tree(),
|
|
|
|
AssetOrDocument::Document(d) => d.maybe_navigation_tree(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 14:41:37 -04:00
|
|
|
pub fn media_type(&self) -> MediaType {
|
|
|
|
match self {
|
|
|
|
AssetOrDocument::Asset(_) => MediaType::TypeScript, // assets are always TypeScript
|
|
|
|
AssetOrDocument::Document(d) => d.media_type(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn get_maybe_dependency(
|
|
|
|
&self,
|
|
|
|
position: &lsp::Position,
|
|
|
|
) -> Option<(String, deno_graph::Dependency, deno_graph::Range)> {
|
|
|
|
self
|
|
|
|
.document()
|
2022-02-24 20:03:12 -05:00
|
|
|
.and_then(|d| d.get_maybe_dependency(position))
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_parsed_source(
|
|
|
|
&self,
|
2022-08-22 12:14:59 -04:00
|
|
|
) -> Option<Result<deno_ast::ParsedSource, deno_ast::Diagnostic>> {
|
2022-02-24 20:03:12 -05:00
|
|
|
self.document().and_then(|d| d.maybe_parsed_source())
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
pub fn document_lsp_version(&self) -> Option<i32> {
|
2022-02-24 20:03:12 -05:00
|
|
|
self.document().and_then(|d| d.maybe_lsp_version())
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
2022-03-02 16:06:38 -05:00
|
|
|
|
|
|
|
pub fn is_open(&self) -> bool {
|
|
|
|
self.document().map(|d| d.is_open()).unwrap_or(false)
|
|
|
|
}
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct DocumentDependencies {
|
|
|
|
deps: BTreeMap<String, deno_graph::Dependency>,
|
|
|
|
maybe_types_dependency: Option<(String, Resolved)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentDependencies {
|
|
|
|
pub fn from_maybe_module(maybe_module: &MaybeModuleResult) -> Self {
|
|
|
|
if let Some(Ok(module)) = &maybe_module {
|
|
|
|
Self::from_module(module)
|
|
|
|
} else {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_module(module: &deno_graph::Module) -> Self {
|
|
|
|
Self {
|
|
|
|
deps: module.dependencies.clone(),
|
|
|
|
maybe_types_dependency: module.maybe_types_dependency.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:14:59 -04:00
|
|
|
type MaybeModuleResult =
|
|
|
|
Option<Result<deno_graph::Module, deno_graph::ModuleGraphError>>;
|
|
|
|
type MaybeParsedSourceResult =
|
|
|
|
Option<Result<ParsedSource, deno_ast::Diagnostic>>;
|
|
|
|
|
2022-12-19 20:22:17 -05:00
|
|
|
#[derive(Debug)]
|
2021-11-12 11:42:04 -05:00
|
|
|
struct DocumentInner {
|
2022-12-19 20:22:17 -05:00
|
|
|
/// Contains the last-known-good set of dependencies from parsing the module.
|
2022-10-21 11:20:18 -04:00
|
|
|
dependencies: Arc<DocumentDependencies>,
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version: String,
|
2021-10-28 19:56:01 -04:00
|
|
|
line_index: Arc<LineIndex>,
|
|
|
|
maybe_language_id: Option<LanguageId>,
|
|
|
|
maybe_lsp_version: Option<i32>,
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_module: MaybeModuleResult,
|
2022-12-19 20:22:17 -05:00
|
|
|
// this is a lazily constructed value based on the state of the document,
|
|
|
|
// so having a mutex to hold it is ok
|
|
|
|
maybe_navigation_tree: Mutex<Option<Arc<tsc::NavigationTree>>>,
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_parsed_source: MaybeParsedSourceResult,
|
2021-03-24 20:13:37 -04:00
|
|
|
specifier: ModuleSpecifier,
|
2021-11-12 11:42:04 -05:00
|
|
|
text_info: SourceTextInfo,
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2022-03-02 16:06:38 -05:00
|
|
|
pub struct Document(Arc<DocumentInner>);
|
2021-11-12 11:42:04 -05:00
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
impl Document {
|
|
|
|
fn new(
|
2021-06-02 06:29:58 -04:00
|
|
|
specifier: ModuleSpecifier,
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version: String,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_headers: Option<&HashMap<String, String>>,
|
2022-05-20 16:40:55 -04:00
|
|
|
content: Arc<str>,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
|
2021-06-02 06:29:58 -04:00
|
|
|
) -> Self {
|
2021-10-28 19:56:01 -04:00
|
|
|
// we only ever do `Document::new` on on disk resources that are supposed to
|
|
|
|
// be diagnosable, unlike `Document::open`, so it is safe to unconditionally
|
|
|
|
// parse the module.
|
2022-08-22 12:14:59 -04:00
|
|
|
let (maybe_module, maybe_parsed_source) = lsp_deno_graph_analyze(
|
2021-10-28 19:56:01 -04:00
|
|
|
&specifier,
|
|
|
|
content.clone(),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_headers,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_resolver,
|
2022-08-22 12:14:59 -04:00
|
|
|
);
|
2022-10-21 11:20:18 -04:00
|
|
|
let dependencies =
|
|
|
|
Arc::new(DocumentDependencies::from_maybe_module(&maybe_module));
|
|
|
|
// todo(dsherret): retrieve this from the parsed source if it exists
|
2021-11-12 11:42:04 -05:00
|
|
|
let text_info = SourceTextInfo::new(content);
|
|
|
|
let line_index = Arc::new(LineIndex::new(text_info.text_str()));
|
|
|
|
Self(Arc::new(DocumentInner {
|
2021-11-16 17:23:25 -05:00
|
|
|
dependencies,
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version,
|
2021-10-28 19:56:01 -04:00
|
|
|
line_index,
|
|
|
|
maybe_language_id: None,
|
|
|
|
maybe_lsp_version: None,
|
|
|
|
maybe_module,
|
2022-12-19 20:22:17 -05:00
|
|
|
maybe_navigation_tree: Mutex::new(None),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_parsed_source,
|
2021-11-12 11:42:04 -05:00
|
|
|
text_info,
|
2021-03-24 20:13:37 -04:00
|
|
|
specifier,
|
2021-11-12 11:42:04 -05:00
|
|
|
}))
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
fn open(
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
version: i32,
|
|
|
|
language_id: LanguageId,
|
2022-05-20 16:40:55 -04:00
|
|
|
content: Arc<str>,
|
2021-11-12 11:42:04 -05:00
|
|
|
maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
|
|
|
|
) -> Self {
|
|
|
|
let maybe_headers = language_id.as_headers();
|
2022-08-22 12:14:59 -04:00
|
|
|
let (maybe_module, maybe_parsed_source) = if language_id.is_diagnosable() {
|
|
|
|
lsp_deno_graph_analyze(
|
2021-11-12 11:42:04 -05:00
|
|
|
&specifier,
|
|
|
|
content.clone(),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_headers,
|
2021-11-12 11:42:04 -05:00
|
|
|
maybe_resolver,
|
2022-08-22 12:14:59 -04:00
|
|
|
)
|
2021-11-12 11:42:04 -05:00
|
|
|
} else {
|
2022-08-22 12:14:59 -04:00
|
|
|
(None, None)
|
2021-11-12 11:42:04 -05:00
|
|
|
};
|
2022-10-21 11:20:18 -04:00
|
|
|
let dependencies =
|
|
|
|
Arc::new(DocumentDependencies::from_maybe_module(&maybe_module));
|
2021-11-12 11:42:04 -05:00
|
|
|
let source = SourceTextInfo::new(content);
|
|
|
|
let line_index = Arc::new(LineIndex::new(source.text_str()));
|
|
|
|
Self(Arc::new(DocumentInner {
|
2021-11-16 17:23:25 -05:00
|
|
|
dependencies,
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version: "1".to_string(),
|
|
|
|
line_index,
|
|
|
|
maybe_language_id: Some(language_id),
|
|
|
|
maybe_lsp_version: Some(version),
|
|
|
|
maybe_module,
|
2022-12-19 20:22:17 -05:00
|
|
|
maybe_navigation_tree: Mutex::new(None),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_parsed_source,
|
2021-11-12 11:42:04 -05:00
|
|
|
text_info: source,
|
|
|
|
specifier,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_change(
|
|
|
|
&self,
|
2021-10-28 19:56:01 -04:00
|
|
|
version: i32,
|
|
|
|
changes: Vec<lsp::TextDocumentContentChangeEvent>,
|
|
|
|
maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
|
2021-11-12 11:42:04 -05:00
|
|
|
) -> Result<Document, AnyError> {
|
|
|
|
let mut content = self.0.text_info.text_str().to_string();
|
|
|
|
let mut line_index = self.0.line_index.clone();
|
2021-01-22 05:03:16 -05:00
|
|
|
let mut index_valid = IndexValid::All;
|
2021-10-28 19:56:01 -04:00
|
|
|
for change in changes {
|
2021-01-22 05:03:16 -05:00
|
|
|
if let Some(range) = change.range {
|
|
|
|
if !index_valid.covers(range.start.line) {
|
2021-10-28 19:56:01 -04:00
|
|
|
line_index = Arc::new(LineIndex::new(&content));
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
index_valid = IndexValid::UpTo(range.start.line);
|
|
|
|
let range = line_index.get_text_range(range)?;
|
|
|
|
content.replace_range(Range::<usize>::from(range), &change.text);
|
|
|
|
} else {
|
2021-09-07 10:39:32 -04:00
|
|
|
content = change.text;
|
2021-01-22 05:03:16 -05:00
|
|
|
index_valid = IndexValid::UpTo(0);
|
|
|
|
}
|
|
|
|
}
|
2022-05-20 16:40:55 -04:00
|
|
|
let content: Arc<str> = content.into();
|
2022-08-22 12:14:59 -04:00
|
|
|
let (maybe_module, maybe_parsed_source) = if self
|
2021-11-12 11:42:04 -05:00
|
|
|
.0
|
2021-10-28 19:56:01 -04:00
|
|
|
.maybe_language_id
|
|
|
|
.as_ref()
|
|
|
|
.map(|li| li.is_diagnosable())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
let maybe_headers = self
|
2021-11-12 11:42:04 -05:00
|
|
|
.0
|
2021-10-28 19:56:01 -04:00
|
|
|
.maybe_language_id
|
|
|
|
.as_ref()
|
2022-02-24 20:03:12 -05:00
|
|
|
.and_then(|li| li.as_headers());
|
2022-08-22 12:14:59 -04:00
|
|
|
lsp_deno_graph_analyze(
|
2021-11-12 11:42:04 -05:00
|
|
|
&self.0.specifier,
|
2021-10-28 19:56:01 -04:00
|
|
|
content.clone(),
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_headers,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_resolver,
|
2022-08-22 12:14:59 -04:00
|
|
|
)
|
2021-10-28 19:56:01 -04:00
|
|
|
} else {
|
2022-08-22 12:14:59 -04:00
|
|
|
(None, None)
|
2021-11-12 11:42:04 -05:00
|
|
|
};
|
2021-11-16 17:23:25 -05:00
|
|
|
let dependencies = if let Some(Ok(module)) = &maybe_module {
|
2022-10-21 11:20:18 -04:00
|
|
|
Arc::new(DocumentDependencies::from_module(module))
|
2021-11-16 17:23:25 -05:00
|
|
|
} else {
|
2022-10-21 11:20:18 -04:00
|
|
|
self.0.dependencies.clone() // use the last known good
|
2021-11-16 17:23:25 -05:00
|
|
|
};
|
|
|
|
let text_info = SourceTextInfo::new(content);
|
2021-11-12 11:42:04 -05:00
|
|
|
let line_index = if index_valid == IndexValid::All {
|
2021-09-07 10:39:32 -04:00
|
|
|
line_index
|
2021-01-22 05:03:16 -05:00
|
|
|
} else {
|
2021-11-16 17:23:25 -05:00
|
|
|
Arc::new(LineIndex::new(text_info.text_str()))
|
2021-01-22 05:03:16 -05:00
|
|
|
};
|
2021-11-12 11:42:04 -05:00
|
|
|
Ok(Document(Arc::new(DocumentInner {
|
2022-12-19 20:22:17 -05:00
|
|
|
specifier: self.0.specifier.clone(),
|
|
|
|
fs_version: self.0.fs_version.clone(),
|
2022-12-20 15:19:35 -05:00
|
|
|
maybe_language_id: self.0.maybe_language_id,
|
2021-11-16 17:23:25 -05:00
|
|
|
dependencies,
|
|
|
|
text_info,
|
2021-11-12 11:42:04 -05:00
|
|
|
line_index,
|
|
|
|
maybe_module,
|
2022-08-22 12:14:59 -04:00
|
|
|
maybe_parsed_source,
|
2021-11-12 11:42:04 -05:00
|
|
|
maybe_lsp_version: Some(version),
|
2022-12-19 20:22:17 -05:00
|
|
|
maybe_navigation_tree: Mutex::new(None),
|
2021-11-12 11:42:04 -05:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn specifier(&self) -> &ModuleSpecifier {
|
|
|
|
&self.0.specifier
|
2021-06-21 02:43:35 -04:00
|
|
|
}
|
2021-06-24 19:06:51 -04:00
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
pub fn content(&self) -> Arc<str> {
|
2021-11-12 11:42:04 -05:00
|
|
|
self.0.text_info.text()
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn text_info(&self) -> SourceTextInfo {
|
|
|
|
self.0.text_info.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn line_index(&self) -> Arc<LineIndex> {
|
|
|
|
self.0.line_index.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fs_version(&self) -> &str {
|
|
|
|
self.0.fs_version.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn script_version(&self) -> String {
|
|
|
|
self
|
|
|
|
.maybe_lsp_version()
|
|
|
|
.map_or_else(|| self.fs_version().to_string(), |v| v.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_diagnosable(&self) -> bool {
|
2021-10-28 19:56:01 -04:00
|
|
|
matches!(
|
|
|
|
self.media_type(),
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Jsx
|
2021-12-09 17:12:21 -05:00
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
2021-10-28 19:56:01 -04:00
|
|
|
| MediaType::TypeScript
|
|
|
|
| MediaType::Tsx
|
2021-12-09 17:12:21 -05:00
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
2021-10-28 19:56:01 -04:00
|
|
|
| MediaType::Dts
|
2021-12-09 17:12:21 -05:00
|
|
|
| MediaType::Dmts
|
|
|
|
| MediaType::Dcts
|
2021-10-28 19:56:01 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn is_open(&self) -> bool {
|
|
|
|
self.0.maybe_lsp_version.is_some()
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn maybe_types_dependency(&self) -> deno_graph::Resolved {
|
2022-10-21 11:20:18 -04:00
|
|
|
if let Some((_, maybe_dep)) =
|
|
|
|
self.0.dependencies.maybe_types_dependency.as_ref()
|
|
|
|
{
|
2022-01-31 17:33:57 -05:00
|
|
|
maybe_dep.clone()
|
|
|
|
} else {
|
|
|
|
deno_graph::Resolved::None
|
|
|
|
}
|
2021-11-07 19:50:48 -05:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
pub fn media_type(&self) -> MediaType {
|
|
|
|
if let Some(Ok(module)) = &self.0.maybe_module {
|
2022-12-20 15:19:35 -05:00
|
|
|
return module.media_type;
|
|
|
|
}
|
|
|
|
let specifier_media_type = MediaType::from(&self.0.specifier);
|
|
|
|
if specifier_media_type != MediaType::Unknown {
|
|
|
|
return specifier_media_type;
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2022-12-20 15:19:35 -05:00
|
|
|
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.maybe_language_id
|
|
|
|
.map(|id| id.as_media_type())
|
|
|
|
.unwrap_or(MediaType::Unknown)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_language_id(&self) -> Option<LanguageId> {
|
|
|
|
self.0.maybe_language_id
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:42:04 -05:00
|
|
|
/// Returns the current language server client version if any.
|
|
|
|
pub fn maybe_lsp_version(&self) -> Option<i32> {
|
|
|
|
self.0.maybe_lsp_version
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_module(
|
|
|
|
&self,
|
2022-01-31 17:33:57 -05:00
|
|
|
) -> Option<&Result<deno_graph::Module, deno_graph::ModuleGraphError>> {
|
2021-11-12 11:42:04 -05:00
|
|
|
self.0.maybe_module.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_parsed_source(
|
|
|
|
&self,
|
2022-08-22 12:14:59 -04:00
|
|
|
) -> Option<Result<deno_ast::ParsedSource, deno_ast::Diagnostic>> {
|
|
|
|
self.0.maybe_parsed_source.clone()
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_navigation_tree(&self) -> Option<Arc<tsc::NavigationTree>> {
|
2022-12-19 20:22:17 -05:00
|
|
|
self.0.maybe_navigation_tree.lock().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_navigation_tree_if_version(
|
|
|
|
&self,
|
|
|
|
tree: Arc<tsc::NavigationTree>,
|
|
|
|
script_version: &str,
|
|
|
|
) {
|
|
|
|
// Ensure we are updating the same document that the navigation tree was
|
|
|
|
// created for. Note: this should not be racy between the version check
|
|
|
|
// and setting the navigation tree, because the document is immutable
|
|
|
|
// and this is enforced by it being wrapped in an Arc.
|
|
|
|
if self.script_version() == script_version {
|
|
|
|
*self.0.maybe_navigation_tree.lock() = Some(tree);
|
|
|
|
}
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
pub fn dependencies(&self) -> &BTreeMap<String, deno_graph::Dependency> {
|
|
|
|
&self.0.dependencies.deps
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the supplied position is within a dependency range, return the resolved
|
|
|
|
/// string specifier for the dependency, the resolved dependency and the range
|
|
|
|
/// in the source document of the specifier.
|
|
|
|
pub fn get_maybe_dependency(
|
|
|
|
&self,
|
|
|
|
position: &lsp::Position,
|
|
|
|
) -> Option<(String, deno_graph::Dependency, deno_graph::Range)> {
|
|
|
|
let module = self.maybe_module()?.as_ref().ok()?;
|
|
|
|
let position = deno_graph::Position {
|
|
|
|
line: position.line as usize,
|
|
|
|
character: position.character as usize,
|
2021-10-28 19:56:01 -04:00
|
|
|
};
|
2021-11-12 11:42:04 -05:00
|
|
|
module.dependencies.iter().find_map(|(s, dep)| {
|
|
|
|
dep
|
|
|
|
.includes(&position)
|
|
|
|
.map(|r| (s.clone(), dep.clone(), r.clone()))
|
|
|
|
})
|
2021-06-24 19:06:51 -04:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn to_hover_text(result: &Resolved) -> String {
|
2021-10-28 19:56:01 -04:00
|
|
|
match result {
|
2022-01-31 17:33:57 -05:00
|
|
|
Resolved::Ok { specifier, .. } => match specifier.scheme() {
|
2021-10-28 19:56:01 -04:00
|
|
|
"data" => "_(a data url)_".to_string(),
|
|
|
|
"blob" => "_(a blob url)_".to_string(),
|
|
|
|
_ => format!(
|
|
|
|
"{}​{}",
|
2022-01-15 01:10:12 -05:00
|
|
|
&specifier[..url::Position::AfterScheme],
|
|
|
|
&specifier[url::Position::AfterScheme..],
|
2022-01-06 19:27:13 -05:00
|
|
|
)
|
|
|
|
.replace('@', "​@"),
|
2021-10-28 19:56:01 -04:00
|
|
|
},
|
2022-01-31 17:33:57 -05:00
|
|
|
Resolved::Err(_) => "_[errored]_".to_string(),
|
|
|
|
Resolved::None => "_[missing]_".to_string(),
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn to_lsp_range(range: &deno_graph::Range) -> lsp::Range {
|
2021-10-28 19:56:01 -04:00
|
|
|
lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: range.start.line as u32,
|
|
|
|
character: range.start.character as u32,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: range.end.line as u32,
|
|
|
|
character: range.end.character as u32,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recurse and collect specifiers that appear in the dependent map.
|
|
|
|
fn recurse_dependents(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
map: &HashMap<ModuleSpecifier, HashSet<ModuleSpecifier>>,
|
|
|
|
dependents: &mut HashSet<ModuleSpecifier>,
|
|
|
|
) {
|
|
|
|
if let Some(deps) = map.get(specifier) {
|
|
|
|
for dep in deps {
|
|
|
|
if !dependents.contains(dep) {
|
|
|
|
dependents.insert(dep.clone());
|
|
|
|
recurse_dependents(dep, map, dependents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
2021-11-18 13:50:24 -05:00
|
|
|
struct SpecifierResolver {
|
2021-10-28 19:56:01 -04:00
|
|
|
cache: HttpCache,
|
2021-11-18 13:50:24 -05:00
|
|
|
redirects: Mutex<HashMap<ModuleSpecifier, ModuleSpecifier>>,
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
impl SpecifierResolver {
|
|
|
|
pub fn new(cache_path: &Path) -> Self {
|
2021-10-28 19:56:01 -04:00
|
|
|
Self {
|
2021-11-18 13:50:24 -05:00
|
|
|
cache: HttpCache::new(cache_path),
|
|
|
|
redirects: Mutex::new(HashMap::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
|
|
|
let scheme = specifier.scheme();
|
|
|
|
if !SUPPORTED_SCHEMES.contains(&scheme) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scheme == "data" || scheme == "blob" || scheme == "file" {
|
|
|
|
Some(specifier.clone())
|
|
|
|
} else {
|
|
|
|
let mut redirects = self.redirects.lock();
|
|
|
|
if let Some(specifier) = redirects.get(specifier) {
|
|
|
|
Some(specifier.clone())
|
|
|
|
} else {
|
|
|
|
let redirect = self.resolve_remote(specifier, 10)?;
|
|
|
|
redirects.insert(specifier.clone(), redirect.clone());
|
|
|
|
Some(redirect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_remote(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
redirect_limit: usize,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
|
|
|
let cache_filename = self.cache.get_cache_filename(specifier)?;
|
|
|
|
if redirect_limit > 0 && cache_filename.is_file() {
|
2022-11-28 17:28:54 -05:00
|
|
|
let headers = CachedUrlMetadata::read(&cache_filename)
|
2021-11-18 13:50:24 -05:00
|
|
|
.ok()
|
|
|
|
.map(|m| m.headers)?;
|
|
|
|
if let Some(location) = headers.get("location") {
|
|
|
|
let redirect =
|
|
|
|
deno_core::resolve_import(location, specifier.as_str()).ok()?;
|
|
|
|
self.resolve_remote(&redirect, redirect_limit - 1)
|
|
|
|
} else {
|
|
|
|
Some(specifier.clone())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct FileSystemDocuments {
|
|
|
|
docs: HashMap<ModuleSpecifier, Document>,
|
|
|
|
dirty: bool,
|
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
impl FileSystemDocuments {
|
2022-11-30 14:19:32 -05:00
|
|
|
pub fn get(
|
2022-11-29 19:32:18 -05:00
|
|
|
&mut self,
|
|
|
|
cache: &HttpCache,
|
|
|
|
maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<Document> {
|
|
|
|
let fs_version = get_document_path(cache, specifier)
|
|
|
|
.and_then(|path| calculate_fs_version(&path));
|
|
|
|
let file_system_doc = self.docs.get(specifier);
|
|
|
|
if file_system_doc.map(|d| d.fs_version().to_string()) != fs_version {
|
|
|
|
// attempt to update the file on the file system
|
|
|
|
self.refresh_document(cache, maybe_resolver, specifier)
|
|
|
|
} else {
|
|
|
|
file_system_doc.cloned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 09:21:59 -05:00
|
|
|
/// Adds or updates a document by reading the document from the file system
|
|
|
|
/// returning the document.
|
2021-11-18 13:50:24 -05:00
|
|
|
fn refresh_document(
|
|
|
|
&mut self,
|
|
|
|
cache: &HttpCache,
|
|
|
|
maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
|
2022-01-24 03:01:33 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2021-11-18 13:50:24 -05:00
|
|
|
) -> Option<Document> {
|
2022-01-24 03:01:33 -05:00
|
|
|
let path = get_document_path(cache, specifier)?;
|
2021-11-18 13:50:24 -05:00
|
|
|
let fs_version = calculate_fs_version(&path)?;
|
2021-10-28 19:56:01 -04:00
|
|
|
let bytes = fs::read(path).ok()?;
|
|
|
|
let doc = if specifier.scheme() == "file" {
|
|
|
|
let maybe_charset =
|
|
|
|
Some(text_encoding::detect_charset(&bytes).to_string());
|
2022-05-20 16:40:55 -04:00
|
|
|
let content = get_source_from_bytes(bytes, maybe_charset).ok()?;
|
2021-10-28 19:56:01 -04:00
|
|
|
Document::new(
|
|
|
|
specifier.clone(),
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version,
|
2021-10-28 19:56:01 -04:00
|
|
|
None,
|
2022-05-20 16:40:55 -04:00
|
|
|
content.into(),
|
2021-11-18 13:50:24 -05:00
|
|
|
maybe_resolver,
|
2021-10-28 19:56:01 -04:00
|
|
|
)
|
|
|
|
} else {
|
2022-01-24 03:01:33 -05:00
|
|
|
let cache_filename = cache.get_cache_filename(specifier)?;
|
2022-11-28 17:28:54 -05:00
|
|
|
let specifier_metadata = CachedUrlMetadata::read(&cache_filename).ok()?;
|
2022-02-01 21:04:26 -05:00
|
|
|
let maybe_content_type =
|
|
|
|
specifier_metadata.headers.get("content-type").cloned();
|
|
|
|
let maybe_headers = Some(&specifier_metadata.headers);
|
2022-01-24 03:01:33 -05:00
|
|
|
let (_, maybe_charset) = map_content_type(specifier, maybe_content_type);
|
2022-05-20 16:40:55 -04:00
|
|
|
let content = get_source_from_bytes(bytes, maybe_charset).ok()?;
|
2021-10-28 19:56:01 -04:00
|
|
|
Document::new(
|
|
|
|
specifier.clone(),
|
2021-11-12 11:42:04 -05:00
|
|
|
fs_version,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_headers,
|
2022-05-20 16:40:55 -04:00
|
|
|
content.into(),
|
2021-11-18 13:50:24 -05:00
|
|
|
maybe_resolver,
|
2021-10-28 19:56:01 -04:00
|
|
|
)
|
|
|
|
};
|
|
|
|
self.dirty = true;
|
2022-01-25 09:21:59 -05:00
|
|
|
self.docs.insert(specifier.clone(), doc.clone());
|
|
|
|
Some(doc)
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
fn get_document_path(
|
|
|
|
cache: &HttpCache,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<PathBuf> {
|
2022-12-13 07:53:32 -05:00
|
|
|
match specifier.scheme() {
|
|
|
|
"npm" | "node" => None,
|
|
|
|
"file" => specifier_to_file_path(specifier).ok(),
|
|
|
|
_ => cache.get_cache_filename(specifier),
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct Documents {
|
2021-11-18 13:50:24 -05:00
|
|
|
/// The DENO_DIR that the documents looks for non-file based modules.
|
|
|
|
cache: HttpCache,
|
|
|
|
/// A flag that indicates that stated data is potentially invalid and needs to
|
|
|
|
/// be recalculated before being considered valid.
|
|
|
|
dirty: bool,
|
|
|
|
/// A map where the key is a specifier and the value is a set of specifiers
|
|
|
|
/// that depend on the key.
|
|
|
|
dependents_map: Arc<HashMap<ModuleSpecifier, HashSet<ModuleSpecifier>>>,
|
|
|
|
/// A map of documents that are "open" in the language server.
|
|
|
|
open_docs: HashMap<ModuleSpecifier, Document>,
|
|
|
|
/// Documents stored on the file system.
|
|
|
|
file_system_docs: Arc<Mutex<FileSystemDocuments>>,
|
|
|
|
/// Any imports to the context supplied by configuration files. This is like
|
|
|
|
/// the imports into the a module graph in CLI.
|
2022-08-09 17:27:22 -04:00
|
|
|
imports: Arc<HashMap<ModuleSpecifier, GraphImport>>,
|
2022-11-02 10:47:02 -04:00
|
|
|
/// A resolver that takes into account currently loaded import map and JSX
|
|
|
|
/// settings.
|
|
|
|
maybe_resolver: Option<CliResolver>,
|
2022-10-21 11:20:18 -04:00
|
|
|
/// The npm package requirements.
|
2022-11-29 19:32:18 -05:00
|
|
|
npm_reqs: Arc<HashSet<NpmPackageReq>>,
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Resolves a specifier to its final redirected to specifier.
|
|
|
|
specifier_resolver: Arc<SpecifierResolver>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Documents {
|
|
|
|
pub fn new(location: &Path) -> Self {
|
|
|
|
Self {
|
|
|
|
cache: HttpCache::new(location),
|
|
|
|
dirty: true,
|
|
|
|
dependents_map: Default::default(),
|
|
|
|
open_docs: HashMap::default(),
|
|
|
|
file_system_docs: Default::default(),
|
|
|
|
imports: Default::default(),
|
2022-11-02 10:47:02 -04:00
|
|
|
maybe_resolver: None,
|
2022-11-29 19:32:18 -05:00
|
|
|
npm_reqs: Default::default(),
|
2021-11-18 13:50:24 -05:00
|
|
|
specifier_resolver: Arc::new(SpecifierResolver::new(location)),
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// "Open" a document from the perspective of the editor, meaning that
|
|
|
|
/// requests for information from the document will come from the in-memory
|
|
|
|
/// representation received from the language server client, versus reading
|
|
|
|
/// information from the disk.
|
|
|
|
pub fn open(
|
|
|
|
&mut self,
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
version: i32,
|
|
|
|
language_id: LanguageId,
|
2022-05-20 16:40:55 -04:00
|
|
|
content: Arc<str>,
|
2021-11-18 13:50:24 -05:00
|
|
|
) -> Document {
|
|
|
|
let maybe_resolver = self.get_maybe_resolver();
|
|
|
|
let document = Document::open(
|
|
|
|
specifier.clone(),
|
|
|
|
version,
|
|
|
|
language_id,
|
|
|
|
content,
|
|
|
|
maybe_resolver,
|
|
|
|
);
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
file_system_docs.docs.remove(&specifier);
|
|
|
|
file_system_docs.dirty = true;
|
|
|
|
self.open_docs.insert(specifier, document.clone());
|
|
|
|
self.dirty = true;
|
|
|
|
document
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply language server content changes to an open document.
|
|
|
|
pub fn change(
|
2021-01-22 05:03:16 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
version: i32,
|
2021-10-28 19:56:01 -04:00
|
|
|
changes: Vec<lsp::TextDocumentContentChangeEvent>,
|
2021-11-12 11:42:04 -05:00
|
|
|
) -> Result<Document, AnyError> {
|
2021-11-18 13:50:24 -05:00
|
|
|
let doc = self
|
|
|
|
.open_docs
|
|
|
|
.get(specifier)
|
|
|
|
.cloned()
|
|
|
|
.or_else(|| {
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
file_system_docs.docs.remove(specifier)
|
|
|
|
})
|
|
|
|
.map_or_else(
|
|
|
|
|| {
|
|
|
|
Err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!("The specifier \"{}\" was not found.", specifier),
|
|
|
|
))
|
|
|
|
},
|
|
|
|
Ok,
|
|
|
|
)?;
|
2021-10-28 19:56:01 -04:00
|
|
|
self.dirty = true;
|
2021-11-12 11:42:04 -05:00
|
|
|
let doc = doc.with_change(version, changes, self.get_maybe_resolver())?;
|
2021-11-18 13:50:24 -05:00
|
|
|
self.open_docs.insert(doc.specifier().clone(), doc.clone());
|
2021-11-12 11:42:04 -05:00
|
|
|
Ok(doc)
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Close an open document, this essentially clears any editor state that is
|
|
|
|
/// being held, and the document store will revert to the file system if
|
|
|
|
/// information about the document is required.
|
|
|
|
pub fn close(&mut self, specifier: &ModuleSpecifier) -> Result<(), AnyError> {
|
|
|
|
if self.open_docs.remove(specifier).is_some() {
|
|
|
|
self.dirty = true;
|
|
|
|
} else {
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
if file_system_docs.docs.remove(specifier).is_some() {
|
|
|
|
file_system_docs.dirty = true;
|
|
|
|
} else {
|
|
|
|
return Err(custom_error(
|
2021-10-28 19:56:01 -04:00
|
|
|
"NotFound",
|
|
|
|
format!("The specifier \"{}\" was not found.", specifier),
|
2021-11-18 13:50:24 -05:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
Ok(())
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return `true` if the provided specifier can be resolved to a document,
|
|
|
|
/// otherwise `false`.
|
|
|
|
pub fn contains_import(
|
|
|
|
&self,
|
2021-10-28 19:56:01 -04:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
) -> bool {
|
2021-11-08 20:26:39 -05:00
|
|
|
let maybe_resolver = self.get_maybe_resolver();
|
2021-10-28 19:56:01 -04:00
|
|
|
let maybe_specifier = if let Some(resolver) = maybe_resolver {
|
2022-01-31 17:33:57 -05:00
|
|
|
resolver.resolve(specifier, referrer).to_result().ok()
|
2021-10-28 19:56:01 -04:00
|
|
|
} else {
|
|
|
|
deno_core::resolve_import(specifier, referrer.as_str()).ok()
|
|
|
|
};
|
|
|
|
if let Some(import_specifier) = maybe_specifier {
|
2022-01-25 09:21:59 -05:00
|
|
|
self.exists(&import_specifier)
|
2021-10-28 19:56:01 -04:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return `true` if the specifier can be resolved to a document.
|
2022-01-25 09:21:59 -05:00
|
|
|
pub fn exists(&self, specifier: &ModuleSpecifier) -> bool {
|
|
|
|
// keep this fast because it's used by op_exists, which is a hot path in tsc
|
|
|
|
let specifier = self.specifier_resolver.resolve(specifier);
|
|
|
|
if let Some(specifier) = specifier {
|
|
|
|
if self.open_docs.contains_key(&specifier) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if let Some(path) = get_document_path(&self.cache, &specifier) {
|
|
|
|
return path.is_file();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return an array of specifiers, if any, that are dependent upon the
|
|
|
|
/// supplied specifier. This is used to determine invalidation of diagnostics
|
|
|
|
/// when a module has been changed.
|
|
|
|
pub fn dependents(
|
2021-10-28 19:56:01 -04:00
|
|
|
&mut self,
|
2021-06-03 07:13:53 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Vec<ModuleSpecifier> {
|
2021-11-18 13:50:24 -05:00
|
|
|
self.calculate_dependents_if_dirty();
|
2021-06-03 07:13:53 -04:00
|
|
|
let mut dependents = HashSet::new();
|
2021-11-18 13:50:24 -05:00
|
|
|
if let Some(specifier) = self.specifier_resolver.resolve(specifier) {
|
2021-10-28 19:56:01 -04:00
|
|
|
recurse_dependents(&specifier, &self.dependents_map, &mut dependents);
|
|
|
|
dependents.into_iter().collect()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
2021-06-03 07:13:53 -04:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
/// Returns a collection of npm package requirements.
|
|
|
|
pub fn npm_package_reqs(&mut self) -> HashSet<NpmPackageReq> {
|
|
|
|
self.calculate_dependents_if_dirty();
|
2022-11-29 19:32:18 -05:00
|
|
|
(*self.npm_reqs).clone()
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return a document for the specifier.
|
2022-02-01 21:04:26 -05:00
|
|
|
pub fn get(&self, original_specifier: &ModuleSpecifier) -> Option<Document> {
|
|
|
|
let specifier = self.specifier_resolver.resolve(original_specifier)?;
|
2021-11-18 13:50:24 -05:00
|
|
|
if let Some(document) = self.open_docs.get(&specifier) {
|
|
|
|
Some(document.clone())
|
2021-10-28 19:56:01 -04:00
|
|
|
} else {
|
2021-11-18 13:50:24 -05:00
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
2022-11-29 19:32:18 -05:00
|
|
|
file_system_docs.get(&self.cache, self.get_maybe_resolver(), &specifier)
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Return a vector of documents that are contained in the document store,
|
|
|
|
/// where `open_only` flag would provide only those documents currently open
|
|
|
|
/// in the editor and `diagnosable_only` would provide only those documents
|
|
|
|
/// that the language server can provide diagnostics for.
|
|
|
|
pub fn documents(
|
2021-11-12 11:42:04 -05:00
|
|
|
&self,
|
|
|
|
open_only: bool,
|
|
|
|
diagnosable_only: bool,
|
|
|
|
) -> Vec<Document> {
|
2021-11-18 13:50:24 -05:00
|
|
|
if open_only {
|
|
|
|
self
|
|
|
|
.open_docs
|
|
|
|
.values()
|
|
|
|
.filter_map(|doc| {
|
|
|
|
if !diagnosable_only || doc.is_diagnosable() {
|
|
|
|
Some(doc.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
// it is technically possible for a Document to end up in both the open
|
|
|
|
// and closed documents so we need to ensure we don't return duplicates
|
|
|
|
let mut seen_documents = HashSet::new();
|
|
|
|
let file_system_docs = self.file_system_docs.lock();
|
|
|
|
self
|
|
|
|
.open_docs
|
|
|
|
.values()
|
|
|
|
.chain(file_system_docs.docs.values())
|
|
|
|
.filter_map(|doc| {
|
|
|
|
// this prefers the open documents
|
|
|
|
if seen_documents.insert(doc.specifier().clone())
|
|
|
|
&& (!diagnosable_only || doc.is_diagnosable())
|
|
|
|
{
|
|
|
|
Some(doc.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// For a given set of string specifiers, resolve each one from the graph,
|
|
|
|
/// for a given referrer. This is used to provide resolution information to
|
|
|
|
/// tsc when type checking.
|
|
|
|
pub fn resolve(
|
|
|
|
&self,
|
2022-12-05 20:09:31 -05:00
|
|
|
specifiers: &[String],
|
2021-10-28 19:56:01 -04:00
|
|
|
referrer: &ModuleSpecifier,
|
2022-10-21 11:20:18 -04:00
|
|
|
maybe_npm_resolver: Option<&NpmPackageResolver>,
|
2021-10-28 19:56:01 -04:00
|
|
|
) -> Option<Vec<Option<(ModuleSpecifier, MediaType)>>> {
|
2021-11-16 17:23:25 -05:00
|
|
|
let dependencies = self.get(referrer)?.0.dependencies.clone();
|
2021-10-28 19:56:01 -04:00
|
|
|
let mut results = Vec::new();
|
2021-11-16 17:23:25 -05:00
|
|
|
for specifier in specifiers {
|
2022-10-21 11:20:18 -04:00
|
|
|
if let Some(npm_resolver) = maybe_npm_resolver {
|
|
|
|
if npm_resolver.in_npm_package(referrer) {
|
|
|
|
// we're in an npm package, so use node resolution
|
|
|
|
results.push(Some(NodeResolution::into_specifier_and_media_type(
|
|
|
|
node::node_resolve(
|
2022-12-05 20:09:31 -05:00
|
|
|
specifier,
|
2022-10-21 11:20:18 -04:00
|
|
|
referrer,
|
2022-11-30 18:07:32 -05:00
|
|
|
NodeResolutionMode::Types,
|
2022-10-21 11:20:18 -04:00
|
|
|
npm_resolver,
|
2023-01-10 08:35:44 -05:00
|
|
|
&mut PermissionsContainer::allow_all(),
|
2022-10-21 11:20:18 -04:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.flatten(),
|
|
|
|
)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// handle npm:<package> urls
|
2021-11-16 17:23:25 -05:00
|
|
|
if specifier.starts_with("asset:") {
|
2022-12-05 20:09:31 -05:00
|
|
|
if let Ok(specifier) = ModuleSpecifier::parse(specifier) {
|
2021-11-16 17:23:25 -05:00
|
|
|
let media_type = MediaType::from(&specifier);
|
|
|
|
results.push(Some((specifier, media_type)));
|
|
|
|
} else {
|
|
|
|
results.push(None);
|
|
|
|
}
|
2022-12-05 20:09:31 -05:00
|
|
|
} else if let Some(dep) = dependencies.deps.get(specifier) {
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Resolved::Ok { specifier, .. } = &dep.maybe_type {
|
2022-10-21 11:20:18 -04:00
|
|
|
results.push(self.resolve_dependency(specifier, maybe_npm_resolver));
|
2022-01-31 17:33:57 -05:00
|
|
|
} else if let Resolved::Ok { specifier, .. } = &dep.maybe_code {
|
2022-10-21 11:20:18 -04:00
|
|
|
results.push(self.resolve_dependency(specifier, maybe_npm_resolver));
|
2021-01-22 05:03:16 -05:00
|
|
|
} else {
|
2021-10-28 19:56:01 -04:00
|
|
|
results.push(None);
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
2022-01-31 17:33:57 -05:00
|
|
|
} else if let Some(Resolved::Ok { specifier, .. }) =
|
2022-12-05 20:09:31 -05:00
|
|
|
self.resolve_imports_dependency(specifier)
|
2021-11-16 17:23:25 -05:00
|
|
|
{
|
|
|
|
// clone here to avoid double borrow of self
|
|
|
|
let specifier = specifier.clone();
|
2022-10-21 11:20:18 -04:00
|
|
|
results.push(self.resolve_dependency(&specifier, maybe_npm_resolver));
|
2022-12-05 20:09:31 -05:00
|
|
|
} else if let Ok(npm_ref) = NpmPackageReference::from_str(specifier) {
|
2022-10-21 11:20:18 -04:00
|
|
|
results.push(maybe_npm_resolver.map(|npm_resolver| {
|
|
|
|
NodeResolution::into_specifier_and_media_type(
|
|
|
|
node_resolve_npm_reference(
|
|
|
|
&npm_ref,
|
|
|
|
NodeResolutionMode::Types,
|
|
|
|
npm_resolver,
|
2023-01-10 08:35:44 -05:00
|
|
|
&mut PermissionsContainer::allow_all(),
|
2022-10-21 11:20:18 -04:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.flatten(),
|
|
|
|
)
|
|
|
|
}));
|
2021-11-16 17:23:25 -05:00
|
|
|
} else {
|
|
|
|
results.push(None);
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(results)
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Update the location of the on disk cache for the document store.
|
2022-02-01 21:04:26 -05:00
|
|
|
pub fn set_location(&mut self, location: &Path) {
|
2021-10-28 19:56:01 -04:00
|
|
|
// TODO update resolved dependencies?
|
2022-02-01 21:04:26 -05:00
|
|
|
self.cache = HttpCache::new(location);
|
|
|
|
self.specifier_resolver = Arc::new(SpecifierResolver::new(location));
|
2021-10-28 19:56:01 -04:00
|
|
|
self.dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Tries to cache a navigation tree that is associated with the provided specifier
|
|
|
|
/// if the document stored has the same script version.
|
|
|
|
pub fn try_cache_navigation_tree(
|
2022-12-19 20:22:17 -05:00
|
|
|
&self,
|
2021-06-04 17:31:44 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2021-11-18 13:50:24 -05:00
|
|
|
script_version: &str,
|
2021-10-28 19:56:01 -04:00
|
|
|
navigation_tree: Arc<tsc::NavigationTree>,
|
2021-06-04 17:31:44 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-19 20:22:17 -05:00
|
|
|
if let Some(doc) = self.open_docs.get(specifier) {
|
|
|
|
doc.update_navigation_tree_if_version(navigation_tree, script_version)
|
2021-11-18 13:50:24 -05:00
|
|
|
} else {
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
if let Some(doc) = file_system_docs.docs.get_mut(specifier) {
|
2022-12-19 20:22:17 -05:00
|
|
|
doc.update_navigation_tree_if_version(navigation_tree, script_version);
|
2021-11-18 13:50:24 -05:00
|
|
|
} else {
|
|
|
|
return Err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!("Specifier not found {}", specifier),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
pub fn update_config(
|
2021-11-08 20:26:39 -05:00
|
|
|
&mut self,
|
|
|
|
maybe_import_map: Option<Arc<import_map::ImportMap>>,
|
|
|
|
maybe_config_file: Option<&ConfigFile>,
|
|
|
|
) {
|
|
|
|
// TODO(@kitsonk) update resolved dependencies?
|
2022-11-02 10:47:02 -04:00
|
|
|
let maybe_jsx_config =
|
|
|
|
maybe_config_file.and_then(|cf| cf.to_maybe_jsx_import_source_config());
|
|
|
|
self.maybe_resolver =
|
|
|
|
CliResolver::maybe_new(maybe_jsx_config, maybe_import_map);
|
2021-11-18 13:50:24 -05:00
|
|
|
self.imports = Arc::new(
|
|
|
|
if let Some(Ok(Some(imports))) =
|
|
|
|
maybe_config_file.map(|cf| cf.to_maybe_imports())
|
|
|
|
{
|
|
|
|
imports
|
|
|
|
.into_iter()
|
|
|
|
.map(|(referrer, dependencies)| {
|
2022-08-09 17:27:22 -04:00
|
|
|
let graph_import = GraphImport::new(
|
2021-11-18 13:50:24 -05:00
|
|
|
referrer.clone(),
|
|
|
|
dependencies,
|
|
|
|
self.get_maybe_resolver(),
|
|
|
|
);
|
2022-08-09 17:27:22 -04:00
|
|
|
(referrer, graph_import)
|
2021-11-18 13:50:24 -05:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
HashMap::new()
|
|
|
|
},
|
|
|
|
);
|
2021-11-08 20:26:39 -05:00
|
|
|
self.dirty = true;
|
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Iterate through the documents, building a map where the key is a unique
|
|
|
|
/// document and the value is a set of specifiers that depend on that
|
|
|
|
/// document.
|
|
|
|
fn calculate_dependents_if_dirty(&mut self) {
|
2022-11-29 19:32:18 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct DocAnalyzer {
|
|
|
|
dependents_map: HashMap<ModuleSpecifier, HashSet<ModuleSpecifier>>,
|
|
|
|
analyzed_specifiers: HashSet<ModuleSpecifier>,
|
|
|
|
pending_specifiers: VecDeque<ModuleSpecifier>,
|
|
|
|
npm_reqs: HashSet<NpmPackageReq>,
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
|
2022-11-29 19:32:18 -05:00
|
|
|
impl DocAnalyzer {
|
|
|
|
fn add(&mut self, dep: &ModuleSpecifier, specifier: &ModuleSpecifier) {
|
|
|
|
if !self.analyzed_specifiers.contains(dep) {
|
|
|
|
self.analyzed_specifiers.insert(dep.clone());
|
|
|
|
// perf: ensure this is not added to unless this specifier has never
|
|
|
|
// been analyzed in order to not cause an extra file system lookup
|
|
|
|
self.pending_specifiers.push_back(dep.clone());
|
|
|
|
if let Ok(reference) = NpmPackageReference::from_specifier(dep) {
|
|
|
|
self.npm_reqs.insert(reference.req);
|
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
|
|
|
|
self
|
|
|
|
.dependents_map
|
2022-10-21 11:20:18 -04:00
|
|
|
.entry(dep.clone())
|
|
|
|
.or_default()
|
|
|
|
.insert(specifier.clone());
|
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
|
|
|
|
fn analyze_doc(&mut self, specifier: &ModuleSpecifier, doc: &Document) {
|
|
|
|
self.analyzed_specifiers.insert(specifier.clone());
|
|
|
|
for dependency in doc.dependencies().values() {
|
|
|
|
if let Some(dep) = dependency.get_code() {
|
|
|
|
self.add(dep, specifier);
|
|
|
|
}
|
|
|
|
if let Some(dep) = dependency.get_type() {
|
|
|
|
self.add(dep, specifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Resolved::Ok { specifier: dep, .. } =
|
|
|
|
doc.maybe_types_dependency()
|
|
|
|
{
|
|
|
|
self.add(&dep, specifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut file_system_docs = self.file_system_docs.lock();
|
|
|
|
if !file_system_docs.dirty && !self.dirty {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut doc_analyzer = DocAnalyzer::default();
|
|
|
|
// favor documents that are open in case a document exists in both collections
|
|
|
|
let documents = file_system_docs.docs.iter().chain(self.open_docs.iter());
|
|
|
|
for (specifier, doc) in documents {
|
|
|
|
doc_analyzer.analyze_doc(specifier, doc);
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
|
|
|
|
let maybe_resolver = self.get_maybe_resolver();
|
|
|
|
while let Some(specifier) = doc_analyzer.pending_specifiers.pop_front() {
|
|
|
|
if let Some(doc) =
|
|
|
|
file_system_docs.get(&self.cache, maybe_resolver, &specifier)
|
|
|
|
{
|
|
|
|
doc_analyzer.analyze_doc(&specifier, &doc);
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
2022-11-29 19:32:18 -05:00
|
|
|
|
|
|
|
self.dependents_map = Arc::new(doc_analyzer.dependents_map);
|
|
|
|
self.npm_reqs = Arc::new(doc_analyzer.npm_reqs);
|
2021-11-18 13:50:24 -05:00
|
|
|
self.dirty = false;
|
|
|
|
file_system_docs.dirty = false;
|
2021-11-12 11:42:04 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
fn get_maybe_resolver(&self) -> Option<&dyn deno_graph::source::Resolver> {
|
2022-11-02 10:47:02 -04:00
|
|
|
self.maybe_resolver.as_ref().map(|r| r.as_graph_resolver())
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
fn resolve_dependency(
|
2021-10-28 19:56:01 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-10-21 11:20:18 -04:00
|
|
|
maybe_npm_resolver: Option<&NpmPackageResolver>,
|
2021-11-18 13:50:24 -05:00
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
2022-10-21 11:20:18 -04:00
|
|
|
if let Ok(npm_ref) = NpmPackageReference::from_specifier(specifier) {
|
|
|
|
return maybe_npm_resolver.map(|npm_resolver| {
|
|
|
|
NodeResolution::into_specifier_and_media_type(
|
|
|
|
node_resolve_npm_reference(
|
|
|
|
&npm_ref,
|
|
|
|
NodeResolutionMode::Types,
|
|
|
|
npm_resolver,
|
2023-01-10 08:35:44 -05:00
|
|
|
&mut PermissionsContainer::allow_all(),
|
2022-10-21 11:20:18 -04:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.flatten(),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
let doc = self.get(specifier)?;
|
2022-02-24 20:03:12 -05:00
|
|
|
let maybe_module = doc.maybe_module().and_then(|r| r.as_ref().ok());
|
|
|
|
let maybe_types_dependency = maybe_module.and_then(|m| {
|
|
|
|
m.maybe_types_dependency
|
|
|
|
.as_ref()
|
|
|
|
.map(|(_, resolved)| resolved.clone())
|
|
|
|
});
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Some(Resolved::Ok { specifier, .. }) = maybe_types_dependency {
|
2022-10-21 11:20:18 -04:00
|
|
|
self.resolve_dependency(&specifier, maybe_npm_resolver)
|
2021-11-18 13:50:24 -05:00
|
|
|
} else {
|
|
|
|
let media_type = doc.media_type();
|
|
|
|
Some((specifier.clone(), media_type))
|
|
|
|
}
|
2021-10-28 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:50:24 -05:00
|
|
|
/// Iterate through any "imported" modules, checking to see if a dependency
|
|
|
|
/// is available. This is used to provide "global" imports like the JSX import
|
|
|
|
/// source.
|
|
|
|
fn resolve_imports_dependency(
|
2021-10-28 19:56:01 -04:00
|
|
|
&self,
|
|
|
|
specifier: &str,
|
2021-11-18 13:50:24 -05:00
|
|
|
) -> Option<&deno_graph::Resolved> {
|
2022-08-09 17:27:22 -04:00
|
|
|
for graph_imports in self.imports.values() {
|
|
|
|
let maybe_dep = graph_imports.dependencies.get(specifier);
|
2021-11-18 13:50:24 -05:00
|
|
|
if maybe_dep.is_some() {
|
2022-01-31 17:33:57 -05:00
|
|
|
return maybe_dep.map(|d| &d.maybe_type);
|
2021-11-18 13:50:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2022-10-20 13:23:21 -04:00
|
|
|
/// Loader that will look at the open documents.
|
2022-10-21 11:20:18 -04:00
|
|
|
pub struct OpenDocumentsGraphLoader<'a> {
|
2022-10-20 13:23:21 -04:00
|
|
|
pub inner_loader: &'a mut dyn deno_graph::source::Loader,
|
|
|
|
pub open_docs: &'a HashMap<ModuleSpecifier, Document>,
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
impl<'a> deno_graph::source::Loader for OpenDocumentsGraphLoader<'a> {
|
2022-10-20 13:23:21 -04:00
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
is_dynamic: bool,
|
|
|
|
) -> deno_graph::source::LoadFuture {
|
|
|
|
if specifier.scheme() == "file" {
|
|
|
|
if let Some(doc) = self.open_docs.get(specifier) {
|
|
|
|
return Box::pin(future::ready(Ok(Some(
|
|
|
|
deno_graph::source::LoadResponse::Module {
|
|
|
|
content: doc.content(),
|
|
|
|
specifier: doc.specifier().clone(),
|
|
|
|
maybe_headers: None,
|
|
|
|
},
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.inner_loader.load(specifier, is_dynamic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:14:59 -04:00
|
|
|
/// The default parser from `deno_graph` does not include the configuration
|
|
|
|
/// options we require for the lsp.
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct LspModuleParser;
|
|
|
|
|
|
|
|
impl deno_graph::ModuleParser for LspModuleParser {
|
|
|
|
fn parse_module(
|
|
|
|
&self,
|
|
|
|
specifier: &deno_graph::ModuleSpecifier,
|
|
|
|
source: Arc<str>,
|
|
|
|
media_type: MediaType,
|
|
|
|
) -> deno_core::anyhow::Result<ParsedSource, deno_ast::Diagnostic> {
|
|
|
|
deno_ast::parse_module(deno_ast::ParseParams {
|
|
|
|
specifier: specifier.to_string(),
|
|
|
|
text_info: SourceTextInfo::new(source),
|
|
|
|
media_type,
|
|
|
|
capture_tokens: true,
|
|
|
|
scope_analysis: true,
|
|
|
|
maybe_syntax: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lsp_deno_graph_analyze(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
content: Arc<str>,
|
|
|
|
maybe_headers: Option<&HashMap<String, String>>,
|
|
|
|
maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
|
|
|
|
) -> (MaybeModuleResult, MaybeParsedSourceResult) {
|
|
|
|
use deno_graph::ModuleParser;
|
|
|
|
|
|
|
|
let analyzer = deno_graph::CapturingModuleAnalyzer::new(
|
2022-12-17 17:20:15 -05:00
|
|
|
Some(Box::<LspModuleParser>::default()),
|
2022-08-22 12:14:59 -04:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
let parsed_source_result = analyzer.parse_module(
|
|
|
|
specifier,
|
|
|
|
content.clone(),
|
2022-09-07 15:06:18 -04:00
|
|
|
MediaType::from_specifier_and_headers(specifier, maybe_headers),
|
2022-08-22 12:14:59 -04:00
|
|
|
);
|
|
|
|
let module_result = match &parsed_source_result {
|
|
|
|
Ok(_) => deno_graph::parse_module(
|
|
|
|
specifier,
|
|
|
|
maybe_headers,
|
|
|
|
content,
|
|
|
|
Some(&deno_graph::ModuleKind::Esm),
|
|
|
|
maybe_resolver,
|
|
|
|
Some(&analyzer),
|
|
|
|
),
|
|
|
|
Err(err) => Err(deno_graph::ModuleGraphError::ParseErr(
|
|
|
|
specifier.clone(),
|
|
|
|
err.clone(),
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
|
|
|
(Some(module_result), Some(parsed_source_result))
|
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-04-01 11:15:37 -04:00
|
|
|
use test_util::TempDir;
|
2021-01-22 05:03:16 -05:00
|
|
|
|
2022-04-01 11:15:37 -04:00
|
|
|
fn setup(temp_dir: &TempDir) -> (Documents, PathBuf) {
|
2021-10-28 19:56:01 -04:00
|
|
|
let location = temp_dir.path().join("deps");
|
|
|
|
let documents = Documents::new(&location);
|
|
|
|
(documents, location)
|
2021-08-18 23:19:12 -04:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
#[test]
|
2021-10-28 19:56:01 -04:00
|
|
|
fn test_documents_open() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let (mut documents, _) = setup(&temp_dir);
|
2021-10-28 19:56:01 -04:00
|
|
|
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let content = r#"import * as b from "./b.ts";
|
2021-10-28 19:56:01 -04:00
|
|
|
console.log(b);
|
2022-05-20 16:40:55 -04:00
|
|
|
"#;
|
|
|
|
let document = documents.open(
|
|
|
|
specifier,
|
|
|
|
1,
|
|
|
|
"javascript".parse().unwrap(),
|
|
|
|
content.into(),
|
2021-06-02 06:29:58 -04:00
|
|
|
);
|
2021-11-12 11:42:04 -05:00
|
|
|
assert!(document.is_open());
|
|
|
|
assert!(document.is_diagnosable());
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-28 19:56:01 -04:00
|
|
|
fn test_documents_change() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let (mut documents, _) = setup(&temp_dir);
|
2021-10-28 19:56:01 -04:00
|
|
|
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let content = r#"import * as b from "./b.ts";
|
2021-10-28 19:56:01 -04:00
|
|
|
console.log(b);
|
2022-05-20 16:40:55 -04:00
|
|
|
"#;
|
2021-10-28 19:56:01 -04:00
|
|
|
documents.open(
|
2021-06-02 06:29:58 -04:00
|
|
|
specifier.clone(),
|
|
|
|
1,
|
2021-10-28 19:56:01 -04:00
|
|
|
"javascript".parse().unwrap(),
|
2022-05-20 16:40:55 -04:00
|
|
|
content.into(),
|
2021-06-02 06:29:58 -04:00
|
|
|
);
|
2021-10-28 19:56:01 -04:00
|
|
|
documents
|
2021-01-22 05:03:16 -05:00
|
|
|
.change(
|
|
|
|
&specifier,
|
|
|
|
2,
|
2021-01-29 14:34:33 -05:00
|
|
|
vec![lsp::TextDocumentContentChangeEvent {
|
|
|
|
range: Some(lsp::Range {
|
|
|
|
start: lsp::Position {
|
2021-10-28 19:56:01 -04:00
|
|
|
line: 1,
|
|
|
|
character: 13,
|
2021-01-22 05:03:16 -05:00
|
|
|
},
|
2021-01-29 14:34:33 -05:00
|
|
|
end: lsp::Position {
|
2021-10-28 19:56:01 -04:00
|
|
|
line: 1,
|
|
|
|
character: 13,
|
2021-01-22 05:03:16 -05:00
|
|
|
},
|
|
|
|
}),
|
2021-10-28 19:56:01 -04:00
|
|
|
range_length: None,
|
|
|
|
text: r#", "hello deno""#.to_string(),
|
2021-01-22 05:03:16 -05:00
|
|
|
}],
|
|
|
|
)
|
2021-10-28 19:56:01 -04:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
2022-05-20 16:40:55 -04:00
|
|
|
&*documents.get(&specifier).unwrap().content(),
|
2021-10-28 19:56:01 -04:00
|
|
|
r#"import * as b from "./b.ts";
|
|
|
|
console.log(b, "hello deno");
|
|
|
|
"#
|
2021-08-18 23:19:12 -04:00
|
|
|
);
|
2021-06-02 06:29:58 -04:00
|
|
|
}
|
2021-11-18 13:50:24 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_documents_ensure_no_duplicates() {
|
|
|
|
// it should never happen that a user of this API causes this to happen,
|
|
|
|
// but we'll guard against it anyway
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let (mut documents, documents_path) = setup(&temp_dir);
|
2021-11-18 13:50:24 -05:00
|
|
|
let file_path = documents_path.join("file.ts");
|
|
|
|
let file_specifier = ModuleSpecifier::from_file_path(&file_path).unwrap();
|
|
|
|
fs::create_dir_all(&documents_path).unwrap();
|
|
|
|
fs::write(&file_path, "").unwrap();
|
|
|
|
|
|
|
|
// open the document
|
|
|
|
documents.open(
|
|
|
|
file_specifier.clone(),
|
|
|
|
1,
|
|
|
|
LanguageId::TypeScript,
|
2022-05-20 16:40:55 -04:00
|
|
|
"".into(),
|
2021-11-18 13:50:24 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// make a clone of the document store and close the document in that one
|
|
|
|
let mut documents2 = documents.clone();
|
|
|
|
documents2.close(&file_specifier).unwrap();
|
|
|
|
|
|
|
|
// At this point the document will be in both documents and the shared file system documents.
|
|
|
|
// Now make sure that the original documents doesn't return both copies
|
|
|
|
assert_eq!(documents.documents(false, false).len(), 1);
|
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|