From b430f1594fc8ef832a8a30b49523eedcc9bbe6af Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 9 Jan 2022 15:05:50 +0100 Subject: [PATCH] Add spans to all commands --- src/cli/show.rs | 4 ++-- src/eval.rs | 2 +- src/files.rs | 24 ++++++++++++------------ src/files/commands.rs | 2 +- src/files/format.rs | 4 ++-- src/files/parse.rs | 8 +++++--- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/cli/show.rs b/src/cli/show.rs index 07c648c..b0c399a 100644 --- a/src/cli/show.rs +++ b/src/cli/show.rs @@ -37,7 +37,7 @@ fn show_entry(files: &Files, entry: &Entry) { } println!("FROM COMMAND"); - show_command(command.value); + show_command(&command.value.value); } fn show_log(files: &Files, log: Sourced<'_, Log>) { @@ -46,7 +46,7 @@ fn show_log(files: &Files, log: Sourced<'_, Log>) { println!("LOG {}", log.value.date); println!("FROM COMMAND"); - show_command(command.value); + show_command(&command.value.value); } fn show_ident(files: &Files, entries: &[Entry], layout: &LineLayout, ident: Ident) { diff --git a/src/eval.rs b/src/eval.rs index b0ca8cf..bd02859 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -24,7 +24,7 @@ impl Files { let mut entries = Entries::new(mode, range); for command in self.commands() { let source = command.source; - if let Some(command) = EvalCommand::new(command.value) { + if let Some(command) = EvalCommand::new(&command.value.value) { for entry in CommandState::new(command, source, range).eval()?.entries() { entries.add(entry); } diff --git a/src/files.rs b/src/files.rs index e1d437a..a62c396 100644 --- a/src/files.rs +++ b/src/files.rs @@ -198,7 +198,7 @@ impl Files { let includes = file .commands .iter() - .filter_map(|c| match c { + .filter_map(|c| match &c.value { Command::Include(path) => Some(path.clone()), _ => None, }) @@ -224,7 +224,7 @@ impl Files { let mut found: Option<(Source, Spanned)> = None; for command in self.commands() { - if let Command::Timezone(tz) = command.value { + if let Command::Timezone(tz) = &command.value.value { if let Some((found_source, found_tz)) = &found { if tz.value != found_tz.value { return Err(Error::TzConflict { @@ -259,14 +259,14 @@ impl Files { fn collect_logs(&mut self) -> Result<()> { for command in Self::commands_of_files(&self.files) { - if let Command::Log(log) = command.value { + if let Command::Log(log) = &command.value.value { match self.logs.entry(log.date.value) { Entry::Vacant(e) => { e.insert(command.source); } Entry::Occupied(e) => { let other_cmd = Self::command_of_files(&self.files, *e.get()); - let other_span = match &other_cmd.value { + let other_span = match &other_cmd.value.value { Command::Log(log) => log.date.span, _ => unreachable!(), }; @@ -313,7 +313,7 @@ impl Files { /* Querying */ - fn commands_of_files(files: &[LoadedFile]) -> Vec> { + fn commands_of_files(files: &[LoadedFile]) -> Vec>> { let mut result = vec![]; for (file_index, file) in files.iter().enumerate() { for (command_index, command) in file.file.commands.iter().enumerate() { @@ -324,22 +324,22 @@ impl Files { result } - pub fn commands(&self) -> Vec> { + pub fn commands(&self) -> Vec>> { Self::commands_of_files(&self.files) } - fn command_of_files(files: &[LoadedFile], source: Source) -> Sourced<'_, Command> { + fn command_of_files(files: &[LoadedFile], source: Source) -> Sourced<'_, Spanned> { let command = &files[source.file].file.commands[source.command]; Sourced::new(source, command) } - pub fn command(&self, source: Source) -> Sourced<'_, Command> { + pub fn command(&self, source: Source) -> Sourced<'_, Spanned> { Self::command_of_files(&self.files, source) } pub fn log(&self, date: NaiveDate) -> Option> { let source = *self.logs.get(&date)?; - match self.command(source).value { + match &self.command(source).value.value { Command::Log(log) => Some(Sourced::new(source, log)), _ => unreachable!(), } @@ -378,13 +378,13 @@ impl Files { fn modify(&mut self, source: Source, edit: impl FnOnce(&mut Command)) { let file = &mut self.files[source.file]; - edit(&mut file.file.commands[source.command]); + edit(&mut file.file.commands[source.command].value); file.dirty = true; } fn insert(&mut self, file: FileSource, command: Command) { let file = &mut self.files[file.0]; - file.file.commands.push(command); + file.file.commands.push(Spanned::dummy(command)); file.dirty = true; } @@ -401,7 +401,7 @@ impl Files { #[must_use] pub fn add_done(&mut self, source: Source, done: Done) -> bool { let file = &mut self.files[source.file]; - match &mut file.file.commands[source.command] { + match &mut file.file.commands[source.command].value { Command::Task(t) => t.done.push(done), _ => return false, } diff --git a/src/files/commands.rs b/src/files/commands.rs index 82c6b51..09eb631 100644 --- a/src/files/commands.rs +++ b/src/files/commands.rs @@ -355,7 +355,7 @@ pub enum Command { #[derive(Debug)] pub struct File { pub contents: String, - pub commands: Vec, + pub commands: Vec>, } impl File { diff --git a/src/files/format.rs b/src/files/format.rs index ddc37fc..b289048 100644 --- a/src/files/format.rs +++ b/src/files/format.rs @@ -331,8 +331,8 @@ impl File { .collect::>(); for i in 0..commands.len() { - let curr = commands[i]; - let next = commands.get(i + 1).copied(); + let curr = &commands[i].value; + let next = commands.get(i + 1).map(|c| &c.value); result.push_str(&format!("{}", curr)); diff --git a/src/files/parse.rs b/src/files/parse.rs index 67c0da9..8637fb0 100644 --- a/src/files/parse.rs +++ b/src/files/parse.rs @@ -818,18 +818,20 @@ fn parse_log(p: Pair<'_, Rule>) -> Result { Ok(Log { date, desc }) } -fn parse_command(p: Pair<'_, Rule>) -> Result { +fn parse_command(p: Pair<'_, Rule>) -> Result> { assert_eq!(p.as_rule(), Rule::command); let p = p.into_inner().next().unwrap(); - Ok(match p.as_rule() { + let span = (&p.as_span()).into(); + let command = match p.as_rule() { Rule::include => Command::Include(parse_include(p)), Rule::timezone => Command::Timezone(parse_timezone(p)), Rule::task => Command::Task(parse_task(p)?), Rule::note => Command::Note(parse_note(p)?), Rule::log => Command::Log(parse_log(p)?), _ => unreachable!(), - }) + }; + Ok(Spanned::new(span, command)) } pub fn parse_file(p: Pair<'_, Rule>, contents: String) -> Result {