Skip to content

Commit c4466a0

Browse files
authored
Merge pull request RustPython#1615 from palaviv/fix-caret
Fix caret diagnostics for mutiple lines
2 parents 300ac26 + 1279079 commit c4466a0

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

compiler/src/error.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,17 @@ impl fmt::Display for CompileError {
9191
CompileErrorType::InvalidYield => "'yield' outside function".to_string(),
9292
};
9393

94-
if self.statement.is_some() && self.location.column() > 0 {
95-
// visualize the error, when location and statement are provided
96-
write!(
97-
f,
98-
"\n{}\n{}",
99-
self.statement.clone().unwrap(),
100-
self.location.visualize(&error_desc)
101-
)
102-
} else {
103-
// print line number
104-
write!(f, "{} at {}", error_desc, self.location)
94+
if let Some(statement) = &self.statement {
95+
if self.location.column() > 0 {
96+
if let Some(line) = statement.lines().nth(self.location.row() - 1) {
97+
// visualize the error, when location and statement are provided
98+
return write!(f, "\n{}\n{}", line, self.location.visualize(&error_desc));
99+
}
100+
}
105101
}
102+
103+
// print line number
104+
write!(f, "{} at {}", error_desc, self.location)
106105
}
107106
}
108107

0 commit comments

Comments
 (0)