Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/folder view #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/components/diff_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use similar::ChangeTag;
use std::rc::Rc;
use syntect::highlighting::Style;
use yew::prelude::*;
use yew_hooks::use_visible;

/// Contains information about contiguous changes
#[derive(PartialEq, Clone)]
Expand Down Expand Up @@ -131,6 +132,38 @@ pub fn DiffView(props: &DiffViewProps) -> Html {
}
}

#[function_component]
pub fn LazyDiffView(props: &DiffViewProps) -> Html {
let node = use_node_ref();
let visible = use_visible(node.clone(), true);

let len_estimation = if visible {
// no need to estimate
None
} else {
// just to clarify: thisis obviously wrong for files w/o changes (but atm those files are visible anyway)
let empty = FileDiff::default();
let file_diff = props.diff.files.get(&props.path).unwrap_or(&empty);
Some(
file_diff
.context_ranges
.iter()
.map(|chunk| chunk.len())
.sum::<usize>(),
)
};

html! {
<div ref={node}>
if visible {
<DiffView diff={props.diff.clone()} path={props.path.clone()} />
} else {
<div style={format!("height: {}px; width: 100%;", len_estimation.unwrap_or(0)*24)} />
}
</div>
}
}

#[derive(Properties, PartialEq)]
pub struct AnyDiffViewProps {
stack: Vec<DiffGroupInfo>,
Expand Down
12 changes: 7 additions & 5 deletions src/components/file_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn FileEntry(props: &FileEntryProps) -> Html {

html! {
<>
<Link to={route} classes={classes!("file-entry", current.then_some("active"))}>
<div class={classes!("file-entry", current.then_some("active"))}>
<button class={classes!("toggle", (*expanded).then_some("active"))} onclick={toggle_expand}>
if props.entry.item.is_dir() {
<ExpandIcon is_expanded={*expanded} />
Expand All @@ -195,9 +195,11 @@ fn FileEntry(props: &FileEntryProps) -> Html {
<FileIcon />
}
</div>
<div class="name">
{&props.entry.name}
</div>
<Link to={route} classes={classes!("name")}>
<div >
{&props.entry.name}
</div>
</Link>
<div class="tags">
if props.entry.changes.added > 0 {
<span class="tag added">{"+"}{props.entry.changes.added}</span>
Expand All @@ -206,7 +208,7 @@ fn FileEntry(props: &FileEntryProps) -> Html {
<span class="tag removed">{"-"}{props.entry.changes.removed}</span>
}
</div>
</Link>
</div>
if props.entry.item.is_dir() && *expanded {
<SubTree
entry={props.entry.clone()}
Expand Down
3 changes: 3 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ impl ChunkInfo {
pub fn end(&self) -> usize {
self.range.end
}
pub fn len(&self) -> usize {
self.range.len()
}
}

#[derive(Default, Clone, Copy, PartialEq, Eq, Debug)]
Expand Down
83 changes: 81 additions & 2 deletions src/views/diff.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{cache::*, components::*, data::*, version::VersionId, Route};
use camino::Utf8PathBuf;
use semver::Version;
use std::sync::Arc;
use std::{collections::BTreeMap, sync::Arc};
use yew::{prelude::*, suspense::*};
use yew_router::prelude::*;

Expand Down Expand Up @@ -268,6 +268,12 @@ pub fn SourceView(props: &SourceViewProps) -> Html {
VersionDiff::new(old.clone(), new.clone())
});
let navigator = use_navigator().unwrap();

let files = determine_display_files(&FileTreeProps {
diff: diff.clone(),
path: props.path.clone(),
});

html! {
<>
<ComplexNavbar
Expand Down Expand Up @@ -300,10 +306,83 @@ pub fn SourceView(props: &SourceViewProps) -> Html {
/>
</nav>
<div id="diff-view" class="flex-1">
<DiffView {diff} path={props.path.clone()} />
{ for (*files).iter().map(|path| html!{<LazyDiffView diff={diff.clone()} path={path.clone()} /> }) }
</div>
</main>
</Content>
</>
}
}

fn determine_display_files(props: &FileTreeProps) -> Vec<Utf8PathBuf> {
if props.diff.files.contains_key(&props.path) {
// found a file e \o/
return vec![props.path.clone()];
}

let path = props.path.clone();
let mut navigator = &props.diff.tree.item;

for path_components in path.components() {
if let Item::Dir(dir) = navigator {
if let Some(deeper) = dir.get(&path_components.to_string()) {
if deeper.changes != Changes::default() {
navigator = &deeper.item;
continue;
}
}
}

return vec![path.clone()];
}

let files_in_path: Vec<Utf8PathBuf> = if let Item::Dir(dir) = navigator {
rel_filepaths_in_tree(dir, true)
.into_iter()
.map(|p| {
let mut dirn_name = props.path.clone();
dirn_name.push(&p);
dirn_name
})
.collect()
} else {
unreachable!()
};

if files_in_path.is_empty() {
vec![props.path.clone()]
} else {
files_in_path
}
}

fn rel_filepaths_in_tree(
dir: &BTreeMap<String, std::rc::Rc<Entry>>,
no_unchanged: bool,
) -> Vec<Utf8PathBuf> {
let mut paths: Vec<Utf8PathBuf> = Vec::new();
for (name, entry) in dir {
if entry.changes == Changes::default() && no_unchanged {
continue;
}

match entry.item {
Item::Dir(ref dir) => {
let mut sub_tree_files = rel_filepaths_in_tree(dir, no_unchanged)
.into_iter()
.map(|files_relative_path| {
let mut cur_dir = Utf8PathBuf::from(name);
cur_dir.push(&files_relative_path);
cur_dir
})
.collect();
paths.append(&mut sub_tree_files);
}
Item::File => {
paths.push(name.into());
}
}
}

paths
}