aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs21
1 files changed, 16 insertions, 5 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index ffd796ae2..d370dfb34 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -58,16 +58,21 @@ fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize
58 let text = db.file_text(file_id); 58 let text = db.file_text(file_id);
59 let mut line_num = 1; 59 let mut line_num = 1;
60 60
61 let pos = pos.to_usize();
62 if pos > text.len() {
63 // FIXME: `pos` at the moment could be an offset inside the "wrong" file
64 // in this case, when we know it's wrong, we return a dummy value
65 return 0;
66 }
61 // Count line end 67 // Count line end
62 for (i, c) in text.chars().enumerate() { 68 for (i, c) in text.chars().enumerate() {
63 if i == pos.to_usize() { 69 if i == pos {
64 break; 70 break;
65 } 71 }
66 if c == '\n' { 72 if c == '\n' {
67 line_num += 1; 73 line_num += 1;
68 } 74 }
69 } 75 }
70
71 line_num 76 line_num
72} 77}
73 78
@@ -119,15 +124,21 @@ fn to_col_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize
119 // FIXME: Use expansion info 124 // FIXME: Use expansion info
120 let file_id = file.original_file(db); 125 let file_id = file.original_file(db);
121 let text = db.file_text(file_id); 126 let text = db.file_text(file_id);
122 let mut col_num = 1;
123 127
124 for c in text[..pos.to_usize()].chars().rev() { 128 let pos = pos.to_usize();
129 if pos > text.len() {
130 // FIXME: `pos` at the moment could be an offset inside the "wrong" file
131 // in this case we return a dummy value so that we don't `panic!`
132 return 0;
133 }
134
135 let mut col_num = 1;
136 for c in text[..pos].chars().rev() {
125 if c == '\n' { 137 if c == '\n' {
126 break; 138 break;
127 } 139 }
128 col_num = col_num + 1; 140 col_num = col_num + 1;
129 } 141 }
130
131 col_num 142 col_num
132} 143}
133 144