Fix current_dir issue with lsp

This commit is contained in:
ef3d0c3e 2024-10-31 01:19:03 +01:00
parent dd5b861db2
commit 448739132a
2 changed files with 45 additions and 8 deletions

View file

@ -75,9 +75,6 @@ pub fn process(
) -> Result<Vec<(RefCell<CompiledDocument>, Option<PostProcess>)>, String> {
let mut compiled = vec![];
let current_dir = std::env::current_dir()
.map_err(|err| format!("Unable to get the current working directory: {err}"))?;
let con = db_path
.as_ref()
.map_or(Connection::open_in_memory(), Connection::open)
@ -98,8 +95,6 @@ pub fn process(
let file_parent_path = file
.parent()
.ok_or(format!("Failed to get parent path for `{file:#?}`"))?;
std::env::set_current_dir(file_parent_path)
.map_err(|err| format!("Failed to move to path `{file_parent_path:#?}`: {err}"))?;
let parse_and_compile = || -> Result<(CompiledDocument, Option<PostProcess>), String> {
// Parse
@ -155,9 +150,6 @@ pub fn process(
})?;
}
std::env::set_current_dir(current_dir)
.map_err(|err| format!("Failed to set current directory: {err}"))?;
Ok(compiled)
}

View file

@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::path::PathBuf;
use std::rc::Rc;
use crate::document::document::Document;
@ -18,6 +19,7 @@ use super::rule::Rule;
use super::source::Cursor;
use super::source::Source;
use super::source::SourceFile;
use super::source::SourcePosition;
use super::source::Token;
use super::util;
@ -89,6 +91,42 @@ impl<'b> Parser for LangParser<'b> {
) -> (Box<dyn Document<'doc> + 'doc>, ParserState<'p, 'a>) {
let doc = LangDocument::new(source.clone(), parent);
let current_dir = match std::env::current_dir() {
Ok(dir) => dir,
Err(err) => {
eprintln!("Unable to get the current working directory: {err}");
return (Box::new(doc), state);
}
};
let path = source
.original_position(0)
.0
.downcast_rc::<SourceFile>()
.ok()
.map(|source| {
let start = if source.path().starts_with("file:///") {
7
} else {
0
};
let mut path = PathBuf::from(&source.path()[start..]);
match path.canonicalize() {
Ok(cano) => path = cano,
Err(err) => eprintln!("Failed to canonicalize path `{}`: {err}", source.path()),
}
path.pop();
path
});
if let Some(path) = path {
if let Err(err) = std::env::set_current_dir(&path) {
eprintln!(
"Failed to set working directory to `{}`: {err}",
path.to_str().unwrap_or("")
);
}
}
// Insert lsp data into state
if let (Some(_), Some(lsp)) = (
source.clone().downcast_rc::<SourceFile>().ok(),
@ -171,6 +209,13 @@ impl<'b> Parser for LangParser<'b> {
);
}
if let Err(err) = std::env::set_current_dir(&current_dir) {
println!(
"Failed to set working directory to `{}`: {err} {source:#?}",
current_dir.to_str().unwrap_or("")
);
}
(Box::new(doc), state)
}