Skip to content

Commit

Permalink
feat(lsp): jupyter notebook analysis (#20719)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Sep 29, 2023
1 parent 61b91e1 commit 2d1af0c
Show file tree
Hide file tree
Showing 7 changed files with 623 additions and 135 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ clap = { version = "=4.3.3", features = ["string"] }
clap_complete = "=4.3.1"
clap_complete_fig = "=4.3.1"
console_static_text.workspace = true
dashmap = "5.5.3"
data-encoding.workspace = true
data-url.workspace = true
dissimilar = "=1.0.4"
Expand Down
12 changes: 9 additions & 3 deletions cli/lsp/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

if base.scheme() != "file" {
return None;
}

let mut base_path = specifier_to_file_path(base).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
Expand All @@ -388,10 +394,10 @@ fn get_local_completions(
let de = de.ok()?;
let label = de.path().file_name()?.to_string_lossy().to_string();
let entry_specifier = resolve_path(de.path().to_str()?, &cwd).ok()?;
if &entry_specifier == base {
if entry_specifier == base {
return None;
}
let full_text = relative_specifier(base, &entry_specifier)?;
let full_text = relative_specifier(&base, &entry_specifier)?;
// this weeds out situations where we are browsing in the parent, but
// we want to filter out non-matches when the completion is manually
// invoked by the user, but still allows for things like `../src/../`
Expand Down
4 changes: 4 additions & 0 deletions cli/lsp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,14 @@ impl WorkspaceSettings {
self.code_lens.implementations || self.code_lens.references
}

// TODO(nayeemrmn): Factor in out-of-band media type here.
pub fn language_settings_for_specifier(
&self,
specifier: &ModuleSpecifier,
) -> Option<&LanguageWorkspaceSettings> {
if specifier.scheme() == "deno-notebook-cell" {
return Some(&self.typescript);
}
match MediaType::from_specifier(specifier) {
MediaType::JavaScript
| MediaType::Jsx
Expand Down
63 changes: 57 additions & 6 deletions cli/lsp/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::cache::HttpCache;
use crate::file_fetcher::get_source_from_bytes;
use crate::file_fetcher::get_source_from_data_url;
use crate::file_fetcher::map_content_type;
use crate::file_fetcher::SUPPORTED_SCHEMES;
use crate::lsp::logging::lsp_warn;
use crate::npm::CliNpmRegistryApi;
use crate::npm::NpmResolution;
Expand Down Expand Up @@ -93,6 +92,15 @@ static TSX_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
.collect()
});

pub const DOCUMENT_SCHEMES: [&str; 6] = [
"data",
"blob",
"file",
"http",
"https",
"deno-notebook-cell",
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LanguageId {
JavaScript,
Expand Down Expand Up @@ -254,6 +262,27 @@ impl AssetOrDocument {
}
}

/// Convert a `deno-notebook-cell:` specifier to a `file:` specifier.
/// ```rust
/// assert_eq!(
/// cell_to_file_specifier(
/// &Url::parse("deno-notebook-cell:/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> {
if specifier.scheme() == "deno-notebook-cell" {
if let Ok(specifier) = ModuleSpecifier::parse(&format!(
"file://{}",
&specifier.as_str()
[url::quirks::internal_components(specifier).host_end as usize..],
)) {
return Some(specifier);
}
}
None
}

#[derive(Debug, Default)]
struct DocumentDependencies {
deps: IndexMap<String, deno_graph::Dependency>,
Expand All @@ -270,10 +299,32 @@ impl DocumentDependencies {
}

pub fn from_module(module: &deno_graph::EsmModule) -> Self {
Self {
let mut deps = Self {
deps: module.dependencies.clone(),
maybe_types_dependency: module.maybe_types_dependency.clone(),
};
if module.specifier.scheme() == "deno-notebook-cell" {
for (_, dep) in &mut deps.deps {
if let Resolution::Ok(resolved) = &mut dep.maybe_code {
if let Some(specifier) = cell_to_file_specifier(&resolved.specifier) {
resolved.specifier = specifier;
}
}
if let Resolution::Ok(resolved) = &mut dep.maybe_type {
if let Some(specifier) = cell_to_file_specifier(&resolved.specifier) {
resolved.specifier = specifier;
}
}
}
if let Some(dep) = &mut deps.maybe_types_dependency {
if let Resolution::Ok(resolved) = &mut dep.dependency {
if let Some(specifier) = cell_to_file_specifier(&resolved.specifier) {
resolved.specifier = specifier;
}
}
}
}
deps
}
}

Expand Down Expand Up @@ -677,13 +728,11 @@ impl SpecifierResolver {
specifier: &ModuleSpecifier,
) -> Option<ModuleSpecifier> {
let scheme = specifier.scheme();
if !SUPPORTED_SCHEMES.contains(&scheme) {
if !DOCUMENT_SCHEMES.contains(&scheme) {
return None;
}

if scheme == "data" || scheme == "blob" || scheme == "file" {
Some(specifier.clone())
} else {
if scheme == "http" || scheme == "https" {
let mut redirects = self.redirects.lock();
if let Some(specifier) = redirects.get(specifier) {
Some(specifier.clone())
Expand All @@ -692,6 +741,8 @@ impl SpecifierResolver {
redirects.insert(specifier.clone(), redirect.clone());
Some(redirect)
}
} else {
Some(specifier.clone())
}
}

Expand Down
Loading

0 comments on commit 2d1af0c

Please sign in to comment.