Ruby: improve encoding related messages

This commit is contained in:
Arthur Baars
2023-02-16 13:11:22 +01:00
parent ecbd768df4
commit 006ee5aad9
2 changed files with 19 additions and 8 deletions

View File

@@ -202,6 +202,11 @@ impl DiagnosticLoggers {
impl DiagnosticMessage {
pub fn full_error_message(&self) -> String {
match &self.location {
Some(Location {
file: Some(path),
start_line: None,
..
}) => format!("{}: {}", path, self.plaintext_message),
Some(Location {
file: Some(path),
start_line: Some(line),
@@ -249,6 +254,11 @@ impl DiagnosticMessage {
self.visibility.telemetry = true;
self
}
pub fn file(&mut self, path: &str) -> &mut Self {
let loc = self.location.get_or_insert(Default::default());
loc.file = Some(path.to_owned());
self
}
pub fn location(
&mut self,
path: &str,

View File

@@ -198,14 +198,14 @@ fn main() -> std::io::Result<()> {
diagnostics_writer.write(
diagnostics_writer
.message(
"character-encoding-error",
"Character encoding error",
"character-decoding-error",
"Character decoding error",
)
.file(&path.to_string_lossy())
.text(&format!(
"{}: character decoding failure: {} ({})",
&path.to_string_lossy(),
"could not decode the file contents as '{}': {}",
&encoding_name,
msg,
&encoding_name
))
.status_page()
.severity(diagnostics::Severity::Warning),
@@ -216,13 +216,14 @@ fn main() -> std::io::Result<()> {
} else {
diagnostics_writer.write(
diagnostics_writer
.message("character-encoding-error", "Character encoding error")
.message("unknown-character-encoding", "Unknown character encoding")
.file(&path.to_string_lossy())
.text(&format!(
"{}: unknown character encoding: '{}'",
&path.to_string_lossy(),
"unknown character encoding '{}' in '#encoding:' directive.",
&encoding_name
))
.status_page()
.help_link("https://docs.ruby-lang.org/en/3.2/syntax/comments_rdoc.html#label-encoding+Directive")
.severity(diagnostics::Severity::Warning),
);
}