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

Include real line number on command parse error #29

Merged
merged 1 commit into from
Oct 25, 2023
Merged
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
67 changes: 43 additions & 24 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::{
execute::{CodeExecuter, ExecutionHandle, ExecutionState, ProcessStatus},
markdown::{
elements::{
Code, ListItem, ListItemType, MarkdownElement, ParagraphElement, StyledText, Table, TableRow, Text,
Code, ListItem, ListItemType, MarkdownElement, ParagraphElement, SourcePosition, StyledText, Table,
TableRow, Text,
},
text::{WeightedLine, WeightedText},
},
Expand All @@ -20,7 +21,7 @@ use crate::{
};
use itertools::Itertools;
use serde::Deserialize;
use std::{borrow::Cow, cell::RefCell, iter, mem, path::PathBuf, rc::Rc, str::FromStr};
use std::{borrow::Cow, cell::RefCell, fmt::Display, iter, mem, path::PathBuf, rc::Rc, str::FromStr};
use unicode_width::UnicodeWidthStr;

// TODO: move to a theme config.
Expand Down Expand Up @@ -116,7 +117,7 @@ impl<'a> PresentationBuilder<'a> {
}

fn process_element(&mut self, element: MarkdownElement) -> Result<(), BuildError> {
let should_clear_last = !matches!(element, MarkdownElement::List(_) | MarkdownElement::Comment(_));
let should_clear_last = !matches!(element, MarkdownElement::List(_) | MarkdownElement::Comment { .. });
match element {
// This one is processed before everything else as it affects how the rest of the
// elements is rendered.
Expand All @@ -128,7 +129,7 @@ impl<'a> PresentationBuilder<'a> {
MarkdownElement::Code(code) => self.push_code(code),
MarkdownElement::Table(table) => self.push_table(table),
MarkdownElement::ThematicBreak => self.push_separator(),
MarkdownElement::Comment(comment) => self.process_comment(comment)?,
MarkdownElement::Comment { comment, source_position } => self.process_comment(comment, source_position)?,
MarkdownElement::BlockQuote(lines) => self.push_block_quote(lines),
MarkdownElement::Image(path) => self.push_image(path)?,
};
Expand Down Expand Up @@ -218,11 +219,14 @@ impl<'a> PresentationBuilder<'a> {
self.terminate_slide();
}

fn process_comment(&mut self, comment: String) -> Result<(), BuildError> {
fn process_comment(&mut self, comment: String, source_position: SourcePosition) -> Result<(), BuildError> {
if Self::should_ignore_comment(&comment) {
return Ok(());
}
let comment = comment.parse::<CommentCommand>()?;
let comment = match comment.parse::<CommentCommand>() {
Ok(comment) => comment,
Err(error) => return Err(BuildError::CommandParse { line: source_position.line, error }),
};
match comment {
CommentCommand::Pause => self.process_pause(),
CommentCommand::EndSlide => self.terminate_slide(),
Expand Down Expand Up @@ -712,8 +716,8 @@ pub enum BuildError {
#[error("need to enter layout column explicitly using `column` command")]
NotInsideColumn,

#[error(transparent)]
CommandParse(#[from] CommandParseError),
#[error("error parsing command at line {line}: {error}")]
CommandParse { line: usize, error: CommandParseError },
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
Expand All @@ -740,9 +744,18 @@ impl FromStr for CommentCommand {
}

#[derive(thiserror::Error, Debug)]
#[error("invalid command: {0}")]
pub struct CommandParseError(#[from] serde_yaml::Error);

impl Display for CommandParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let inner = self.0.to_string();
// Remove the trailing "at line X, ..." that comes from serde_yaml. This otherwise claims
// we're always in line 1 because the yaml is parsed in isolation out of the HTML comment.
let inner = inner.split(" at line").next().unwrap();
write!(f, "{inner}")
}
}

#[derive(Debug)]
struct RunCodeOperationInner {
handle: Option<ExecutionHandle>,
Expand Down Expand Up @@ -949,19 +962,19 @@ mod test {
}

fn build_pause() -> MarkdownElement {
MarkdownElement::Comment("pause".into())
MarkdownElement::Comment { comment: "pause".into(), source_position: Default::default() }
}

fn build_end_slide() -> MarkdownElement {
MarkdownElement::Comment("end_slide".into())
MarkdownElement::Comment { comment: "end_slide".into(), source_position: Default::default() }
}

fn build_column_layout(width: u8) -> MarkdownElement {
MarkdownElement::Comment(format!("column_layout: [{width}]"))
MarkdownElement::Comment { comment: format!("column_layout: [{width}]"), source_position: Default::default() }
}

fn build_column(column: u8) -> MarkdownElement {
MarkdownElement::Comment(format!("column: {column}"))
MarkdownElement::Comment { comment: format!("column: {column}"), source_position: Default::default() }
}

fn is_visible(operation: &RenderOperation) -> bool {
Expand Down Expand Up @@ -1016,7 +1029,7 @@ mod test {
let elements = vec![
MarkdownElement::FrontMatter("author: bob".to_string()),
MarkdownElement::Heading { text: Text::from("hello"), level: 1 },
MarkdownElement::Comment("end_slide".to_string()),
build_end_slide(),
MarkdownElement::Heading { text: Text::from("bye"), level: 1 },
];
let presentation = build_presentation(elements);
Expand All @@ -1035,7 +1048,7 @@ mod test {
let elements = vec![
MarkdownElement::FrontMatter("author: bob".to_string()),
MarkdownElement::Heading { text: Text::from("hello"), level: 1 },
MarkdownElement::Comment("end_slide".to_string()),
build_end_slide(),
MarkdownElement::Heading { text: Text::from("bye"), level: 1 },
];
let presentation = build_presentation(elements);
Expand Down Expand Up @@ -1094,26 +1107,28 @@ mod test {

#[test]
fn layout_without_init() {
let elements = vec![MarkdownElement::Comment("column: 0".into())];
let elements = vec![build_column(0)];
let result = try_build_presentation(elements);
assert!(result.is_err());
}

#[test]
fn already_in_column() {
let elements = vec![
MarkdownElement::Comment("column_layout: [1]".into()),
MarkdownElement::Comment("column: 0".into()),
MarkdownElement::Comment("column: 0".into()),
MarkdownElement::Comment { comment: "column_layout: [1]".into(), source_position: Default::default() },
MarkdownElement::Comment { comment: "column: 0".into(), source_position: Default::default() },
MarkdownElement::Comment { comment: "column: 0".into(), source_position: Default::default() },
];
let result = try_build_presentation(elements);
assert!(result.is_err());
}

#[test]
fn column_index_overflow() {
let elements =
vec![MarkdownElement::Comment("column_layout: [1]".into()), MarkdownElement::Comment("column: 1".into())];
let elements = vec![
MarkdownElement::Comment { comment: "column_layout: [1]".into(), source_position: Default::default() },
MarkdownElement::Comment { comment: "column: 1".into(), source_position: Default::default() },
];
let result = try_build_presentation(elements);
assert!(result.is_err());
}
Expand All @@ -1123,14 +1138,18 @@ mod test {
#[case::zero("column_layout: [0]")]
#[case::one_is_zero("column_layout: [1, 0]")]
fn invalid_layouts(#[case] definition: &str) {
let elements = vec![MarkdownElement::Comment(definition.into())];
let elements =
vec![MarkdownElement::Comment { comment: definition.into(), source_position: Default::default() }];
let result = try_build_presentation(elements);
assert!(result.is_err());
}

#[test]
fn operation_without_enter_column() {
let elements = vec![MarkdownElement::Comment("column_layout: [1]".into()), MarkdownElement::ThematicBreak];
let elements = vec![
MarkdownElement::Comment { comment: "column_layout: [1]".into(), source_position: Default::default() },
MarkdownElement::ThematicBreak,
];
let result = try_build_presentation(elements);
assert!(result.is_err());
}
Expand Down Expand Up @@ -1209,7 +1228,7 @@ mod test {
ListItem { depth: 1, contents: "one_one".into(), item_type: ListItemType::OrderedPeriod },
ListItem { depth: 1, contents: "one_two".into(), item_type: ListItemType::OrderedPeriod },
]),
MarkdownElement::Comment("pause".into()),
build_pause(),
MarkdownElement::List(vec![ListItem {
depth: 0,
contents: "two".into(),
Expand Down
7 changes: 6 additions & 1 deletion src/markdown/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ pub(crate) enum MarkdownElement {
ThematicBreak,

/// An HTML comment.
Comment(String),
Comment { comment: String, source_position: SourcePosition },

/// A quote.
BlockQuote(Vec<String>),
}

#[derive(Clone, Debug, Default)]
pub(crate) struct SourcePosition {
pub(crate) line: usize,
}

/// The components that make up a paragraph.
///
/// This does not map one-to-one with the commonmark spec and only handles text (including its
Expand Down
10 changes: 7 additions & 3 deletions src/markdown/parse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::elements::SourcePosition;
use crate::{
markdown::elements::{
Code, CodeFlags, CodeLanguage, ListItem, ListItemType, MarkdownElement, ParagraphElement, StyledText, Table,
Expand Down Expand Up @@ -97,7 +98,10 @@ impl<'a> MarkdownParser<'a> {
}
let block = &block[start_tag.len()..];
let block = &block[0..block.len() - end_tag.len()];
Ok(MarkdownElement::Comment(block.into()))
Ok(MarkdownElement::Comment {
comment: block.into(),
source_position: SourcePosition { line: sourcepos.start.line },
})
}

fn parse_block_quote(node: &'a AstNode<'a>) -> ParseResult<MarkdownElement> {
Expand Down Expand Up @@ -705,8 +709,8 @@ echo hi mom
<!-- foo -->
",
);
let MarkdownElement::Comment(text) = parsed else { panic!("not a comment: {parsed:?}") };
assert_eq!(text, " foo ");
let MarkdownElement::Comment { comment, .. } = parsed else { panic!("not a comment: {parsed:?}") };
assert_eq!(comment, " foo ");
}

#[test]
Expand Down
Loading