@@ -163,6 +163,12 @@ pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
163
163
expn_id : COMMAND_LINE_EXPN } ;
164
164
165
165
impl Span {
166
+ /// Returns a new span representing just the end-point of this span
167
+ pub fn end_point ( self ) -> Span {
168
+ let lo = cmp:: max ( self . hi . 0 - 1 , self . lo . 0 ) ;
169
+ Span { lo : BytePos ( lo) , hi : self . hi , expn_id : self . expn_id }
170
+ }
171
+
166
172
/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
167
173
pub fn substitute_dummy ( self , other : Span ) -> Span {
168
174
if self . source_equal ( & DUMMY_SP ) { other } else { self }
@@ -794,7 +800,7 @@ impl CodeMap {
794
800
/// Creates a new filemap and sets its line information.
795
801
pub fn new_filemap_and_lines ( & self , filename : & str , src : & str ) -> Rc < FileMap > {
796
802
let fm = self . new_filemap ( filename. to_string ( ) , src. to_owned ( ) ) ;
797
- let mut byte_pos: u32 = 0 ;
803
+ let mut byte_pos: u32 = fm . start_pos . 0 ;
798
804
for line in src. lines ( ) {
799
805
// register the start of this line
800
806
fm. next_line ( BytePos ( byte_pos) ) ;
@@ -1126,7 +1132,9 @@ impl CodeMap {
1126
1132
// numbers in Loc are 1-based, so we subtract 1 to get 0-based
1127
1133
// lines.
1128
1134
for line_index in lo. line -1 .. hi. line -1 {
1129
- let line_len = lo. file . get_line ( line_index) . map ( |s| s. len ( ) ) . unwrap_or ( 0 ) ;
1135
+ let line_len = lo. file . get_line ( line_index)
1136
+ . map ( |s| s. chars ( ) . count ( ) )
1137
+ . unwrap_or ( 0 ) ;
1130
1138
lines. push ( LineInfo { line_index : line_index,
1131
1139
start_col : start_col,
1132
1140
end_col : CharPos :: from_usize ( line_len) } ) ;
@@ -1584,13 +1592,13 @@ mod tests {
1584
1592
assert_eq ! ( file_lines. lines[ 0 ] . line_index, 1 ) ;
1585
1593
}
1586
1594
1587
- /// Given a string like " ^ ~~~~~~~~~~~ ", produces a span
1595
+ /// Given a string like " ~ ~~~~~~~~~~~ ", produces a span
1588
1596
/// coverting that range. The idea is that the string has the same
1589
1597
/// length as the input, and we uncover the byte positions. Note
1590
1598
/// that this can span lines and so on.
1591
1599
fn span_from_selection ( input : & str , selection : & str ) -> Span {
1592
1600
assert_eq ! ( input. len( ) , selection. len( ) ) ;
1593
- let left_index = selection. find ( '^ ' ) . unwrap ( ) as u32 ;
1601
+ let left_index = selection. find ( '~ ' ) . unwrap ( ) as u32 ;
1594
1602
let right_index = selection. rfind ( '~' ) . map ( |x|x as u32 ) . unwrap_or ( left_index) ;
1595
1603
Span { lo : BytePos ( left_index) , hi : BytePos ( right_index + 1 ) , expn_id : NO_EXPANSION }
1596
1604
}
@@ -1601,7 +1609,7 @@ mod tests {
1601
1609
fn span_to_snippet_and_lines_spanning_multiple_lines ( ) {
1602
1610
let cm = CodeMap :: new ( ) ;
1603
1611
let inputtext = "aaaaa\n bbbbBB\n CCC\n DDDDDddddd\n eee\n " ;
1604
- let selection = " \n ^ ~\n ~~~\n ~~~~~ \n \n " ;
1612
+ let selection = " \n ~ ~\n ~~~\n ~~~~~ \n \n " ;
1605
1613
cm. new_filemap_and_lines ( "blork.rs" , inputtext) ;
1606
1614
let span = span_from_selection ( inputtext, selection) ;
1607
1615
@@ -1751,73 +1759,4 @@ r"blork2.rs:2:1: 2:12
1751
1759
" ;
1752
1760
assert_eq ! ( sstr, res_str) ;
1753
1761
}
1754
-
1755
- #[ test]
1756
- fn t13 ( ) {
1757
- // Test that collecting multiple spans into line-groups works correctly
1758
- let cm = CodeMap :: new ( ) ;
1759
- let inp = "_aaaaa__bbb\n vv\n w\n x\n y\n z\n cccccc__ddddee__" ;
1760
- let sp1 = " ^~~~~ \n \n \n \n \n \n " ;
1761
- let sp2 = " \n \n \n \n \n ^\n " ;
1762
- let sp3 = " ^~~\n ~~\n \n \n \n \n " ;
1763
- let sp4 = " \n \n \n \n \n \n ^~~~~~ " ;
1764
- let sp5 = " \n \n \n \n \n \n ^~~~ " ;
1765
- let sp6 = " \n \n \n \n \n \n ^~~~ " ;
1766
- let sp_trim = " \n \n \n \n \n \n ^~ " ;
1767
- let sp_merge = " \n \n \n \n \n \n ^~~~~~ " ;
1768
- let sp7 = " \n ^\n \n \n \n \n " ;
1769
- let sp8 = " \n \n ^\n \n \n \n " ;
1770
- let sp9 = " \n \n \n ^\n \n \n " ;
1771
- let sp10 = " \n \n \n \n ^\n \n " ;
1772
-
1773
- let span = |sp, expected| {
1774
- let sp = span_from_selection ( inp, sp) ;
1775
- assert_eq ! ( & cm. span_to_snippet( sp) . unwrap( ) , expected) ;
1776
- sp
1777
- } ;
1778
-
1779
- cm. new_filemap_and_lines ( "blork.rs" , inp) ;
1780
- let sp1 = span ( sp1, "aaaaa" ) ;
1781
- let sp2 = span ( sp2, "z" ) ;
1782
- let sp3 = span ( sp3, "bbb\n vv" ) ;
1783
- let sp4 = span ( sp4, "cccccc" ) ;
1784
- let sp5 = span ( sp5, "dddd" ) ;
1785
- let sp6 = span ( sp6, "ddee" ) ;
1786
- let sp7 = span ( sp7, "v" ) ;
1787
- let sp8 = span ( sp8, "w" ) ;
1788
- let sp9 = span ( sp9, "x" ) ;
1789
- let sp10 = span ( sp10, "y" ) ;
1790
- let sp_trim = span ( sp_trim, "ee" ) ;
1791
- let sp_merge = span ( sp_merge, "ddddee" ) ;
1792
-
1793
- let spans = vec ! [ sp5, sp2, sp4, sp9, sp10, sp7, sp3, sp8, sp1, sp6] ;
1794
-
1795
- macro_rules! check_next {
1796
- ( $groups: expr, $expected: expr) => ( {
1797
- let actual = $groups. next( ) . map( |g|& g. spans[ ..] ) ;
1798
- let expected = $expected;
1799
- println!( "actual:\n {:?}\n " , actual) ;
1800
- println!( "expected:\n {:?}\n " , expected) ;
1801
- assert_eq!( actual, expected. as_ref( ) . map( |x|& x[ ..] ) ) ;
1802
- } ) ;
1803
- }
1804
-
1805
- let _groups = cm. group_spans ( spans. clone ( ) ) ;
1806
- let it = & mut _groups. iter ( ) ;
1807
-
1808
- check_next ! ( it, Some ( [ sp1, sp7, sp8, sp9, sp10, sp2] ) ) ;
1809
- // New group because we're exceeding MAX_HIGHLIGHT_LINES
1810
- check_next ! ( it, Some ( [ sp4, sp_merge] ) ) ;
1811
- check_next ! ( it, Some ( [ sp3] ) ) ;
1812
- check_next ! ( it, None :: <[ Span ; 0 ] >) ;
1813
-
1814
- let _groups = cm. end_group_spans ( spans) ;
1815
- let it = & mut _groups. iter ( ) ;
1816
-
1817
- check_next ! ( it, Some ( [ sp1, sp7, sp8, sp9, sp10, sp2] ) ) ;
1818
- // New group because we're exceeding MAX_HIGHLIGHT_LINES
1819
- check_next ! ( it, Some ( [ sp4, sp5, sp_trim] ) ) ;
1820
- check_next ! ( it, Some ( [ sp3] ) ) ;
1821
- check_next ! ( it, None :: <[ Span ; 0 ] >) ;
1822
- }
1823
1762
}
0 commit comments