Skip to content

Commit

Permalink
Fix opening untitled documents (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster authored Oct 23, 2024
1 parent d9ac8a1 commit c64a8e7
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Fix opening `untitled` documents ([#1242](https://github.com/latex-lsp/texlab/issues/1242))

## [5.20.0] - 2024-10-08

### Added
Expand Down
2 changes: 1 addition & 1 deletion crates/base-db/src/deps/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn watch(
) {
let roots = workspace
.iter()
.map(|document| &document.dir)
.filter_map(|document| document.dir.as_ref())
.filter(|dir| dir.scheme() == "file")
.unique()
.map(|dir| ProjectRoot::walk_and_find(workspace, dir));
Expand Down
8 changes: 6 additions & 2 deletions crates/base-db/src/deps/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ impl Graph {
start: start.uri.clone(),
};

let root = ProjectRoot::walk_and_find(workspace, &start.dir);
let Some(start_dir) = &start.dir else {
return graph;
};

let root = ProjectRoot::walk_and_find(workspace, start_dir);

let mut stack = vec![(start, Rc::new(root))];
let mut visited = FxHashSet::default();
Expand Down Expand Up @@ -135,7 +139,7 @@ impl Graph {
.as_deref()
.and_then(|dir| Url::from_directory_path(dir).ok());

let working_dir = working_dir.as_ref().unwrap_or(&start.source.dir);
let working_dir = working_dir.as_ref().or(start.source.dir.as_ref())?;
let new_root = Arc::new(ProjectRoot::from_config(workspace, working_dir));

for target_uri in file_list
Expand Down
6 changes: 3 additions & 3 deletions crates/base-db/src/deps/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl ProjectRoot {
pub fn from_tectonic(workspace: &Workspace, dir: &Url) -> Option<Self> {
let exists = workspace
.iter()
.filter(|document| document.dir == *dir)
.filter(|document| document.dir.as_ref() == Some(dir))
.any(|document| matches!(document.data, DocumentData::Tectonic));

if !exists {
Expand Down Expand Up @@ -77,7 +77,7 @@ impl ProjectRoot {
let config = workspace.config();
let rcfile = workspace
.iter()
.filter(|document| document.dir == *dir)
.filter(|document| document.dir.as_ref() == Some(dir))
.find_map(|document| document.data.as_latexmkrc())?;

let compile_dir = dir.clone();
Expand Down Expand Up @@ -121,7 +121,7 @@ impl ProjectRoot {
pub fn from_rootfile(workspace: &Workspace, dir: &Url) -> Option<Self> {
let exists = workspace
.iter()
.filter(|document| document.dir == *dir)
.filter(|document| document.dir.as_ref() == Some(dir))
.any(|document| matches!(document.data, DocumentData::Root));

if !exists {
Expand Down
4 changes: 2 additions & 2 deletions crates/base-db/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct DocumentParams<'a> {
#[derive(Clone)]
pub struct Document {
pub uri: Url,
pub dir: Url,
pub dir: Option<Url>,
pub path: Option<PathBuf>,
pub text: String,
pub line_index: LineIndex,
Expand All @@ -42,7 +42,7 @@ impl Document {
pub fn parse(params: DocumentParams) -> Self {
let DocumentParams { uri, text, .. } = params;

let dir = uri.join(".").unwrap();
let dir = uri.join(".").ok();

let path = if uri.scheme() == "file" {
uri.to_file_path().ok()
Expand Down
6 changes: 5 additions & 1 deletion crates/commands/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ impl BuildCommand {
.next()
.unwrap_or(document);

let Some(document_dir) = &document.dir else {
return Err(BuildError::NotLocal(document.uri.clone()));
};

let Some(path) = document.path.as_deref().and_then(Path::to_str) else {
return Err(BuildError::NotLocal(document.uri.clone()));
};
Expand All @@ -55,7 +59,7 @@ impl BuildCommand {
let program = config.program.clone();
let args = replace_placeholders(&config.args, &[('f', path)]);

let root = ProjectRoot::walk_and_find(workspace, &document.dir);
let root = ProjectRoot::walk_and_find(workspace, document_dir);

let Ok(working_dir) = root.compile_dir.to_file_path() else {
return Err(BuildError::NotLocal(document.uri.clone()));
Expand Down
6 changes: 5 additions & 1 deletion crates/commands/src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ impl CleanCommand {
anyhow::bail!("document '{}' is not a local file", document.uri)
};

let root = ProjectRoot::walk_and_find(workspace, &document.dir);
let Some(document_dir) = &document.dir else {
anyhow::bail!("document '{}' is not a local file", document.uri)
};

let root = ProjectRoot::walk_and_find(workspace, document_dir);

let flag = match target {
CleanTarget::Auxiliary => "-c",
Expand Down
6 changes: 5 additions & 1 deletion crates/commands/src/fwd_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ impl ForwardSearch {
}

fn find_pdf(workspace: &Workspace, document: &Document) -> Result<PathBuf, ForwardSearchError> {
let root = ProjectRoot::walk_and_find(workspace, &document.dir);
let Some(document_dir) = &document.dir else {
return Err(ForwardSearchError::NotLocal(document.uri.clone()));
};

let root = ProjectRoot::walk_and_find(workspace, document_dir);

log::debug!("[FwdSearch] root={root:#?}");

Expand Down
2 changes: 1 addition & 1 deletion crates/completion/src/providers/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn current_dir(
.next()
.map_or(params.document, Clone::clone);

let root = ProjectRoot::walk_and_find(workspace, &parent.dir);
let root = ProjectRoot::walk_and_find(workspace, parent.dir.as_ref()?);

let mut path = PathBuf::new();
if let Some(graphics_path) = graphics_path {
Expand Down
2 changes: 1 addition & 1 deletion crates/diagnostics/src/chktex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Command {
return None;
}

let root = ProjectRoot::walk_and_find(workspace, &parent.dir);
let root = ProjectRoot::walk_and_find(workspace, parent.dir.as_ref()?);

let working_dir = root.src_dir.to_file_path().ok()?;
log::debug!("Calling ChkTeX from directory: {}", working_dir.display());
Expand Down
2 changes: 1 addition & 1 deletion crates/texlab/src/features/formatting/latexindent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn format_with_latexindent(
) -> Option<Vec<lsp_types::TextEdit>> {
let config = workspace.config();
let target_dir = tempdir().ok()?;
let root = ProjectRoot::walk_and_find(workspace, &document.dir);
let root = ProjectRoot::walk_and_find(workspace, document.dir.as_ref()?);
let source_dir = root.src_dir.to_file_path().ok()?;

let target_file = target_dir
Expand Down

0 comments on commit c64a8e7

Please sign in to comment.