Diff view (#32922)
Todo: * [x] Open diffed files as regular buffers * [x] Update diff when buffers change * [x] Show diffed filenames in the tab title * [x] Investigate why syntax highlighting isn't reliably handled for old text * [x] remove unstage/restore buttons Release Notes: - Adds `zed --diff A B` to show the diff between the two files --------- Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com> Co-authored-by: Ben Brandt <benjamin.j.brandt@gmail.com> Co-authored-by: Agus Zubiaga <agus@zed.dev>
This commit is contained in:
parent
2f52e2d285
commit
45b5b2e60d
14 changed files with 655 additions and 35 deletions
|
@ -46,10 +46,10 @@ use uuid::Uuid;
|
|||
use welcome::{BaseKeymap, FIRST_OPEN, show_welcome_view};
|
||||
use workspace::{AppState, SerializedWorkspaceLocation, WorkspaceSettings, WorkspaceStore};
|
||||
use zed::{
|
||||
OpenListener, OpenRequest, app_menus, build_window_options, derive_paths_with_position,
|
||||
handle_cli_connection, handle_keymap_file_changes, handle_settings_changed,
|
||||
handle_settings_file_changes, initialize_workspace, inline_completion_registry,
|
||||
open_paths_with_positions,
|
||||
OpenListener, OpenRequest, RawOpenRequest, app_menus, build_window_options,
|
||||
derive_paths_with_position, handle_cli_connection, handle_keymap_file_changes,
|
||||
handle_settings_changed, handle_settings_file_changes, initialize_workspace,
|
||||
inline_completion_registry, open_paths_with_positions,
|
||||
};
|
||||
|
||||
#[cfg(feature = "mimalloc")]
|
||||
|
@ -329,7 +329,12 @@ pub fn main() {
|
|||
|
||||
app.on_open_urls({
|
||||
let open_listener = open_listener.clone();
|
||||
move |urls| open_listener.open_urls(urls)
|
||||
move |urls| {
|
||||
open_listener.open(RawOpenRequest {
|
||||
urls,
|
||||
diff_paths: Vec::new(),
|
||||
})
|
||||
}
|
||||
});
|
||||
app.on_reopen(move |cx| {
|
||||
if let Some(app_state) = AppState::try_global(cx).and_then(|app_state| app_state.upgrade())
|
||||
|
@ -658,15 +663,21 @@ pub fn main() {
|
|||
.filter_map(|arg| parse_url_arg(arg, cx).log_err())
|
||||
.collect();
|
||||
|
||||
if !urls.is_empty() {
|
||||
open_listener.open_urls(urls)
|
||||
let diff_paths: Vec<[String; 2]> = args
|
||||
.diff
|
||||
.chunks(2)
|
||||
.map(|chunk| [chunk[0].clone(), chunk[1].clone()])
|
||||
.collect();
|
||||
|
||||
if !urls.is_empty() || !diff_paths.is_empty() {
|
||||
open_listener.open(RawOpenRequest { urls, diff_paths })
|
||||
}
|
||||
|
||||
match open_rx
|
||||
.try_next()
|
||||
.ok()
|
||||
.flatten()
|
||||
.and_then(|urls| OpenRequest::parse(urls, cx).log_err())
|
||||
.and_then(|request| OpenRequest::parse(request, cx).log_err())
|
||||
{
|
||||
Some(request) => {
|
||||
handle_open_request(request, app_state.clone(), cx);
|
||||
|
@ -733,13 +744,14 @@ fn handle_open_request(request: OpenRequest, app_state: Arc<AppState>, cx: &mut
|
|||
}
|
||||
|
||||
let mut task = None;
|
||||
if !request.open_paths.is_empty() {
|
||||
if !request.open_paths.is_empty() || !request.diff_paths.is_empty() {
|
||||
let app_state = app_state.clone();
|
||||
task = Some(cx.spawn(async move |mut cx| {
|
||||
let paths_with_position =
|
||||
derive_paths_with_position(app_state.fs.as_ref(), request.open_paths).await;
|
||||
let (_window, results) = open_paths_with_positions(
|
||||
&paths_with_position,
|
||||
&request.diff_paths,
|
||||
app_state,
|
||||
workspace::OpenOptions::default(),
|
||||
&mut cx,
|
||||
|
@ -1027,6 +1039,10 @@ struct Args {
|
|||
/// URLs can either be `file://` or `zed://` scheme, or relative to <https://zed.dev>.
|
||||
paths_or_urls: Vec<String>,
|
||||
|
||||
/// Pairs of file paths to diff. Can be specified multiple times.
|
||||
#[arg(long, action = clap::ArgAction::Append, num_args = 2, value_names = ["OLD_PATH", "NEW_PATH"])]
|
||||
diff: Vec<String>,
|
||||
|
||||
/// Sets a custom directory for all user data (e.g., database, extensions, logs).
|
||||
/// This overrides the default platform-specific data directory location.
|
||||
/// On macOS, the default is `~/Library/Application Support/Zed`.
|
||||
|
|
|
@ -570,7 +570,10 @@ fn register_actions(
|
|||
window.toggle_fullscreen();
|
||||
})
|
||||
.register_action(|_, action: &OpenZedUrl, _, cx| {
|
||||
OpenListener::global(cx).open_urls(vec![action.url.clone()])
|
||||
OpenListener::global(cx).open(RawOpenRequest {
|
||||
urls: vec![action.url.clone()],
|
||||
..Default::default()
|
||||
})
|
||||
})
|
||||
.register_action(|_, action: &OpenBrowser, _window, cx| cx.open_url(&action.url))
|
||||
.register_action(|workspace, _: &workspace::Open, window, cx| {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::handle_open_request;
|
||||
use crate::restorable_workspace_locations;
|
||||
use anyhow::{Context as _, Result};
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use cli::{CliRequest, CliResponse, ipc::IpcSender};
|
||||
use cli::{IpcHandshake, ipc};
|
||||
use client::parse_zed_link;
|
||||
|
@ -12,6 +12,7 @@ use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender};
|
|||
use futures::channel::{mpsc, oneshot};
|
||||
use futures::future::join_all;
|
||||
use futures::{FutureExt, SinkExt, StreamExt};
|
||||
use git_ui::diff_view::DiffView;
|
||||
use gpui::{App, AsyncApp, Global, WindowHandle};
|
||||
use language::Point;
|
||||
use recent_projects::{SshSettings, open_ssh_project};
|
||||
|
@ -31,6 +32,7 @@ use workspace::{AppState, OpenOptions, SerializedWorkspaceLocation, Workspace};
|
|||
pub struct OpenRequest {
|
||||
pub cli_connection: Option<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)>,
|
||||
pub open_paths: Vec<String>,
|
||||
pub diff_paths: Vec<[String; 2]>,
|
||||
pub open_channel_notes: Vec<(u64, Option<String>)>,
|
||||
pub join_channel: Option<u64>,
|
||||
pub ssh_connection: Option<SshConnectionOptions>,
|
||||
|
@ -38,9 +40,9 @@ pub struct OpenRequest {
|
|||
}
|
||||
|
||||
impl OpenRequest {
|
||||
pub fn parse(urls: Vec<String>, cx: &App) -> Result<Self> {
|
||||
pub fn parse(request: RawOpenRequest, cx: &App) -> Result<Self> {
|
||||
let mut this = Self::default();
|
||||
for url in urls {
|
||||
for url in request.urls {
|
||||
if let Some(server_name) = url.strip_prefix("zed-cli://") {
|
||||
this.cli_connection = Some(connect_to_cli(server_name)?);
|
||||
} else if let Some(action_index) = url.strip_prefix("zed-dock-action://") {
|
||||
|
@ -61,6 +63,8 @@ impl OpenRequest {
|
|||
}
|
||||
}
|
||||
|
||||
this.diff_paths = request.diff_paths;
|
||||
|
||||
Ok(this)
|
||||
}
|
||||
|
||||
|
@ -130,19 +134,25 @@ impl OpenRequest {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OpenListener(UnboundedSender<Vec<String>>);
|
||||
pub struct OpenListener(UnboundedSender<RawOpenRequest>);
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RawOpenRequest {
|
||||
pub urls: Vec<String>,
|
||||
pub diff_paths: Vec<[String; 2]>,
|
||||
}
|
||||
|
||||
impl Global for OpenListener {}
|
||||
|
||||
impl OpenListener {
|
||||
pub fn new() -> (Self, UnboundedReceiver<Vec<String>>) {
|
||||
pub fn new() -> (Self, UnboundedReceiver<RawOpenRequest>) {
|
||||
let (tx, rx) = mpsc::unbounded();
|
||||
(OpenListener(tx), rx)
|
||||
}
|
||||
|
||||
pub fn open_urls(&self, urls: Vec<String>) {
|
||||
pub fn open(&self, request: RawOpenRequest) {
|
||||
self.0
|
||||
.unbounded_send(urls)
|
||||
.unbounded_send(request)
|
||||
.context("no listener for open requests")
|
||||
.log_err();
|
||||
}
|
||||
|
@ -164,7 +174,10 @@ pub fn listen_for_cli_connections(opener: OpenListener) -> Result<()> {
|
|||
thread::spawn(move || {
|
||||
let mut buf = [0u8; 1024];
|
||||
while let Ok(len) = listener.recv(&mut buf) {
|
||||
opener.open_urls(vec![String::from_utf8_lossy(&buf[..len]).to_string()]);
|
||||
opener.open(RawOpenRequest {
|
||||
urls: vec![String::from_utf8_lossy(&buf[..len]).to_string()],
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
|
@ -201,6 +214,7 @@ fn connect_to_cli(
|
|||
|
||||
pub async fn open_paths_with_positions(
|
||||
path_positions: &[PathWithPosition],
|
||||
diff_paths: &[[String; 2]],
|
||||
app_state: Arc<AppState>,
|
||||
open_options: workspace::OpenOptions,
|
||||
cx: &mut AsyncApp,
|
||||
|
@ -225,11 +239,27 @@ pub async fn open_paths_with_positions(
|
|||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let (workspace, items) = cx
|
||||
let (workspace, mut items) = cx
|
||||
.update(|cx| workspace::open_paths(&paths, app_state, open_options, cx))?
|
||||
.await?;
|
||||
|
||||
for (item, path) in items.iter().zip(&paths) {
|
||||
for diff_pair in diff_paths {
|
||||
let old_path = Path::new(&diff_pair[0]).canonicalize()?;
|
||||
let new_path = Path::new(&diff_pair[1]).canonicalize()?;
|
||||
if let Ok(diff_view) = workspace.update(cx, |workspace, window, cx| {
|
||||
DiffView::open(old_path, new_path, workspace, window, cx)
|
||||
}) {
|
||||
if let Some(diff_view) = diff_view.await.log_err() {
|
||||
items.push(Some(Ok(Box::new(diff_view))))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (item, path) in items.iter_mut().zip(&paths) {
|
||||
if let Some(Err(error)) = item {
|
||||
*error = anyhow!("error opening {path:?}: {error}");
|
||||
continue;
|
||||
}
|
||||
let Some(Ok(item)) = item else {
|
||||
continue;
|
||||
};
|
||||
|
@ -260,14 +290,15 @@ pub async fn handle_cli_connection(
|
|||
CliRequest::Open {
|
||||
urls,
|
||||
paths,
|
||||
diff_paths,
|
||||
wait,
|
||||
open_new_workspace,
|
||||
env,
|
||||
user_data_dir: _, // Ignore user_data_dir
|
||||
user_data_dir: _,
|
||||
} => {
|
||||
if !urls.is_empty() {
|
||||
cx.update(|cx| {
|
||||
match OpenRequest::parse(urls, cx) {
|
||||
match OpenRequest::parse(RawOpenRequest { urls, diff_paths }, cx) {
|
||||
Ok(open_request) => {
|
||||
handle_open_request(open_request, app_state.clone(), cx);
|
||||
responses.send(CliResponse::Exit { status: 0 }).log_err();
|
||||
|
@ -288,6 +319,7 @@ pub async fn handle_cli_connection(
|
|||
|
||||
let open_workspace_result = open_workspaces(
|
||||
paths,
|
||||
diff_paths,
|
||||
open_new_workspace,
|
||||
&responses,
|
||||
wait,
|
||||
|
@ -306,6 +338,7 @@ pub async fn handle_cli_connection(
|
|||
|
||||
async fn open_workspaces(
|
||||
paths: Vec<String>,
|
||||
diff_paths: Vec<[String; 2]>,
|
||||
open_new_workspace: Option<bool>,
|
||||
responses: &IpcSender<CliResponse>,
|
||||
wait: bool,
|
||||
|
@ -362,6 +395,7 @@ async fn open_workspaces(
|
|||
|
||||
let workspace_failed_to_open = open_local_workspace(
|
||||
workspace_paths,
|
||||
diff_paths.clone(),
|
||||
open_new_workspace,
|
||||
wait,
|
||||
responses,
|
||||
|
@ -411,6 +445,7 @@ async fn open_workspaces(
|
|||
|
||||
async fn open_local_workspace(
|
||||
workspace_paths: Vec<String>,
|
||||
diff_paths: Vec<[String; 2]>,
|
||||
open_new_workspace: Option<bool>,
|
||||
wait: bool,
|
||||
responses: &IpcSender<CliResponse>,
|
||||
|
@ -424,6 +459,7 @@ async fn open_local_workspace(
|
|||
derive_paths_with_position(app_state.fs.as_ref(), workspace_paths).await;
|
||||
match open_paths_with_positions(
|
||||
&paths_with_position,
|
||||
&diff_paths,
|
||||
app_state.clone(),
|
||||
workspace::OpenOptions {
|
||||
open_new_workspace,
|
||||
|
@ -437,7 +473,7 @@ async fn open_local_workspace(
|
|||
Ok((workspace, items)) => {
|
||||
let mut item_release_futures = Vec::new();
|
||||
|
||||
for (item, path) in items.into_iter().zip(&paths_with_position) {
|
||||
for item in items {
|
||||
match item {
|
||||
Some(Ok(item)) => {
|
||||
cx.update(|cx| {
|
||||
|
@ -456,7 +492,7 @@ async fn open_local_workspace(
|
|||
Some(Err(err)) => {
|
||||
responses
|
||||
.send(CliResponse::Stderr {
|
||||
message: format!("error opening {path:?}: {err}"),
|
||||
message: err.to_string(),
|
||||
})
|
||||
.log_err();
|
||||
errored = true;
|
||||
|
@ -468,7 +504,7 @@ async fn open_local_workspace(
|
|||
if wait {
|
||||
let background = cx.background_executor().clone();
|
||||
let wait = async move {
|
||||
if paths_with_position.is_empty() {
|
||||
if paths_with_position.is_empty() && diff_paths.is_empty() {
|
||||
let (done_tx, done_rx) = oneshot::channel();
|
||||
let _subscription = workspace.update(cx, |_, _, cx| {
|
||||
cx.on_release(move |_, _| {
|
||||
|
@ -549,8 +585,16 @@ mod tests {
|
|||
cx.update(|cx| {
|
||||
SshSettings::register(cx);
|
||||
});
|
||||
let request =
|
||||
cx.update(|cx| OpenRequest::parse(vec!["ssh://me@localhost:/".into()], cx).unwrap());
|
||||
let request = cx.update(|cx| {
|
||||
OpenRequest::parse(
|
||||
RawOpenRequest {
|
||||
urls: vec!["ssh://me@localhost:/".into()],
|
||||
..Default::default()
|
||||
},
|
||||
cx,
|
||||
)
|
||||
.unwrap()
|
||||
});
|
||||
assert_eq!(
|
||||
request.ssh_connection.unwrap(),
|
||||
SshConnectionOptions {
|
||||
|
@ -692,6 +736,7 @@ mod tests {
|
|||
.spawn(|mut cx| async move {
|
||||
open_local_workspace(
|
||||
workspace_paths,
|
||||
vec![],
|
||||
open_new_workspace,
|
||||
false,
|
||||
&response_tx,
|
||||
|
|
|
@ -23,7 +23,7 @@ use windows::{
|
|||
core::HSTRING,
|
||||
};
|
||||
|
||||
use crate::{Args, OpenListener};
|
||||
use crate::{Args, OpenListener, RawOpenRequest};
|
||||
|
||||
pub fn is_first_instance() -> bool {
|
||||
unsafe {
|
||||
|
@ -40,7 +40,14 @@ pub fn is_first_instance() -> bool {
|
|||
pub fn handle_single_instance(opener: OpenListener, args: &Args, is_first_instance: bool) -> bool {
|
||||
if is_first_instance {
|
||||
// We are the first instance, listen for messages sent from other instances
|
||||
std::thread::spawn(move || with_pipe(|url| opener.open_urls(vec![url])));
|
||||
std::thread::spawn(move || {
|
||||
with_pipe(|url| {
|
||||
opener.open(RawOpenRequest {
|
||||
urls: vec![url],
|
||||
..Default::default()
|
||||
})
|
||||
})
|
||||
});
|
||||
} else if !args.foreground {
|
||||
// We are not the first instance, send args to the first instance
|
||||
send_args_to_instance(args).log_err();
|
||||
|
@ -109,6 +116,7 @@ fn send_args_to_instance(args: &Args) -> anyhow::Result<()> {
|
|||
let request = {
|
||||
let mut paths = vec![];
|
||||
let mut urls = vec![];
|
||||
let mut diff_paths = vec![];
|
||||
for path in args.paths_or_urls.iter() {
|
||||
match std::fs::canonicalize(&path) {
|
||||
Ok(path) => paths.push(path.to_string_lossy().to_string()),
|
||||
|
@ -126,9 +134,22 @@ fn send_args_to_instance(args: &Args) -> anyhow::Result<()> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
for path in args.diff.chunks(2) {
|
||||
let old = std::fs::canonicalize(&path[0]).log_err();
|
||||
let new = std::fs::canonicalize(&path[1]).log_err();
|
||||
if let Some((old, new)) = old.zip(new) {
|
||||
diff_paths.push([
|
||||
old.to_string_lossy().to_string(),
|
||||
new.to_string_lossy().to_string(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
CliRequest::Open {
|
||||
paths,
|
||||
urls,
|
||||
diff_paths,
|
||||
wait: false,
|
||||
open_new_workspace: None,
|
||||
env: None,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue