crates: vendor annotate-snippets crate

This merely adds the crate to our repository. Some cosmetic changes are
made to make it work in our repo and follow our conventions, such as
changing the name to `ruff_annotate_snippets`. We retain the original
license information. We do drop some things, such as benchmarks, but
keep tests and examples.
This commit is contained in:
Andrew Gallant 2024-12-20 12:55:16 -05:00 committed by Andrew Gallant
parent 4f3209a3ec
commit 9c27c57b5b
58 changed files with 6171 additions and 8 deletions

View file

@ -0,0 +1,37 @@
[package]
name = "ruff_annotate_snippets"
version = "0.1.0"
publish = false
authors = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
documentation = { workspace = true }
repository = { workspace = true }
license = "MIT OR Apache-2.0"
[lib]
[features]
default = []
testing-colors = []
[dependencies]
anstyle = { workspace = true }
memchr = { workspace = true }
unicode-width = { workspace = true }
[dev-dependencies]
ruff_annotate_snippets = { workspace = true, features = ["testing-colors"] }
anstream = { workspace = true }
serde = { workspace = true, features = ["derive"] }
snapbox = { workspace = true, features = ["diff", "term-svg", "cmd", "examples"] }
toml = { workspace = true }
tryfn = { workspace = true }
[[test]]
name = "fixtures"
harness = false
[lints]
workspace = true

View file

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -0,0 +1,19 @@
Copyright (c) Individual contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,15 @@
This is a fork of the [`annotate-snippets` crate]. The principle motivation for
this fork, at the time of writing, is [issue #167]. Specifically, we wanted to
upgrade our version of `annotate-snippets`, but do so _without_ changing our
diagnostic message format.
This copy of `annotate-snippets` is basically identical to upstream, but with
an extra `Level::None` variant that permits skipping over a new non-optional
header emitted by `annotate-snippets`.
More generally, it seems plausible that we may want to tweak other aspects of
the output format in the future, so it might make sense to stick with our own
copy so that we can be masters of our own destiny.
[issue #167]: https://github.com/rust-lang/annotate-snippets-rs/issues/167
[`annotate-snippets` crate]: https://github.com/rust-lang/annotate-snippets-rs

View file

@ -0,0 +1,23 @@
use ruff_annotate_snippets::{Level, Renderer, Snippet};
fn main() {
let source = r#" annotations: vec![SourceAnnotation {
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
,
range: <22, 25>,"#;
let message = Level::Error.title("expected type, found `22`").snippet(
Snippet::source(source)
.line_start(26)
.origin("examples/footer.rs")
.fold(true)
.annotation(
Level::Error
.span(193..195)
.label("expected struct `annotate_snippets::snippet::Slice`, found reference"),
)
.annotation(Level::Info.span(34..50).label("while parsing this struct")),
);
let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(message));
}

View file

@ -0,0 +1,46 @@
<svg width="860px" height="218px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan><tspan>: </tspan><tspan class="bold">expected type, found `22`</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> examples/footer.rs:29:25</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">26 |</tspan><tspan> annotations: vec![SourceAnnotation {</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-blue bold">----------------</tspan><tspan> </tspan><tspan class="fg-bright-blue bold">info: while parsing this struct</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan class="fg-bright-blue bold">27 |</tspan><tspan> label: "expected struct `annotate_snippets::snippet::Slice`, found reference"</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-bright-blue bold">28 |</tspan><tspan> ,</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan class="fg-bright-blue bold">29 |</tspan><tspan> range: &lt;22, 25&gt;,</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">expected struct `annotate_snippets::snippet::Slice`, found reference</tspan>
</tspan>
<tspan x="10px" y="190px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="208px">
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

@ -0,0 +1,22 @@
use ruff_annotate_snippets::{Level, Renderer, Snippet};
fn main() {
let message =
Level::Error
.title("mismatched types")
.id("E0308")
.snippet(
Snippet::source(" slices: vec![\"A\",")
.line_start(13)
.origin("src/multislice.rs")
.annotation(Level::Error.span(21..24).label(
"expected struct `annotate_snippets::snippet::Slice`, found reference",
)),
)
.footer(Level::Note.title(
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
));
let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(message));
}

View file

@ -0,0 +1,43 @@
<svg width="844px" height="182px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-green { fill: #55FF55 }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0308]</tspan><tspan>: </tspan><tspan class="bold">mismatched types</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> src/multislice.rs:13:22</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">13 |</tspan><tspan> slices: vec!["A",</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">expected struct `annotate_snippets::snippet::Slice`, found reference</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan> </tspan><tspan class="fg-bright-blue bold">= </tspan><tspan class="fg-bright-green bold">note</tspan><tspan>: expected type: `snippet::Annotation`</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan> found type: `__&amp;__snippet::Annotation`</tspan>
</tspan>
<tspan x="10px" y="172px">
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -0,0 +1,44 @@
use ruff_annotate_snippets::{Level, Renderer, Snippet};
fn main() {
let source = r#") -> Option<String> {
for ann in annotations {
match (ann.range.0, ann.range.1) {
(None, None) => continue,
(Some(start), Some(end)) if start > end_index => continue,
(Some(start), Some(end)) if start >= start_index => {
let label = if let Some(ref label) = ann.label {
format!(" {}", label)
} else {
String::from("")
};
return Some(format!(
"{}{}{}",
" ".repeat(start - start_index),
"^".repeat(end - start),
label
));
}
_ => continue,
}
}"#;
let message = Level::Error.title("mismatched types").id("E0308").snippet(
Snippet::source(source)
.line_start(51)
.origin("src/format.rs")
.annotation(
Level::Warning
.span(5..19)
.label("expected `Option<String>` because of return type"),
)
.annotation(
Level::Error
.span(26..724)
.label("expected enum `std::option::Option`"),
),
);
let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(message));
}

View file

@ -0,0 +1,83 @@
<svg width="740px" height="542px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.fg-yellow { fill: #AA5500 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0308]</tspan><tspan>: </tspan><tspan class="bold">mismatched types</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> src/format.rs:51:6</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">51 |</tspan><tspan> ) -&gt; Option&lt;String&gt; {</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-yellow bold">--------------</tspan><tspan> </tspan><tspan class="fg-yellow bold">expected `Option&lt;String&gt;` because of return type</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan class="fg-bright-blue bold">52 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">/</tspan><tspan> for ann in annotations {</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-bright-blue bold">53 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> match (ann.range.0, ann.range.1) {</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan class="fg-bright-blue bold">54 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> (None, None) =&gt; continue,</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan class="fg-bright-blue bold">55 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> (Some(start), Some(end)) if start &gt; end_index =&gt; continue,</tspan>
</tspan>
<tspan x="10px" y="190px"><tspan class="fg-bright-blue bold">56 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> (Some(start), Some(end)) if start &gt;= start_index =&gt; {</tspan>
</tspan>
<tspan x="10px" y="208px"><tspan class="fg-bright-blue bold">57 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> let label = if let Some(ref label) = ann.label {</tspan>
</tspan>
<tspan x="10px" y="226px"><tspan class="fg-bright-blue bold">58 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> format!(" {}", label)</tspan>
</tspan>
<tspan x="10px" y="244px"><tspan class="fg-bright-blue bold">59 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> } else {</tspan>
</tspan>
<tspan x="10px" y="262px"><tspan class="fg-bright-blue bold">60 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> String::from("")</tspan>
</tspan>
<tspan x="10px" y="280px"><tspan class="fg-bright-blue bold">61 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> };</tspan>
</tspan>
<tspan x="10px" y="298px"><tspan class="fg-bright-blue bold">62 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan>
</tspan>
<tspan x="10px" y="316px"><tspan class="fg-bright-blue bold">63 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> return Some(format!(</tspan>
</tspan>
<tspan x="10px" y="334px"><tspan class="fg-bright-blue bold">64 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> "{}{}{}",</tspan>
</tspan>
<tspan x="10px" y="352px"><tspan class="fg-bright-blue bold">65 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> " ".repeat(start - start_index),</tspan>
</tspan>
<tspan x="10px" y="370px"><tspan class="fg-bright-blue bold">66 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> "^".repeat(end - start),</tspan>
</tspan>
<tspan x="10px" y="388px"><tspan class="fg-bright-blue bold">67 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> label</tspan>
</tspan>
<tspan x="10px" y="406px"><tspan class="fg-bright-blue bold">68 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> ));</tspan>
</tspan>
<tspan x="10px" y="424px"><tspan class="fg-bright-blue bold">69 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> }</tspan>
</tspan>
<tspan x="10px" y="442px"><tspan class="fg-bright-blue bold">70 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> _ =&gt; continue,</tspan>
</tspan>
<tspan x="10px" y="460px"><tspan class="fg-bright-blue bold">71 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> }</tspan>
</tspan>
<tspan x="10px" y="478px"><tspan class="fg-bright-blue bold">72 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> }</tspan>
</tspan>
<tspan x="10px" y="496px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|____^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">expected enum `std::option::Option`</tspan>
</tspan>
<tspan x="10px" y="514px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="532px">
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 5.8 KiB

View file

@ -0,0 +1,19 @@
use ruff_annotate_snippets::{Level, Renderer, Snippet};
fn main() {
let message = Level::Error
.title("mismatched types")
.snippet(
Snippet::source("Foo")
.line_start(51)
.origin("src/format.rs"),
)
.snippet(
Snippet::source("Faa")
.line_start(129)
.origin("src/display.rs"),
);
let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(message));
}

View file

@ -0,0 +1,44 @@
<svg width="740px" height="200px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan><tspan>: </tspan><tspan class="bold">mismatched types</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> src/format.rs</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold"> 51 |</tspan><tspan> Foo</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">:::</tspan><tspan> src/display.rs</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan class="fg-bright-blue bold">129 |</tspan><tspan> Faa</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="190px">
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -0,0 +1,69 @@
//! A library for formatting of text or programming code snippets.
//!
//! It's primary purpose is to build an ASCII-graphical representation of the snippet
//! with annotations.
//!
//! # Example
//!
//! ```rust
#![doc = include_str!("../examples/expected_type.rs")]
//! ```
//!
#![doc = include_str!("../examples/expected_type.svg")]
//!
//! The crate uses a three stage process with two conversions between states:
//!
//! ```text
//! Message --> Renderer --> impl Display
//! ```
//!
//! The input type - [Message] is a structure designed
//! to align with likely output from any parser whose code snippet is to be
//! annotated.
//!
//! The middle structure - [Renderer] is a structure designed
//! to convert a snippet into an internal structure that is designed to store
//! the snippet data in a way that is easy to format.
//! [Renderer] also handles the user-configurable formatting
//! options, such as color, or margins.
//!
//! Finally, `impl Display` into a final `String` output.
//!
//! # features
//! - `testing-colors` - Makes [Renderer::styled] colors OS independent, which
//! allows for easier testing when testing colored output. It should be added as
//! a feature in `[dev-dependencies]`, which can be done with the following command:
//! ```text
//! cargo add annotate-snippets --dev --feature testing-colors
//! ```
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]
#![warn(missing_debug_implementations)]
// Since this is a vendored copy of `annotate-snippets`, we squash Clippy
// warnings from upstream in order to the reduce the diff. If our copy drifts
// far from upstream such that patches become impractical to apply in both
// places, then we can get rid of these suppressions and fix the lints.
#![allow(
clippy::return_self_not_must_use,
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::explicit_iter_loop,
clippy::unused_self,
clippy::unnecessary_wraps,
clippy::range_plus_one,
clippy::redundant_closure_for_method_calls,
clippy::struct_field_names,
clippy::cloned_instead_of_copied,
clippy::cast_sign_loss,
clippy::needless_as_bytes,
clippy::unnecessary_map_or
)]
pub mod renderer;
mod snippet;
#[doc(inline)]
pub use renderer::Renderer;
pub use snippet::*;

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,119 @@
use std::cmp::{max, min};
const ELLIPSIS_PASSING: usize = 6;
const LONG_WHITESPACE: usize = 20;
const LONG_WHITESPACE_PADDING: usize = 4;
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Margin {
/// The available whitespace in the left that can be consumed when centering.
whitespace_left: usize,
/// The column of the beginning of left-most span.
span_left: usize,
/// The column of the end of right-most span.
span_right: usize,
/// The beginning of the line to be displayed.
computed_left: usize,
/// The end of the line to be displayed.
computed_right: usize,
/// The current width of the terminal. 140 by default and in tests.
term_width: usize,
/// The end column of a span label, including the span. Doesn't account for labels not in the
/// same line as the span.
label_right: usize,
}
impl Margin {
pub(crate) fn new(
whitespace_left: usize,
span_left: usize,
span_right: usize,
label_right: usize,
term_width: usize,
max_line_len: usize,
) -> Self {
// The 6 is padding to give a bit of room for `...` when displaying:
// ```
// error: message
// --> file.rs:16:58
// |
// 16 | ... fn foo(self) -> Self::Bar {
// | ^^^^^^^^^
// ```
let mut m = Margin {
whitespace_left: whitespace_left.saturating_sub(ELLIPSIS_PASSING),
span_left: span_left.saturating_sub(ELLIPSIS_PASSING),
span_right: span_right + ELLIPSIS_PASSING,
computed_left: 0,
computed_right: 0,
term_width,
label_right: label_right + ELLIPSIS_PASSING,
};
m.compute(max_line_len);
m
}
pub(crate) fn was_cut_left(&self) -> bool {
self.computed_left > 0
}
pub(crate) fn was_cut_right(&self, line_len: usize) -> bool {
let right =
if self.computed_right == self.span_right || self.computed_right == self.label_right {
// Account for the "..." padding given above. Otherwise we end up with code lines that
// do fit but end in "..." as if they were trimmed.
self.computed_right - ELLIPSIS_PASSING
} else {
self.computed_right
};
right < line_len && self.computed_left + self.term_width < line_len
}
fn compute(&mut self, max_line_len: usize) {
// When there's a lot of whitespace (>20), we want to trim it as it is useless.
self.computed_left = if self.whitespace_left > LONG_WHITESPACE {
self.whitespace_left - (LONG_WHITESPACE - LONG_WHITESPACE_PADDING) // We want some padding.
} else {
0
};
// We want to show as much as possible, max_line_len is the right-most boundary for the
// relevant code.
self.computed_right = max(max_line_len, self.computed_left);
if self.computed_right - self.computed_left > self.term_width {
// Trimming only whitespace isn't enough, let's get craftier.
if self.label_right - self.whitespace_left <= self.term_width {
// Attempt to fit the code window only trimming whitespace.
self.computed_left = self.whitespace_left;
self.computed_right = self.computed_left + self.term_width;
} else if self.label_right - self.span_left <= self.term_width {
// Attempt to fit the code window considering only the spans and labels.
let padding_left = (self.term_width - (self.label_right - self.span_left)) / 2;
self.computed_left = self.span_left.saturating_sub(padding_left);
self.computed_right = self.computed_left + self.term_width;
} else if self.span_right - self.span_left <= self.term_width {
// Attempt to fit the code window considering the spans and labels plus padding.
let padding_left = (self.term_width - (self.span_right - self.span_left)) / 5 * 2;
self.computed_left = self.span_left.saturating_sub(padding_left);
self.computed_right = self.computed_left + self.term_width;
} else {
// Mostly give up but still don't show the full line.
self.computed_left = self.span_left;
self.computed_right = self.span_right;
}
}
}
pub(crate) fn left(&self, line_len: usize) -> usize {
min(self.computed_left, line_len)
}
pub(crate) fn right(&self, line_len: usize) -> usize {
if line_len.saturating_sub(self.computed_left) <= self.term_width {
line_len
} else {
min(line_len, self.computed_right)
}
}
}

View file

@ -0,0 +1,163 @@
//! The renderer for [`Message`]s
//!
//! # Example
//! ```
//! use ruff_annotate_snippets::{Renderer, Snippet, Level};
//! let snippet = Level::Error.title("mismatched types")
//! .snippet(Snippet::source("Foo").line_start(51).origin("src/format.rs"))
//! .snippet(Snippet::source("Faa").line_start(129).origin("src/display.rs"));
//!
//! let renderer = Renderer::styled();
//! println!("{}", renderer.render(snippet));
mod display_list;
mod margin;
mod styled_buffer;
pub(crate) mod stylesheet;
use crate::snippet::Message;
pub use anstyle::*;
use display_list::DisplayList;
use margin::Margin;
use std::fmt::Display;
use stylesheet::Stylesheet;
pub const DEFAULT_TERM_WIDTH: usize = 140;
/// A renderer for [`Message`]s
#[derive(Clone, Debug)]
pub struct Renderer {
anonymized_line_numbers: bool,
term_width: usize,
stylesheet: Stylesheet,
}
impl Renderer {
/// No terminal styling
pub const fn plain() -> Self {
Self {
anonymized_line_numbers: false,
term_width: DEFAULT_TERM_WIDTH,
stylesheet: Stylesheet::plain(),
}
}
/// Default terminal styling
///
/// # Note
/// When testing styled terminal output, see the [`testing-colors` feature](crate#features)
pub const fn styled() -> Self {
const USE_WINDOWS_COLORS: bool = cfg!(windows) && !cfg!(feature = "testing-colors");
const BRIGHT_BLUE: Style = if USE_WINDOWS_COLORS {
AnsiColor::BrightCyan.on_default()
} else {
AnsiColor::BrightBlue.on_default()
};
Self {
stylesheet: Stylesheet {
error: AnsiColor::BrightRed.on_default().effects(Effects::BOLD),
warning: if USE_WINDOWS_COLORS {
AnsiColor::BrightYellow.on_default()
} else {
AnsiColor::Yellow.on_default()
}
.effects(Effects::BOLD),
info: BRIGHT_BLUE.effects(Effects::BOLD),
note: AnsiColor::BrightGreen.on_default().effects(Effects::BOLD),
help: AnsiColor::BrightCyan.on_default().effects(Effects::BOLD),
line_no: BRIGHT_BLUE.effects(Effects::BOLD),
emphasis: if USE_WINDOWS_COLORS {
AnsiColor::BrightWhite.on_default()
} else {
Style::new()
}
.effects(Effects::BOLD),
none: Style::new(),
},
..Self::plain()
}
}
/// Anonymize line numbers
///
/// This enables (or disables) line number anonymization. When enabled, line numbers are replaced
/// with `LL`.
///
/// # Example
///
/// ```text
/// --> $DIR/whitespace-trimming.rs:4:193
/// |
/// LL | ... let _: () = 42;
/// | ^^ expected (), found integer
/// |
/// ```
pub const fn anonymized_line_numbers(mut self, anonymized_line_numbers: bool) -> Self {
self.anonymized_line_numbers = anonymized_line_numbers;
self
}
// Set the terminal width
pub const fn term_width(mut self, term_width: usize) -> Self {
self.term_width = term_width;
self
}
/// Set the output style for `error`
pub const fn error(mut self, style: Style) -> Self {
self.stylesheet.error = style;
self
}
/// Set the output style for `warning`
pub const fn warning(mut self, style: Style) -> Self {
self.stylesheet.warning = style;
self
}
/// Set the output style for `info`
pub const fn info(mut self, style: Style) -> Self {
self.stylesheet.info = style;
self
}
/// Set the output style for `note`
pub const fn note(mut self, style: Style) -> Self {
self.stylesheet.note = style;
self
}
/// Set the output style for `help`
pub const fn help(mut self, style: Style) -> Self {
self.stylesheet.help = style;
self
}
/// Set the output style for line numbers
pub const fn line_no(mut self, style: Style) -> Self {
self.stylesheet.line_no = style;
self
}
/// Set the output style for emphasis
pub const fn emphasis(mut self, style: Style) -> Self {
self.stylesheet.emphasis = style;
self
}
/// Set the output style for none
pub const fn none(mut self, style: Style) -> Self {
self.stylesheet.none = style;
self
}
/// Render a snippet into a `Display`able object
pub fn render<'a>(&'a self, msg: Message<'a>) -> impl Display + 'a {
DisplayList::new(
msg,
&self.stylesheet,
self.anonymized_line_numbers,
self.term_width,
)
}
}

View file

@ -0,0 +1,97 @@
//! Adapted from [styled_buffer]
//!
//! [styled_buffer]: https://github.com/rust-lang/rust/blob/894f7a4ba6554d3797404bbf550d9919df060b97/compiler/rustc_errors/src/styled_buffer.rs
use crate::renderer::stylesheet::Stylesheet;
use anstyle::Style;
use std::fmt;
use std::fmt::Write;
#[derive(Debug)]
pub(crate) struct StyledBuffer {
lines: Vec<Vec<StyledChar>>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct StyledChar {
ch: char,
style: Style,
}
impl StyledChar {
pub(crate) const SPACE: Self = StyledChar::new(' ', Style::new());
pub(crate) const fn new(ch: char, style: Style) -> StyledChar {
StyledChar { ch, style }
}
}
impl StyledBuffer {
pub(crate) fn new() -> StyledBuffer {
StyledBuffer { lines: vec![] }
}
fn ensure_lines(&mut self, line: usize) {
if line >= self.lines.len() {
self.lines.resize(line + 1, Vec::new());
}
}
pub(crate) fn render(&self, stylesheet: &Stylesheet) -> Result<String, fmt::Error> {
let mut str = String::new();
for (i, line) in self.lines.iter().enumerate() {
let mut current_style = stylesheet.none;
for ch in line {
if ch.style != current_style {
if !line.is_empty() {
write!(str, "{}", current_style.render_reset())?;
}
current_style = ch.style;
write!(str, "{}", current_style.render())?;
}
write!(str, "{}", ch.ch)?;
}
write!(str, "{}", current_style.render_reset())?;
if i != self.lines.len() - 1 {
writeln!(str)?;
}
}
Ok(str)
}
/// Sets `chr` with `style` for given `line`, `col`.
/// If `line` does not exist in our buffer, adds empty lines up to the given
/// and fills the last line with unstyled whitespace.
pub(crate) fn putc(&mut self, line: usize, col: usize, chr: char, style: Style) {
self.ensure_lines(line);
if col >= self.lines[line].len() {
self.lines[line].resize(col + 1, StyledChar::SPACE);
}
self.lines[line][col] = StyledChar::new(chr, style);
}
/// Sets `string` with `style` for given `line`, starting from `col`.
/// If `line` does not exist in our buffer, adds empty lines up to the given
/// and fills the last line with unstyled whitespace.
pub(crate) fn puts(&mut self, line: usize, col: usize, string: &str, style: Style) {
let mut n = col;
for c in string.chars() {
self.putc(line, n, c, style);
n += 1;
}
}
/// For given `line` inserts `string` with `style` after old content of that line,
/// adding lines if needed
pub(crate) fn append(&mut self, line: usize, string: &str, style: Style) {
if line >= self.lines.len() {
self.puts(line, 0, string, style);
} else {
let col = self.lines[line].len();
self.puts(line, col, string, style);
}
}
pub(crate) fn num_lines(&self) -> usize {
self.lines.len()
}
}

View file

@ -0,0 +1,68 @@
use anstyle::Style;
#[derive(Clone, Copy, Debug)]
pub(crate) struct Stylesheet {
pub(crate) error: Style,
pub(crate) warning: Style,
pub(crate) info: Style,
pub(crate) note: Style,
pub(crate) help: Style,
pub(crate) line_no: Style,
pub(crate) emphasis: Style,
pub(crate) none: Style,
}
impl Default for Stylesheet {
fn default() -> Self {
Self::plain()
}
}
impl Stylesheet {
pub(crate) const fn plain() -> Self {
Self {
error: Style::new(),
warning: Style::new(),
info: Style::new(),
note: Style::new(),
help: Style::new(),
line_no: Style::new(),
emphasis: Style::new(),
none: Style::new(),
}
}
}
impl Stylesheet {
pub(crate) fn error(&self) -> &Style {
&self.error
}
pub(crate) fn warning(&self) -> &Style {
&self.warning
}
pub(crate) fn info(&self) -> &Style {
&self.info
}
pub(crate) fn note(&self) -> &Style {
&self.note
}
pub(crate) fn help(&self) -> &Style {
&self.help
}
pub(crate) fn line_no(&self) -> &Style {
&self.line_no
}
pub(crate) fn emphasis(&self) -> &Style {
&self.emphasis
}
pub(crate) fn none(&self) -> &Style {
&self.none
}
}

View file

@ -0,0 +1,157 @@
//! Structures used as an input for the library.
//!
//! Example:
//!
//! ```
//! use ruff_annotate_snippets::*;
//!
//! Level::Error.title("mismatched types")
//! .snippet(Snippet::source("Foo").line_start(51).origin("src/format.rs"))
//! .snippet(Snippet::source("Faa").line_start(129).origin("src/display.rs"));
//! ```
use std::ops::Range;
/// Primary structure provided for formatting
///
/// See [`Level::title`] to create a [`Message`]
#[derive(Debug)]
pub struct Message<'a> {
pub(crate) level: Level,
pub(crate) id: Option<&'a str>,
pub(crate) title: &'a str,
pub(crate) snippets: Vec<Snippet<'a>>,
pub(crate) footer: Vec<Message<'a>>,
}
impl<'a> Message<'a> {
pub fn id(mut self, id: &'a str) -> Self {
self.id = Some(id);
self
}
pub fn snippet(mut self, slice: Snippet<'a>) -> Self {
self.snippets.push(slice);
self
}
pub fn snippets(mut self, slice: impl IntoIterator<Item = Snippet<'a>>) -> Self {
self.snippets.extend(slice);
self
}
pub fn footer(mut self, footer: Message<'a>) -> Self {
self.footer.push(footer);
self
}
pub fn footers(mut self, footer: impl IntoIterator<Item = Message<'a>>) -> Self {
self.footer.extend(footer);
self
}
}
/// Structure containing the slice of text to be annotated and
/// basic information about the location of the slice.
///
/// One `Snippet` is meant to represent a single, continuous,
/// slice of source code that you want to annotate.
#[derive(Debug)]
pub struct Snippet<'a> {
pub(crate) origin: Option<&'a str>,
pub(crate) line_start: usize,
pub(crate) source: &'a str,
pub(crate) annotations: Vec<Annotation<'a>>,
pub(crate) fold: bool,
}
impl<'a> Snippet<'a> {
pub fn source(source: &'a str) -> Self {
Self {
origin: None,
line_start: 1,
source,
annotations: vec![],
fold: false,
}
}
pub fn line_start(mut self, line_start: usize) -> Self {
self.line_start = line_start;
self
}
pub fn origin(mut self, origin: &'a str) -> Self {
self.origin = Some(origin);
self
}
pub fn annotation(mut self, annotation: Annotation<'a>) -> Self {
self.annotations.push(annotation);
self
}
pub fn annotations(mut self, annotation: impl IntoIterator<Item = Annotation<'a>>) -> Self {
self.annotations.extend(annotation);
self
}
/// Hide lines without [`Annotation`]s
pub fn fold(mut self, fold: bool) -> Self {
self.fold = fold;
self
}
}
/// An annotation for a [`Snippet`].
///
/// See [`Level::span`] to create a [`Annotation`]
#[derive(Debug)]
pub struct Annotation<'a> {
/// The byte range of the annotation in the `source` string
pub(crate) range: Range<usize>,
pub(crate) label: Option<&'a str>,
pub(crate) level: Level,
}
impl<'a> Annotation<'a> {
pub fn label(mut self, label: &'a str) -> Self {
self.label = Some(label);
self
}
}
/// Types of annotations.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Level {
/// Error annotations are displayed using red color and "^" character.
Error,
/// Warning annotations are displayed using blue color and "-" character.
Warning,
Info,
Note,
Help,
}
impl Level {
pub fn title(self, title: &str) -> Message<'_> {
Message {
level: self,
id: None,
title,
snippets: vec![],
footer: vec![],
}
}
/// Create a [`Annotation`] with the given span for a [`Snippet`]
pub fn span<'a>(self, span: Range<usize>) -> Annotation<'a> {
Annotation {
range: span,
label: None,
level: self,
}
}
}

