feat: basic pr diff rendering

This commit is contained in:
2026-05-24 16:44:10 +01:00
parent 1843622540
commit b3e041a257
23 changed files with 903 additions and 353 deletions

View File

@@ -1,21 +1,43 @@
use std::sync::Arc;
use crate::{
api, app,
component::text::text,
query::{self, QueryStatus, read_query, use_query},
component::{
diff_view::{DiffViewContent, DiffViewState, diff_view},
text::text,
},
query::{self, QueryStatus, observe_query, read_query, use_query},
};
use gpui::{ParentElement, Styled, div};
pub(crate) struct PullRequestDiffView {
selected_file_path: Option<String>,
selected_file_path: Option<Arc<str>>,
pr_query: query::Entity<api::issues::FetchPullRequest>,
file_tree_query: query::Entity<api::issues::FetchPullRequestFileTree>,
content_diff_query: Option<query::Entity<api::repo::FetchFileDiff>>,
diff_view_state: DiffViewState,
diff_view_content: Option<DiffViewContent>,
}
fn new(pr_id: api::issues::Id, cx: &mut gpui::Context<PullRequestDiffView>) -> PullRequestDiffView {
pub(crate) fn new(
pr_id: api::issues::Id,
cx: &mut gpui::Context<PullRequestDiffView>,
) -> PullRequestDiffView {
let mut view = PullRequestDiffView {
selected_file_path: None,
pr_query: use_query(api::issues::FetchPullRequest { id: pr_id }, cx),
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,
),
content_diff_query: None,
diff_view_state: DiffViewState::new(),
diff_view_content: None,
};
view.on_create(cx);
view
@@ -29,22 +51,42 @@ impl PullRequestDiffView {
})
.detach();
_ = cx
.observe(&self.file_tree_query, |this, _, cx| {
this.start_content_queries(cx);
})
.detach();
// if pr is already loaded, start content queries
self.start_content_queries(cx);
}
fn start_content_queries(&mut self, cx: &mut gpui::Context<Self>) {
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;
};
let Some((old_file_ref, new_file_ref)) = ({
if let QueryStatus::Loaded(pr) = read_query(&self.pr_query, cx) {
Some((
api::repo::FileRef {
repo_slug: pr.base_repo_slug.clone(),
path: pr.base_branch_name.clone(),
path: Arc::from(selected_file_path),
reff: Some(pr.base_ref.clone()),
},
api::repo::FileRef {
repo_slug: pr.head_repo_slug.clone(),
path: pr.head_branch_name.clone(),
path: Arc::from(selected_file_path),
reff: Some(pr.head_ref.clone()),
},
))
@@ -63,6 +105,20 @@ impl PullRequestDiffView {
cx,
);
_ = observe_query(
&content_diff_query,
|this, query, cx| {
if let QueryStatus::Loaded(diff) = read_query(query, cx) {
println!("diff len {}", diff.len());
this.diff_view_state.reset(diff.len());
this.diff_view_content = Some(Arc::clone(diff).into());
}
cx.notify();
},
cx,
)
.detach();
self.content_diff_query = Some(content_diff_query);
}
}
@@ -73,20 +129,27 @@ impl gpui::Render for PullRequestDiffView {
_window: &mut gpui::Window,
cx: &mut gpui::Context<Self>,
) -> impl gpui::IntoElement {
use gpui::{ParentElement, Styled, div};
let theme = app::current_theme(cx);
div()
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()
.size_full()
.bg(theme.colors.surface)
.p_4()
.child(
text(
"Pull request diff rendering is still under construction. Launch the DiffOps playground with NOVEM_DIFFOPS_PLAYGROUND=1 cargo run.",
)
.text_sm()
.text_color(theme.colors.text_muted),
)
.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(),
},
}
}
}

View File

@@ -15,7 +15,7 @@ use crate::{
text::text,
},
query::{self, QueryStatus, read_query, use_query},
screen::dashboard::pull_request_diff_view::PullRequestDiffView,
screen::dashboard::pull_request_diff_view::{self, PullRequestDiffView},
};
pub(crate) struct PullRequestView {
@@ -49,12 +49,14 @@ impl PullRequestView {
_ = cx
.observe(&query.clone(), move |this, _, cx| {
this.load_markdown_content(cx);
this.load_pr_diff(cx);
})
.detach();
// cached query will not trigger observe callback
// this is required so that content is loaded immediately for cached query
self.load_markdown_content(cx);
self.load_pr_diff(cx);
cx.notify();
}
@@ -78,6 +80,25 @@ impl PullRequestView {
cx.notify();
}
fn load_pr_diff(&mut self, cx: &mut gpui::Context<Self>) {
let Some(query) = &self.pull_request_query else {
return;
};
let pr_id = {
let data = read_query(&query, cx);
if let QueryStatus::Loaded(pr) = data {
Some(pr.id.clone())
} else {
None
}
};
self.diff_view = pr_id.map(|id| cx.new(|cx| pull_request_diff_view::new(id, cx)));
cx.notify();
}
fn pr_content(
&self,
pr: &api::issues::DetailedPullRequest,
@@ -264,7 +285,11 @@ impl gpui::Render for PullRequestView {
) -> impl gpui::IntoElement {
div().size_full().child(match &self.pull_request_query {
| Some(q) => match read_query(q, cx) {
| QueryStatus::Loaded(pr) => self.pr_content(pr, cx),
| QueryStatus::Loaded(pr) => match &self.diff_view {
| Some(v) => v.clone().into_any_element(),
| None => self.pr_content(pr, cx),
},
| QueryStatus::Err(e) => div()
.size_full()
.child(format!("{:?}", e))
@@ -274,6 +299,7 @@ impl gpui::Render for PullRequestView {
.child("loading pr content")
.into_any_element(),
},
| None => div().size_full().child("no pr selected").into_any_element(),
})
}

View File

@@ -4,6 +4,7 @@ use crate::{
api, app,
screen::dashboard::{
issue_list::{self, IssueList},
pull_request_diff_view::{self, PullRequestDiffView},
pull_request_view::{self, PullRequestView},
titlebar::{self, TitleBar},
},
@@ -13,6 +14,7 @@ pub(crate) struct Screen {
titlebar: gpui::Entity<TitleBar>,
issue_list: gpui::Entity<IssueList>,
pull_request_view: gpui::Entity<PullRequestView>,
pull_request_diff_view: Option<gpui::Entity<PullRequestDiffView>>,
issue_filter: Option<&'static str>,
}
@@ -22,6 +24,7 @@ pub(crate) fn new(cx: &mut gpui::Context<Screen>) -> Screen {
titlebar: cx.new(titlebar::new),
issue_list: cx.new(issue_list::new),
pull_request_view: cx.new(pull_request_view::new),
pull_request_diff_view: None,
issue_filter: None,
};
@@ -33,9 +36,9 @@ impl Screen {
fn on_create(&mut self, cx: &mut gpui::Context<Self>) {
_ = cx
.subscribe(&self.issue_list, |this, _, event, cx| match event {
| issue_list::Event::ItemSelected(pr_id) => {
this.handle_issue_list_item_selected(pr_id, cx);
}
| issue_list::Event::ItemSelected(pr_id) => {
this.handle_issue_list_item_selected(pr_id, cx);
}
})
.detach();
}
@@ -50,7 +53,9 @@ impl Screen {
view.change_displayed_pull_request(id.clone(), cx);
println!("change displayed pull request: {:?}", id);
cx.notify();
})
});
self.pull_request_diff_view =
Some(cx.new(|cx| pull_request_diff_view::new(id.clone(), cx)));
}
}