summaryrefslogtreecommitdiff
path: root/cli/lsp/documents.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/lsp/documents.rs')
-rw-r--r--cli/lsp/documents.rs56
1 files changed, 25 insertions, 31 deletions
diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs
index f18284db7..11662a8fc 100644
--- a/cli/lsp/documents.rs
+++ b/cli/lsp/documents.rs
@@ -1,6 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use super::cache::calculate_fs_version;
+use super::cache::calculate_fs_version_at_path;
use super::text::LineIndex;
use super::tsc;
use super::tsc::AssetDocument;
@@ -9,7 +10,6 @@ use crate::args::package_json;
use crate::args::package_json::PackageJsonDeps;
use crate::args::ConfigFile;
use crate::args::JsxImportSourceConfig;
-use crate::cache::CachedUrlMetadata;
use crate::cache::FastInsecureHasher;
use crate::cache::HttpCache;
use crate::file_fetcher::get_source_from_bytes;
@@ -656,7 +656,7 @@ fn recurse_dependents(
}
}
-#[derive(Debug, Default)]
+#[derive(Debug)]
struct SpecifierResolver {
cache: HttpCache,
redirects: Mutex<HashMap<ModuleSpecifier, ModuleSpecifier>>,
@@ -698,10 +698,12 @@ impl SpecifierResolver {
specifier: &ModuleSpecifier,
redirect_limit: usize,
) -> Option<ModuleSpecifier> {
- let cache_filename = self.cache.get_cache_filename(specifier)?;
- if redirect_limit > 0 && cache_filename.is_file() {
- let headers = CachedUrlMetadata::read(&cache_filename)
+ if redirect_limit > 0 {
+ let headers = self
+ .cache
+ .get(specifier)
.ok()
+ .and_then(|i| i.read_metadata().ok()?)
.map(|m| m.headers)?;
if let Some(location) = headers.get("location") {
let redirect =
@@ -732,8 +734,7 @@ impl FileSystemDocuments {
let fs_version = if specifier.scheme() == "data" {
Some("1".to_string())
} else {
- get_document_path(cache, specifier)
- .and_then(|path| calculate_fs_version(&path))
+ calculate_fs_version(cache, specifier)
};
let file_system_doc = self.docs.get(specifier);
if file_system_doc.map(|d| d.fs_version().to_string()) != fs_version {
@@ -753,8 +754,8 @@ impl FileSystemDocuments {
specifier: &ModuleSpecifier,
) -> Option<Document> {
let doc = if specifier.scheme() == "file" {
- let path = get_document_path(cache, specifier)?;
- let fs_version = calculate_fs_version(&path)?;
+ let path = specifier_to_file_path(specifier).ok()?;
+ let fs_version = calculate_fs_version_at_path(&path)?;
let bytes = fs::read(path).ok()?;
let maybe_charset =
Some(text_encoding::detect_charset(&bytes).to_string());
@@ -776,11 +777,10 @@ impl FileSystemDocuments {
resolver,
)
} else {
- let path = get_document_path(cache, specifier)?;
- let fs_version = calculate_fs_version(&path)?;
- let bytes = fs::read(path).ok()?;
- let cache_filename = cache.get_cache_filename(specifier)?;
- let specifier_metadata = CachedUrlMetadata::read(&cache_filename).ok()?;
+ let fs_version = calculate_fs_version(cache, specifier)?;
+ let cache_item = cache.get(specifier).ok()?;
+ let bytes = cache_item.read_to_bytes().ok()??;
+ let specifier_metadata = cache_item.read_metadata().ok()??;
let maybe_content_type = specifier_metadata.headers.get("content-type");
let (_, maybe_charset) = map_content_type(specifier, maybe_content_type);
let maybe_headers = Some(specifier_metadata.headers);
@@ -799,17 +799,6 @@ impl FileSystemDocuments {
}
}
-fn get_document_path(
- cache: &HttpCache,
- specifier: &ModuleSpecifier,
-) -> Option<PathBuf> {
- match specifier.scheme() {
- "npm" | "node" | "data" | "blob" => None,
- "file" => specifier_to_file_path(specifier).ok(),
- _ => cache.get_cache_filename(specifier),
- }
-}
-
pub struct UpdateDocumentConfigOptions<'a> {
pub enabled_urls: Vec<Url>,
pub document_preload_limit: usize,
@@ -831,7 +820,7 @@ pub enum DocumentsFilter {
OpenDiagnosable,
}
-#[derive(Debug, Clone, Default)]
+#[derive(Debug, Clone)]
pub struct Documents {
/// The DENO_DIR that the documents looks for non-file based modules.
cache: HttpCache,
@@ -864,8 +853,7 @@ pub struct Documents {
}
impl Documents {
- pub fn new(location: PathBuf) -> Self {
- let cache = HttpCache::new(location);
+ pub fn new(cache: HttpCache) -> Self {
Self {
cache: cache.clone(),
dirty: true,
@@ -990,8 +978,13 @@ impl Documents {
if specifier.scheme() == "data" {
return true;
}
- if let Some(path) = get_document_path(&self.cache, &specifier) {
- return path.is_file();
+ if specifier.scheme() == "file" {
+ return specifier_to_file_path(&specifier)
+ .map(|p| p.is_file())
+ .unwrap_or(false);
+ }
+ if self.cache.contains(&specifier) {
+ return true;
}
}
false
@@ -1859,7 +1852,8 @@ mod tests {
fn setup(temp_dir: &TempDir) -> (Documents, PathRef) {
let location = temp_dir.path().join("deps");
- let documents = Documents::new(location.to_path_buf());
+ let cache = HttpCache::new(location.to_path_buf());
+ let documents = Documents::new(cache);
(documents, location)
}