Skip to content

Commit 006ee5a

Browse files
committed
Ruby: improve encoding related messages
1 parent ecbd768 commit 006ee5a

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

ruby/extractor/src/diagnostics.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ impl DiagnosticLoggers {
202202
impl DiagnosticMessage {
203203
pub fn full_error_message(&self) -> String {
204204
match &self.location {
205+
Some(Location {
206+
file: Some(path),
207+
start_line: None,
208+
..
209+
}) => format!("{}: {}", path, self.plaintext_message),
205210
Some(Location {
206211
file: Some(path),
207212
start_line: Some(line),
@@ -249,6 +254,11 @@ impl DiagnosticMessage {
249254
self.visibility.telemetry = true;
250255
self
251256
}
257+
pub fn file(&mut self, path: &str) -> &mut Self {
258+
let loc = self.location.get_or_insert(Default::default());
259+
loc.file = Some(path.to_owned());
260+
self
261+
}
252262
pub fn location(
253263
&mut self,
254264
path: &str,

ruby/extractor/src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ fn main() -> std::io::Result<()> {
198198
diagnostics_writer.write(
199199
diagnostics_writer
200200
.message(
201-
"character-encoding-error",
202-
"Character encoding error",
201+
"character-decoding-error",
202+
"Character decoding error",
203203
)
204+
.file(&path.to_string_lossy())
204205
.text(&format!(
205-
"{}: character decoding failure: {} ({})",
206-
&path.to_string_lossy(),
206+
"could not decode the file contents as '{}': {}",
207+
&encoding_name,
207208
msg,
208-
&encoding_name
209209
))
210210
.status_page()
211211
.severity(diagnostics::Severity::Warning),
@@ -216,13 +216,14 @@ fn main() -> std::io::Result<()> {
216216
} else {
217217
diagnostics_writer.write(
218218
diagnostics_writer
219-
.message("character-encoding-error", "Character encoding error")
219+
.message("unknown-character-encoding", "Unknown character encoding")
220+
.file(&path.to_string_lossy())
220221
.text(&format!(
221-
"{}: unknown character encoding: '{}'",
222-
&path.to_string_lossy(),
222+
"unknown character encoding '{}' in '#encoding:' directive.",
223223
&encoding_name
224224
))
225225
.status_page()
226+
.help_link("https://docs.ruby-lang.org/en/3.2/syntax/comments_rdoc.html#label-encoding+Directive")
226227
.severity(diagnostics::Severity::Warning),
227228
);
228229
}

0 commit comments

Comments
 (0)