2026-05-24 16:44:10 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2026-05-18 22:30:46 +08:00
|
|
|
use crate::{
|
2026-05-23 12:28:45 +01:00
|
|
|
api, app,
|
2026-05-24 16:44:10 +01:00
|
|
|
component::{
|
|
|
|
|
diff_view::{DiffViewContent, DiffViewState, diff_view},
|
|
|
|
|
text::text,
|
|
|
|
|
},
|
2026-05-25 00:08:22 +01:00
|
|
|
query::{self, QueryStatus, read_query, use_query, watch_query},
|
|
|
|
|
util,
|
2026-05-18 22:30:46 +08:00
|
|
|
};
|
2026-05-25 00:08:22 +01:00
|
|
|
use gpui::{AppContext, ParentElement, Styled, div};
|
2026-05-18 22:30:46 +08:00
|
|
|
|
|
|
|
|
pub(crate) struct PullRequestDiffView {
|
2026-05-24 16:44:10 +01:00
|
|
|
selected_file_path: Option<Arc<str>>,
|
2026-05-18 22:30:46 +08:00
|
|
|
|
|
|
|
|
pr_query: query::Entity<api::issues::FetchPullRequest>,
|
2026-05-24 16:44:10 +01:00
|
|
|
file_tree_query: query::Entity<api::issues::FetchPullRequestFileTree>,
|
2026-05-23 12:28:45 +01:00
|
|
|
content_diff_query: Option<query::Entity<api::repo::FetchFileDiff>>,
|
2026-05-24 16:44:10 +01:00
|
|
|
|
|
|
|
|
diff_view_state: DiffViewState,
|
|
|
|
|
diff_view_content: Option<DiffViewContent>,
|
2026-05-18 22:30:46 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-24 16:44:10 +01:00
|
|
|
pub(crate) fn new(
|
|
|
|
|
pr_id: api::issues::Id,
|
|
|
|
|
cx: &mut gpui::Context<PullRequestDiffView>,
|
|
|
|
|
) -> PullRequestDiffView {
|
2026-05-18 22:30:46 +08:00
|
|
|
let mut view = PullRequestDiffView {
|
|
|
|
|
selected_file_path: None,
|
2026-05-24 16:44:10 +01:00
|
|
|
pr_query: use_query(api::issues::FetchPullRequest { id: pr_id.clone() }, cx),
|
|
|
|
|
file_tree_query: use_query(
|
|
|
|
|
api::issues::FetchPullRequestFileTree {
|
|
|
|
|
id: pr_id,
|
|
|
|
|
first: 100,
|
|
|
|
|
},
|
|
|
|
|
cx,
|
|
|
|
|
),
|
2026-05-23 12:28:45 +01:00
|
|
|
content_diff_query: None,
|
2026-05-24 16:44:10 +01:00
|
|
|
diff_view_state: DiffViewState::new(),
|
|
|
|
|
diff_view_content: None,
|
2026-05-18 22:30:46 +08:00
|
|
|
};
|
|
|
|
|
view.on_create(cx);
|
|
|
|
|
view
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PullRequestDiffView {
|
|
|
|
|
fn on_create(&mut self, cx: &mut gpui::Context<Self>) {
|
|
|
|
|
_ = cx
|
|
|
|
|
.observe(&self.pr_query, |this, _, cx| {
|
|
|
|
|
this.start_content_queries(cx);
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
|
2026-05-24 16:44:10 +01:00
|
|
|
_ = cx
|
|
|
|
|
.observe(&self.file_tree_query, |this, _, cx| {
|
|
|
|
|
this.start_content_queries(cx);
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
|
2026-05-18 22:30:46 +08:00
|
|
|
// if pr is already loaded, start content queries
|
|
|
|
|
self.start_content_queries(cx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_content_queries(&mut self, cx: &mut gpui::Context<Self>) {
|
2026-05-24 16:44:10 +01:00
|
|
|
if self.content_diff_query.is_some() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.selected_file_path.is_none()
|
|
|
|
|
&& let QueryStatus::Loaded(files) = read_query(&self.file_tree_query, cx)
|
|
|
|
|
{
|
|
|
|
|
self.selected_file_path = files.first().map(|file| Arc::clone(&file.path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let Some(selected_file_path) = self.selected_file_path.as_deref() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-23 12:28:45 +01:00
|
|
|
let Some((old_file_ref, new_file_ref)) = ({
|
2026-05-18 22:30:46 +08:00
|
|
|
if let QueryStatus::Loaded(pr) = read_query(&self.pr_query, cx) {
|
|
|
|
|
Some((
|
2026-05-23 12:28:45 +01:00
|
|
|
api::repo::FileRef {
|
2026-05-18 22:30:46 +08:00
|
|
|
repo_slug: pr.base_repo_slug.clone(),
|
2026-05-24 16:44:10 +01:00
|
|
|
path: Arc::from(selected_file_path),
|
2026-05-18 22:30:46 +08:00
|
|
|
reff: Some(pr.base_ref.clone()),
|
|
|
|
|
},
|
2026-05-23 12:28:45 +01:00
|
|
|
api::repo::FileRef {
|
2026-05-18 22:30:46 +08:00
|
|
|
repo_slug: pr.head_repo_slug.clone(),
|
2026-05-24 16:44:10 +01:00
|
|
|
path: Arc::from(selected_file_path),
|
2026-05-18 22:30:46 +08:00
|
|
|
reff: Some(pr.head_ref.clone()),
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}) else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-23 12:28:45 +01:00
|
|
|
let content_diff_query = use_query(
|
|
|
|
|
api::repo::FetchFileDiff {
|
|
|
|
|
base: old_file_ref,
|
|
|
|
|
head: new_file_ref,
|
|
|
|
|
},
|
|
|
|
|
cx,
|
|
|
|
|
);
|
2026-05-25 00:08:22 +01:00
|
|
|
_ = watch_query(&content_diff_query, Self::sync_content_diff_query, cx).detach();
|
2026-05-24 16:44:10 +01:00
|
|
|
|
2026-05-23 12:28:45 +01:00
|
|
|
self.content_diff_query = Some(content_diff_query);
|
2026-05-18 22:30:46 +08:00
|
|
|
}
|
2026-05-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
fn sync_content_diff_query(
|
|
|
|
|
&mut self,
|
|
|
|
|
query: &query::Entity<api::repo::FetchFileDiff>,
|
|
|
|
|
cx: &mut gpui::Context<Self>,
|
|
|
|
|
) {
|
|
|
|
|
if let Some(diff) = {
|
|
|
|
|
match read_query(query, cx) {
|
|
|
|
|
| QueryStatus::Loaded(diff) => Some(Arc::clone(diff)),
|
|
|
|
|
| _ => None,
|
|
|
|
|
}
|
|
|
|
|
} {
|
|
|
|
|
self.load_diff_view(diff, cx);
|
|
|
|
|
cx.notify();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn load_diff_view(
|
|
|
|
|
&mut self,
|
|
|
|
|
content_diff: Arc<util::diff::ContentDiff>,
|
|
|
|
|
cx: &mut gpui::Context<Self>,
|
|
|
|
|
) {
|
|
|
|
|
let theme = app::current_theme(cx);
|
|
|
|
|
let old_content = content_diff.old_content.clone();
|
|
|
|
|
let new_content = content_diff.new_content.clone();
|
|
|
|
|
|
|
|
|
|
self.diff_view_state.reset(content_diff.len());
|
|
|
|
|
self.diff_view_content = Some(content_diff.into());
|
|
|
|
|
|
|
|
|
|
let theme_syntax = theme.syntax;
|
|
|
|
|
|
|
|
|
|
if let Some(path) = &self.selected_file_path {
|
|
|
|
|
let path = Arc::clone(&path);
|
|
|
|
|
let file_type = util::file::file_type_from_path(&path);
|
|
|
|
|
|
|
|
|
|
let t1 = cx.background_spawn(async move {
|
|
|
|
|
util::syntax_highlight::highlight_content(old_content, file_type, &theme_syntax)
|
|
|
|
|
});
|
|
|
|
|
let t2 = cx.background_spawn(async move {
|
|
|
|
|
util::syntax_highlight::highlight_content(new_content, file_type, &theme_syntax)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_ = cx
|
|
|
|
|
.spawn(async move |weak, cx| match tokio::join!(t1, t2) {
|
|
|
|
|
| (Some(old_side_highlights), Some(new_side_highlights)) => {
|
|
|
|
|
_ = weak.update(cx, |this, cx| {
|
|
|
|
|
this.diff_view_state
|
|
|
|
|
.set_old_side_highlights(old_side_highlights);
|
|
|
|
|
this.diff_view_state
|
|
|
|
|
.set_new_side_highlights(new_side_highlights);
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
| _ => {}
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-18 22:30:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl gpui::Render for PullRequestDiffView {
|
|
|
|
|
fn render(
|
|
|
|
|
&mut self,
|
2026-05-23 12:28:45 +01:00
|
|
|
_window: &mut gpui::Window,
|
2026-05-18 22:30:46 +08:00
|
|
|
cx: &mut gpui::Context<Self>,
|
|
|
|
|
) -> impl gpui::IntoElement {
|
2026-05-23 12:28:45 +01:00
|
|
|
let theme = app::current_theme(cx);
|
|
|
|
|
|
2026-05-24 16:44:10 +01:00
|
|
|
let content_diff = self
|
|
|
|
|
.content_diff_query
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|q| read_query(q, cx))
|
|
|
|
|
.unwrap_or(QueryStatus::Loading);
|
|
|
|
|
|
|
|
|
|
match content_diff {
|
|
|
|
|
| QueryStatus::Err(_) | QueryStatus::Loading => div()
|
2026-05-23 12:28:45 +01:00
|
|
|
.size_full()
|
|
|
|
|
.bg(theme.colors.surface)
|
|
|
|
|
.p_4()
|
2026-05-24 16:44:10 +01:00
|
|
|
.child(text("asd")),
|
|
|
|
|
|
|
|
|
|
| QueryStatus::Loaded(_) => match &self.diff_view_content {
|
|
|
|
|
| Some(content) => div()
|
|
|
|
|
.size_full()
|
|
|
|
|
.child(diff_view(self.diff_view_state.clone(), content.clone())),
|
|
|
|
|
| None => div(),
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-05-18 22:30:46 +08:00
|
|
|
}
|
|
|
|
|
}
|