fix: Don't internally use relative paths in the buffer picker

Fixes #619
This commit is contained in:
Blaž Hrastnik 2021-08-24 09:56:09 +09:00
parent e1c9f13263
commit 1d45f50781
2 changed files with 14 additions and 13 deletions

View file

@ -2288,16 +2288,17 @@ fn buffer_picker(cx: &mut Context) {
cx.editor cx.editor
.documents .documents
.iter() .iter()
.map(|(id, doc)| (id, doc.relative_path())) .map(|(id, doc)| (id, doc.path().cloned()))
.collect(), .collect(),
move |(id, path): &(DocumentId, Option<PathBuf>)| { move |(id, path): &(DocumentId, Option<PathBuf>)| {
// format_fn use helix_view::document::relative_path;
let path = path.as_deref().map(relative_path);
match path.as_ref().and_then(|path| path.to_str()) { match path.as_ref().and_then(|path| path.to_str()) {
Some(path) => { Some(path) => {
if *id == current { if *id == current {
format!("{} (*)", path).into() format!("{} (*)", &path).into()
} else { } else {
path.into() path.to_owned().into()
} }
} }
None => "[scratch buffer]".into(), None => "[scratch buffer]".into(),

View file

@ -398,6 +398,14 @@ pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
Ok(normalize_path(&path)) Ok(normalize_path(&path))
} }
pub fn relative_path(mut path: &Path) -> PathBuf {
let cwdir = std::env::current_dir().expect("couldn't determine current directory");
if path.is_absolute() {
path = path.strip_prefix(cwdir).unwrap_or(path)
};
fold_home_dir(path)
}
use helix_lsp::lsp; use helix_lsp::lsp;
use url::Url; use url::Url;
@ -936,15 +944,7 @@ impl Document {
} }
pub fn relative_path(&self) -> Option<PathBuf> { pub fn relative_path(&self) -> Option<PathBuf> {
let cwdir = std::env::current_dir().expect("couldn't determine current directory"); self.path.as_deref().map(relative_path)
self.path.as_ref().map(|path| {
let mut path = path.as_path();
if path.is_absolute() {
path = path.strip_prefix(cwdir).unwrap_or(path)
};
fold_home_dir(path)
})
} }
// pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds { // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {