feat: syntax highlighting for diff view

This commit is contained in:
2026-05-25 00:08:22 +01:00
parent b3e041a257
commit a6cf96ea96
20 changed files with 1295 additions and 722 deletions

View File

@@ -6,9 +6,10 @@ use crate::{
diff_view::{DiffViewContent, DiffViewState, diff_view},
text::text,
},
query::{self, QueryStatus, observe_query, read_query, use_query},
query::{self, QueryStatus, read_query, use_query, watch_query},
util,
};
use gpui::{ParentElement, Styled, div};
use gpui::{AppContext, ParentElement, Styled, div};
pub(crate) struct PullRequestDiffView {
selected_file_path: Option<Arc<str>>,
@@ -104,23 +105,68 @@ 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();
_ = watch_query(&content_diff_query, Self::sync_content_diff_query, cx).detach();
self.content_diff_query = Some(content_diff_query);
}
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();
}
}
}
impl gpui::Render for PullRequestDiffView {