Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
fix(regression): Basic single file support
Browse files Browse the repository at this point in the history
If `PackageManager` tries to resolve a package and finds nothing (e.g. `current` is empty because there's no workspace), we create a temporary package from the file's parent directory.

Resolves: #232
  • Loading branch information
YDX-2147483647 authored and nvarner committed Aug 19, 2023
1 parent bfed5b3 commit e7abf09
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/workspace/package/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::{error, info, trace, warn};
use typst::diag::{FileError, PackageError as TypstPackageError};
use typst::syntax::{FileId, PackageSpec};

use crate::ext::UriError;
use crate::ext::{UriError, UrlExt};
use crate::workspace::fs::{FsError, FsResult};
use crate::workspace::package::external::manager::ExternalPackageManager;

Expand Down Expand Up @@ -50,6 +50,14 @@ impl PackageManager {
.current
.get(uri)
.cloned()
.or_else(|| {
trace!(
?uri,
"Take a single file's parent directory as the package root!"
);

Some(Package::new(uri.clone()))
})
.ok_or(CurrentPackageError::NotFound)?,
PackageIdInner::External(spec) => self.external_package(spec).await?,
};
Expand All @@ -64,6 +72,7 @@ impl PackageManager {
self.external
.full_id(uri)
.or_else(|| self.current_full_id(uri))
.or_else(|| self.current_single_file_full_id(uri))
.ok_or_else(|| FsError::NotProvided(anyhow!("could not find provider for URI")))
}

Expand All @@ -88,6 +97,24 @@ impl PackageManager {
Some(full_file_id)
}

fn current_single_file_full_id(&self, uri: &Url) -> Option<FullFileId> {
// Take uri's parent directory as the package root…
let mut root = uri.clone();
root.path_segments_mut().ok()?.pop();

// … and its filename as the path
let path = root.make_relative_rooted(uri).ok()?;
// This is equivalent to `Package::new(root.clone()).uri_to_path(uri).ok()?`,
// but doesn't clone `root`.

let package_id = PackageId::new_current(root);
let full_file_id = FullFileId::new(package_id, path);

trace!(?full_file_id, "chose a single file full id!");

Some(full_file_id)
}

#[tracing::instrument]
pub fn handle_change_event(&mut self, event: &WorkspaceFoldersChangeEvent) {
let removed = event.removed.iter().map(|folder| &folder.uri).collect_vec();
Expand Down

0 comments on commit e7abf09

Please sign in to comment.