about summary refs log tree commit diff
path: root/src/source_map.rs
diff options
context:
space:
mode:
authorStéphane Campinas <stephane.campinas@gmail.com>2019-03-19 10:19:45 +0100
committerStéphane Campinas <stephane.campinas@gmail.com>2019-03-19 10:19:45 +0100
commitcdd08da27bfb76d24dc44848e4b086df7d25d4f6 (patch)
tree82f6ba6b29f058116f6a4caec8c8e4847a1c4634 /src/source_map.rs
parent1427e4c20ba5cdc80a338347585c9de71a0dea4d (diff)
downloadrust-cdd08da27bfb76d24dc44848e4b086df7d25d4f6.tar.gz
rust-cdd08da27bfb76d24dc44848e4b086df7d25d4f6.zip
fix line numbering in missed spans and handle file_lines in edge cases
- a leading/trailing newline character in missed spans was throwing off the
  start/end of ranges used to compare against file_lines
- fix handling of file_lines when closing a block

Close #3442
Diffstat (limited to 'src/source_map.rs')
-rw-r--r--src/source_map.rs8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/source_map.rs b/src/source_map.rs
index c98b7596383..096a7ce5713 100644
--- a/src/source_map.rs
+++ b/src/source_map.rs
@@ -71,6 +71,7 @@ impl<'a> SpanUtils for SnippetProvider<'a> {
 
 impl LineRangeUtils for SourceMap {
     fn lookup_line_range(&self, span: Span) -> LineRange {
+        let snippet = self.span_to_snippet(span).unwrap_or(String::new());
         let lo = self.lookup_line(span.lo()).unwrap();
         let hi = self.lookup_line(span.hi()).unwrap();
 
@@ -80,11 +81,14 @@ impl LineRangeUtils for SourceMap {
             lo, hi
         );
 
+        // in case the span starts with a newline, the line range is off by 1 without the
+        // adjustment below
+        let offset = 1 + if snippet.starts_with('\n') { 1 } else { 0 };
         // Line numbers start at 1
         LineRange {
             file: lo.sf.clone(),
-            lo: lo.line + 1,
-            hi: hi.line + 1,
+            lo: lo.line + offset,
+            hi: hi.line + offset,
         }
     }
 }