fix: search result cursor advancing
This commit is contained in:
@@ -28,7 +28,7 @@ struct DiffViewStateInner {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct DiffViewContent {
|
pub(crate) struct DiffViewContent {
|
||||||
diff: Arc<util::diff::ContentDiff>,
|
pub(crate) diff: Arc<util::diff::ContentDiff>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, gpui::IntoElement)]
|
#[derive(Clone, gpui::IntoElement)]
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ use crate::{
|
|||||||
text_input::{self, TextInput, text_input},
|
text_input::{self, TextInput, text_input},
|
||||||
},
|
},
|
||||||
query::{self, QueryStatus, read_query, use_query, watch_query},
|
query::{self, QueryStatus, read_query, use_query, watch_query},
|
||||||
util,
|
util::{self, diff::DiffLineIndex},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) struct PullRequestDiffView {
|
pub(crate) struct PullRequestDiffView {
|
||||||
@@ -33,15 +33,19 @@ pub(crate) struct PullRequestDiffView {
|
|||||||
search_input: gpui::Entity<TextInput>,
|
search_input: gpui::Entity<TextInput>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DiffSearchHit {
|
||||||
|
diff_line_i: DiffLineIndex,
|
||||||
|
src_byte_range: std::ops::Range<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
struct DiffSearchResult {
|
struct DiffSearchResult {
|
||||||
cursor: DiffSearchResultCursor,
|
cursor: DiffSearchResultCursor,
|
||||||
old_side: Vec<(usize, std::ops::Range<usize>)>,
|
old_side: Vec<DiffSearchHit>,
|
||||||
new_side: Vec<(usize, std::ops::Range<usize>)>,
|
new_side: Vec<DiffSearchHit>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DiffSearchResultCursor {
|
struct DiffSearchResultCursor {
|
||||||
is_old: bool,
|
side: util::diff::DiffSide,
|
||||||
line_index: usize,
|
|
||||||
index: usize,
|
index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,6 +208,10 @@ impl PullRequestDiffView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_in_diff(&mut self, search_str: &str, cx: &mut gpui::Context<Self>) {
|
fn search_in_diff(&mut self, search_str: &str, cx: &mut gpui::Context<Self>) {
|
||||||
|
let Some(diff_view_content) = &self.diff_view_content else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let diff_view_state_in_search_mode = self
|
let diff_view_state_in_search_mode = self
|
||||||
.diff_view_state_in_search_mode
|
.diff_view_state_in_search_mode
|
||||||
.get_or_insert(DiffViewState::fork_from(&self.diff_view_state));
|
.get_or_insert(DiffViewState::fork_from(&self.diff_view_state));
|
||||||
@@ -226,7 +234,13 @@ impl PullRequestDiffView {
|
|||||||
.diff_view_state
|
.diff_view_state
|
||||||
.old_side_highlights()?
|
.old_side_highlights()?
|
||||||
.line_index_of_range(&range)?;
|
.line_index_of_range(&range)?;
|
||||||
Some((line_idx, range))
|
let diff_line_i = diff_view_content
|
||||||
|
.diff
|
||||||
|
.diff_line_index_for_line(util::diff::DiffSide::Old, line_idx);
|
||||||
|
Some(DiffSearchHit {
|
||||||
|
diff_line_i,
|
||||||
|
src_byte_range: range,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let new_search_result = memchr::memmem::find_iter(&diff.new_content, search_str.as_bytes())
|
let new_search_result = memchr::memmem::find_iter(&diff.new_content, search_str.as_bytes())
|
||||||
@@ -236,21 +250,27 @@ impl PullRequestDiffView {
|
|||||||
.diff_view_state
|
.diff_view_state
|
||||||
.new_side_highlights()?
|
.new_side_highlights()?
|
||||||
.line_index_of_range(&range)?;
|
.line_index_of_range(&range)?;
|
||||||
Some((line_idx, range))
|
let diff_line_i = diff_view_content
|
||||||
|
.diff
|
||||||
|
.diff_line_index_for_line(util::diff::DiffSide::New, line_idx);
|
||||||
|
Some(DiffSearchHit {
|
||||||
|
diff_line_i,
|
||||||
|
src_byte_range: range,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let old_side_highlights =
|
let old_side_highlights =
|
||||||
old_search_result
|
old_search_result
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, r)| -> util::syntax_highlight::HighlightedRange {
|
.map(|hit| -> util::syntax_highlight::HighlightedRange {
|
||||||
(r.clone(), symbol_highlight_style(theme))
|
(hit.src_byte_range.clone(), symbol_highlight_style(theme))
|
||||||
});
|
});
|
||||||
let new_side_highlights =
|
let new_side_highlights =
|
||||||
new_search_result
|
new_search_result
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, r)| -> util::syntax_highlight::HighlightedRange {
|
.map(|hit| -> util::syntax_highlight::HighlightedRange {
|
||||||
(r.clone(), symbol_highlight_style(theme))
|
(hit.src_byte_range.clone(), symbol_highlight_style(theme))
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(h) = self
|
if let Some(h) = self
|
||||||
@@ -276,14 +296,12 @@ impl PullRequestDiffView {
|
|||||||
} else {
|
} else {
|
||||||
let cursor = if !old_search_result.is_empty() {
|
let cursor = if !old_search_result.is_empty() {
|
||||||
DiffSearchResultCursor {
|
DiffSearchResultCursor {
|
||||||
is_old: true,
|
side: util::diff::DiffSide::Old,
|
||||||
line_index: old_search_result[0].0,
|
|
||||||
index: 0,
|
index: 0,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
DiffSearchResultCursor {
|
DiffSearchResultCursor {
|
||||||
is_old: false,
|
side: util::diff::DiffSide::New,
|
||||||
line_index: new_search_result[0].0,
|
|
||||||
index: 0,
|
index: 0,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -305,21 +323,23 @@ impl PullRequestDiffView {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let (current_side, other_side) = if search_result.cursor.is_old {
|
let (current_side, other_side) = match search_result.cursor.side {
|
||||||
(&search_result.old_side, &search_result.new_side)
|
| util::diff::DiffSide::Old => (&search_result.old_side, &search_result.new_side),
|
||||||
} else {
|
| util::diff::DiffSide::New => (&search_result.new_side, &search_result.old_side),
|
||||||
(&search_result.new_side, &search_result.old_side)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let theme = app::current_theme(cx);
|
let theme = app::current_theme(cx);
|
||||||
|
|
||||||
let current_line_index = search_result.cursor.line_index;
|
let current_search_hit = ¤t_side[search_result.cursor.index];
|
||||||
let highlight_range = match current_side.get(search_result.cursor.index + 1) {
|
let highlight_range = match current_side.get(search_result.cursor.index + 1) {
|
||||||
| Some((next_highlight_line, next_range)) if *next_highlight_line == current_line_index => {
|
| Some(DiffSearchHit {
|
||||||
|
diff_line_i,
|
||||||
|
src_byte_range,
|
||||||
|
}) if *diff_line_i == current_search_hit.diff_line_i => {
|
||||||
// go to next search result on same side & same line
|
// go to next search result on same side & same line
|
||||||
search_result.cursor.index += 1;
|
search_result.cursor.index += 1;
|
||||||
Some((
|
Some((
|
||||||
next_range.clone(),
|
src_byte_range.clone(),
|
||||||
gpui::HighlightStyle {
|
gpui::HighlightStyle {
|
||||||
background_color: Some(gpui::red()),
|
background_color: Some(gpui::red()),
|
||||||
..symbol_highlight_style(theme)
|
..symbol_highlight_style(theme)
|
||||||
@@ -327,33 +347,42 @@ impl PullRequestDiffView {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
| next => {
|
| next => {
|
||||||
let next_highlight_line = next.map(|(line, _)| *line).unwrap_or(usize::MAX);
|
let next_highlight_line = next
|
||||||
|
.map(|hit| hit.diff_line_i)
|
||||||
|
.unwrap_or(DiffLineIndex::MAX);
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let mut other_side_result: Option<(usize, usize, &std::ops::Range<usize>)> = None;
|
let mut other_side_result: Option<(DiffLineIndex, usize, &std::ops::Range<usize>)> =
|
||||||
while let Some((other_side_line_i, r)) = &other_side.get(i) {
|
None;
|
||||||
if *other_side_line_i == current_line_index {
|
while let Some(DiffSearchHit {
|
||||||
|
diff_line_i,
|
||||||
|
src_byte_range,
|
||||||
|
}) = &other_side.get(i)
|
||||||
|
{
|
||||||
|
if *diff_line_i == current_search_hit.diff_line_i
|
||||||
|
&& matches!(search_result.cursor.side, util::diff::DiffSide::Old)
|
||||||
|
{
|
||||||
// found other side highlight on the current line
|
// found other side highlight on the current line
|
||||||
other_side_result = Some((*other_side_line_i, i, r));
|
// we are on old side, so jump to new side on same line
|
||||||
|
other_side_result = Some((*diff_line_i, i, src_byte_range));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if *other_side_line_i > current_line_index
|
if *diff_line_i > current_search_hit.diff_line_i
|
||||||
&& *other_side_line_i < next_highlight_line
|
&& *diff_line_i <= next_highlight_line
|
||||||
{
|
{
|
||||||
// found other side highlight in between current side highlight and next side highlight
|
// found other side highlight in between current side highlight and next side highlight
|
||||||
other_side_result = Some((*other_side_line_i, i, r));
|
other_side_result = Some((*diff_line_i, i, src_byte_range));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if *other_side_line_i > next_highlight_line {
|
if *diff_line_i > next_highlight_line {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((other_side_line_i, other_side_i, r)) = other_side_result {
|
if let Some((_, other_side_i, r)) = other_side_result {
|
||||||
// next cursor should be on other side with the found index
|
// next cursor should be on other side with the found index
|
||||||
search_result.cursor.is_old = !search_result.cursor.is_old;
|
search_result.cursor.side = search_result.cursor.side.flipped();
|
||||||
search_result.cursor.line_index = other_side_line_i;
|
|
||||||
search_result.cursor.index = other_side_i;
|
search_result.cursor.index = other_side_i;
|
||||||
Some((
|
Some((
|
||||||
r.clone(),
|
r.clone(),
|
||||||
@@ -364,10 +393,9 @@ impl PullRequestDiffView {
|
|||||||
))
|
))
|
||||||
} else if let Some(next) = next {
|
} else if let Some(next) = next {
|
||||||
// stay on old side, point to next old side highlight
|
// stay on old side, point to next old side highlight
|
||||||
search_result.cursor.line_index = next_highlight_line;
|
|
||||||
search_result.cursor.index = search_result.cursor.index + 1;
|
search_result.cursor.index = search_result.cursor.index + 1;
|
||||||
Some((
|
Some((
|
||||||
next.1.clone(),
|
next.src_byte_range.clone(),
|
||||||
gpui::HighlightStyle {
|
gpui::HighlightStyle {
|
||||||
background_color: Some(gpui::red()),
|
background_color: Some(gpui::red()),
|
||||||
..symbol_highlight_style(theme)
|
..symbol_highlight_style(theme)
|
||||||
@@ -380,13 +408,11 @@ impl PullRequestDiffView {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Some(highlight_range) = highlight_range {
|
if let Some(highlight_range) = highlight_range {
|
||||||
let content = if search_result.cursor.is_old {
|
let content = match search_result.cursor.side {
|
||||||
diff_view_state.old_side_highlights_mut()
|
| util::diff::DiffSide::Old => diff_view_state.old_side_highlights_mut(),
|
||||||
} else {
|
| util::diff::DiffSide::New => diff_view_state.new_side_highlights_mut(),
|
||||||
diff_view_state.new_side_highlights_mut()
|
|
||||||
};
|
};
|
||||||
if let Some(mut content) = content {
|
if let Some(mut content) = content {
|
||||||
println!("replacing highlight range {:?}", highlight_range);
|
|
||||||
content.replace_highlight_range(&highlight_range);
|
content.replace_highlight_range(&highlight_range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
299
src/util/diff.rs
299
src/util/diff.rs
@@ -4,6 +4,12 @@ use similar::DiffableStr;
|
|||||||
|
|
||||||
use crate::util;
|
use crate::util;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub(crate) enum DiffSide {
|
||||||
|
Old,
|
||||||
|
New,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub(crate) enum Op {
|
pub(crate) enum Op {
|
||||||
Equal,
|
Equal,
|
||||||
@@ -12,6 +18,9 @@ pub(crate) enum Op {
|
|||||||
Replace,
|
Replace,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct DiffLineIndex(usize);
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct DiffLine {
|
pub(crate) struct DiffLine {
|
||||||
pub(crate) op: Op,
|
pub(crate) op: Op,
|
||||||
@@ -26,12 +35,31 @@ pub(crate) struct DiffLine {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct ContentDiff {
|
pub(crate) struct ContentDiff {
|
||||||
pub(crate) diff_lines: Vec<DiffLine>,
|
pub(crate) diff_lines: Vec<DiffLine>,
|
||||||
|
|
||||||
|
// translates source line indices to diff_lines indices.
|
||||||
|
old_line_to_diff_line_indices: Vec<DiffLineIndex>,
|
||||||
|
new_line_to_diff_line_indices: Vec<DiffLineIndex>,
|
||||||
|
|
||||||
pub(crate) old_content: bytes::Bytes,
|
pub(crate) old_content: bytes::Bytes,
|
||||||
pub(crate) old_line_count: usize,
|
pub(crate) old_line_count: usize,
|
||||||
|
|
||||||
pub(crate) new_content: bytes::Bytes,
|
pub(crate) new_content: bytes::Bytes,
|
||||||
pub(crate) new_line_count: usize,
|
pub(crate) new_line_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DiffLineIndex {
|
||||||
|
pub(crate) const MAX: DiffLineIndex = DiffLineIndex(usize::MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DiffSide {
|
||||||
|
pub(crate) fn flipped(&self) -> Self {
|
||||||
|
match self {
|
||||||
|
| DiffSide::New => DiffSide::Old,
|
||||||
|
| DiffSide::Old => DiffSide::New,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn diff_content(
|
pub(crate) fn diff_content(
|
||||||
old_content: bytes::Bytes,
|
old_content: bytes::Bytes,
|
||||||
new_content: bytes::Bytes,
|
new_content: bytes::Bytes,
|
||||||
@@ -41,135 +69,181 @@ pub(crate) fn diff_content(
|
|||||||
let diff = similar::TextDiff::from_lines::<[u8]>(&old_content, &new_content);
|
let diff = similar::TextDiff::from_lines::<[u8]>(&old_content, &new_content);
|
||||||
|
|
||||||
let mut diff_lines: Vec<DiffLine> = Vec::new();
|
let mut diff_lines: Vec<DiffLine> = Vec::new();
|
||||||
|
let mut old_line_to_diff_line_indices: Vec<DiffLineIndex> =
|
||||||
|
Vec::with_capacity(old_line_ranges.len());
|
||||||
|
let mut new_line_to_diff_line_indices: Vec<DiffLineIndex> =
|
||||||
|
Vec::with_capacity(new_line_ranges.len());
|
||||||
|
|
||||||
|
fn push_diff_line_index(
|
||||||
|
line_i: usize,
|
||||||
|
diff_line_i: DiffLineIndex,
|
||||||
|
indices: &mut Vec<DiffLineIndex>,
|
||||||
|
) {
|
||||||
|
if indices.get(line_i).is_none() {
|
||||||
|
indices.push(diff_line_i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for op in diff.ops() {
|
for op in diff.ops() {
|
||||||
match op {
|
match op {
|
||||||
| &similar::DiffOp::Equal {
|
| &similar::DiffOp::Equal {
|
||||||
old_index,
|
old_index,
|
||||||
new_index,
|
new_index,
|
||||||
len,
|
len,
|
||||||
} => {
|
} => {
|
||||||
for i in 0..len {
|
for i in 0..len {
|
||||||
let old_line = old_index + i;
|
let old_line = old_index + i;
|
||||||
let new_line = new_index + i;
|
let new_line = new_index + i;
|
||||||
let old_line_range = &old_line_ranges[old_line];
|
let old_line_range = &old_line_ranges[old_line];
|
||||||
let content = Arc::from(old_content.slice(old_line_range.clone()).as_str()?);
|
let content = Arc::from(old_content.slice(old_line_range.clone()).as_str()?);
|
||||||
diff_lines.push(DiffLine {
|
|
||||||
op: Op::Equal,
|
|
||||||
old_line: Some(old_line),
|
|
||||||
old_content: Some(Arc::clone(&content)),
|
|
||||||
old_byte_range: old_line_range.clone(),
|
|
||||||
new_line: Some(new_line),
|
|
||||||
new_content: Some(content),
|
|
||||||
new_byte_range: new_line_ranges[new_line].clone(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
| &similar::DiffOp::Insert {
|
diff_lines.push(DiffLine {
|
||||||
new_index, new_len, ..
|
op: Op::Equal,
|
||||||
} => {
|
old_line: Some(old_line),
|
||||||
for i in 0..new_len {
|
old_content: Some(Arc::clone(&content)),
|
||||||
let new_line_range = &new_line_ranges[new_index + i];
|
old_byte_range: old_line_range.clone(),
|
||||||
let content = Arc::from(new_content.slice(new_line_range.clone()).as_str()?);
|
new_line: Some(new_line),
|
||||||
diff_lines.push(DiffLine {
|
new_content: Some(content),
|
||||||
op: Op::Insert,
|
new_byte_range: new_line_ranges[new_line].clone(),
|
||||||
|
});
|
||||||
|
|
||||||
|
let diff_line_i = DiffLineIndex(diff_lines.len() - 1);
|
||||||
|
|
||||||
|
push_diff_line_index(old_line, diff_line_i, &mut old_line_to_diff_line_indices);
|
||||||
|
push_diff_line_index(new_line, diff_line_i, &mut new_line_to_diff_line_indices);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
| &similar::DiffOp::Insert {
|
||||||
|
new_index, new_len, ..
|
||||||
|
} => {
|
||||||
|
for i in 0..new_len {
|
||||||
|
let new_line_range = &new_line_ranges[new_index + i];
|
||||||
|
let content = Arc::from(new_content.slice(new_line_range.clone()).as_str()?);
|
||||||
|
let new_line = new_index + i;
|
||||||
|
diff_lines.push(DiffLine {
|
||||||
|
op: Op::Insert,
|
||||||
|
old_line: None,
|
||||||
|
old_content: None,
|
||||||
|
old_byte_range: 0..0,
|
||||||
|
new_line: Some(new_line),
|
||||||
|
new_content: Some(content),
|
||||||
|
new_byte_range: new_line_range.clone(),
|
||||||
|
});
|
||||||
|
push_diff_line_index(
|
||||||
|
new_line,
|
||||||
|
DiffLineIndex(diff_lines.len() - 1),
|
||||||
|
&mut new_line_to_diff_line_indices,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
| &similar::DiffOp::Replace {
|
||||||
|
old_index,
|
||||||
|
old_len,
|
||||||
|
new_index,
|
||||||
|
new_len,
|
||||||
|
} => {
|
||||||
|
for i in 0..new_len.max(old_len) {
|
||||||
|
let old_line = old_index + i;
|
||||||
|
let new_line = new_index + i;
|
||||||
|
let diff_line_i = DiffLineIndex(diff_lines.len());
|
||||||
|
|
||||||
|
let diff_line = match (old_line_ranges.get(old_line), new_line_ranges.get(new_line))
|
||||||
|
{
|
||||||
|
| (Some(old_range), Some(new_range)) => {
|
||||||
|
push_diff_line_index(old_line, diff_line_i, &mut old_line_to_diff_line_indices);
|
||||||
|
push_diff_line_index(new_line, diff_line_i, &mut new_line_to_diff_line_indices);
|
||||||
|
|
||||||
|
DiffLine {
|
||||||
|
op: Op::Replace,
|
||||||
|
old_line: Some(old_line),
|
||||||
|
old_content: Some(Arc::from(
|
||||||
|
old_content.slice(old_range.clone()).as_str()?,
|
||||||
|
)),
|
||||||
|
old_byte_range: old_range.clone(),
|
||||||
|
new_line: Some(new_line),
|
||||||
|
new_content: Some(Arc::from(
|
||||||
|
new_content.slice(new_range.clone()).as_str()?,
|
||||||
|
)),
|
||||||
|
new_byte_range: new_range.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
| (None, Some(new_range)) => {
|
||||||
|
push_diff_line_index(new_line, diff_line_i, &mut new_line_to_diff_line_indices);
|
||||||
|
|
||||||
|
DiffLine {
|
||||||
|
op: Op::Replace,
|
||||||
old_line: None,
|
old_line: None,
|
||||||
old_content: None,
|
old_content: None,
|
||||||
old_byte_range: 0..0,
|
old_byte_range: 0..0,
|
||||||
new_line: Some(new_index + i),
|
new_line: Some(new_line),
|
||||||
new_content: Some(content),
|
new_content: Some(Arc::from(
|
||||||
new_byte_range: new_line_range.clone(),
|
new_content.slice(new_range.clone()).as_str()?,
|
||||||
})
|
)),
|
||||||
|
new_byte_range: new_range.clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
| &similar::DiffOp::Replace {
|
| (Some(old_range), None) => {
|
||||||
old_index,
|
push_diff_line_index(old_line, diff_line_i, &mut old_line_to_diff_line_indices);
|
||||||
old_len,
|
|
||||||
new_index,
|
|
||||||
new_len,
|
|
||||||
} => {
|
|
||||||
for i in 0..new_len.max(old_len) {
|
|
||||||
let old_line = old_index + i;
|
|
||||||
let new_line = new_index + i;
|
|
||||||
|
|
||||||
let diff_line = match (
|
DiffLine {
|
||||||
old_line_ranges.get(old_line),
|
op: Op::Replace,
|
||||||
new_line_ranges.get(new_line),
|
old_line: Some(old_line),
|
||||||
) {
|
old_content: Some(Arc::from(
|
||||||
| (Some(old_range), Some(new_range)) => DiffLine {
|
old_content.slice(old_range.clone()).as_str()?,
|
||||||
op: Op::Replace,
|
)),
|
||||||
old_line: Some(old_line),
|
old_byte_range: old_range.clone(),
|
||||||
old_content: Some(Arc::from(
|
|
||||||
old_content.slice(old_range.clone()).as_str()?,
|
|
||||||
)),
|
|
||||||
old_byte_range: old_range.clone(),
|
|
||||||
new_line: Some(new_line),
|
|
||||||
new_content: Some(Arc::from(
|
|
||||||
new_content.slice(new_range.clone()).as_str()?,
|
|
||||||
)),
|
|
||||||
new_byte_range: new_range.clone(),
|
|
||||||
},
|
|
||||||
|
|
||||||
| (None, Some(new_range)) => DiffLine {
|
|
||||||
op: Op::Replace,
|
|
||||||
old_line: None,
|
|
||||||
old_content: None,
|
|
||||||
old_byte_range: 0..0,
|
|
||||||
new_line: Some(new_index + i),
|
|
||||||
new_content: Some(Arc::from(
|
|
||||||
new_content.slice(new_range.clone()).as_str()?,
|
|
||||||
)),
|
|
||||||
new_byte_range: new_range.clone(),
|
|
||||||
},
|
|
||||||
|
|
||||||
| (Some(old_range), None) => DiffLine {
|
|
||||||
op: Op::Replace,
|
|
||||||
old_line: Some(old_index + i),
|
|
||||||
old_content: Some(Arc::from(
|
|
||||||
old_content.slice(old_range.clone()).as_str()?,
|
|
||||||
)),
|
|
||||||
old_byte_range: old_range.clone(),
|
|
||||||
new_line: None,
|
|
||||||
new_content: None,
|
|
||||||
new_byte_range: 0..0,
|
|
||||||
},
|
|
||||||
|
|
||||||
| (None, None) => {
|
|
||||||
// unlickly to happen, but if it does, idk
|
|
||||||
panic!(
|
|
||||||
"the unlikely happened: both old & new index of DiffOps::Replace don't point to any line in the parsed line ranges."
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
diff_lines.push(diff_line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
| &similar::DiffOp::Delete {
|
|
||||||
old_index, old_len, ..
|
|
||||||
} => {
|
|
||||||
for i in 0..old_len {
|
|
||||||
let old_line_range = &old_line_ranges[old_index];
|
|
||||||
let content = Arc::from(old_content.slice(old_line_range.clone()).as_str()?);
|
|
||||||
diff_lines.push(DiffLine {
|
|
||||||
op: Op::Delete,
|
|
||||||
old_line: Some(old_index + i),
|
|
||||||
old_content: Some(content),
|
|
||||||
old_byte_range: old_line_range.clone(),
|
|
||||||
new_line: None,
|
new_line: None,
|
||||||
new_content: None,
|
new_content: None,
|
||||||
new_byte_range: 0..0,
|
new_byte_range: 0..0,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
| (None, None) => {
|
||||||
|
// unlickly to happen, but if it does, idk
|
||||||
|
panic!(
|
||||||
|
"the unlikely happened: both old & new index of DiffOps::Replace don't point to any line in the parsed line ranges."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
diff_lines.push(diff_line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
| &similar::DiffOp::Delete {
|
||||||
|
old_index, old_len, ..
|
||||||
|
} => {
|
||||||
|
for i in 0..old_len {
|
||||||
|
let old_line = old_index + i;
|
||||||
|
let old_line_range = &old_line_ranges[old_index];
|
||||||
|
let content = Arc::from(old_content.slice(old_line_range.clone()).as_str()?);
|
||||||
|
diff_lines.push(DiffLine {
|
||||||
|
op: Op::Delete,
|
||||||
|
old_line: Some(old_index + i),
|
||||||
|
old_content: Some(content),
|
||||||
|
old_byte_range: old_line_range.clone(),
|
||||||
|
new_line: None,
|
||||||
|
new_content: None,
|
||||||
|
new_byte_range: 0..0,
|
||||||
|
});
|
||||||
|
push_diff_line_index(
|
||||||
|
old_line,
|
||||||
|
DiffLineIndex(diff_lines.len() - 1),
|
||||||
|
&mut old_line_to_diff_line_indices,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(ContentDiff {
|
Some(ContentDiff {
|
||||||
diff_lines,
|
diff_lines,
|
||||||
|
old_line_to_diff_line_indices,
|
||||||
|
new_line_to_diff_line_indices,
|
||||||
old_content,
|
old_content,
|
||||||
old_line_count: old_line_ranges.len(),
|
old_line_count: old_line_ranges.len(),
|
||||||
new_content,
|
new_content,
|
||||||
@@ -189,4 +263,11 @@ impl ContentDiff {
|
|||||||
pub(crate) fn last(&self) -> Option<&DiffLine> {
|
pub(crate) fn last(&self) -> Option<&DiffLine> {
|
||||||
self.diff_lines.last()
|
self.diff_lines.last()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn diff_line_index_for_line(&self, side: DiffSide, line_i: usize) -> DiffLineIndex {
|
||||||
|
match side {
|
||||||
|
| DiffSide::New => self.new_line_to_diff_line_indices[line_i],
|
||||||
|
| DiffSide::Old => self.old_line_to_diff_line_indices[line_i],
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
172
src/util/file.rs
172
src/util/file.rs
@@ -43,17 +43,17 @@ pub(crate) fn classify_content(content: &[u8]) -> ContentType {
|
|||||||
ContentType::Text
|
ContentType::Text
|
||||||
} else {
|
} else {
|
||||||
match memchr(0, &content[..content.len().min(8192)]) {
|
match memchr(0, &content[..content.len().min(8192)]) {
|
||||||
| None => ContentType::Text,
|
| None => ContentType::Text,
|
||||||
| Some(_) => ContentType::Binary,
|
| Some(_) => ContentType::Binary,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn file_type_from_path(path: &str) -> FileType {
|
pub(crate) fn file_type_from_path(path: &str) -> FileType {
|
||||||
match Path::new(path).extension().map(|it| it.to_str()).flatten() {
|
match Path::new(path).extension().map(|it| it.to_str()).flatten() {
|
||||||
| Some("rs") => FileType::Rust,
|
| Some("rs") => FileType::Rust,
|
||||||
| Some("js") | Some("jsx") => FileType::JavaScript,
|
| Some("js") | Some("jsx") => FileType::JavaScript,
|
||||||
| _ => FileType::Unknown,
|
| _ => FileType::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,18 +71,18 @@ pub(crate) fn line_ranges(content: &[u8]) -> Vec<std::ops::Range<usize>> {
|
|||||||
let c = content[i];
|
let c = content[i];
|
||||||
|
|
||||||
match (c, content.get(i + 1)) {
|
match (c, content.get(i + 1)) {
|
||||||
| (b'\r', Some(b'\n')) => {
|
| (b'\r', Some(b'\n')) => {
|
||||||
// if \r found, check if its \r\n or if its a lone \r
|
// if \r found, check if its \r\n or if its a lone \r
|
||||||
// if \r\n, then treat as one line break
|
// if \r\n, then treat as one line break
|
||||||
ranges.push(line_start..i + 1);
|
ranges.push(line_start..i + 1);
|
||||||
// because we already counted the \n byte, the next iter into it needs to be skipped
|
// because we already counted the \n byte, the next iter into it needs to be skipped
|
||||||
skip_next = true;
|
skip_next = true;
|
||||||
line_start = i + 2;
|
line_start = i + 2;
|
||||||
}
|
}
|
||||||
| _ => {
|
| _ => {
|
||||||
ranges.push(line_start..i);
|
ranges.push(line_start..i);
|
||||||
line_start = i + 1;
|
line_start = i + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,28 +101,28 @@ pub(crate) fn sort_by_path<T>(mut items: Vec<T>, key: impl Fn(&T) -> &str) -> So
|
|||||||
let b_is_root_file = !b_path.contains('/');
|
let b_is_root_file = !b_path.contains('/');
|
||||||
|
|
||||||
match (a_is_root_file, b_is_root_file) {
|
match (a_is_root_file, b_is_root_file) {
|
||||||
| (true, false) => return std::cmp::Ordering::Greater,
|
| (true, false) => return std::cmp::Ordering::Greater,
|
||||||
| (false, true) => return std::cmp::Ordering::Less,
|
| (false, true) => return std::cmp::Ordering::Less,
|
||||||
| _ => {}
|
| _ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut a_parts = a_path.split('/').peekable();
|
let mut a_parts = a_path.split('/').peekable();
|
||||||
let mut b_parts = b_path.split('/').peekable();
|
let mut b_parts = b_path.split('/').peekable();
|
||||||
loop {
|
loop {
|
||||||
match (a_parts.next(), b_parts.next()) {
|
match (a_parts.next(), b_parts.next()) {
|
||||||
| (Some(a), Some(b)) => {
|
| (Some(a), Some(b)) => {
|
||||||
if a != b {
|
if a != b {
|
||||||
match (a_parts.peek().is_some(), b_parts.peek().is_some()) {
|
match (a_parts.peek().is_some(), b_parts.peek().is_some()) {
|
||||||
| (true, false) => return std::cmp::Ordering::Less,
|
| (true, false) => return std::cmp::Ordering::Less,
|
||||||
| (false, true) => return std::cmp::Ordering::Greater,
|
| (false, true) => return std::cmp::Ordering::Greater,
|
||||||
| _ => {}
|
| _ => {}
|
||||||
}
|
|
||||||
return a.cmp(b);
|
|
||||||
}
|
}
|
||||||
|
return a.cmp(b);
|
||||||
}
|
}
|
||||||
| (Some(_), None) => return std::cmp::Ordering::Greater,
|
}
|
||||||
| (None, Some(_)) => return std::cmp::Ordering::Less,
|
| (Some(_), None) => return std::cmp::Ordering::Greater,
|
||||||
| (None, None) => return std::cmp::Ordering::Equal,
|
| (None, Some(_)) => return std::cmp::Ordering::Less,
|
||||||
|
| (None, None) => return std::cmp::Ordering::Equal,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -222,66 +222,66 @@ pub(crate) fn build_file_tree<T>(
|
|||||||
for path in paths.0.iter() {
|
for path in paths.0.iter() {
|
||||||
let path = key(path);
|
let path = key(path);
|
||||||
match path.rsplit_once('/') {
|
match path.rsplit_once('/') {
|
||||||
| None => {
|
| None => {
|
||||||
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, base_depth);
|
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, base_depth);
|
||||||
stack.clear();
|
stack.clear();
|
||||||
// top level file
|
// top level file
|
||||||
items.push(FileTreeItem {
|
items.push(FileTreeItem {
|
||||||
kind: FileTreeItemKind::File,
|
kind: FileTreeItemKind::File,
|
||||||
full_path: path.into(),
|
full_path: path.into(),
|
||||||
name: path.into(),
|
name: path.into(),
|
||||||
level: 0,
|
level: 0,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
| Some((parent, _)) => {
|
| Some((parent, _)) => {
|
||||||
let mut common_depth = 0;
|
let mut common_depth = 0;
|
||||||
|
|
||||||
for (i, seg) in parent.split('/').enumerate() {
|
for (i, seg) in parent.split('/').enumerate() {
|
||||||
let stack_item = stack.get(i);
|
let stack_item = stack.get(i);
|
||||||
if stack_item.is_none() {
|
if stack_item.is_none() {
|
||||||
// segment is unseen, push to stack
|
// segment is unseen, push to stack
|
||||||
stack.push(seg);
|
stack.push(seg);
|
||||||
common_depth += 1;
|
common_depth += 1;
|
||||||
} else if Some(&seg) == stack.get(i) {
|
} else if Some(&seg) == stack.get(i) {
|
||||||
// segment matches stack, continue comparison
|
// segment matches stack, continue comparison
|
||||||
common_depth += 1;
|
common_depth += 1;
|
||||||
} else {
|
|
||||||
// segment differs from stack, stop comparison
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if common_depth == stack.len() {
|
|
||||||
// current path is in same directory as stack, add to leafs
|
|
||||||
leafs.push(path);
|
|
||||||
base_depth = common_depth;
|
|
||||||
} else {
|
} else {
|
||||||
// e.g. stack = ["a", "b", "c"], path = ["a", "c"]
|
// segment differs from stack, stop comparison
|
||||||
// common dir path = "a/", stack dir path = "a/b/c", common count = 1
|
break;
|
||||||
// push common dir a to items
|
|
||||||
// also push stack dir a/b/c to items but strip a from name so that it becomes "b/c" with level equal to common_count
|
|
||||||
// finally push any leaf under a/b/c
|
|
||||||
|
|
||||||
let base_dir_created =
|
|
||||||
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, common_depth);
|
|
||||||
|
|
||||||
// pop top of stack minus common dir
|
|
||||||
stack.truncate(common_depth);
|
|
||||||
|
|
||||||
if base_dir_created {
|
|
||||||
emitted_depth = common_depth;
|
|
||||||
} else {
|
|
||||||
emitted_depth = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for seg in parent.split('/').skip(common_depth) {
|
|
||||||
stack.push(seg);
|
|
||||||
}
|
|
||||||
|
|
||||||
leafs.push(path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if common_depth == stack.len() {
|
||||||
|
// current path is in same directory as stack, add to leafs
|
||||||
|
leafs.push(path);
|
||||||
|
base_depth = common_depth;
|
||||||
|
} else {
|
||||||
|
// e.g. stack = ["a", "b", "c"], path = ["a", "c"]
|
||||||
|
// common dir path = "a/", stack dir path = "a/b/c", common count = 1
|
||||||
|
// push common dir a to items
|
||||||
|
// also push stack dir a/b/c to items but strip a from name so that it becomes "b/c" with level equal to common_count
|
||||||
|
// finally push any leaf under a/b/c
|
||||||
|
|
||||||
|
let base_dir_created =
|
||||||
|
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, common_depth);
|
||||||
|
|
||||||
|
// pop top of stack minus common dir
|
||||||
|
stack.truncate(common_depth);
|
||||||
|
|
||||||
|
if base_dir_created {
|
||||||
|
emitted_depth = common_depth;
|
||||||
|
} else {
|
||||||
|
emitted_depth = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for seg in parent.split('/').skip(common_depth) {
|
||||||
|
stack.push(seg);
|
||||||
|
}
|
||||||
|
|
||||||
|
leafs.push(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user