-
Notifications
You must be signed in to change notification settings - Fork 27
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
WiP: comrak sourcepos (#80) #116
Draft
SamWilsn
wants to merge
1
commit into
master
Choose a base branch
from
comrak-sourcepos
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,12 @@ mod known_lints; | |
pub mod markdown; | ||
pub mod preamble; | ||
|
||
use eipw_snippets::{Level, Message}; | ||
use eipw_snippets::{Level, Message, Snippet}; | ||
|
||
use comrak::nodes::AstNode; | ||
use comrak::nodes::{Ast, AstNode, LineColumn}; | ||
|
||
use crate::reporters::{self, Reporter}; | ||
use crate::{LevelExt, SnippetExt}; | ||
|
||
use educe::Educe; | ||
|
||
|
@@ -23,7 +24,6 @@ pub use self::known_lints::DefaultLint; | |
use snafu::Snafu; | ||
|
||
use std::cell::RefCell; | ||
use std::cmp::max; | ||
use std::collections::{HashMap, HashSet}; | ||
use std::fmt::Debug; | ||
use std::ops::Deref; | ||
|
@@ -90,29 +90,69 @@ where | |
&self.inner.preamble | ||
} | ||
|
||
/// XXX: comrak doesn't include a source field with its `AstNode`, so use | ||
/// this instead. Don't expose it publicly since it's really hacky. | ||
/// Yes, lines start at one. | ||
pub(crate) fn line(&self, mut line: usize) -> &'a str { | ||
assert_ne!(line, 0); | ||
line -= 1; | ||
self.inner.source.split('\n').nth(line).unwrap() | ||
pub fn line_index(&self, line: usize) -> usize { | ||
let src = self.inner.source; | ||
let (idx, _) = src | ||
.bytes() | ||
.enumerate() | ||
.filter(|(_, chr)| *chr == b'\n') | ||
.take(line - 1) | ||
.last() | ||
.expect("could not find ast line in source"); | ||
assert_eq!(src.as_bytes().get(idx), Some(&b'\n')); | ||
idx + 1 | ||
} | ||
|
||
/// XXX: comrak doesn't include a source field with its `AstNode`, so use | ||
/// this instead. Don't expose it publicly since it's really hacky. | ||
pub(crate) fn source_for_text(&self, line: usize, text: &str) -> String { | ||
assert_ne!(line, 0); | ||
fn line_column_index(&self, line_column: LineColumn) -> usize { | ||
let line_index = self.line_index(line_column.line); | ||
line_index + line_column.column - 1 | ||
} | ||
|
||
pub fn ast_source(&self, ast: &Ast) -> &'a str { | ||
let start = self.line_column_index(ast.sourcepos.start); | ||
let end = self.line_column_index(ast.sourcepos.end); | ||
&self.inner.source[start..=end] | ||
} | ||
|
||
let newlines = max(1, text.chars().filter(|c| *c == '\n').count()); | ||
pub fn ast_lines(&self, ast: &Ast) -> &'a str { | ||
let line_start_index = self.line_index(ast.sourcepos.start.line); | ||
let line_end_index = self.line_index(ast.sourcepos.end.line); | ||
let line_end_index = self.inner.source[line_end_index..] | ||
.find('\n') | ||
.map(|idx| idx + line_end_index) | ||
.unwrap_or_else(|| self.inner.source.len()); | ||
|
||
self.inner | ||
.source | ||
.split('\n') | ||
.skip(line - 1) | ||
.take(newlines) | ||
.collect::<Vec<_>>() | ||
.join("\n") | ||
&self.inner.source[line_start_index..line_end_index] | ||
} | ||
|
||
pub fn ast_snippet<'l, L: Into<Option<Level>>, O: Into<Option<&'l str>>>( | ||
&self, | ||
ast: &Ast, | ||
level: L, | ||
label: O, | ||
) -> Snippet<'l> | ||
where | ||
'a: 'l, | ||
{ | ||
let line_start_index = self.line_index(ast.sourcepos.start.line); | ||
let level = level.into().unwrap_or(self.annotation_level()); | ||
|
||
let start_index = self.line_column_index(ast.sourcepos.start) - line_start_index; | ||
let end_index = self.line_column_index(ast.sourcepos.end) - line_start_index; | ||
|
||
let source = self.ast_lines(ast); | ||
let annotation = level.span_utf8(source, start_index, end_index + 1); | ||
|
||
let annotation = match label.into() { | ||
None => annotation, | ||
Some(label) => annotation.label(label.as_ref()), | ||
}; | ||
|
||
Snippet::source(source) | ||
.fold(true) | ||
.line_start(ast.sourcepos.start.line) | ||
.origin_opt(self.origin()) | ||
.annotation(annotation) | ||
} | ||
|
||
pub fn body_source(&self) -> &'a str { | ||
|
@@ -192,3 +232,190 @@ impl Lint for Box<dyn Lint> { | |
lint.lint(slug, ctx) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use comrak::{ | ||
arena_tree::{Node, NodeEdge}, | ||
nodes::NodeValue, | ||
}; | ||
|
||
use super::*; | ||
|
||
fn get_context_ast_source(source: &str, pred: impl FnMut(&Ast) -> bool) -> String { | ||
let arena = comrak::Arena::new(); | ||
let context = Context { | ||
annotation_level: Level::Error, | ||
eips: &Default::default(), | ||
reporter: &crate::reporters::Null, | ||
inner: crate::process(&crate::reporters::Null, &arena, Some("eip-1234.md"), source) | ||
.unwrap() | ||
.unwrap(), | ||
}; | ||
|
||
let link = context | ||
.body() | ||
.traverse() | ||
.filter_map(|x| match x { | ||
NodeEdge::Start(Node { data, .. }) => Some(data.borrow().clone()), | ||
_ => None, | ||
}) | ||
.filter(pred) | ||
.next() | ||
.unwrap(); | ||
|
||
context.ast_source(&link).to_owned() | ||
} | ||
|
||
#[test] | ||
#[ignore] // https://github.com/kivikakk/comrak/issues/478 | ||
fn context_ast_source_autolink_email() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
[email protected] hello world | ||
"# | ||
.trim(); | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::Link(_))); | ||
assert_eq!(actual, "[email protected]"); | ||
} | ||
|
||
#[test] | ||
#[ignore] // https://github.com/kivikakk/comrak/issues/478 | ||
fn context_ast_source_link_start() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
<https://example.com> hello world | ||
"# | ||
.trim(); | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::Link(_))); | ||
assert_eq!(actual, "<https://example.com>"); | ||
} | ||
|
||
#[test] | ||
fn context_ast_source_inline_link_start() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
[hello](https://example.com) hello world | ||
"# | ||
.trim(); | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::Link(_))); | ||
assert_eq!(actual, "[hello](https://example.com)"); | ||
} | ||
|
||
#[test] | ||
fn context_ast_source_emphasis_unicode() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
*áemphá* hello world | ||
"# | ||
.trim(); | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::Emph)); | ||
assert_eq!(actual, "*áemphá*"); | ||
} | ||
|
||
#[test] | ||
fn context_ast_source_emphasis_start() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
*emphasis* hello world | ||
"# | ||
.trim(); | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::Emph)); | ||
assert_eq!(actual, "*emphasis*"); | ||
} | ||
|
||
#[test] | ||
#[ignore] // https://github.com/kivikakk/comrak/issues/478 | ||
fn context_ast_source_link_mid() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
hello <https://example.com> world | ||
"# | ||
.trim(); | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::Link(_))); | ||
assert_eq!(actual, "<https://example.com>"); | ||
} | ||
|
||
#[test] | ||
fn context_ast_source_inline_link_mid() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
hello [hello](https://example.com) world | ||
"# | ||
.trim(); | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::Link(_))); | ||
assert_eq!(actual, "[hello](https://example.com)"); | ||
} | ||
|
||
#[test] | ||
fn context_ast_source_emphasis_mid() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
hello *emphasis* world | ||
"# | ||
.trim(); | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::Emph)); | ||
assert_eq!(actual, "*emphasis*"); | ||
} | ||
|
||
#[test] | ||
fn context_ast_source_code_block() { | ||
let source = r#" | ||
--- | ||
eip: 1234 | ||
--- | ||
|
||
hello | ||
|
||
``` | ||
this is a | ||
multiline | ||
code block | ||
``` | ||
|
||
world | ||
"# | ||
.trim(); | ||
|
||
let expected = r#"``` | ||
this is a | ||
multiline | ||
code block | ||
```"#; | ||
|
||
let actual = get_context_ast_source(source, |d| matches!(d.value, NodeValue::CodeBlock(_))); | ||
assert_eq!(actual, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: if
end.column
is non-zero and less thanstart.column
, this line causes problems (and I've seen it happen.)