View file

@ -0,0 +1,37 @@
#[test]
fn expected_type() {
let target = "expected_type";
let expected = snapbox::file!["../examples/expected_type.svg": TermSvg];
assert_example(target, expected);
}
#[test]
fn footer() {
let target = "footer";
let expected = snapbox::file!["../examples/footer.svg": TermSvg];
assert_example(target, expected);
}
#[test]
fn format() {
let target = "format";
let expected = snapbox::file!["../examples/format.svg": TermSvg];
assert_example(target, expected);
}
#[test]
fn multislice() {
let target = "multislice";
let expected = snapbox::file!["../examples/multislice.svg": TermSvg];
assert_example(target, expected);
}
#[track_caller]
fn assert_example(target: &str, expected: snapbox::Data) {
let bin_path = snapbox::cmd::compile_example(target, ["--features=testing-colors"]).unwrap();
snapbox::cmd::Command::new(bin_path)
.env("CLICOLOR_FORCE", "1")
.assert()
.success()
.stdout_eq(expected.raw());
}

View file

@ -0,0 +1,36 @@
<svg width="740px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan><tspan>: </tspan><tspan class="bold">expected `.`, `=`</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:1:5</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">1 |</tspan><tspan> asdf</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -0,0 +1,15 @@
[message]
level = "Error"
title = "expected `.`, `=`"
[[message.snippets]]
source = "asdf"
line_start = 1
origin = "Cargo.toml"
[[message.snippets.annotations]]
label = ""
level = "Error"
range = [4, 4]
[renderer]
color = true

View file

@ -0,0 +1,36 @@
<svg width="740px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan><tspan>: </tspan><tspan class="bold">expected `.`, `=`</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:1:3</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">1 |</tspan><tspan> asf</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">'d' belongs here</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -0,0 +1,15 @@
[message]
level = "Error"
title = "expected `.`, `=`"
[[message.snippets]]
source = "asf"
line_start = 1
origin = "Cargo.toml"
[[message.snippets.annotations]]
label = "'d' belongs here"
level = "Error"
range = [2, 2]
[renderer]
color = true

View file

@ -0,0 +1,42 @@
<svg width="740px" height="182px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0027]</tspan><tspan>: </tspan><tspan class="bold">pattern does not mention fields `lineno`, `content`</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> src/display_list.rs:139:32</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">139 |</tspan><tspan> if let DisplayLine::Source {</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">________________________________^</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan class="fg-bright-blue bold">140 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> ref mut inline_marks,</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-bright-blue bold">141 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> } = body[body_idx]</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|_________________________^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">missing fields `lineno`, `content`</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 2.1 KiB

View file

@ -0,0 +1,21 @@
[message]
level = "Error"
id = "E0027"
title = "pattern does not mention fields `lineno`, `content`"
[[message.snippets]]
source = """
if let DisplayLine::Source {
ref mut inline_marks,
} = body[body_idx]
"""
line_start = 139
origin = "src/display_list.rs"
fold = false
[[message.snippets.annotations]]
label = "missing fields `lineno`, `content`"
level = "Error"
range = [31, 128]
[renderer]
color = true

View file

@ -0,0 +1,40 @@
<svg width="740px" height="164px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E####]</tspan><tspan>: </tspan><tspan class="bold">spacing error found</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> foo.txt:26:12</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">26 |</tspan><tspan> This is an example</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^^^^^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">this should not be on separate lines</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan class="fg-bright-blue bold">27 |</tspan><tspan> of an edge case of an annotation overflowing</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-bright-blue bold">28 |</tspan><tspan> to exactly one character on next line.</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.7 KiB

View file

@ -0,0 +1,21 @@
[message]
level = "Error"
id = "E####"
title = "spacing error found"
[[message.snippets]]
source = """
This is an example
of an edge case of an annotation overflowing
to exactly one character on next line.
"""
line_start = 26
origin = "foo.txt"
fold = false
[[message.snippets.annotations]]
label = "this should not be on separate lines"
level = "Error"
range = [11, 19]
[renderer]
color = true

View file

@ -0,0 +1,36 @@
<svg width="740px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan><tspan>: </tspan><tspan class="bold">expected `.`, `=`</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:1:5</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">1 |</tspan><tspan> asdf</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -0,0 +1,15 @@
[message]
level = "Error"
title = "expected `.`, `=`"
[[message.snippets]]
source = "asdf"
line_start = 1
origin = "Cargo.toml"
[[message.snippets.annotations]]
label = ""
level = "Error"
range = [4, 5]
[renderer]
color = true

View file

@ -0,0 +1,36 @@
<svg width="1356px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan><tspan>: </tspan><tspan class="bold">invalid character ` ` in package name: `haha this isn't a valid name 🐛`, characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> &lt;file&gt;:7:1</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">7 |</tspan><tspan> "haha this isn't a valid name 🐛" = { package = "libc", version = "0.1" }</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -0,0 +1,18 @@
[message]
title = "invalid character ` ` in package name: `haha this isn't a valid name 🐛`, characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)"
level = "Error"
[[message.snippets]]
source = """
"haha this isn't a valid name 🐛" = { package = "libc", version = "0.1" }
"""
line_start = 7
origin = "<file>"
[[message.snippets.annotations]]
label = ""
level = "Error"
range = [0, 35]
[renderer]
color = true

View file

@ -0,0 +1,49 @@
<svg width="869px" height="236px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.fg-yellow { fill: #AA5500 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0308]</tspan><tspan>: </tspan><tspan class="bold">mismatched types</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> src/format.rs:51:6</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">51 |</tspan><tspan> ) -&gt; Option&lt;String&gt; {</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-yellow bold">--------------</tspan><tspan> </tspan><tspan class="fg-yellow bold">expected `std::option::Option&lt;std::string::String&gt;` because of return type</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan class="fg-bright-blue bold">52 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">/</tspan><tspan> for ann in annotations {</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-bright-blue bold">53 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> match (ann.range.0, ann.range.1) {</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan class="fg-bright-blue bold">...</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan class="fg-bright-blue bold">71 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> }</tspan>
</tspan>
<tspan x="10px" y="190px"><tspan class="fg-bright-blue bold">72 |</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan><tspan> }</tspan>
</tspan>
<tspan x="10px" y="208px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|_____^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">expected enum `std::option::Option`, found ()</tspan>
</tspan>
<tspan x="10px" y="226px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 2.7 KiB

View file

@ -0,0 +1,44 @@
[message]
level = "Error"
id = "E0308"
title = "mismatched types"
[[message.snippets]]
source = """
) -> Option<String> {
for ann in annotations {
match (ann.range.0, ann.range.1) {
(None, None) => continue,
(Some(start), Some(end)) if start > end_index || end < start_index => continue,
(Some(start), Some(end)) if start >= start_index && end <= end_index => {
let label = if let Some(ref label) = ann.label {
format!(" {}", label)
} else {
String::from("")
};
return Some(format!(
"{}{}{}",
" ".repeat(start - start_index),
"^".repeat(end - start),
label
));
}
_ => continue,
}
}
"""
line_start = 51
origin = "src/format.rs"
fold = true
[[message.snippets.annotations]]
label = "expected `std::option::Option<std::string::String>` because of return type"
level = "Warning"
range = [5, 19]
[[message.snippets.annotations]]
label = "expected enum `std::option::Option`, found ()"
level = "Error"
range = [22, 766]
[renderer]
color = true

View file

@ -0,0 +1,37 @@
<svg width="740px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.fg-yellow { fill: #AA5500 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> path/to/error.rs:3:1</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">3 |</tspan><tspan> invalid syntax</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-yellow bold">--------------</tspan><tspan> </tspan><tspan class="fg-yellow bold">error here</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -0,0 +1,20 @@
[message]
level = "Error"
title = ""
[[message.snippets]]
source = """
invalid syntax
"""
line_start = 1
origin = "path/to/error.rs"
fold = true
[[message.snippets.annotations]]
label = "error here"
level = "Warning"
range = [2,16]
[renderer]
color = true

View file

@ -0,0 +1,36 @@
<svg width="740px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0308]</tspan><tspan>: </tspan><tspan class="bold">invalid type: integer `20`, expected a bool</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:11:13</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">11 |</tspan><tspan> workspace = 20</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -0,0 +1,29 @@
[message]
level = "Error"
id = "E0308"
title = "invalid type: integer `20`, expected a bool"
[[message.snippets]]
source = """
[workspace]
[package]
name = "hello"
version = "1.0.0"
license = "MIT"
rust-version = "1.70"
edition = "2021"
[lints]
workspace = 20
"""
line_start = 1
origin = "Cargo.toml"
fold = true
[[message.snippets.annotations]]
label = ""
level = "Error"
range = [132, 134]
[renderer]
color = true

View file

@ -0,0 +1,36 @@
<svg width="740px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0308]</tspan><tspan>: </tspan><tspan class="bold">invalid type: integer `20`, expected a lints table</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:1:9</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">1 |</tspan><tspan> lints = 20</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -0,0 +1,28 @@
[message]
level = "Error"
id = "E0308"
title = "invalid type: integer `20`, expected a lints table"
[[message.snippets]]
source = """
lints = 20
[workspace]
[package]
name = "hello"
version = "1.0.0"
license = "MIT"
rust-version = "1.70"
edition = "2021"
"""
line_start = 1
origin = "Cargo.toml"
fold = true
[[message.snippets.annotations]]
label = ""
level = "Error"
range = [8, 10]
[renderer]
color = true

View file

@ -0,0 +1,49 @@
<svg width="911px" height="236px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.fg-yellow { fill: #AA5500 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan><tspan>: </tspan><tspan class="bold">expected one of `.`, `;`, `?`, or an operator, found `for`</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> /code/rust/src/test/ui/annotate-snippet/suggestion.rs:4:5</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">4 |</tspan><tspan> let x = vec![1];</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-yellow bold">-</tspan><tspan> </tspan><tspan class="fg-yellow bold">move occurs because `x` has type `std::vec::Vec&lt;i32&gt;`, which does not implement the `Copy` trait</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-bright-blue bold">7 |</tspan><tspan> let y = x;</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-yellow bold">-</tspan><tspan> </tspan><tspan class="fg-yellow bold">value moved here</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="190px"><tspan class="fg-bright-blue bold">9 |</tspan><tspan> x;</tspan>
</tspan>
<tspan x="10px" y="208px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">value used here after move</tspan>
</tspan>
<tspan x="10px" y="226px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 2.5 KiB

View file

@ -0,0 +1,31 @@
[message]
level = "Error"
title = "expected one of `.`, `;`, `?`, or an operator, found `for`"
[[message.snippets]]
source = "let x = vec![1];"
line_start = 4
origin = "/code/rust/src/test/ui/annotate-snippet/suggestion.rs"
[[message.snippets.annotations]]
label = "move occurs because `x` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait"
level = "Warning"
range = [4, 5]
[[message.snippets]]
source = "let y = x;"
line_start = 7
[[message.snippets.annotations]]
label = "value moved here"
level = "Warning"
range = [8, 9]
[[message.snippets]]
source = "x;"
line_start = 9
[[message.snippets.annotations]]
label = "value used here after move"
level = "Error"
range = [0, 1]
[renderer]
color = true

View file

@ -0,0 +1,54 @@
<svg width="768px" height="290px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan class="fg-bright-blue bold"> 96 |</tspan><tspan> fn add_title_line(result: &amp;mut Vec&lt;String&gt;, main_annotation: Option&lt;&amp;Annotation&gt;) {</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold"> 97 |</tspan><tspan> if let Some(annotation) = main_annotation {</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^^^^^^^^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">Variable defined here</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan class="fg-bright-blue bold"> 98 |</tspan><tspan> result.push(format_title_line(</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-bright-blue bold"> 99 |</tspan><tspan> &amp;annotation.annotation_type,</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^^^^^^^^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">Referenced here</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan class="fg-bright-blue bold">100 |</tspan><tspan> None,</tspan>
</tspan>
<tspan x="10px" y="190px"><tspan class="fg-bright-blue bold">101 |</tspan><tspan> &amp;annotation.label,</tspan>
</tspan>
<tspan x="10px" y="208px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^^^^^^^^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">Referenced again here</tspan>
</tspan>
<tspan x="10px" y="226px"><tspan class="fg-bright-blue bold">102 |</tspan><tspan> ));</tspan>
</tspan>
<tspan x="10px" y="244px"><tspan class="fg-bright-blue bold">103 |</tspan><tspan> }</tspan>
</tspan>
<tspan x="10px" y="262px"><tspan class="fg-bright-blue bold">104 |</tspan><tspan> }</tspan>
</tspan>
<tspan x="10px" y="280px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 2.9 KiB

View file

@ -0,0 +1,32 @@
[message]
level = "Error"
title = ""
[[message.snippets]]
source = """
fn add_title_line(result: &mut Vec<String>, main_annotation: Option<&Annotation>) {
if let Some(annotation) = main_annotation {
result.push(format_title_line(
&annotation.annotation_type,
None,
&annotation.label,
));
}
}
"""
line_start = 96
[[message.snippets.annotations]]
label = "Variable defined here"
level = "Error"
range = [100, 110]
[[message.snippets.annotations]]
label = "Referenced here"
level = "Error"
range = [184, 194]
[[message.snippets.annotations]]
label = "Referenced again here"
level = "Error"
range = [243, 253]
[renderer]
color = true

View file

@ -0,0 +1,43 @@
<svg width="740px" height="182px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.fg-yellow { fill: #AA5500 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error</tspan><tspan>: </tspan><tspan class="bold">expected one of `.`, `;`, `?`, or an operator, found `for`</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> src/format_color.rs:171:9</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">169 |</tspan><tspan> })</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-yellow bold">-</tspan><tspan> </tspan><tspan class="fg-yellow bold">expected one of `.`, `;`, `?`, or an operator here</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan class="fg-bright-blue bold">170 |</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-bright-blue bold">171 |</tspan><tspan> for line in &amp;self.body {</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">unexpected token</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 2 KiB

View file

@ -0,0 +1,22 @@
[message]
level = "Error"
title = "expected one of `.`, `;`, `?`, or an operator, found `for`"
[[message.snippets]]
source = """
})
for line in &self.body {"""
line_start = 169
origin = "src/format_color.rs"
[[message.snippets.annotations]]
label = "unexpected token"
level = "Error"
range = [20, 23]
[[message.snippets.annotations]]
label = "expected one of `.`, `;`, `?`, or an operator here"
level = "Warning"
range = [10, 11]
[renderer]
color = true

View file

@ -0,0 +1,36 @@
<svg width="740px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0308]</tspan><tspan>: </tspan><tspan class="bold">mismatched types</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> $DIR/whitespace-trimming.rs:4:193</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">LL |</tspan><tspan> </tspan><tspan class="fg-bright-blue bold">...</tspan><tspan> let _: () = 42;</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">expected (), found integer</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -0,0 +1,18 @@
[message]
level = "Error"
id = "E0308"
title = "mismatched types"
[[message.snippets]]
source = " let _: () = 42;"
line_start = 4
origin = "$DIR/whitespace-trimming.rs"
[[message.snippets.annotations]]
label = "expected (), found integer"
level = "Error"
range = [192, 194]
[renderer]
color = true
anonymized_line_numbers = true

View file

@ -0,0 +1,36 @@
<svg width="740px" height="128px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0308]</tspan><tspan>: </tspan><tspan class="bold">mismatched types</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> $DIR/whitespace-trimming.rs:4:193</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">LL |</tspan><tspan> </tspan><tspan class="fg-bright-blue bold">...</tspan><tspan> let _: () = 42ñ</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">expected (), found integer</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -0,0 +1,18 @@
[message]
level = "Error"
id = "E0308"
title = "mismatched types"
[[message.snippets]]
source = " let _: () = 42ñ"
line_start = 4
origin = "$DIR/whitespace-trimming.rs"
[[message.snippets.annotations]]
label = "expected (), found integer"
level = "Error"
range = [192, 194]
[renderer]
color = true
anonymized_line_numbers = true

View file

@ -0,0 +1,40 @@
<svg width="1196px" height="164px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-red { fill: #FF5555 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-bright-red bold">error[E0308]</tspan><tspan>: </tspan><tspan class="bold">mismatched types</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> $DIR/non-whitespace-trimming.rs:4:242</tspan>
</tspan>
<tspan x="10px" y="64px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
<tspan x="10px" y="82px"><tspan class="fg-bright-blue bold">LL |</tspan><tspan> </tspan><tspan class="fg-bright-blue bold">...</tspan><tspan> = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () </tspan><tspan class="fg-bright-blue bold">...</tspan>
</tspan>
<tspan x="10px" y="100px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">^^</tspan><tspan> </tspan><tspan class="fg-bright-red bold">expected `()`, found integer</tspan>
</tspan>
<tspan x="10px" y="118px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">|</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan><tspan> </tspan><tspan class="fg-bright-red bold">expected due to this</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan> </tspan><tspan class="fg-bright-blue bold">|</tspan>
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -0,0 +1,26 @@
[message]
level = "Error"
id = "E0308"
title = "mismatched types"
[[message.snippets]]
source = """
let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();
"""
line_start = 4
origin = "$DIR/non-whitespace-trimming.rs"
[[message.snippets.annotations]]
label = "expected `()`, found integer"
level = "Error"
range = [241, 243]
[[message.snippets.annotations]]
label = "expected due to this"
level = "Error"
range = [236, 238]
[renderer]
anonymized_line_numbers = true
color = true

View file

@ -0,0 +1,130 @@
use serde::Deserialize;
use std::ops::Range;
use ruff_annotate_snippets::renderer::DEFAULT_TERM_WIDTH;
use ruff_annotate_snippets::{Annotation, Level, Message, Renderer, Snippet};
#[derive(Deserialize)]
pub(crate) struct Fixture {
#[serde(default)]
pub(crate) renderer: RendererDef,
pub(crate) message: MessageDef,
}
#[derive(Deserialize)]
pub struct MessageDef {
#[serde(with = "LevelDef")]
pub level: Level,
pub title: String,
#[serde(default)]
pub id: Option<String>,
#[serde(default)]
pub footer: Vec<MessageDef>,
pub snippets: Vec<SnippetDef>,
}
impl<'a> From<&'a MessageDef> for Message<'a> {
fn from(val: &'a MessageDef) -> Self {
let MessageDef {
level,
title,
id,
footer,
snippets,
} = val;
let mut message = level.title(title);
if let Some(id) = id {
message = message.id(id);
}
message = message.snippets(snippets.iter().map(Snippet::from));
message = message.footers(footer.iter().map(Into::into));
message
}
}
#[derive(Deserialize)]
pub struct SnippetDef {
pub source: String,
pub line_start: usize,
pub origin: Option<String>,
pub annotations: Vec<AnnotationDef>,
#[serde(default)]
pub fold: bool,
}
impl<'a> From<&'a SnippetDef> for Snippet<'a> {
fn from(val: &'a SnippetDef) -> Self {
let SnippetDef {
source,
line_start,
origin,
annotations,
fold,
} = val;
let mut snippet = Snippet::source(source).line_start(*line_start).fold(*fold);
if let Some(origin) = origin {
snippet = snippet.origin(origin);
}
snippet = snippet.annotations(annotations.iter().map(Into::into));
snippet
}
}
#[derive(Deserialize)]
pub struct AnnotationDef {
pub range: Range<usize>,
pub label: String,
#[serde(with = "LevelDef")]
pub level: Level,
}
impl<'a> From<&'a AnnotationDef> for Annotation<'a> {
fn from(val: &'a AnnotationDef) -> Self {
let AnnotationDef {
range,
label,
level,
} = val;
level.span(range.start..range.end).label(label)
}
}
#[allow(dead_code)]
#[derive(Deserialize)]
#[serde(remote = "Level")]
enum LevelDef {
Error,
Warning,
Info,
Note,
Help,
}
#[derive(Default, Deserialize)]
pub struct RendererDef {
#[serde(default)]
anonymized_line_numbers: bool,
#[serde(default)]
term_width: Option<usize>,
#[serde(default)]
color: bool,
}
impl From<RendererDef> for Renderer {
fn from(val: RendererDef) -> Self {
let RendererDef {
anonymized_line_numbers,
term_width,
color,
} = val;
let renderer = if color {
Renderer::styled()
} else {
Renderer::plain()
};
renderer
.anonymized_line_numbers(anonymized_line_numbers)
.term_width(term_width.unwrap_or(DEFAULT_TERM_WIDTH))
}
}

View file

@ -0,0 +1,42 @@
mod deserialize;
use crate::deserialize::Fixture;
use ruff_annotate_snippets::{Message, Renderer};
use snapbox::data::DataFormat;
use snapbox::Data;
use std::error::Error;
fn main() {
#[cfg(not(windows))]
tryfn::Harness::new("tests/fixtures/", setup, test)
.select(["*/*.toml"])
.test();
}
fn setup(input_path: std::path::PathBuf) -> tryfn::Case {
let parent = input_path
.parent()
.unwrap()
.file_name()
.unwrap()
.to_str()
.unwrap();
let file_name = input_path.file_name().unwrap().to_str().unwrap();
let name = format!("{parent}/{file_name}");
let expected = Data::read_from(&input_path.with_extension("svg"), None);
tryfn::Case {
name,
fixture: input_path,
expected,
}
}
fn test(input_path: &std::path::Path) -> Result<Data, Box<dyn Error>> {
let src = std::fs::read_to_string(input_path)?;
let fixture: Fixture = toml::from_str(&src)?;
let renderer: Renderer = fixture.renderer.into();
let message: Message<'_> = (&fixture.message).into();
let actual = renderer.render(message).to_string();
Ok(Data::from(actual).coerce_to(DataFormat::TermSvg))
}

View file

