Fix relative path opening from project symbols

Co-Authored-By: Max <max@zed.dev>
This commit is contained in:
Conrad Irwin 2024-01-08 13:56:52 -07:00
parent 604fcd8f1d
commit 71149bc7cc
5 changed files with 27 additions and 20 deletions

View file

@ -4936,10 +4936,10 @@ async fn test_project_symbols(
.await .await
.unwrap(); .unwrap();
buffer_b_2.read_with(cx_b, |buffer, _| { buffer_b_2.read_with(cx_b, |buffer, cx| {
assert_eq!( assert_eq!(
buffer.file().unwrap().path().as_ref(), buffer.file().unwrap().full_path(cx),
Path::new("../crate-2/two.rs") Path::new("/code/crate-2/two.rs")
); );
}); });

View file

@ -1297,7 +1297,7 @@ mod tests {
// so that one should be sorted earlier // so that one should be sorted earlier
let b_path = ProjectPath { let b_path = ProjectPath {
worktree_id, worktree_id,
path: Arc::from(Path::new("/root/dir2/b.txt")), path: Arc::from(Path::new("dir2/b.txt")),
}; };
workspace workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {

View file

@ -4732,7 +4732,8 @@ impl Project {
} else { } else {
return Task::ready(Err(anyhow!("worktree not found for symbol"))); return Task::ready(Err(anyhow!("worktree not found for symbol")));
}; };
let symbol_abs_path = worktree_abs_path.join(&symbol.path.path);
let symbol_abs_path = resolve_path(worktree_abs_path, &symbol.path.path);
let symbol_uri = if let Ok(uri) = lsp::Url::from_file_path(symbol_abs_path) { let symbol_uri = if let Ok(uri) = lsp::Url::from_file_path(symbol_abs_path) {
uri uri
} else { } else {
@ -8725,6 +8726,20 @@ fn relativize_path(base: &Path, path: &Path) -> PathBuf {
components.iter().map(|c| c.as_os_str()).collect() components.iter().map(|c| c.as_os_str()).collect()
} }
fn resolve_path(base: &Path, path: &Path) -> PathBuf {
let mut result = base.to_path_buf();
for component in path.components() {
match component {
Component::ParentDir => {
result.pop();
}
Component::CurDir => (),
_ => result.push(component),
}
}
result
}
impl Item for Buffer { impl Item for Buffer {
fn entry_id(&self, cx: &AppContext) -> Option<ProjectEntryId> { fn entry_id(&self, cx: &AppContext) -> Option<ProjectEntryId> {
File::from_dyn(self.file()).and_then(|file| file.project_entry_id(cx)) File::from_dyn(self.file()).and_then(|file| file.project_entry_id(cx))

View file

@ -4337,20 +4337,14 @@ async fn test_create_entry(cx: &mut gpui::TestAppContext) {
] ]
); );
// ************************************ // And we cannot open buffers with '..'
// Note: unsure if this is the best fix for the integration failure, but assuming we want
// to keep that behavior, then this test should cover it
// ************************************
// But we can open buffers with '..'
let result = project let result = project
.update(cx, |project, cx| { .update(cx, |project, cx| {
let id = project.worktrees().next().unwrap().read(cx).id(); let id = project.worktrees().next().unwrap().read(cx).id();
project.open_buffer((id, "../c.rs"), cx) project.open_buffer((id, "../c.rs"), cx)
}) })
.await; .await;
assert!(result.is_err())
assert!(dbg!(result).is_ok())
} }
async fn search( async fn search(

View file

@ -559,7 +559,7 @@ impl SemanticIndex {
.spawn(async move { .spawn(async move {
let mut changed_paths = BTreeMap::new(); let mut changed_paths = BTreeMap::new();
for file in worktree.files(false, 0) { for file in worktree.files(false, 0) {
let absolute_path = worktree.absolutize(&file.path); let absolute_path = worktree.absolutize(&file.path)?;
if file.is_external || file.is_ignored || file.is_symlink { if file.is_external || file.is_ignored || file.is_symlink {
continue; continue;
@ -1068,11 +1068,10 @@ impl SemanticIndex {
return true; return true;
}; };
worktree_state.changed_paths.retain(|path, info| { for (path, info) in &worktree_state.changed_paths {
if info.is_deleted { if info.is_deleted {
files_to_delete.push((worktree_state.db_id, path.clone())); files_to_delete.push((worktree_state.db_id, path.clone()));
} else { } else if let Ok(absolute_path) = worktree.read(cx).absolutize(path) {
let absolute_path = worktree.read(cx).absolutize(path);
let job_handle = JobHandle::new(pending_file_count_tx); let job_handle = JobHandle::new(pending_file_count_tx);
pending_files.push(PendingFile { pending_files.push(PendingFile {
absolute_path, absolute_path,
@ -1083,9 +1082,8 @@ impl SemanticIndex {
worktree_db_id: worktree_state.db_id, worktree_db_id: worktree_state.db_id,
}); });
} }
}
false worktree_state.changed_paths.clear();
});
true true
}); });