refactor: remove unused review state and duplicate storage reads
This commit is contained in:
+17
-10
@@ -49,13 +49,11 @@ impl Editor {
|
||||
self.review_diff_mut().unwrap().local_review.initialized = true;
|
||||
self.review_diff_mut().unwrap().local_review.document = Review::new(root.clone());
|
||||
let store = crate::review::Store::open(&directory)?;
|
||||
let choices = store.list(&root)?;
|
||||
if choices.len() == 1 {
|
||||
self.review_diff_mut().unwrap().local_review.document =
|
||||
store.load(&root, choices[0].0)?;
|
||||
} else if choices.len() > 1 {
|
||||
self.review_diff_mut().unwrap().local_review.choices = choices;
|
||||
self.open_review_selector()?;
|
||||
let mut reviews = store.list(&root)?;
|
||||
if reviews.len() == 1 {
|
||||
self.review_diff_mut().unwrap().local_review.document = reviews.pop().unwrap();
|
||||
} else if reviews.len() > 1 {
|
||||
self.show_review_selector(reviews)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -71,15 +69,24 @@ impl Editor {
|
||||
.as_ref()
|
||||
.ok_or("Comparison is loading")?
|
||||
.root;
|
||||
let choices = crate::review::Store::open(directory)?.list(root)?;
|
||||
let reviews = crate::review::Store::open(directory)?.list(root)?;
|
||||
self.show_review_selector(reviews)
|
||||
}
|
||||
|
||||
fn show_review_selector(&mut self, reviews: Vec<Review>) -> Result<(), String> {
|
||||
let mut text = String::from(
|
||||
"# Local reviews\n\nEnter opens the review under the caret.\n:diff.review.new starts a separate review.\n",
|
||||
);
|
||||
for (id, label) in &choices {
|
||||
for review in reviews {
|
||||
let id = review.storage_id.unwrap();
|
||||
let label = review
|
||||
.summary
|
||||
.lines()
|
||||
.find(|line| !line.trim().is_empty())
|
||||
.map_or_else(|| format!("Review {id}"), |line| line.trim().to_owned());
|
||||
use std::fmt::Write as _;
|
||||
writeln!(text, "\n## Review #{id} · {label}").unwrap();
|
||||
}
|
||||
self.review_diff_mut().unwrap().local_review.choices = choices;
|
||||
let row = text
|
||||
.lines()
|
||||
.position(|line| line.starts_with("## Review #"))
|
||||
|
||||
+4
-16
@@ -59,10 +59,6 @@ impl Anchor {
|
||||
self.start.row..=end
|
||||
}
|
||||
|
||||
pub fn matches(&self, revision: &str, path: &Path, side: Side, text: &str) -> bool {
|
||||
self.revision == revision && self.path == path && self.side == side && self.content == text
|
||||
}
|
||||
|
||||
pub fn matches_lines(
|
||||
&self,
|
||||
revision: &str,
|
||||
@@ -116,7 +112,6 @@ impl Default for Review {
|
||||
pub struct ReviewState {
|
||||
pub document: Review,
|
||||
pub initialized: bool,
|
||||
pub choices: Vec<(i64, String)>,
|
||||
pub path: Option<PathBuf>,
|
||||
pub selected: Option<u64>,
|
||||
pub pending_navigation: Option<u64>,
|
||||
@@ -407,7 +402,7 @@ impl Store {
|
||||
Ok(Self { connection })
|
||||
}
|
||||
|
||||
pub fn list(&self, root: &Path) -> Result<Vec<(i64, String)>, String> {
|
||||
pub fn list(&self, root: &Path) -> Result<Vec<Review>, String> {
|
||||
let root = fs::canonicalize(root).map_err(|error| error.to_string())?;
|
||||
let root_text = root
|
||||
.to_str()
|
||||
@@ -419,18 +414,11 @@ impl Store {
|
||||
)
|
||||
.map_err(database_error)?;
|
||||
let mut rows = statement.query([root_text]).map_err(database_error)?;
|
||||
let mut choices = Vec::new();
|
||||
let mut reviews = Vec::new();
|
||||
while let Some(row) = rows.next().map_err(database_error)? {
|
||||
let review = stored_review(row, &root)?;
|
||||
let id = review.storage_id.unwrap();
|
||||
let label = review
|
||||
.summary
|
||||
.lines()
|
||||
.find(|line| !line.trim().is_empty())
|
||||
.map_or_else(|| format!("Review {id}"), |line| line.trim().to_owned());
|
||||
choices.push((id, label));
|
||||
reviews.push(stored_review(row, &root)?);
|
||||
}
|
||||
Ok(choices)
|
||||
Ok(reviews)
|
||||
}
|
||||
|
||||
pub fn load(&self, root: &Path, id: i64) -> Result<Review, String> {
|
||||
|
||||
+2
-6
@@ -125,12 +125,8 @@ pub fn run(request: &Request, config: Option<&Path>) -> Result<(), String> {
|
||||
Some(store) => store
|
||||
.list(&root)?
|
||||
.into_iter()
|
||||
.map(|(id, _)| {
|
||||
store
|
||||
.load(&root, id)
|
||||
.map(|review| envelope(&review, true, false))
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
.map(|review| envelope(&review, true, false))
|
||||
.collect::<Vec<_>>(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
json!({"repository": root, "reviews": reviews})
|
||||
|
||||
Reference in New Issue
Block a user