Skip to content

Commit 5b49b94

Browse files
committed
chore: use from-to instead of position+length for spans
Signed-off-by: azjezz <[email protected]>
1 parent 4999e5e commit 5b49b94

File tree

9 files changed

+97
-88
lines changed

9 files changed

+97
-88
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ara_reporting"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
edition = "2021"
55
description = "A Reporting library for for Ara Programming Langauge 📃"
66
readme = "README.md"

README.md

+20-18
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,27 @@ $b = match $a {
4343
};
4444
"#;
4545

46-
let map = SourceMap::new(vec![
47-
Source::new(SourceKind::Script, origin, code)
48-
]);
49-
50-
let report = Report::new()
51-
.with_issue(
52-
Issue::error("E0417", "`match` arms have incompatible types", origin, 6, 61)
53-
.with_annotation(
54-
Annotation::new(origin, 26, 1).with_message("this is found to be of type `{int}`"),
55-
)
56-
.with_annotation(
57-
Annotation::new(origin, 38, 1).with_message("this is found to be of type `{int}`"),
58-
)
59-
.with_annotation(
60-
Annotation::new(origin, 56, 8).with_message("expected `{int}`, found `{string}`"),
61-
)
62-
.with_note("for more information about this error, try `ara --explain E0417`"),
46+
let map = SourceMap::new(vec![Source::new(SourceKind::Script, origin, code)]);
47+
48+
let report = Report::new().with_issue(
49+
Issue::error(
50+
"E0417",
51+
"`match` arms have incompatible types",
52+
origin,
53+
6,
54+
67,
6355
)
64-
;
56+
.with_annotation(
57+
Annotation::new(origin, 26, 27).with_message("this is found to be of type `{int}`"),
58+
)
59+
.with_annotation(
60+
Annotation::new(origin, 38, 39).with_message("this is found to be of type `{int}`"),
61+
)
62+
.with_annotation(
63+
Annotation::new(origin, 56, 64).with_message("expected `{int}`, found `{string}`"),
64+
)
65+
.with_note("for more information about this error, try `ara --explain E0417`"),
66+
);
6567

6668
let builder = ReportBuilder::new(&map, report)
6769
.with_colors(ColorChoice::Always)

examples/cross_file.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ function add(int $a, int $b): int {
4242
"mismatched types expected `{int}`, found `{string}`",
4343
"src/main.ara",
4444
68,
45-
3,
45+
71,
4646
)
4747
.with_annotation(
48-
Annotation::new("src/main.ara", 61, 12)
48+
Annotation::new("src/main.ara", 61, 64)
4949
.with_message("arguments to this function are incorrect"),
5050
)
5151
.with_annotation(
52-
Annotation::new("vendor/some-vendor/some-lib/src/add.ara", 27, 24)
52+
Annotation::new("vendor/some-vendor/some-lib/src/add.ara", 27, 51)
5353
.with_message("function defined here"),
5454
)
5555
.with_note(

examples/example.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,40 @@ function main(): int|string {
3333

3434
let report = Report::new()
3535
.with_issue(
36-
Issue::error("E123", "some error here", DEFAULT_NAME, 35, 7)
37-
.with_annotation(Annotation::new(DEFAULT_NAME, 39, 1).with_message("an annotation"))
36+
Issue::error("E123", "some error here", DEFAULT_NAME, 35, 41)
37+
.with_annotation(
38+
Annotation::new(DEFAULT_NAME, 39, 40).with_message("an annotation"),
39+
)
3840
.with_help("this is a help")
3941
.with_note("this is a note"),
4042
)
4143
.with_issue(
42-
Issue::warning("W123", "some warning here", DEFAULT_NAME, 29, 158)
44+
Issue::warning("W123", "some warning here", DEFAULT_NAME, 29, 187)
4345
.with_annotation(
44-
Annotation::new(DEFAULT_NAME, 126, 1).with_message("an annotation"),
46+
Annotation::new(DEFAULT_NAME, 126, 127).with_message("an annotation"),
4547
)
4648
.with_help("this is a help")
4749
.with_note("this is a note"),
4850
)
4951
.with_issue(
50-
Issue::note("N123", "some note here", DEFAULT_NAME, 84, 80)
52+
Issue::note("N123", "some note here", DEFAULT_NAME, 84, 163)
53+
.with_annotation(
54+
Annotation::new(DEFAULT_NAME, 105, 112).with_message("an annotation"),
55+
)
5156
.with_annotation(
52-
Annotation::new(DEFAULT_NAME, 105, 7).with_message("an annotation"),
57+
Annotation::new(DEFAULT_NAME, 121, 128).with_message("another annotation"),
5358
)
5459
.with_annotation(
55-
Annotation::new(DEFAULT_NAME, 121, 7).with_message("another annotation"),
60+
Annotation::new(DEFAULT_NAME, 137, 147).with_message("and another"),
5661
)
57-
.with_annotation(Annotation::new(DEFAULT_NAME, 137, 20).with_message("and another"))
5862
.with_help("this is a help")
5963
.with_note("this is a note"),
6064
)
6165
.with_issue(
62-
Issue::help("H123", "some help here", DEFAULT_NAME, 137, 20)
63-
.with_annotation(Annotation::new(DEFAULT_NAME, 35, 7).with_message("an annotation"))
66+
Issue::help("H123", "some help here", DEFAULT_NAME, 137, 147)
67+
.with_annotation(
68+
Annotation::new(DEFAULT_NAME, 35, 42).with_message("an annotation"),
69+
)
6470
.with_help("this is a help")
6571
.with_note("this is a note"),
6672
)
@@ -70,18 +76,18 @@ function main(): int|string {
7076
"`match` arms have incompatible types",
7177
DEFAULT_NAME,
7278
84,
73-
80,
79+
163,
7480
)
7581
.with_annotation(
76-
Annotation::new(DEFAULT_NAME, 110, 1)
82+
Annotation::new(DEFAULT_NAME, 110, 111)
7783
.with_message("this is found to be of type `{int}`"),
7884
)
7985
.with_annotation(
80-
Annotation::new(DEFAULT_NAME, 126, 1)
86+
Annotation::new(DEFAULT_NAME, 126, 127)
8187
.with_message("this is found to be of type `{int}`"),
8288
)
8389
.with_annotation(
84-
Annotation::new(DEFAULT_NAME, 148, 8)
90+
Annotation::new(DEFAULT_NAME, 148, 156)
8591
.with_message("expected `{int}`, found `{string}`"),
8692
)
8793
.with_note("for more information about this error, try `ara --explain E0308`"),

examples/simple.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ara_reporting::Report;
88
use ara_source::source::Source;
99
use ara_source::source::SourceKind;
1010
use ara_source::SourceMap;
11+
1112
fn main() -> Result<(), Error> {
1213
let origin = "example.ara";
1314
let code = r#"
@@ -26,16 +27,16 @@ $b = match $a {
2627
"`match` arms have incompatible types",
2728
origin,
2829
6,
29-
61,
30+
67,
3031
)
3132
.with_annotation(
32-
Annotation::new(origin, 26, 1).with_message("this is found to be of type `{int}`"),
33+
Annotation::new(origin, 26, 27).with_message("this is found to be of type `{int}`"),
3334
)
3435
.with_annotation(
35-
Annotation::new(origin, 38, 1).with_message("this is found to be of type `{int}`"),
36+
Annotation::new(origin, 38, 39).with_message("this is found to be of type `{int}`"),
3637
)
3738
.with_annotation(
38-
Annotation::new(origin, 56, 8).with_message("expected `{int}`, found `{string}`"),
39+
Annotation::new(origin, 56, 64).with_message("expected `{int}`, found `{string}`"),
3940
)
4041
.with_note("for more information about this error, try `ara --explain E0417`"),
4142
);

src/annotation.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use serde::Serialize;
77
pub struct Annotation {
88
pub message: Option<String>,
99
pub origin: String,
10-
pub position: usize,
11-
pub length: usize,
10+
pub from: usize,
11+
pub to: usize,
1212
}
1313

1414
impl Annotation {
@@ -23,15 +23,15 @@ impl Annotation {
2323
///
2424
/// assert_eq!(annotation.message, None);
2525
/// assert_eq!(annotation.origin, "main.ara");
26-
/// assert_eq!(annotation.position, 0);
27-
/// assert_eq!(annotation.length, 5);
26+
/// assert_eq!(annotation.from, 0);
27+
/// assert_eq!(annotation.to, 5);
2828
/// ```
29-
pub fn new<O: Into<String>>(origin: O, position: usize, length: usize) -> Self {
29+
pub fn new<O: Into<String>>(origin: O, from: usize, to: usize) -> Self {
3030
Self {
3131
message: None,
3232
origin: origin.into(),
33-
position,
34-
length,
33+
from,
34+
to,
3535
}
3636
}
3737

src/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl ReportBuilder<'_> {
211211
.with_message(&issue.message)
212212
.with_labels(vec![Label::primary(
213213
*ids.get(&issue.origin).unwrap_or(&0),
214-
issue.position..(issue.position + issue.length),
214+
issue.from..issue.to,
215215
)
216216
.with_message(&issue.message)]);
217217

@@ -230,7 +230,7 @@ impl ReportBuilder<'_> {
230230
.map(|annotation| {
231231
let mut label = Label::secondary(
232232
*ids.get(&annotation.origin).unwrap_or(&0),
233-
(annotation.position)..(annotation.position + annotation.length),
233+
annotation.from..annotation.to,
234234
);
235235

236236
if let Some(message) = &annotation.message {

0 commit comments

Comments
 (0)