Reorder commands inside a file again
This commit is contained in:
parent
d169037b7a
commit
039b7b73ee
2 changed files with 42 additions and 6 deletions
|
|
@ -26,8 +26,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
- It now displays the source command (file and line) of the entry
|
- It now displays the source command (file and line) of the entry
|
||||||
- When saving...
|
- When saving...
|
||||||
- Unchanged files are no longer overwritten
|
- Unchanged files are no longer overwritten
|
||||||
- Commands are no longer reordered
|
- Imports are now sorted alphabetically
|
||||||
- Done and cancel dates are simplified
|
- Done and cancel dates are now simplified where possible
|
||||||
- Always prints import-based path, not absolute path
|
- Always prints import-based path, not absolute path
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
|
||||||
|
|
@ -318,20 +318,56 @@ impl fmt::Display for Log {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl File {
|
impl File {
|
||||||
|
fn sort(commands: &mut Vec<&Command>) {
|
||||||
|
// Order of commands in a file:
|
||||||
|
// 1. Imports, sorted alphabetically
|
||||||
|
// 2. Time zone(s)
|
||||||
|
// 3. Log entries, sorted by date (ascending)
|
||||||
|
// 4. Tasks and notes, in original order
|
||||||
|
|
||||||
|
// There should always be at most one time zone, so we don't care about
|
||||||
|
// their order.
|
||||||
|
|
||||||
|
// In the individual steps we must use a stable sort so the order of 4.
|
||||||
|
// is not lost.
|
||||||
|
|
||||||
|
// Order imports alphabetically
|
||||||
|
commands.sort_by_key(|c| match c {
|
||||||
|
Command::Include(path) => Some(&path.value),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Order log entries by date
|
||||||
|
commands.sort_by_key(|c| match c {
|
||||||
|
Command::Log(Log { date, .. }) => Some(date.value),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Order by type
|
||||||
|
commands.sort_by_key(|c| match c {
|
||||||
|
Command::Include(_) => 0,
|
||||||
|
Command::Timezone(_) => 1,
|
||||||
|
Command::Log(_) => 2,
|
||||||
|
Command::Task(_) | Command::Note(_) => 3,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn format(&self, removed: &HashSet<usize>) -> String {
|
pub fn format(&self, removed: &HashSet<usize>) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
|
|
||||||
let commands = self
|
let mut commands = self
|
||||||
.commands
|
.commands
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(i, _)| !removed.contains(i))
|
.filter(|(i, _)| !removed.contains(i))
|
||||||
.map(|(_, c)| c)
|
.map(|(_, c)| &c.value)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Self::sort(&mut commands);
|
||||||
|
|
||||||
for i in 0..commands.len() {
|
for i in 0..commands.len() {
|
||||||
let curr = &commands[i].value;
|
let curr = &commands[i];
|
||||||
let next = commands.get(i + 1).map(|c| &c.value);
|
let next = commands.get(i + 1);
|
||||||
|
|
||||||
result.push_str(&format!("{}", curr));
|
result.push_str(&format!("{}", curr));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue