feat: add dummy search bar in diff view

This commit is contained in:
2026-05-31 00:45:25 +01:00
parent 4a7ebf660e
commit f5ebb210ac
11 changed files with 1273 additions and 262 deletions

View File

@@ -43,17 +43,17 @@ pub(crate) fn classify_content(content: &[u8]) -> ContentType {
ContentType::Text
} else {
match memchr(0, &content[..content.len().min(8192)]) {
| None => ContentType::Text,
| Some(_) => ContentType::Binary,
| None => ContentType::Text,
| Some(_) => ContentType::Binary,
}
}
}
pub(crate) fn file_type_from_path(path: &str) -> FileType {
match Path::new(path).extension().map(|it| it.to_str()).flatten() {
| Some("rs") => FileType::Rust,
| Some("js") | Some("jsx") => FileType::JavaScript,
| _ => FileType::Unknown,
| Some("rs") => FileType::Rust,
| Some("js") | Some("jsx") => FileType::JavaScript,
| _ => FileType::Unknown,
}
}
@@ -71,18 +71,18 @@ pub(crate) fn line_ranges(content: &[u8]) -> Vec<std::ops::Range<usize>> {
let c = content[i];
match (c, content.get(i + 1)) {
| (b'\r', Some(b'\n')) => {
// if \r found, check if its \r\n or if its a lone \r
// if \r\n, then treat as one line break
ranges.push(line_start..i + 1);
// because we already counted the \n byte, the next iter into it needs to be skipped
skip_next = true;
line_start = i + 2;
}
| _ => {
ranges.push(line_start..i);
line_start = i + 1;
}
| (b'\r', Some(b'\n')) => {
// if \r found, check if its \r\n or if its a lone \r
// if \r\n, then treat as one line break
ranges.push(line_start..i + 1);
// because we already counted the \n byte, the next iter into it needs to be skipped
skip_next = true;
line_start = i + 2;
}
| _ => {
ranges.push(line_start..i);
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('/');
match (a_is_root_file, b_is_root_file) {
| (true, false) => return std::cmp::Ordering::Greater,
| (false, true) => return std::cmp::Ordering::Less,
| _ => {}
| (true, false) => return std::cmp::Ordering::Greater,
| (false, true) => return std::cmp::Ordering::Less,
| _ => {}
}
let mut a_parts = a_path.split('/').peekable();
let mut b_parts = b_path.split('/').peekable();
loop {
match (a_parts.next(), b_parts.next()) {
| (Some(a), Some(b)) => {
if a != b {
match (a_parts.peek().is_some(), b_parts.peek().is_some()) {
| (true, false) => return std::cmp::Ordering::Less,
| (false, true) => return std::cmp::Ordering::Greater,
| _ => {}
| (Some(a), Some(b)) => {
if a != b {
match (a_parts.peek().is_some(), b_parts.peek().is_some()) {
| (true, false) => return std::cmp::Ordering::Less,
| (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,
| (None, None) => return std::cmp::Ordering::Equal,
| (Some(_), None) => return std::cmp::Ordering::Greater,
| (None, Some(_)) => return std::cmp::Ordering::Less,
| (None, None) => return std::cmp::Ordering::Equal,
}
}
});
@@ -222,67 +222,67 @@ pub(crate) fn build_file_tree<T>(
for path in paths.0.iter() {
let path = key(path);
match path.rsplit_once('/') {
| None => {
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, base_depth);
stack.clear();
// top level file
items.push(FileTreeItem {
kind: FileTreeItemKind::File,
full_path: path.into(),
name: path.into(),
level: 0,
});
}
| Some((parent, _)) => {
let mut common_depth = 0;
for (i, seg) in parent.split('/').enumerate() {
let stack_item = stack.get(i);
if stack_item.is_none() {
// segment is unseen, push to stack
stack.push(seg);
common_depth += 1;
} else if Some(&seg) == stack.get(i) {
// segment matches stack, continue comparison
common_depth += 1;
} else {
// segment differs from stack, stop comparison
break;
}
| None => {
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, base_depth);
stack.clear();
// top level file
items.push(FileTreeItem {
kind: FileTreeItemKind::File,
full_path: path.into(),
name: path.into(),
level: 0,
});
}
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
| Some((parent, _)) => {
let mut common_depth = 0;
let base_dir_created =
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, common_depth);
for (i, seg) in parent.split('/').enumerate() {
let stack_item = stack.get(i);
if stack_item.is_none() {
// segment is unseen, push to stack
stack.push(seg);
common_depth += 1;
} else if Some(&seg) == stack.get(i) {
// segment matches stack, continue comparison
common_depth += 1;
} else {
// segment differs from stack, stop comparison
break;
}
}
// pop top of stack minus common dir
stack.truncate(common_depth);
if base_dir_created {
emitted_depth = common_depth;
if common_depth == stack.len() {
// current path is in same directory as stack, add to leafs
leafs.push(path);
base_depth = common_depth;
} else {
emitted_depth = 0;
}
// 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
for seg in parent.split('/').skip(common_depth) {
stack.push(seg);
}
let base_dir_created =
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, common_depth);
leafs.push(path);
// 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);
}
}
}
}
}
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, base_depth);