Fix current_dir issue with lsp
This commit is contained in:
parent
dd5b861db2
commit
448739132a
2 changed files with 45 additions and 8 deletions
|
@ -75,9 +75,6 @@ pub fn process(
|
||||||
) -> Result<Vec<(RefCell<CompiledDocument>, Option<PostProcess>)>, String> {
|
) -> Result<Vec<(RefCell<CompiledDocument>, Option<PostProcess>)>, String> {
|
||||||
let mut compiled = vec![];
|
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
|
let con = db_path
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(Connection::open_in_memory(), Connection::open)
|
.map_or(Connection::open_in_memory(), Connection::open)
|
||||||
|
@ -98,8 +95,6 @@ pub fn process(
|
||||||
let file_parent_path = file
|
let file_parent_path = file
|
||||||
.parent()
|
.parent()
|
||||||
.ok_or(format!("Failed to get parent path for `{file:#?}`"))?;
|
.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> {
|
let parse_and_compile = || -> Result<(CompiledDocument, Option<PostProcess>), String> {
|
||||||
// Parse
|
// 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)
|
Ok(compiled)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::document::document::Document;
|
use crate::document::document::Document;
|
||||||
|
@ -18,6 +19,7 @@ use super::rule::Rule;
|
||||||
use super::source::Cursor;
|
use super::source::Cursor;
|
||||||
use super::source::Source;
|
use super::source::Source;
|
||||||
use super::source::SourceFile;
|
use super::source::SourceFile;
|
||||||
|
use super::source::SourcePosition;
|
||||||
use super::source::Token;
|
use super::source::Token;
|
||||||
use super::util;
|
use super::util;
|
||||||
|
|
||||||
|
@ -89,6 +91,42 @@ impl<'b> Parser for LangParser<'b> {
|
||||||
) -> (Box<dyn Document<'doc> + 'doc>, ParserState<'p, 'a>) {
|
) -> (Box<dyn Document<'doc> + 'doc>, ParserState<'p, 'a>) {
|
||||||
let doc = LangDocument::new(source.clone(), parent);
|
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
|
// Insert lsp data into state
|
||||||
if let (Some(_), Some(lsp)) = (
|
if let (Some(_), Some(lsp)) = (
|
||||||
source.clone().downcast_rc::<SourceFile>().ok(),
|
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(¤t_dir) {
|
||||||
|
println!(
|
||||||
|
"Failed to set working directory to `{}`: {err} {source:#?}",
|
||||||
|
current_dir.to_str().unwrap_or("")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
(Box::new(doc), state)
|
(Box::new(doc), state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue