Skip to content

Commit

Permalink
Fix(compiler-base-error): CodeSnippet component generates the line … (
Browse files Browse the repository at this point in the history
#249)

* Fix(compiler-base-error): `CodeSnippet` component generates the line number plus one.

In `CodeSnippet` components, the line number plus one.

issue #115

* add some comments
  • Loading branch information
zong-zhe authored Oct 20, 2022
1 parent 9a2ef1a commit 51b374d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions compiler_base/3rdparty/rustc_errors/src/styled_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ where
T: Clone + PartialEq + Eq + Style,
{
/// Constructs a new `StyledString` by string and style.
///
///
/// # Examples
///
///
/// ```ignore
/// // You need to choose a style for the generic parameter `T` of `StyledString`.
/// #[derive(Clone, PartialEq, Eq)]
Expand All @@ -46,7 +46,7 @@ where
/// impl Style for MyStyle {
/// ...
/// }
///
///
/// let styled_string = StyledString::<MyStyle>::new("Hello Styled String".to_string(), Some<MyStyle::Style_1>);
/// ```
#[inline]
Expand Down
5 changes: 4 additions & 1 deletion compiler_base/error/src/diagnostic/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ impl Component<DiagnosticStyle> for CodeSnippet {
)) {
Some(sf) => {
for line in affected_lines.lines {
let line_index = line.line_index.to_string();
// The line number shown in diagnostic should begin from 1.
// The `line.line_index` get from `SourceMap` begin from 0.
// So, the line number shown in diagnostic should be equal to line.line_index + 1.
let line_index = (line.line_index + 1).to_string();
let indent = line_index.len() + 1;
IndentWithPrefix::new(line_index, indent, Some(DiagnosticStyle::Url))
.format(sb, errs);
Expand Down
10 changes: 5 additions & 5 deletions compiler_base/error/src/diagnostic/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ mod test_components {
sm.new_source_file(PathBuf::from(filename.clone()).into(), src);

let code_span = SpanData {
lo: new_byte_pos(23),
hi: new_byte_pos(25),
lo: new_byte_pos(0),
hi: new_byte_pos(5),
}
.span();

Expand All @@ -155,15 +155,15 @@ mod test_components {
assert_eq!(errs.len(), 0);
assert_eq!(result.len(), 1);
assert_eq!(result.get(0).unwrap().len(), 6);
let expected_path = format!("---> File: {}:2:3: 2:5", filename);
let expected_path = format!("---> File: {}:1:1: 1:6", filename);
assert_eq!(result.get(0).unwrap().get(0).unwrap().text, expected_path);
assert_eq!(result.get(0).unwrap().get(1).unwrap().text, "\n ");
assert_eq!(result.get(0).unwrap().get(2).unwrap().text, "1");
assert_eq!(
result.get(0).unwrap().get(3).unwrap().text,
"|Line 2 Code Snippet.\n | "
"|Line 1 Code Snippet.\n |"
);
assert_eq!(result.get(0).unwrap().get(4).unwrap().text, "^^");
assert_eq!(result.get(0).unwrap().get(4).unwrap().text, "^^^^^");
assert_eq!(result.get(0).unwrap().get(5).unwrap().text, "\n");
}
}
Expand Down

0 comments on commit 51b374d

Please sign in to comment.