Closes https://github.com/zed-industries/zed/issues/38690 Closes #37353 ### Background On Windows, paths are normally separated by `\`, unlike mac and linux where they are separated by `/`. When editing code in a project that uses a different path style than your local system (e.g. remoting from Windows to Linux, using WSL, and collaboration between windows and unix users), the correct separator for a path may differ from the "native" separator. Previously, to work around this, Zed converted paths' separators in numerous places. This was applied to both absolute and relative paths, leading to incorrect conversions in some cases. ### Solution Many code paths in Zed use paths that are *relative* to either a worktree root or a git repository. This PR introduces a dedicated type for these paths called `RelPath`, which stores the path in the same way regardless of host platform, and offers `Path`-like manipulation APIs. RelPath supports *displaying* the path using either separator, so that we can display paths in a style that is determined at runtime based on the current project. The representation of absolute paths is left untouched, for now. Absolute paths are different from relative paths because (except in contexts where we know that the path refers to the local filesystem) they should generally be treated as opaque strings. Currently we use a mix of types for these paths (std::path::Path, String, SanitizedPath). Release Notes: - N/A --------- Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Co-authored-by: Peter Tripp <petertripp@gmail.com> Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com> Co-authored-by: Lukas Wirth <me@lukaswirth.dev>
125 lines
4.0 KiB
Rust
125 lines
4.0 KiB
Rust
use anyhow::Context as _;
|
|
use gpui::{App, Context, Entity, Window};
|
|
use language::Language;
|
|
use project::lsp_store::lsp_ext_command::SwitchSourceHeaderResult;
|
|
use rpc::proto;
|
|
use url::Url;
|
|
use util::paths::PathStyle;
|
|
use workspace::{OpenOptions, OpenVisible};
|
|
|
|
use crate::lsp_ext::find_specific_language_server_in_selection;
|
|
|
|
use crate::{Editor, SwitchSourceHeader, element::register_action};
|
|
|
|
use project::lsp_store::clangd_ext::CLANGD_SERVER_NAME;
|
|
|
|
fn is_c_language(language: &Language) -> bool {
|
|
language.name() == "C++".into() || language.name() == "C".into()
|
|
}
|
|
|
|
pub fn switch_source_header(
|
|
editor: &mut Editor,
|
|
_: &SwitchSourceHeader,
|
|
window: &mut Window,
|
|
cx: &mut Context<Editor>,
|
|
) {
|
|
let Some(project) = &editor.project else {
|
|
return;
|
|
};
|
|
let Some(workspace) = editor.workspace() else {
|
|
return;
|
|
};
|
|
|
|
let Some((_, _, server_to_query, buffer)) =
|
|
find_specific_language_server_in_selection(editor, cx, is_c_language, CLANGD_SERVER_NAME)
|
|
else {
|
|
return;
|
|
};
|
|
let project = project.clone();
|
|
let upstream_client = project.read(cx).lsp_store().read(cx).upstream_client();
|
|
cx.spawn_in(window, async move |_editor, cx| {
|
|
let source_file = buffer.read_with(cx, |buffer, _| {
|
|
buffer
|
|
.file()
|
|
.map(|file| file.path())
|
|
.map(|path| path.display(PathStyle::local()).to_string())
|
|
.unwrap_or_else(|| "Unknown".to_string())
|
|
})?;
|
|
|
|
let switch_source_header = if let Some((client, project_id)) = upstream_client {
|
|
let buffer_id = buffer.read_with(cx, |buffer, _| buffer.remote_id())?;
|
|
let request = proto::LspExtSwitchSourceHeader {
|
|
project_id,
|
|
buffer_id: buffer_id.to_proto(),
|
|
};
|
|
let response = client
|
|
.request(request)
|
|
.await
|
|
.context("lsp ext switch source header proto request")?;
|
|
SwitchSourceHeaderResult(response.target_file)
|
|
} else {
|
|
project
|
|
.update(cx, |project, cx| {
|
|
project.request_lsp(
|
|
buffer,
|
|
project::LanguageServerToQuery::Other(server_to_query),
|
|
project::lsp_store::lsp_ext_command::SwitchSourceHeader,
|
|
cx,
|
|
)
|
|
})?
|
|
.await
|
|
.with_context(|| {
|
|
format!("Switch source/header LSP request for path \"{source_file}\" failed")
|
|
})?
|
|
};
|
|
|
|
if switch_source_header.0.is_empty() {
|
|
return Ok(());
|
|
}
|
|
|
|
let goto = Url::parse(&switch_source_header.0).with_context(|| {
|
|
format!(
|
|
"Parsing URL \"{}\" returned from switch source/header failed",
|
|
switch_source_header.0
|
|
)
|
|
})?;
|
|
|
|
let path = goto
|
|
.to_file_path()
|
|
.map_err(|()| anyhow::anyhow!("URL conversion to file path failed for \"{goto}\""))?;
|
|
|
|
workspace
|
|
.update_in(cx, |workspace, window, cx| {
|
|
workspace.open_abs_path(
|
|
path,
|
|
OpenOptions {
|
|
visible: Some(OpenVisible::None),
|
|
..Default::default()
|
|
},
|
|
window,
|
|
cx,
|
|
)
|
|
})
|
|
.with_context(|| {
|
|
format!("Switch source/header could not open \"{goto}\" in workspace")
|
|
})?
|
|
.await
|
|
.map(|_| ())
|
|
})
|
|
.detach_and_log_err(cx);
|
|
}
|
|
|
|
pub fn apply_related_actions(editor: &Entity<Editor>, window: &mut Window, cx: &mut App) {
|
|
if editor
|
|
.read(cx)
|
|
.buffer()
|
|
.read(cx)
|
|
.all_buffers()
|
|
.into_iter()
|
|
.filter_map(|buffer| buffer.read(cx).language())
|
|
.any(|language| is_c_language(language))
|
|
{
|
|
register_action(editor, window, switch_source_header);
|
|
}
|
|
}
|