summaryrefslogtreecommitdiff
path: root/cli/lsp
diff options
context:
space:
mode:
Diffstat (limited to 'cli/lsp')
-rw-r--r--cli/lsp/completions.rs8
-rw-r--r--cli/lsp/diagnostics.rs63
-rw-r--r--cli/lsp/documents.rs3
-rw-r--r--cli/lsp/language_server.rs40
-rw-r--r--cli/lsp/tsc.rs3
5 files changed, 45 insertions, 72 deletions
diff --git a/cli/lsp/completions.rs b/cli/lsp/completions.rs
index b23145989..0282bd08c 100644
--- a/cli/lsp/completions.rs
+++ b/cli/lsp/completions.rs
@@ -5,6 +5,7 @@ use super::lsp_custom;
use super::tsc;
use crate::fs_util::is_supported_ext;
+use crate::fs_util::specifier_to_file_path;
use deno_core::normalize_path;
use deno_core::resolve_path;
@@ -180,7 +181,7 @@ fn get_local_completions(
return None;
}
- let mut base_path = base.to_file_path().ok()?;
+ let mut base_path = specifier_to_file_path(base).ok()?;
base_path.pop();
let mut current_path = normalize_path(base_path.join(current));
// if the current text does not end in a `/` then we are still selecting on
@@ -340,7 +341,10 @@ fn relative_specifier(
|| specifier.port_or_known_default() != base.port_or_known_default()
{
if specifier.scheme() == "file" {
- specifier.to_file_path().unwrap().to_string_lossy().into()
+ specifier_to_file_path(specifier)
+ .unwrap()
+ .to_string_lossy()
+ .into()
} else {
specifier.as_str().into()
}
diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs
index 6ce28f440..9bf45c5ae 100644
--- a/cli/lsp/diagnostics.rs
+++ b/cli/lsp/diagnostics.rs
@@ -8,6 +8,7 @@ use super::language_server;
use super::tsc;
use crate::diagnostics;
+use crate::fs_util::specifier_to_file_path;
use deno_core::anyhow::anyhow;
use deno_core::error::AnyError;
@@ -301,68 +302,46 @@ fn ts_json_to_diagnostics(
.collect()
}
-// Returns `ConfigSnapshot::root_uri` in the correct format.
-fn get_root_specifier(
- snapshot: &language_server::StateSnapshot,
-) -> Option<ModuleSpecifier> {
- let root = snapshot.config.root_uri.as_ref()?;
- let root = root.to_file_path().ok()?;
-
- // FIXME: `root_uri` from `ConfigSnapshot` are without a trailing slash,
- // so `Url::join` treats the root as a file, not a directory, and erases the folder name.
- // To fix that behaviour we just parsing `root_uri` again.
- ModuleSpecifier::from_directory_path(root).ok()
-}
-
// Filters documents according to the `include` and the `exclude` lists (from `StateSnapshot::maybe_lint_config`).
// If a document is in the `exclude` list - then it be removed.
// If the `include` list is not empty, and a document is not in - then it be removed too.
-fn filter_documents(
+fn filter_lint_documents(
snapshot: &language_server::StateSnapshot,
documents: &mut Vec<Document>,
) {
- let root_uri = match get_root_specifier(snapshot) {
- Some(uri) => uri,
- None => return,
- };
-
- let linter_config = match &snapshot.maybe_lint_config {
+ let lint_config = match &snapshot.maybe_lint_config {
Some(config) => config,
None => return,
};
- let join_specifiers = |specifiers: &Vec<String>| -> Vec<ModuleSpecifier> {
- specifiers
- .iter()
- .filter_map(|i| root_uri.join(i).ok())
- .collect()
- };
-
- let ignore_specifiers = join_specifiers(&linter_config.files.exclude);
- let include_specifiers = join_specifiers(&linter_config.files.include);
-
documents.retain(|doc| {
- let path = doc.specifier().path();
+ let path = if let Ok(file_path) = specifier_to_file_path(doc.specifier()) {
+ file_path
+ } else {
+ return false;
+ };
// Skip files which is in the exclude list.
- if ignore_specifiers.iter().any(|i| path.starts_with(i.path())) {
+ if lint_config
+ .files
+ .exclude
+ .iter()
+ .any(|i| path.starts_with(i))
+ {
return false;
}
// Early return if the include list is empty.
- if include_specifiers.is_empty() {
+ if lint_config.files.include.is_empty() {
return true;
}
- // Ignore files which is not in the include list.
- if !include_specifiers
+ // Ignore files not in the include list.
+ lint_config
+ .files
+ .include
.iter()
- .any(|i| path.starts_with(i.path()))
- {
- return false;
- }
-
- true
+ .any(|i| path.starts_with(i))
});
}
@@ -374,7 +353,7 @@ async fn generate_lint_diagnostics(
let workspace_settings = snapshot.config.settings.workspace.clone();
let maybe_lint_config = snapshot.maybe_lint_config.clone();
- filter_documents(snapshot, &mut documents);
+ filter_lint_documents(snapshot, &mut documents);
tokio::task::spawn(async move {
let mut diagnostics_vec = Vec::new();
diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs
index a46fdb8ce..d5acac8d7 100644
--- a/cli/lsp/documents.rs
+++ b/cli/lsp/documents.rs
@@ -8,6 +8,7 @@ use crate::config_file::ConfigFile;
use crate::file_fetcher::get_source_from_bytes;
use crate::file_fetcher::map_content_type;
use crate::file_fetcher::SUPPORTED_SCHEMES;
+use crate::fs_util::specifier_to_file_path;
use crate::http_cache;
use crate::http_cache::HttpCache;
use crate::resolver::ImportMapResolver;
@@ -741,7 +742,7 @@ fn get_document_path(
specifier: &ModuleSpecifier,
) -> Option<PathBuf> {
if specifier.scheme() == "file" {
- specifier.to_file_path().ok()
+ specifier_to_file_path(specifier).ok()
} else {
let path = cache.get_cache_filename(specifier)?;
if path.is_file() {
diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs
index a0d0ee0ad..e13215efb 100644
--- a/cli/lsp/language_server.rs
+++ b/cli/lsp/language_server.rs
@@ -60,6 +60,7 @@ use crate::config_file::TsConfig;
use crate::deno_dir;
use crate::file_fetcher::get_source_from_data_url;
use crate::fs_util;
+use crate::fs_util::specifier_to_file_path;
use crate::logger;
use crate::tools::fmt::format_file;
use crate::tools::fmt::format_parsed_source;
@@ -303,9 +304,7 @@ impl Inner {
let config_url = if let Ok(url) = Url::from_file_path(config_str) {
Ok(url)
} else if let Some(root_uri) = maybe_root_uri {
- let root_path = root_uri
- .to_file_path()
- .map_err(|_| anyhow!("Bad root_uri: {}", root_uri))?;
+ let root_path = specifier_to_file_path(&root_uri)?;
let config_path = root_path.join(config_str);
Url::from_file_path(config_path).map_err(|_| {
anyhow!("Bad file path for configuration file: \"{}\"", config_str)
@@ -319,9 +318,7 @@ impl Inner {
info!(" Resolved configuration file: \"{}\"", config_url);
let config_file = {
- let buffer = config_url
- .to_file_path()
- .map_err(|_| anyhow!("Bad uri: \"{}\"", config_url))?;
+ let buffer = specifier_to_file_path(&config_url)?;
let path = buffer
.to_str()
.ok_or_else(|| anyhow!("Bad uri: \"{}\"", config_url))?;
@@ -404,9 +401,7 @@ impl Inner {
let cache_url = if let Ok(url) = Url::from_file_path(cache_str) {
Ok(url)
} else if let Some(root_uri) = &maybe_root_uri {
- let root_path = root_uri
- .to_file_path()
- .map_err(|_| anyhow!("Bad root_uri: {}", root_uri))?;
+ let root_path = specifier_to_file_path(root_uri)?;
let cache_path = root_path.join(cache_str);
Url::from_file_path(cache_path).map_err(|_| {
anyhow!("Bad file path for import path: {:?}", cache_str)
@@ -417,9 +412,7 @@ impl Inner {
cache_str
))
}?;
- let cache_path = cache_url.to_file_path().map_err(|_| {
- anyhow!("Cannot convert \"{}\" into a file path.", cache_url)
- })?;
+ let cache_path = specifier_to_file_path(&cache_url)?;
info!(
" Resolved cache path: \"{}\"",
cache_path.to_string_lossy()
@@ -464,9 +457,7 @@ impl Inner {
anyhow!("Bad data url for import map: {:?}", import_map_str)
})
} else if let Some(root_uri) = &maybe_root_uri {
- let root_path = root_uri
- .to_file_path()
- .map_err(|_| anyhow!("Bad root_uri: {}", root_uri))?;
+ let root_path = specifier_to_file_path(root_uri)?;
let import_map_path = root_path.join(import_map_str);
Url::from_file_path(import_map_path).map_err(|_| {
anyhow!("Bad file path for import map: {:?}", import_map_str)
@@ -481,9 +472,7 @@ impl Inner {
let import_map_json = if import_map_url.scheme() == "data" {
get_source_from_data_url(&import_map_url)?.0
} else {
- let import_map_path = import_map_url.to_file_path().map_err(|_| {
- anyhow!("Cannot convert \"{}\" into a file path.", import_map_url)
- })?;
+ let import_map_path = specifier_to_file_path(&import_map_url)?;
info!(
" Resolved import map: \"{}\"",
import_map_path.to_string_lossy()
@@ -1024,11 +1013,10 @@ impl Inner {
};
let mark = self.performance.mark("formatting", Some(&params));
let file_path =
- if let Ok(file_path) = params.text_document.uri.to_file_path() {
- file_path
- } else {
- PathBuf::from(params.text_document.uri.path())
- };
+ specifier_to_file_path(&params.text_document.uri).map_err(|err| {
+ error!("{}", err);
+ LspError::invalid_request()
+ })?;
let fmt_options = if let Some(fmt_config) = self.maybe_fmt_config.as_ref() {
fmt_config.options.clone()
@@ -1913,7 +1901,7 @@ impl Inner {
.config
.root_uri
.as_ref()
- .and_then(|uri| uri.to_file_path().ok());
+ .and_then(|uri| specifier_to_file_path(uri).ok());
let mut resolved_items = Vec::<CallHierarchyIncomingCall>::new();
for item in incoming_calls.iter() {
if let Some(resolved) = item
@@ -1962,7 +1950,7 @@ impl Inner {
.config
.root_uri
.as_ref()
- .and_then(|uri| uri.to_file_path().ok());
+ .and_then(|uri| specifier_to_file_path(uri).ok());
let mut resolved_items = Vec::<CallHierarchyOutgoingCall>::new();
for item in outgoing_calls.iter() {
if let Some(resolved) = item
@@ -2018,7 +2006,7 @@ impl Inner {
.config
.root_uri
.as_ref()
- .and_then(|uri| uri.to_file_path().ok());
+ .and_then(|uri| specifier_to_file_path(uri).ok());
let mut resolved_items = Vec::<CallHierarchyItem>::new();
match one_or_many {
tsc::OneOrMany::One(item) => {
diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs
index 9647a79fc..6eaaf7ff5 100644
--- a/cli/lsp/tsc.rs
+++ b/cli/lsp/tsc.rs
@@ -16,6 +16,7 @@ use super::text::LineIndex;
use super::urls::INVALID_SPECIFIER;
use crate::config_file::TsConfig;
+use crate::fs_util::specifier_to_file_path;
use crate::tsc;
use crate::tsc::ResolveArgs;
@@ -1512,7 +1513,7 @@ impl CallHierarchyItem {
let use_file_name = self.is_source_file_item();
let maybe_file_path = if uri.scheme() == "file" {
- uri.to_file_path().ok()
+ specifier_to_file_path(&uri).ok()
} else {
None
};