@@ -1,6 +1,6 @@
1use anyhow::{Context, Error};
1use anyhow::{Context, Error};
2use std::future::Future;
2use std::future::Future;
3−use std::path::{Path, PathBuf};
3+use std::path::{Component, Path, PathBuf};
4use std::sync::Arc;
4use std::sync::Arc;
6use helix_core::{
6use helix_core::{
@@ -64,6 +64,42 @@ where
67+/// Normalize a path, removing things like `.` and `..`.
69+/// CAUTION: This does not resolve symlinks (unlike
70+/// [`std::fs::canonicalize`]). This may cause incorrect or surprising
71+/// behavior at times. This should be used carefully. Unfortunately,
72+/// [`std::fs::canonicalize`] can be hard to use correctly, since it can often
73+/// fail, or on Windows returns annoying device paths. This is a problem Cargo
74+/// needs to improve on.
75+/// Copied from cargo: https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81
76+pub fn normalize_path(path: &Path) -> PathBuf {
77+ let mut components = path.components().peekable();
78+ let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
80+ PathBuf::from(c.as_os_str())
85+ for component in components {
87+ Component::Prefix(..) => unreachable!(),
88+ Component::RootDir => {
89+ ret.push(component.as_os_str());
91+ Component::CurDir => {}
92+ Component::ParentDir => {
95+ Component::Normal(c) => {
67use helix_lsp::lsp;
103use helix_lsp::lsp;
68use url::Url;
104use url::Url;
@@ -176,6 +212,20 @@ impl Document {
215+ pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
216+ // canonicalize path to absolute value
217+ let current_dir = std::env::current_dir()?;
218+ let path = normalize_path(¤t_dir.join(path));
220+ if let Some(parent) = path.parent() {
221+ // TODO: return error as necessary
222+ if parent.exists() {
223+ self.path = Some(path);
179 pub fn set_language(
229 pub fn set_language(
180 &mut self,
230 &mut self,
181 language_config: Option<Arc<helix_core::syntax::LanguageConfiguration>>,
231 language_config: Option<Arc<helix_core::syntax::LanguageConfiguration>>,