Skip to content

Commit

Permalink
fix/misalignment: use a relative offset instead of the source file span
Browse files Browse the repository at this point in the history
Closes #1, closes #5 .
  • Loading branch information
drahnr committed Jun 3, 2020
1 parent f7e446b commit 5ae45d5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,22 @@ Erronbeous bold uetchkp"#;
let docs = crate::documentation::Documentation::from((&path, stream));

let suggestions = dbg!(crate::checker::check(&docs, &config)).expect("Must not error");
let (path2, _literal_set) = docs.iter().next().expect("Must contain exactly one");
let (path2, literal_set) = docs.iter().next().expect("Must contain exactly one");
assert_eq!(&path, path2);

let literal_set_no1 = literal_set.first().expect("Must cotain at least one literalset");
assert_eq!(RAW, literal_set_no1.to_string().as_str());
assert_eq!(PLAIN, literal_set_no1.erase_markdown().as_str());

let mut it = suggestions.iter();

let mut expected = |word: &'static str| {
let suggestion = dbg!(it.next()).expect("Must contain one missspelled word");
let range = suggestion.span.start.column..suggestion.span.end.column;
assert_eq!(word, &suggestion.literal.as_ref().as_str()[range]);
let range: Range = suggestion.span.try_into().expect("Must be a single line");
let s = suggestion.literal.as_ref().as_str();
let x = crate::TrimmedLiteralRangePrint::from((suggestion.literal.as_ref(), range.clone()));
println!("Foxxy funkster: {:?}", x);
assert_eq!(word, &s[range]);
println!("Found word >> {} <<", word);
};

Expand Down
2 changes: 1 addition & 1 deletion src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<'a> PlainOverlay<'a> {
assert!(plain_range.start <= plain_range.end);
mapping.insert(plain_range, raw_range);
}
dbg!((plain, mapping))
(plain, mapping)
}

// @todo consider returning a Vec<PlainOverlay<'a>> to account for list items
Expand Down
23 changes: 23 additions & 0 deletions src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ impl Hash for Span {
}
}


impl Span {
/// Only works for literals spanning a single line.
pub fn relative_to<X: Into<Span>>(&self, scope: X) -> anyhow::Result<Range> {
let scope: Span = scope.into();
let scope : Range = scope.try_into()?;
let me: Range = self.try_into()?;
if scope.start > me.start {
return Err(anyhow::anyhow!("start of {:?} is not inside of {:?}", me, scope))
}
if scope.end < me.end {
return Err(anyhow::anyhow!("end of {:?} is not inside of {:?}", me, scope))
}
let offset = me.start - scope.start;
let length = me.end - me.start;
let range = Range {
start: offset,
end: offset + length,
};
Ok(range)
}
}

use std::convert::{From, TryInto};


Expand Down
12 changes: 9 additions & 3 deletions src/suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,20 @@ impl<'s> fmt::Display for Suggestion<'s> {
self.literal.len().saturating_sub(self.span.start.column)
};

use crate::literalset::Range;
use std::convert::TryInto;
let literal_span: Span = Span::from(self.literal.as_ref().literal.span());
let marker_range_relative: Range = self.span.relative_to(literal_span).expect("Must be ok");


// if the offset starts from 0, we still want to continue if the length
// of the marker is at least length 1
let offset = if self.literal.pre() <= self.span.start.column {
self.span.start.column - self.literal.pre()
let offset = if self.literal.pre() <= marker_range_relative.start {
marker_range_relative.start - self.literal.pre()
} else {
trace!("Reducing marker length!");
// reduce the marker size
marker_size -= self.span.start.column;
marker_size -= marker_range_relative.start;
marker_size -= self.literal.pre();
// @todo figure out why this is needed, currently this is a hack
// to make the following work:
Expand Down

0 comments on commit 5ae45d5

Please sign in to comment.