Rename head text to indicate that it's not always going to be from head

Co-Authored-By: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Julia 2022-10-03 15:11:06 -04:00
parent a5c2f22bf7
commit e6487de069
11 changed files with 78 additions and 78 deletions

View File

@ -948,7 +948,7 @@ async fn test_propagate_saves_and_fs_changes(
}
#[gpui::test(iterations = 10)]
async fn test_git_head_text(
async fn test_git_diff_base_change(
executor: Arc<Deterministic>,
cx_a: &mut TestAppContext,
cx_b: &mut TestAppContext,
@ -977,13 +977,13 @@ async fn test_git_head_text(
)
.await;
let head_text = "
let diff_base = "
one
three
"
.unindent();
let new_head_text = "
let new_diff_base = "
one
two
"
@ -992,9 +992,9 @@ async fn test_git_head_text(
client_a
.fs
.as_fake()
.set_head_state_for_git_repository(
.set_index_for_repo(
Path::new("/dir/.git"),
&[(Path::new("a.txt"), head_text.clone())],
&[(Path::new("a.txt"), diff_base.clone())],
)
.await;
@ -1012,11 +1012,11 @@ async fn test_git_head_text(
// Smoke test diffing
buffer_a.read_with(cx_a, |buffer, _| {
assert_eq!(buffer.head_text(), Some(head_text.as_ref()));
assert_eq!(buffer.diff_base(), Some(diff_base.as_ref()));
git::diff::assert_hunks(
buffer.snapshot().git_diff_hunks_in_range(0..4),
&buffer,
&head_text,
&diff_base,
&[(1..2, "", "two\n")],
);
});
@ -1032,11 +1032,11 @@ async fn test_git_head_text(
// Smoke test diffing
buffer_b.read_with(cx_b, |buffer, _| {
assert_eq!(buffer.head_text(), Some(head_text.as_ref()));
assert_eq!(buffer.diff_base(), Some(diff_base.as_ref()));
git::diff::assert_hunks(
buffer.snapshot().git_diff_hunks_in_range(0..4),
&buffer,
&head_text,
&diff_base,
&[(1..2, "", "two\n")],
);
});
@ -1044,9 +1044,9 @@ async fn test_git_head_text(
client_a
.fs
.as_fake()
.set_head_state_for_git_repository(
.set_index_for_repo(
Path::new("/dir/.git"),
&[(Path::new("a.txt"), new_head_text.clone())],
&[(Path::new("a.txt"), new_diff_base.clone())],
)
.await;
@ -1055,23 +1055,23 @@ async fn test_git_head_text(
// Smoke test new diffing
buffer_a.read_with(cx_a, |buffer, _| {
assert_eq!(buffer.head_text(), Some(new_head_text.as_ref()));
assert_eq!(buffer.diff_base(), Some(new_diff_base.as_ref()));
git::diff::assert_hunks(
buffer.snapshot().git_diff_hunks_in_range(0..4),
&buffer,
&head_text,
&diff_base,
&[(2..3, "", "three\n")],
);
});
// Smoke test B
buffer_b.read_with(cx_b, |buffer, _| {
assert_eq!(buffer.head_text(), Some(new_head_text.as_ref()));
assert_eq!(buffer.diff_base(), Some(new_diff_base.as_ref()));
git::diff::assert_hunks(
buffer.snapshot().git_diff_hunks_in_range(0..4),
&buffer,
&head_text,
&diff_base,
&[(2..3, "", "three\n")],
);
});

View File

@ -206,7 +206,7 @@ impl Server {
.add_message_handler(Server::unfollow)
.add_message_handler(Server::update_followers)
.add_request_handler(Server::get_channel_messages)
.add_message_handler(Server::update_head_text)
.add_message_handler(Server::update_diff_base)
.add_request_handler(Server::get_private_user_info);
Arc::new(server)
@ -1729,9 +1729,9 @@ impl Server {
Ok(())
}
async fn update_head_text(
async fn update_diff_base(
self: Arc<Server>,
request: TypedEnvelope<proto::UpdateHeadText>,
request: TypedEnvelope<proto::UpdateDiffBase>,
) -> Result<()> {
let receiver_ids = self.store().await.project_connection_ids(
ProjectId::from_proto(request.payload.project_id),

View File

@ -111,11 +111,11 @@ impl BufferDiff {
}
}
pub async fn update(&mut self, head_text: &str, buffer: &text::BufferSnapshot) {
pub async fn update(&mut self, diff_base: &str, buffer: &text::BufferSnapshot) {
let mut tree = SumTree::new();
let buffer_text = buffer.as_rope().to_string();
let patch = Self::diff(&head_text, &buffer_text);
let patch = Self::diff(&diff_base, &buffer_text);
if let Some(patch) = patch {
let mut divergence = 0;
@ -228,7 +228,7 @@ impl BufferDiff {
pub fn assert_hunks<Iter>(
diff_hunks: Iter,
buffer: &BufferSnapshot,
head_text: &str,
diff_base: &str,
expected_hunks: &[(Range<u32>, &str, &str)],
) where
Iter: Iterator<Item = DiffHunk<u32>>,
@ -237,7 +237,7 @@ pub fn assert_hunks<Iter>(
.map(|hunk| {
(
hunk.buffer_range.clone(),
&head_text[hunk.head_byte_range],
&diff_base[hunk.head_byte_range],
buffer
.text_for_range(
Point::new(hunk.buffer_range.start, 0)
@ -264,7 +264,7 @@ mod tests {
#[test]
fn test_buffer_diff_simple() {
let head_text = "
let diff_base = "
one
two
three
@ -280,27 +280,27 @@ mod tests {
let mut buffer = Buffer::new(0, 0, buffer_text);
let mut diff = BufferDiff::new();
smol::block_on(diff.update(&head_text, &buffer));
smol::block_on(diff.update(&diff_base, &buffer));
assert_hunks(
diff.hunks(&buffer),
&buffer,
&head_text,
&diff_base,
&[(1..2, "two\n", "HELLO\n")],
);
buffer.edit([(0..0, "point five\n")]);
smol::block_on(diff.update(&head_text, &buffer));
smol::block_on(diff.update(&diff_base, &buffer));
assert_hunks(
diff.hunks(&buffer),
&buffer,
&head_text,
&diff_base,
&[(0..1, "", "point five\n"), (2..3, "two\n", "HELLO\n")],
);
}
#[test]
fn test_buffer_diff_range() {
let head_text = "
let diff_base = "
one
two
three
@ -337,13 +337,13 @@ mod tests {
let buffer = Buffer::new(0, 0, buffer_text);
let mut diff = BufferDiff::new();
smol::block_on(diff.update(&head_text, &buffer));
smol::block_on(diff.update(&diff_base, &buffer));
assert_eq!(diff.hunks(&buffer).count(), 8);
assert_hunks(
diff.hunks_in_range(7..12, &buffer),
&buffer,
&head_text,
&diff_base,
&[
(6..7, "", "HELLO\n"),
(9..10, "six\n", "SIXTEEN\n"),

View File

@ -10,12 +10,12 @@ pub use git2::Repository as LibGitRepository;
#[async_trait::async_trait]
pub trait GitRepository: Send {
fn load_head_text(&self, relative_file_path: &Path) -> Option<String>;
fn load_index(&self, relative_file_path: &Path) -> Option<String>;
}
#[async_trait::async_trait]
impl GitRepository for LibGitRepository {
fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
fn load_index(&self, relative_file_path: &Path) -> Option<String> {
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
const STAGE_NORMAL: i32 = 0;
let index = repo.index()?;
@ -25,8 +25,7 @@ impl GitRepository for LibGitRepository {
};
let content = repo.find_blob(oid)?.content().to_owned();
let head_text = String::from_utf8(content)?;
Ok(Some(head_text))
Ok(Some(String::from_utf8(content)?))
}
match logic(&self, relative_file_path) {
@ -55,7 +54,7 @@ impl FakeGitRepository {
#[async_trait::async_trait]
impl GitRepository for FakeGitRepository {
fn load_head_text(&self, path: &Path) -> Option<String> {
fn load_index(&self, path: &Path) -> Option<String> {
let state = self.state.lock();
state.index_contents.get(path).cloned()
}

View File

@ -53,7 +53,7 @@ struct GitDiffStatus {
pub struct Buffer {
text: TextBuffer,
head_text: Option<String>,
diff_base: Option<String>,
git_diff_status: GitDiffStatus,
file: Option<Arc<dyn File>>,
saved_version: clock::Global,
@ -346,13 +346,13 @@ impl Buffer {
pub fn from_file<T: Into<String>>(
replica_id: ReplicaId,
base_text: T,
head_text: Option<T>,
diff_base: Option<T>,
file: Arc<dyn File>,
cx: &mut ModelContext<Self>,
) -> Self {
Self::build(
TextBuffer::new(replica_id, cx.model_id() as u64, base_text.into()),
head_text.map(|h| h.into().into_boxed_str().into()),
diff_base.map(|h| h.into().into_boxed_str().into()),
Some(file),
)
}
@ -365,7 +365,7 @@ impl Buffer {
let buffer = TextBuffer::new(replica_id, message.id, message.base_text);
let mut this = Self::build(
buffer,
message.head_text.map(|text| text.into_boxed_str().into()),
message.diff_base.map(|text| text.into_boxed_str().into()),
file,
);
this.text.set_line_ending(proto::deserialize_line_ending(
@ -380,7 +380,7 @@ impl Buffer {
id: self.remote_id(),
file: self.file.as_ref().map(|f| f.to_proto()),
base_text: self.base_text().to_string(),
head_text: self.head_text.as_ref().map(|h| h.to_string()),
diff_base: self.diff_base.as_ref().map(|h| h.to_string()),
line_ending: proto::serialize_line_ending(self.line_ending()) as i32,
}
}
@ -423,7 +423,7 @@ impl Buffer {
self
}
fn build(buffer: TextBuffer, head_text: Option<String>, file: Option<Arc<dyn File>>) -> Self {
fn build(buffer: TextBuffer, diff_base: Option<String>, file: Option<Arc<dyn File>>) -> Self {
let saved_mtime = if let Some(file) = file.as_ref() {
file.mtime()
} else {
@ -437,7 +437,7 @@ impl Buffer {
transaction_depth: 0,
was_dirty_before_starting_transaction: None,
text: buffer,
head_text,
diff_base,
git_diff_status: GitDiffStatus {
diff: git::diff::BufferDiff::new(),
update_in_progress: false,
@ -663,12 +663,12 @@ impl Buffer {
}
#[cfg(any(test, feature = "test-support"))]
pub fn head_text(&self) -> Option<&str> {
self.head_text.as_deref()
pub fn diff_base(&self) -> Option<&str> {
self.diff_base.as_deref()
}
pub fn update_head_text(&mut self, head_text: Option<String>, cx: &mut ModelContext<Self>) {
self.head_text = head_text;
pub fn update_diff_base(&mut self, diff_base: Option<String>, cx: &mut ModelContext<Self>) {
self.diff_base = diff_base;
self.git_diff_recalc(cx);
}
@ -682,13 +682,13 @@ impl Buffer {
return;
}
if let Some(head_text) = &self.head_text {
if let Some(diff_base) = &self.diff_base {
let snapshot = self.snapshot();
let head_text = head_text.clone();
let diff_base = diff_base.clone();
let mut diff = self.git_diff_status.diff.clone();
let diff = cx.background().spawn(async move {
diff.update(&head_text, &snapshot).await;
diff.update(&diff_base, &snapshot).await;
diff
});

View File

@ -490,11 +490,7 @@ impl FakeFs {
.boxed()
}
pub async fn set_head_state_for_git_repository(
&self,
dot_git: &Path,
head_state: &[(&Path, String)],
) {
pub async fn set_index_for_repo(&self, dot_git: &Path, head_state: &[(&Path, String)]) {
let content_path = dot_git.parent().unwrap();
let mut state = self.state.lock().await;
let entry = state.read_path(dot_git).await.unwrap();

View File

@ -424,7 +424,7 @@ impl Project {
client.add_model_request_handler(Self::handle_open_buffer_by_id);
client.add_model_request_handler(Self::handle_open_buffer_by_path);
client.add_model_request_handler(Self::handle_save_buffer);
client.add_model_message_handler(Self::handle_update_head_text);
client.add_model_message_handler(Self::handle_update_diff_base);
}
pub fn local(
@ -4675,22 +4675,22 @@ impl Project {
let client = self.client.clone();
cx.spawn(|_, mut cx| async move {
let head_text = cx
let diff_base = cx
.background()
.spawn(async move { repo.repo.lock().load_head_text(&path) })
.spawn(async move { repo.repo.lock().load_index(&path) })
.await;
let buffer_id = buffer.update(&mut cx, |buffer, cx| {
buffer.update_head_text(head_text.clone(), cx);
buffer.update_diff_base(diff_base.clone(), cx);
buffer.remote_id()
});
if let Some(project_id) = shared_remote_id {
client
.send(proto::UpdateHeadText {
.send(proto::UpdateDiffBase {
project_id,
buffer_id: buffer_id as u64,
head_text,
diff_base,
})
.log_err();
}
@ -5272,22 +5272,22 @@ impl Project {
})
}
async fn handle_update_head_text(
async fn handle_update_diff_base(
this: ModelHandle<Self>,
envelope: TypedEnvelope<proto::UpdateHeadText>,
envelope: TypedEnvelope<proto::UpdateDiffBase>,
_: Arc<Client>,
mut cx: AsyncAppContext,
) -> Result<()> {
this.update(&mut cx, |this, cx| {
let buffer_id = envelope.payload.buffer_id;
let head_text = envelope.payload.head_text;
let diff_base = envelope.payload.diff_base;
let buffer = this
.opened_buffers
.get_mut(&buffer_id)
.and_then(|b| b.upgrade(cx))
.ok_or_else(|| anyhow!("No such buffer {}", buffer_id))?;
buffer.update(cx, |buffer, cx| buffer.update_head_text(head_text, cx));
buffer.update(cx, |buffer, cx| buffer.update_diff_base(diff_base, cx));
Ok(())
})

View File

@ -481,11 +481,11 @@ impl LocalWorktree {
) -> Task<Result<ModelHandle<Buffer>>> {
let path = Arc::from(path);
cx.spawn(move |this, mut cx| async move {
let (file, contents, head_text) = this
let (file, contents, diff_base) = this
.update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
.await?;
Ok(cx.add_model(|cx| {
let mut buffer = Buffer::from_file(0, contents, head_text, Arc::new(file), cx);
let mut buffer = Buffer::from_file(0, contents, diff_base, Arc::new(file), cx);
buffer.git_diff_recalc(cx);
buffer
}))
@ -673,13 +673,13 @@ impl LocalWorktree {
cx.spawn(|this, mut cx| async move {
let text = fs.load(&abs_path).await?;
let head_text = match files_included {
let diff_base = match files_included {
settings::GitFilesIncluded::All | settings::GitFilesIncluded::OnlyTracked => {
let results = if let Some(repo) = snapshot.repo_for(&abs_path) {
cx.background()
.spawn({
let path = path.clone();
async move { repo.repo.lock().load_head_text(&path) }
async move { repo.repo.lock().load_index(&path) }
})
.await
} else {
@ -714,7 +714,7 @@ impl LocalWorktree {
is_local: true,
},
text,
head_text,
diff_base,
))
})
}

View File

@ -110,7 +110,7 @@ message Envelope {
Unfollow unfollow = 95;
GetPrivateUserInfo get_private_user_info = 96;
GetPrivateUserInfoResponse get_private_user_info_response = 97;
UpdateHeadText update_head_text = 98;
UpdateDiffBase update_diff_base = 98;
}
}
@ -830,7 +830,7 @@ message BufferState {
uint64 id = 1;
optional File file = 2;
string base_text = 3;
optional string head_text = 4;
optional string diff_base = 4;
LineEnding line_ending = 5;
}
@ -1002,8 +1002,8 @@ message WorktreeMetadata {
bool visible = 3;
}
message UpdateHeadText {
message UpdateDiffBase {
uint64 project_id = 1;
uint64 buffer_id = 2;
optional string head_text = 3;
optional string diff_base = 3;
}

View File

@ -167,7 +167,7 @@ messages!(
(UpdateProject, Foreground),
(UpdateWorktree, Foreground),
(UpdateWorktreeExtensions, Background),
(UpdateHeadText, Background),
(UpdateDiffBase, Background),
(GetPrivateUserInfo, Foreground),
(GetPrivateUserInfoResponse, Foreground),
);
@ -267,7 +267,7 @@ entity_messages!(
UpdateProject,
UpdateWorktree,
UpdateWorktreeExtensions,
UpdateHeadText
UpdateDiffBase
);
entity_messages!(channel_id, ChannelMessageSent);

View File

@ -55,11 +55,11 @@ impl FeatureFlags {
#[derive(Copy, Clone, Debug, Default, Deserialize, JsonSchema)]
pub struct GitSettings {
pub git_gutter: Option<GitGutterSettings>,
pub git_gutter: Option<GitGutter>,
}
#[derive(Clone, Copy, Debug, Default, Deserialize, JsonSchema)]
pub struct GitGutterSettings {
pub struct GitGutter {
pub files_included: GitFilesIncluded,
pub debounce_delay_millis: Option<u64>,
}
@ -406,7 +406,12 @@ impl Settings {
editor_overrides: Default::default(),
terminal_defaults: Default::default(),
terminal_overrides: Default::default(),
git: Default::default(),
git: GitSettings {
git_gutter: Some(GitGutter {
files_included: GitFilesIncluded::All,
debounce_delay_millis: None,
}),
},
language_defaults: Default::default(),
language_overrides: Default::default(),
lsp: Default::default(),