Add spans to all commands

This commit is contained in:
Joscha 2022-01-09 15:05:50 +01:00
parent e0b6c839e2
commit b430f1594f
6 changed files with 23 additions and 21 deletions

View file

@ -37,7 +37,7 @@ fn show_entry(files: &Files, entry: &Entry) {
} }
println!("FROM COMMAND"); println!("FROM COMMAND");
show_command(command.value); show_command(&command.value.value);
} }
fn show_log(files: &Files, log: Sourced<'_, Log>) { 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!("LOG {}", log.value.date);
println!("FROM COMMAND"); println!("FROM COMMAND");
show_command(command.value); show_command(&command.value.value);
} }
fn show_ident(files: &Files, entries: &[Entry], layout: &LineLayout, ident: Ident) { fn show_ident(files: &Files, entries: &[Entry], layout: &LineLayout, ident: Ident) {

View file

@ -24,7 +24,7 @@ impl Files {
let mut entries = Entries::new(mode, range); let mut entries = Entries::new(mode, range);
for command in self.commands() { for command in self.commands() {
let source = command.source; 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() { for entry in CommandState::new(command, source, range).eval()?.entries() {
entries.add(entry); entries.add(entry);
} }

View file

@ -198,7 +198,7 @@ impl Files {
let includes = file let includes = file
.commands .commands
.iter() .iter()
.filter_map(|c| match c { .filter_map(|c| match &c.value {
Command::Include(path) => Some(path.clone()), Command::Include(path) => Some(path.clone()),
_ => None, _ => None,
}) })
@ -224,7 +224,7 @@ impl Files {
let mut found: Option<(Source, Spanned<String>)> = None; let mut found: Option<(Source, Spanned<String>)> = None;
for command in self.commands() { 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 let Some((found_source, found_tz)) = &found {
if tz.value != found_tz.value { if tz.value != found_tz.value {
return Err(Error::TzConflict { return Err(Error::TzConflict {
@ -259,14 +259,14 @@ impl Files {
fn collect_logs(&mut self) -> Result<()> { fn collect_logs(&mut self) -> Result<()> {
for command in Self::commands_of_files(&self.files) { 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) { match self.logs.entry(log.date.value) {
Entry::Vacant(e) => { Entry::Vacant(e) => {
e.insert(command.source); e.insert(command.source);
} }
Entry::Occupied(e) => { Entry::Occupied(e) => {
let other_cmd = Self::command_of_files(&self.files, *e.get()); 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, Command::Log(log) => log.date.span,
_ => unreachable!(), _ => unreachable!(),
}; };
@ -313,7 +313,7 @@ impl Files {
/* Querying */ /* Querying */
fn commands_of_files(files: &[LoadedFile]) -> Vec<Sourced<'_, Command>> { fn commands_of_files(files: &[LoadedFile]) -> Vec<Sourced<'_, Spanned<Command>>> {
let mut result = vec![]; let mut result = vec![];
for (file_index, file) in files.iter().enumerate() { for (file_index, file) in files.iter().enumerate() {
for (command_index, command) in file.file.commands.iter().enumerate() { for (command_index, command) in file.file.commands.iter().enumerate() {
@ -324,22 +324,22 @@ impl Files {
result result
} }
pub fn commands(&self) -> Vec<Sourced<'_, Command>> { pub fn commands(&self) -> Vec<Sourced<'_, Spanned<Command>>> {
Self::commands_of_files(&self.files) 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<Command>> {
let command = &files[source.file].file.commands[source.command]; let command = &files[source.file].file.commands[source.command];
Sourced::new(source, command) Sourced::new(source, command)
} }
pub fn command(&self, source: Source) -> Sourced<'_, Command> { pub fn command(&self, source: Source) -> Sourced<'_, Spanned<Command>> {
Self::command_of_files(&self.files, source) Self::command_of_files(&self.files, source)
} }
pub fn log(&self, date: NaiveDate) -> Option<Sourced<'_, Log>> { pub fn log(&self, date: NaiveDate) -> Option<Sourced<'_, Log>> {
let source = *self.logs.get(&date)?; 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)), Command::Log(log) => Some(Sourced::new(source, log)),
_ => unreachable!(), _ => unreachable!(),
} }
@ -378,13 +378,13 @@ impl Files {
fn modify(&mut self, source: Source, edit: impl FnOnce(&mut Command)) { fn modify(&mut self, source: Source, edit: impl FnOnce(&mut Command)) {
let file = &mut self.files[source.file]; 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; file.dirty = true;
} }
fn insert(&mut self, file: FileSource, command: Command) { fn insert(&mut self, file: FileSource, command: Command) {
let file = &mut self.files[file.0]; let file = &mut self.files[file.0];
file.file.commands.push(command); file.file.commands.push(Spanned::dummy(command));
file.dirty = true; file.dirty = true;
} }
@ -401,7 +401,7 @@ impl Files {
#[must_use] #[must_use]
pub fn add_done(&mut self, source: Source, done: Done) -> bool { pub fn add_done(&mut self, source: Source, done: Done) -> bool {
let file = &mut self.files[source.file]; 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), Command::Task(t) => t.done.push(done),
_ => return false, _ => return false,
} }

View file

@ -355,7 +355,7 @@ pub enum Command {
#[derive(Debug)] #[derive(Debug)]
pub struct File { pub struct File {
pub contents: String, pub contents: String,
pub commands: Vec<Command>, pub commands: Vec<Spanned<Command>>,
} }
impl File { impl File {

View file

@ -331,8 +331,8 @@ impl File {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for i in 0..commands.len() { for i in 0..commands.len() {
let curr = commands[i]; let curr = &commands[i].value;
let next = commands.get(i + 1).copied(); let next = commands.get(i + 1).map(|c| &c.value);
result.push_str(&format!("{}", curr)); result.push_str(&format!("{}", curr));

View file

@ -818,18 +818,20 @@ fn parse_log(p: Pair<'_, Rule>) -> Result<Log> {
Ok(Log { date, desc }) Ok(Log { date, desc })
} }
fn parse_command(p: Pair<'_, Rule>) -> Result<Command> { fn parse_command(p: Pair<'_, Rule>) -> Result<Spanned<Command>> {
assert_eq!(p.as_rule(), Rule::command); assert_eq!(p.as_rule(), Rule::command);
let p = p.into_inner().next().unwrap(); 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::include => Command::Include(parse_include(p)),
Rule::timezone => Command::Timezone(parse_timezone(p)), Rule::timezone => Command::Timezone(parse_timezone(p)),
Rule::task => Command::Task(parse_task(p)?), Rule::task => Command::Task(parse_task(p)?),
Rule::note => Command::Note(parse_note(p)?), Rule::note => Command::Note(parse_note(p)?),
Rule::log => Command::Log(parse_log(p)?), Rule::log => Command::Log(parse_log(p)?),
_ => unreachable!(), _ => unreachable!(),
}) };
Ok(Spanned::new(span, command))
} }
pub fn parse_file(p: Pair<'_, Rule>, contents: String) -> Result<File> { pub fn parse_file(p: Pair<'_, Rule>, contents: String) -> Result<File> {