@ -0,0 +1,963 @@
// Since this is a vendored copy of `annotate-snippets`, we squash Clippy
// warnings from upstream in order to the reduce the diff. If our copy drifts
// far from upstream such that patches become impractical to apply in both
// places, then we can get rid of these suppressions and fix the lints.
#![allow(clippy::redundant_clone, clippy::should_panic_without_expect)]
use ruff_annotate_snippets::{Level, Renderer, Snippet};
use snapbox::{assert_data_eq, str};
#[test]
fn test_i_29() {
let snippets = Level::Error.title("oops").snippet(
Snippet::source("First line\r\nSecond oops line")
.origin("<current file>")
.annotation(Level::Error.span(19..23).label("oops"))
.fold(true),
);
let expected = str![[r#"
error: oops
--> <current file>:2:8
|
2 | Second oops line
| ^^^^ oops
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(snippets).to_string(), expected);
}
#[test]
fn test_point_to_double_width_characters() {
let snippets = Level::Error.title("").snippet(
Snippet::source("こんにちは、世界")
.origin("<current file>")
.annotation(Level::Error.span(18..24).label("world")),
);
let expected = str![[r#"
error
--> <current file>:1:7
|
1 |
| ^^^^ world
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(snippets).to_string(), expected);
}
#[test]
fn test_point_to_double_width_characters_across_lines() {
let snippets = Level::Error.title("").snippet(
Snippet::source("おはよう\nございます")
.origin("<current file>")
.annotation(Level::Error.span(6..22).label("Good morning")),
);
let expected = str![[r#"
error
--> <current file>:1:3
|
1 |
| _____^
2 | |
| |______^ Good morning
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(snippets).to_string(), expected);
}
#[test]
fn test_point_to_double_width_characters_multiple() {
let snippets = Level::Error.title("").snippet(
Snippet::source("お寿司\n食べたい🍣")
.origin("<current file>")
.annotation(Level::Error.span(0..9).label("Sushi1"))
.annotation(Level::Note.span(16..22).label("Sushi2")),
);
let expected = str![[r#"
error
--> <current file>:1:1
|
1 | 寿
| ^^^^^^ Sushi1
2 | 🍣
| ---- note: Sushi2
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(snippets).to_string(), expected);
}
#[test]
fn test_point_to_double_width_characters_mixed() {
let snippets = Level::Error.title("").snippet(
Snippet::source("こんにちは、新しいWorld")
.origin("<current file>")
.annotation(Level::Error.span(18..32).label("New world")),
);
let expected = str![[r#"
error
--> <current file>:1:7
|
1 | World
| ^^^^^^^^^^^ New world
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(snippets).to_string(), expected);
}
#[test]
fn test_format_title() {
let input = Level::Error.title("This is a title").id("E0001");
let expected = str![r#"error[E0001]: This is a title"#];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn test_format_snippet_only() {
let source = "This is line 1\nThis is line 2";
let input = Level::Error
.title("")
.snippet(Snippet::source(source).line_start(5402));
let expected = str![[r#"
error
|
5402 | This is line 1
5403 | This is line 2
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn test_format_snippets_continuation() {
let src_0 = "This is slice 1";
let src_1 = "This is slice 2";
let input = Level::Error
.title("")
.snippet(Snippet::source(src_0).line_start(5402).origin("file1.rs"))
.snippet(Snippet::source(src_1).line_start(2).origin("file2.rs"));
let expected = str![[r#"
error
--> file1.rs
|
5402 | This is slice 1
|
::: file2.rs
|
2 | This is slice 2
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn test_format_snippet_annotation_standalone() {
let line_1 = "This is line 1";
let line_2 = "This is line 2";
let source = [line_1, line_2].join("\n");
// In line 2
let range = 22..24;
let input = Level::Error.title("").snippet(
Snippet::source(&source)
.line_start(5402)
.annotation(Level::Info.span(range.clone()).label("Test annotation")),
);
let expected = str![[r#"
error
|
5402 | This is line 1
5403 | This is line 2
| -- info: Test annotation
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn test_format_footer_title() {
let input = Level::Error
.title("")
.footer(Level::Error.title("This __is__ a title"));
let expected = str![[r#"
error
= error: This __is__ a title
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
#[should_panic]
fn test_i26() {
let source = "short";
let label = "label";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.line_start(0)
.annotation(Level::Error.span(0..source.len() + 2).label(label)),
);
let renderer = Renderer::plain();
let _ = renderer.render(input).to_string();
}
#[test]
fn test_source_content() {
let source = "This is an example\nof content lines";
let input = Level::Error
.title("")
.snippet(Snippet::source(source).line_start(56));
let expected = str![[r#"
error
|
56 | This is an example
57 | of content lines
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn test_source_annotation_standalone_singleline() {
let source = "tests";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.line_start(1)
.annotation(Level::Help.span(0..5).label("Example string")),
);
let expected = str![[r#"
error
|
1 | tests
| ----- help: Example string
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn test_source_annotation_standalone_multiline() {
let source = "tests";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.line_start(1)
.annotation(Level::Help.span(0..5).label("Example string"))
.annotation(Level::Help.span(0..5).label("Second line")),
);
let expected = str![[r#"
error
|
1 | tests
| -----
| |
| help: Example string
| help: Second line
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn test_only_source() {
let input = Level::Error
.title("")
.snippet(Snippet::source("").origin("file.rs"));
let expected = str![[r#"
error
--> file.rs
|
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn test_anon_lines() {
let source = "This is an example\nof content lines\n\nabc";
let input = Level::Error
.title("")
.snippet(Snippet::source(source).line_start(56));
let expected = str![[r#"
error
|
LL | This is an example
LL | of content lines
LL |
LL | abc
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(true);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn issue_130() {
let input = Level::Error.title("dummy").snippet(
Snippet::source("foo\nbar\nbaz")
.origin("file/path")
.line_start(3)
.fold(true)
.annotation(Level::Error.span(4..11)), // bar\nbaz
);
let expected = str![[r#"
error: dummy
--> file/path:4:1
|
4 | / bar
5 | | baz
| |___^
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn unterminated_string_multiline() {
let source = "\
a\"
// ...
";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.fold(true)
.annotation(Level::Error.span(0..10)), // 1..10 works
);
let expected = str![[r#"
error
--> file/path:3:1
|
3 | / a"
4 | | // ...
| |_______^
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn char_and_nl_annotate_char() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(0..2)), // a\r
);
let expected = str![[r#"
error
--> file/path:3:1
|
3 | a
| ^
4 | b
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn char_eol_annotate_char() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(0..3)), // a\r\n
);
let expected = str![[r#"
error
--> file/path:3:1
|
3 | a
| ^
4 | b
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn char_eol_annotate_char_double_width() {
let snippets = Level::Error.title("").snippet(
Snippet::source("こん\r\nにちは\r\n世界")
.origin("<current file>")
.annotation(Level::Error.span(3..8)), // ん\r\n
);
let expected = str![[r#"
error
--> <current file>:1:2
|
1 |
| ^^
2 |
3 |
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(snippets).to_string(), expected);
}
#[test]
fn annotate_eol() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(1..2)), // \r
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| ^
4 | b
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn annotate_eol2() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(1..3)), // \r\n
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| ^
4 | b
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn annotate_eol3() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(2..3)), // \n
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| ^
4 | b
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn annotate_eol4() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(2..2)), // \n
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| ^
4 | b
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn annotate_eol_double_width() {
let snippets = Level::Error.title("").snippet(
Snippet::source("こん\r\nにちは\r\n世界")
.origin("<current file>")
.annotation(Level::Error.span(7..8)), // \n
);
let expected = str![[r#"
error
--> <current file>:1:3
|
1 |
| ^
2 |
3 |
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(snippets).to_string(), expected);
}
#[test]
fn multiline_eol_start() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(1..4)), // \r\nb
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| __^
4 | | b
| |_^
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiline_eol_start2() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(2..4)), // \nb
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| __^
4 | | b
| |_^
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiline_eol_start3() {
let source = "a\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(1..3)), // \nb
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| __^
4 | | b
| |_^
|"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiline_eol_start_double_width() {
let snippets = Level::Error.title("").snippet(
Snippet::source("こん\r\nにちは\r\n世界")
.origin("<current file>")
.annotation(Level::Error.span(7..11)), // \r\nに
);
let expected = str![[r#"
error
--> <current file>:1:3
|
1 |
| _____^
2 | |
| |__^
3 |
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(snippets).to_string(), expected);
}
#[test]
fn multiline_eol_start_eol_end() {
let source = "a\nb\nc";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(1..4)), // \nb\n
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| __^
4 | | b
| |__^
5 | c
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiline_eol_start_eol_end2() {
let source = "a\r\nb\r\nc";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(2..5)), // \nb\r
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| __^
4 | | b
| |__^
5 | c
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiline_eol_start_eol_end3() {
let source = "a\r\nb\r\nc";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(2..6)), // \nb\r\n
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| __^
4 | | b
| |__^
5 | c
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiline_eol_start_eof_end() {
let source = "a\r\nb";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(1..5)), // \r\nb(EOF)
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 | a
| __^
4 | | b
| |__^
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiline_eol_start_eof_end_double_width() {
let source = "\r\n";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.origin("file/path")
.line_start(3)
.annotation(Level::Error.span(3..9)), // \r\nに(EOF)
);
let expected = str![[r#"
error
--> file/path:3:2
|
3 |
| ___^
4 | |
| |___^
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn two_single_line_same_line() {
let source = r#"bar = { version = "0.1.0", optional = true }"#;
let input = Level::Error.title("unused optional dependency").snippet(
Snippet::source(source)
.origin("Cargo.toml")
.line_start(4)
.annotation(
Level::Error
.span(0..3)
.label("I need this to be really long so I can test overlaps"),
)
.annotation(
Level::Info
.span(27..42)
.label("This should also be long but not too long"),
),
);
let expected = str![[r#"
error: unused optional dependency
--> Cargo.toml:4:1
|
4 | bar = { version = "0.1.0", optional = true }
| ^^^ --------------- info: This should also be long but not too long
| |
| I need this to be really long so I can test overlaps
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multi_and_single() {
let source = r#"bar = { version = "0.1.0", optional = true }
this is another line
so is this
bar = { version = "0.1.0", optional = true }
"#;
let input = Level::Error.title("unused optional dependency").snippet(
Snippet::source(source)
.line_start(4)
.annotation(
Level::Error
.span(41..119)
.label("I need this to be really long so I can test overlaps"),
)
.annotation(
Level::Info
.span(27..42)
.label("This should also be long but not too long"),
),
);
let expected = str![[r#"
error: unused optional dependency
|
4 | bar = { version = "0.1.0", optional = true }
| ____________________________--------------^
| | |
| | info: This should also be long but not too long
5 | | this is another line
6 | | so is this
7 | | bar = { version = "0.1.0", optional = true }
| |__________________________________________^ I need this to be really long so I can test overlaps
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn two_multi_and_single() {
let source = r#"bar = { version = "0.1.0", optional = true }
this is another line
so is this
bar = { version = "0.1.0", optional = true }
"#;
let input = Level::Error.title("unused optional dependency").snippet(
Snippet::source(source)
.line_start(4)
.annotation(
Level::Error
.span(41..119)
.label("I need this to be really long so I can test overlaps"),
)
.annotation(
Level::Error
.span(8..102)
.label("I need this to be really long so I can test overlaps"),
)
.annotation(
Level::Info
.span(27..42)
.label("This should also be long but not too long"),
),
);
let expected = str![[r#"
error: unused optional dependency
|
4 | bar = { version = "0.1.0", optional = true }
| _________^__________________--------------^
| | | |
| |_________| info: This should also be long but not too long
| ||
5 | || this is another line
6 | || so is this
7 | || bar = { version = "0.1.0", optional = true }
| ||_________________________^________________^ I need this to be really long so I can test overlaps
| |__________________________|
| I need this to be really long so I can test overlaps
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn three_multi_and_single() {
let source = r#"bar = { version = "0.1.0", optional = true }
this is another line
so is this
bar = { version = "0.1.0", optional = true }
this is another line
"#;
let input = Level::Error.title("unused optional dependency").snippet(
Snippet::source(source)
.line_start(4)
.annotation(
Level::Error
.span(41..119)
.label("I need this to be really long so I can test overlaps"),
)
.annotation(
Level::Error
.span(8..102)
.label("I need this to be really long so I can test overlaps"),
)
.annotation(
Level::Error
.span(48..126)
.label("I need this to be really long so I can test overlaps"),
)
.annotation(
Level::Info
.span(27..42)
.label("This should also be long but not too long"),
),
);
let expected = str![[r#"
error: unused optional dependency
|
4 | bar = { version = "0.1.0", optional = true }
| __________^__________________--------------^
| | | |
| |__________| info: This should also be long but not too long
| ||
5 | || this is another line
| || ____^
6 | ||| so is this
7 | ||| bar = { version = "0.1.0", optional = true }
| |||_________________________^________________^ I need this to be really long so I can test overlaps
| |_|_________________________|
| | I need this to be really long so I can test overlaps
8 | | this is another line
| |____^ I need this to be really long so I can test overlaps
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn origin_correct_start_line() {
let source = "aaa\nbbb\nccc\nddd\n";
let input = Level::Error.title("title").snippet(
Snippet::source(source)
.origin("origin.txt")
.fold(false)
.annotation(Level::Error.span(8..8 + 3).label("annotation")),
);
let expected = str![[r#"
error: title
--> origin.txt:3:1
|
1 | aaa
2 | bbb
3 | ccc
| ^^^ annotation
4 | ddd
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn origin_correct_mid_line() {
let source = "aaa\nbbb\nccc\nddd\n";
let input = Level::Error.title("title").snippet(
Snippet::source(source)
.origin("origin.txt")
.fold(false)
.annotation(Level::Error.span(8 + 1..8 + 3).label("annotation")),
);
let expected = str![[r#"
error: title
--> origin.txt:3:2
|
1 | aaa
2 | bbb
3 | ccc
| ^^ annotation
4 | ddd
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}

View file

@ -0,0 +1,783 @@
//! These tests have been adapted from [Rust's parser tests][parser-tests].
//!
//! [parser-tests]: https://github.com/rust-lang/rust/blob/894f7a4ba6554d3797404bbf550d9919df060b97/compiler/rustc_parse/src/parser/tests.rs
use ruff_annotate_snippets::{Level, Renderer, Snippet};
use snapbox::{assert_data_eq, str};
#[test]
fn ends_on_col0() {
let source = r#"
fn foo() {
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(10..13).label("test")),
);
let expected = str![[r#"
error: foo
--> test.rs:2:10
|
2 | fn foo() {
| __________^
3 | | }
| |_^ test
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn ends_on_col2() {
let source = r#"
fn foo() {
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(10..17).label("test")),
);
let expected = str![[r#"
error: foo
--> test.rs:2:10
|
2 | fn foo() {
| __________^
3 | |
4 | |
5 | | }
| |___^ test
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn non_nested() {
let source = r#"
fn foo() {
X0 Y0
X1 Y1
X2 Y2
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..32).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(17..35)
.label("`Y` is a good letter too"),
),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | X0 Y0
| ____^ -
| | ______|
4 | || X1 Y1
5 | || X2 Y2
| ||____^__- `Y` is a good letter too
| |_____|
| `X` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn nested() {
let source = r#"
fn foo() {
X0 Y0
Y1 X1
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..27).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(17..24)
.label("`Y` is a good letter too"),
),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | X0 Y0
| ____^ -
| | ______|
4 | || Y1 X1
| ||____-__^ `X` is a good letter
| |____|
| `Y` is a good letter too
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn different_overlap() {
let source = r#"
fn foo() {
X0 Y0 Z0
X1 Y1 Z1
X2 Y2 Z2
X3 Y3 Z3
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(17..38).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(31..49)
.label("`Y` is a good letter too"),
),
);
let expected = str![[r#"
error: foo
--> test.rs:3:6
|
3 | X0 Y0 Z0
| _______^
4 | | X1 Y1 Z1
| | _________-
5 | || X2 Y2 Z2
| ||____^ `X` is a good letter
6 | | X3 Y3 Z3
| |____- `Y` is a good letter too
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn triple_overlap() {
let source = r#"
fn foo() {
X0 Y0 Z0
X1 Y1 Z1
X2 Y2 Z2
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..38).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(17..41)
.label("`Y` is a good letter too"),
)
.annotation(Level::Warning.span(20..44).label("`Z` label")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | X0 Y0 Z0
| _____^ - -
| | _______| |
| || _________|
4 | ||| X1 Y1 Z1
5 | ||| X2 Y2 Z2
| |||____^__-__- `Z` label
| ||_____|__|
| |______| `Y` is a good letter too
| `X` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn triple_exact_overlap() {
let source = r#"
fn foo() {
X0 Y0 Z0
X1 Y1 Z1
X2 Y2 Z2
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..38).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(14..38)
.label("`Y` is a good letter too"),
)
.annotation(Level::Warning.span(14..38).label("`Z` label")),
);
// This should have a `^` but we currently don't support the idea of a
// "primary" annotation, which would solve this
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | / X0 Y0 Z0
4 | | X1 Y1 Z1
5 | | X2 Y2 Z2
| | -
| |____|
| `X` is a good letter
| `Y` is a good letter too
| `Z` label
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn minimum_depth() {
let source = r#"
fn foo() {
X0 Y0 Z0
X1 Y1 Z1
X2 Y2 Z2
X3 Y3 Z3
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(17..27).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(28..44)
.label("`Y` is a good letter too"),
)
.annotation(Level::Warning.span(36..52).label("`Z`")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:6
|
3 | X0 Y0 Z0
| _______^
4 | | X1 Y1 Z1
| | ____^_-
| ||____|
| | `X` is a good letter
5 | | X2 Y2 Z2
| |___-______- `Y` is a good letter too
| ___|
| |
6 | | X3 Y3 Z3
| |_______- `Z`
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn non_overlapping() {
let source = r#"
fn foo() {
X0 Y0 Z0
X1 Y1 Z1
X2 Y2 Z2
X3 Y3 Z3
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..27).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(39..55)
.label("`Y` is a good letter too"),
),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | / X0 Y0 Z0
4 | | X1 Y1 Z1
| |____^ `X` is a good letter
5 | X2 Y2 Z2
| ______-
6 | | X3 Y3 Z3
| |__________- `Y` is a good letter too
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn overlapping_start_and_end() {
let source = r#"
fn foo() {
X0 Y0 Z0
X1 Y1 Z1
X2 Y2 Z2
X3 Y3 Z3
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(17..27).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(31..55)
.label("`Y` is a good letter too"),
),
);
let expected = str![[r#"
error: foo
--> test.rs:3:6
|
3 | X0 Y0 Z0
| _______^
4 | | X1 Y1 Z1
| | ____^____-
| ||____|
| | `X` is a good letter
5 | | X2 Y2 Z2
6 | | X3 Y3 Z3
| |__________- `Y` is a good letter too
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiple_labels_primary_without_message() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(18..25).label(""))
.annotation(Level::Warning.span(14..27).label("`a` is a good letter"))
.annotation(Level::Warning.span(22..23).label("")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:7
|
3 | a { b { c } d }
| ----^^^^-^^-- `a` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiple_labels_secondary_without_message() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..27).label("`a` is a good letter"))
.annotation(Level::Warning.span(18..25).label("")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | a { b { c } d }
| ^^^^-------^^ `a` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiple_labels_primary_without_message_2() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(18..25).label("`b` is a good letter"))
.annotation(Level::Warning.span(14..27).label(""))
.annotation(Level::Warning.span(22..23).label("")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:7
|
3 | a { b { c } d }
| ----^^^^-^^--
| |
| `b` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiple_labels_secondary_without_message_2() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..27).label(""))
.annotation(Level::Warning.span(18..25).label("`b` is a good letter")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | a { b { c } d }
| ^^^^-------^^
| |
| `b` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiple_labels_secondary_without_message_3() {
let source = r#"
fn foo() {
a bc d
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..18).label("`a` is a good letter"))
.annotation(Level::Warning.span(18..22).label("")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | a bc d
| ^^^^----
| |
| `a` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiple_labels_without_message() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..27).label(""))
.annotation(Level::Warning.span(18..25).label("")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | a { b { c } d }
| ^^^^-------^^
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiple_labels_without_message_2() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(18..25).label(""))
.annotation(Level::Warning.span(14..27).label(""))
.annotation(Level::Warning.span(22..23).label("")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:7
|
3 | a { b { c } d }
| ----^^^^-^^--
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn multiple_labels_with_message() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..27).label("`a` is a good letter"))
.annotation(Level::Warning.span(18..25).label("`b` is a good letter")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | a { b { c } d }
| ^^^^-------^^
| | |
| | `b` is a good letter
| `a` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn ingle_label_with_message() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..27).label("`a` is a good letter")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | a { b { c } d }
| ^^^^^^^^^^^^^ `a` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn single_label_without_message() {
let source = r#"
fn foo() {
a { b { c } d }
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(14..27).label("")),
);
let expected = str![[r#"
error: foo
--> test.rs:3:3
|
3 | a { b { c } d }
| ^^^^^^^^^^^^^
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn long_snippet() {
let source = r#"
fn foo() {
X0 Y0 Z0
X1 Y1 Z1
1
2
3
4
5
6
7
8
9
10
X2 Y2 Z2
X3 Y3 Z3
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(17..27).label("`X` is a good letter"))
.annotation(
Level::Warning
.span(31..76)
.label("`Y` is a good letter too"),
),
);
let expected = str![[r#"
error: foo
--> test.rs:3:6
|
3 | X0 Y0 Z0
| _______^
4 | | X1 Y1 Z1
| | ____^____-
| ||____|
| | `X` is a good letter
5 | | 1
... |
15 | | X2 Y2 Z2
16 | | X3 Y3 Z3
| |__________- `Y` is a good letter too
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn long_snippet_multiple_spans() {
let source = r#"
fn foo() {
X0 Y0 Z0
1
2
3
X1 Y1 Z1
4
5
6
X2 Y2 Z2
7
8
9
10
X3 Y3 Z3
}
"#;
let input = Level::Error.title("foo").snippet(
Snippet::source(source)
.line_start(1)
.origin("test.rs")
.fold(true)
.annotation(Level::Error.span(17..73).label("`Y` is a good letter"))
.annotation(
Level::Warning
.span(37..56)
.label("`Z` is a good letter too"),
),
);
let expected = str![[r#"
error: foo
--> test.rs:3:6
|
3 | X0 Y0 Z0
| _______^
4 | | 1
5 | | 2
6 | | 3
7 | | X1 Y1 Z1
| | _________-
8 | || 4
9 | || 5
10 | || 6
11 | || X2 Y2 Z2
| ||__________- `Z` is a good letter too
12 | | 7
... |
15 | | 10
16 | | X3 Y3 Z3
| |________^ `Y` is a good letter
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}