1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 08:39:09 -05:00

fix(lsp): show diagnostics for untitled files (#20916)

This commit is contained in:
Nayeem Rahman 2023-10-16 20:21:57 +01:00 committed by GitHub
parent cb70c4d0c4
commit ebb7fe412e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 18 deletions

View file

@ -2,7 +2,7 @@
use super::client::Client; use super::client::Client;
use super::config::ConfigSnapshot; use super::config::ConfigSnapshot;
use super::documents::cell_to_file_specifier; use super::documents::file_like_to_file_specifier;
use super::documents::Documents; use super::documents::Documents;
use super::documents::DocumentsFilter; use super::documents::DocumentsFilter;
use super::lsp_custom; use super::lsp_custom;
@ -365,7 +365,7 @@ fn get_local_completions(
current: &str, current: &str,
range: &lsp::Range, range: &lsp::Range,
) -> Option<Vec<lsp::CompletionItem>> { ) -> Option<Vec<lsp::CompletionItem>> {
let base = match cell_to_file_specifier(base) { let base = match file_like_to_file_specifier(base) {
Some(s) => s, Some(s) => s,
None => base.clone(), None => base.clone(),
}; };

View file

@ -89,12 +89,13 @@ static TSX_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
.collect() .collect()
}); });
pub const DOCUMENT_SCHEMES: [&str; 6] = [ pub const DOCUMENT_SCHEMES: [&str; 7] = [
"data", "data",
"blob", "blob",
"file", "file",
"http", "http",
"https", "https",
"untitled",
"deno-notebook-cell", "deno-notebook-cell",
]; ];
@ -259,22 +260,24 @@ impl AssetOrDocument {
} }
} }
/// Convert a `deno-notebook-cell:` specifier to a `file:` specifier. /// Convert a e.g. `deno-notebook-cell:` specifier to a `file:` specifier.
/// ```rust /// ```rust
/// assert_eq!( /// assert_eq!(
/// cell_to_file_specifier( /// file_like_to_file_specifier(
/// &Url::parse("deno-notebook-cell:/path/to/file.ipynb#abc").unwrap(), /// &Url::parse("deno-notebook-cell:/path/to/file.ipynb#abc").unwrap(),
/// ), /// ),
/// Some(Url::parse("file:///path/to/file.ipynb#abc").unwrap()), /// Some(Url::parse("file:///path/to/file.ipynb#abc").unwrap()),
/// ); /// );
pub fn cell_to_file_specifier(specifier: &Url) -> Option<Url> { pub fn file_like_to_file_specifier(specifier: &Url) -> Option<Url> {
if specifier.scheme() == "deno-notebook-cell" { if matches!(specifier.scheme(), "untitled" | "deno-notebook-cell") {
if let Ok(specifier) = ModuleSpecifier::parse(&format!( if let Ok(mut s) = ModuleSpecifier::parse(&format!(
"file://{}", "file://{}",
&specifier.as_str() &specifier.as_str()
[url::quirks::internal_components(specifier).host_end as usize..], [url::quirks::internal_components(specifier).host_end as usize..],
)) { )) {
return Some(specifier); s.query_pairs_mut()
.append_pair("scheme", specifier.scheme());
return Some(s);
} }
} }
None None
@ -300,22 +303,28 @@ impl DocumentDependencies {
deps: module.dependencies.clone(), deps: module.dependencies.clone(),
maybe_types_dependency: module.maybe_types_dependency.clone(), maybe_types_dependency: module.maybe_types_dependency.clone(),
}; };
if module.specifier.scheme() == "deno-notebook-cell" { if file_like_to_file_specifier(&module.specifier).is_some() {
for (_, dep) in &mut deps.deps { for (_, dep) in &mut deps.deps {
if let Resolution::Ok(resolved) = &mut dep.maybe_code { if let Resolution::Ok(resolved) = &mut dep.maybe_code {
if let Some(specifier) = cell_to_file_specifier(&resolved.specifier) { if let Some(specifier) =
file_like_to_file_specifier(&resolved.specifier)
{
resolved.specifier = specifier; resolved.specifier = specifier;
} }
} }
if let Resolution::Ok(resolved) = &mut dep.maybe_type { if let Resolution::Ok(resolved) = &mut dep.maybe_type {
if let Some(specifier) = cell_to_file_specifier(&resolved.specifier) { if let Some(specifier) =
file_like_to_file_specifier(&resolved.specifier)
{
resolved.specifier = specifier; resolved.specifier = specifier;
} }
} }
} }
if let Some(dep) = &mut deps.maybe_types_dependency { if let Some(dep) = &mut deps.maybe_types_dependency {
if let Resolution::Ok(resolved) = &mut dep.dependency { if let Resolution::Ok(resolved) = &mut dep.dependency {
if let Some(specifier) = cell_to_file_specifier(&resolved.specifier) { if let Some(specifier) =
file_like_to_file_specifier(&resolved.specifier)
{
resolved.specifier = specifier; resolved.specifier = specifier;
} }
} }

View file

@ -3,7 +3,7 @@
use super::analysis::CodeActionData; use super::analysis::CodeActionData;
use super::code_lens; use super::code_lens;
use super::config; use super::config;
use super::documents::cell_to_file_specifier; use super::documents::file_like_to_file_specifier;
use super::documents::AssetOrDocument; use super::documents::AssetOrDocument;
use super::documents::DocumentsFilter; use super::documents::DocumentsFilter;
use super::language_server; use super::language_server;
@ -3664,11 +3664,12 @@ impl TscSpecifierMap {
if let Some(specifier) = self.denormalized_specifiers.get(original) { if let Some(specifier) = self.denormalized_specifiers.get(original) {
return specifier.to_string(); return specifier.to_string();
} }
let mut specifier = original.to_string(); let mut specifier = if let Some(s) = file_like_to_file_specifier(original) {
s.to_string()
} else {
original.to_string()
};
let media_type = if original.scheme() == "deno-notebook-cell" { let media_type = if original.scheme() == "deno-notebook-cell" {
if let Some(s) = cell_to_file_specifier(original) {
specifier = s.to_string();
}
MediaType::TypeScript MediaType::TypeScript
} else { } else {
MediaType::from_specifier(original) MediaType::from_specifier(original)

View file

@ -8041,6 +8041,49 @@ fn lsp_jupyter_diagnostics() {
client.shutdown(); client.shutdown();
} }
#[test]
fn lsp_untitled_file_diagnostics() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default();
let diagnostics = client.did_open(json!({
"textDocument": {
"uri": "untitled:///a/file.ts",
"languageId": "typescript",
"version": 1,
"text": "Deno.readTextFileSync(1234);",
},
}));
assert_eq!(
json!(diagnostics.all_messages()),
json!([
{
"uri": "untitled:///a/file.ts",
"diagnostics": [
{
"range": {
"start": {
"line": 0,
"character": 22,
},
"end": {
"line": 0,
"character": 26,
},
},
"severity": 1,
"code": 2345,
"source": "deno-ts",
"message": "Argument of type 'number' is not assignable to parameter of type 'string | URL'.",
},
],
"version": 1,
},
])
);
client.shutdown();
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct PerformanceAverage { pub struct PerformanceAverage {