diff options
author | Aleksey Kladov <[email protected]> | 2020-04-24 23:57:47 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-04-25 10:59:18 +0100 |
commit | 63a462f37ca584e1a585a69e30823ce25d4d252f (patch) | |
tree | 005ab4d5b50f7d031be9f4056bd1fccd68473587 /crates | |
parent | dc2151085e9b117bc87307bf47edf3d17a170b49 (diff) |
Switch to TryFrom
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/handlers/add_custom_impl.rs | 4 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/add_function.rs | 2 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/add_new.rs | 4 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/merge_match_arms.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_db/src/line_index.rs | 22 | ||||
-rw-r--r-- | crates/ra_ide_db/src/line_index_utils.rs | 11 | ||||
-rw-r--r-- | crates/ra_ide_db/src/search.rs | 4 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 12 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/tokens.rs | 14 | ||||
-rw-r--r-- | crates/ra_syntax/src/fuzz.rs | 11 | ||||
-rw-r--r-- | crates/ra_syntax/src/parsing/lexer.rs | 24 | ||||
-rw-r--r-- | crates/ra_syntax/src/tests.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/validation.rs | 4 |
13 files changed, 63 insertions, 53 deletions
diff --git a/crates/ra_assists/src/handlers/add_custom_impl.rs b/crates/ra_assists/src/handlers/add_custom_impl.rs index 7bb90dba3..4ea26a550 100644 --- a/crates/ra_assists/src/handlers/add_custom_impl.rs +++ b/crates/ra_assists/src/handlers/add_custom_impl.rs | |||
@@ -60,7 +60,6 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> { | |||
60 | .collect::<Vec<SmolStr>>(); | 60 | .collect::<Vec<SmolStr>>(); |
61 | let has_more_derives = !new_attr_input.is_empty(); | 61 | let has_more_derives = !new_attr_input.is_empty(); |
62 | let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string(); | 62 | let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string(); |
63 | let new_attr_input_len = new_attr_input.len(); | ||
64 | 63 | ||
65 | let mut buf = String::new(); | 64 | let mut buf = String::new(); |
66 | buf.push_str("\n\nimpl "); | 65 | buf.push_str("\n\nimpl "); |
@@ -70,8 +69,9 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> { | |||
70 | buf.push_str(" {\n"); | 69 | buf.push_str(" {\n"); |
71 | 70 | ||
72 | let cursor_delta = if has_more_derives { | 71 | let cursor_delta = if has_more_derives { |
72 | let delta = input.syntax().text_range().len() - TextSize::of(&new_attr_input); | ||
73 | edit.replace(input.syntax().text_range(), new_attr_input); | 73 | edit.replace(input.syntax().text_range(), new_attr_input); |
74 | input.syntax().text_range().len() - TextSize::from_usize(new_attr_input_len) | 74 | delta |
75 | } else { | 75 | } else { |
76 | let attr_range = attr.syntax().text_range(); | 76 | let attr_range = attr.syntax().text_range(); |
77 | edit.delete(attr_range); | 77 | edit.delete(attr_range); |
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index 7a8f5705f..6c7456579 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs | |||
@@ -129,7 +129,7 @@ impl FunctionBuilder { | |||
129 | let fn_def = indent_once.increase_indent(fn_def); | 129 | let fn_def = indent_once.increase_indent(fn_def); |
130 | let fn_def = ast::make::add_trailing_newlines(1, fn_def); | 130 | let fn_def = ast::make::add_trailing_newlines(1, fn_def); |
131 | let fn_def = indent.increase_indent(fn_def); | 131 | let fn_def = indent.increase_indent(fn_def); |
132 | (fn_def, it.syntax().text_range().start() + TextSize::from_usize(1)) | 132 | (fn_def, it.syntax().text_range().start() + TextSize::of('{')) |
133 | } | 133 | } |
134 | }; | 134 | }; |
135 | 135 | ||
diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs index 0698cce88..0f9174a29 100644 --- a/crates/ra_assists/src/handlers/add_new.rs +++ b/crates/ra_assists/src/handlers/add_new.rs | |||
@@ -77,13 +77,13 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> { | |||
77 | .text_range() | 77 | .text_range() |
78 | .end(); | 78 | .end(); |
79 | 79 | ||
80 | Some((start, TextSize::from_usize(1))) | 80 | Some((start, TextSize::of("\n"))) |
81 | }) | 81 | }) |
82 | .unwrap_or_else(|| { | 82 | .unwrap_or_else(|| { |
83 | buf = generate_impl_text(&strukt, &buf); | 83 | buf = generate_impl_text(&strukt, &buf); |
84 | let start = strukt.syntax().text_range().end(); | 84 | let start = strukt.syntax().text_range().end(); |
85 | 85 | ||
86 | (start, TextSize::from_usize(3)) | 86 | (start, TextSize::of("\n}\n")) |
87 | }); | 87 | }); |
88 | 88 | ||
89 | edit.set_cursor(start_offset + TextSize::of(&buf) - end_offset); | 89 | edit.set_cursor(start_offset + TextSize::of(&buf) - end_offset); |
diff --git a/crates/ra_assists/src/handlers/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs index cd0416f01..5a77d3dbc 100644 --- a/crates/ra_assists/src/handlers/merge_match_arms.rs +++ b/crates/ra_assists/src/handlers/merge_match_arms.rs | |||
@@ -89,7 +89,7 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> { | |||
89 | 89 | ||
90 | edit.target(current_text_range); | 90 | edit.target(current_text_range); |
91 | edit.set_cursor(match cursor_pos { | 91 | edit.set_cursor(match cursor_pos { |
92 | CursorPos::InExpr(back_offset) => start + TextSize::from_usize(arm.len()) - back_offset, | 92 | CursorPos::InExpr(back_offset) => start + TextSize::of(&arm) - back_offset, |
93 | CursorPos::InPat(offset) => offset, | 93 | CursorPos::InPat(offset) => offset, |
94 | }); | 94 | }); |
95 | edit.replace(TextRange::new(start, end), arm); | 95 | edit.replace(TextRange::new(start, end), arm); |
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs index 81eebc711..00ba95913 100644 --- a/crates/ra_ide_db/src/line_index.rs +++ b/crates/ra_ide_db/src/line_index.rs | |||
@@ -1,8 +1,9 @@ | |||
1 | //! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` | 1 | //! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` |
2 | //! representation. | 2 | //! representation. |
3 | use std::iter; | ||
4 | |||
3 | use ra_syntax::{TextRange, TextSize}; | 5 | use ra_syntax::{TextRange, TextSize}; |
4 | use rustc_hash::FxHashMap; | 6 | use rustc_hash::FxHashMap; |
5 | use std::iter; | ||
6 | use superslice::Ext; | 7 | use superslice::Ext; |
7 | 8 | ||
8 | #[derive(Clone, Debug, PartialEq, Eq)] | 9 | #[derive(Clone, Debug, PartialEq, Eq)] |
@@ -116,12 +117,11 @@ impl LineIndex { | |||
116 | res | 117 | res |
117 | } | 118 | } |
118 | 119 | ||
119 | fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize { | 120 | fn utf16_to_utf8_col(&self, line: u32, mut col: u32) -> TextSize { |
120 | let mut col: TextSize = col.into(); | ||
121 | if let Some(utf16_chars) = self.utf16_lines.get(&line) { | 121 | if let Some(utf16_chars) = self.utf16_lines.get(&line) { |
122 | for c in utf16_chars { | 122 | for c in utf16_chars { |
123 | if col >= c.start { | 123 | if col >= u32::from(c.start) { |
124 | col += c.len() - TextSize::from_usize(1); | 124 | col += u32::from(c.len()) - 1; |
125 | } else { | 125 | } else { |
126 | // From here on, all utf16 characters come *after* the character we are mapping, | 126 | // From here on, all utf16 characters come *after* the character we are mapping, |
127 | // so we don't need to take them into account | 127 | // so we don't need to take them into account |
@@ -130,12 +130,12 @@ impl LineIndex { | |||
130 | } | 130 | } |
131 | } | 131 | } |
132 | 132 | ||
133 | col | 133 | col.into() |
134 | } | 134 | } |
135 | } | 135 | } |
136 | 136 | ||
137 | #[cfg(test)] | 137 | #[cfg(test)] |
138 | mod test_line_index { | 138 | mod tests { |
139 | use super::*; | 139 | use super::*; |
140 | 140 | ||
141 | #[test] | 141 | #[test] |
@@ -224,12 +224,12 @@ const C: char = \"メ メ\"; | |||
224 | assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); | 224 | assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); |
225 | 225 | ||
226 | // UTF-16 to UTF-8 | 226 | // UTF-16 to UTF-8 |
227 | assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from_usize(15)); | 227 | assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15)); |
228 | 228 | ||
229 | assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from_usize(20)); | 229 | assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from(20)); |
230 | assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from_usize(23)); | 230 | assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(23)); |
231 | 231 | ||
232 | assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from_usize(15)); | 232 | assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from(15)); |
233 | } | 233 | } |
234 | 234 | ||
235 | #[test] | 235 | #[test] |
diff --git a/crates/ra_ide_db/src/line_index_utils.rs b/crates/ra_ide_db/src/line_index_utils.rs index f050fe77f..039a12c0d 100644 --- a/crates/ra_ide_db/src/line_index_utils.rs +++ b/crates/ra_ide_db/src/line_index_utils.rs | |||
@@ -7,6 +7,8 @@ | |||
7 | //! Code in this module applies this "to (Line, Column) after edit" | 7 | //! Code in this module applies this "to (Line, Column) after edit" |
8 | //! transformation. | 8 | //! transformation. |
9 | 9 | ||
10 | use std::convert::TryInto; | ||
11 | |||
10 | use ra_syntax::{TextRange, TextSize}; | 12 | use ra_syntax::{TextRange, TextSize}; |
11 | use ra_text_edit::{AtomTextEdit, TextEdit}; | 13 | use ra_text_edit::{AtomTextEdit, TextEdit}; |
12 | 14 | ||
@@ -139,14 +141,15 @@ impl Iterator for OffsetStepIter<'_> { | |||
139 | .text | 141 | .text |
140 | .char_indices() | 142 | .char_indices() |
141 | .filter_map(|(i, c)| { | 143 | .filter_map(|(i, c)| { |
144 | let i: TextSize = i.try_into().unwrap(); | ||
145 | let char_len = TextSize::of(c); | ||
142 | if c == '\n' { | 146 | if c == '\n' { |
143 | let next_offset = self.offset + TextSize::from_usize(i + 1); | 147 | let next_offset = self.offset + i + char_len; |
144 | let next = Step::Newline(next_offset); | 148 | let next = Step::Newline(next_offset); |
145 | Some((next, next_offset)) | 149 | Some((next, next_offset)) |
146 | } else { | 150 | } else { |
147 | let char_len = TextSize::of(c); | 151 | if !c.is_ascii() { |
148 | if char_len > TextSize::from_usize(1) { | 152 | let start = self.offset + i; |
149 | let start = self.offset + TextSize::from_usize(i); | ||
150 | let end = start + char_len; | 153 | let end = start + char_len; |
151 | let next = Step::Utf16Char(TextRange::new(start, end)); | 154 | let next = Step::Utf16Char(TextRange::new(start, end)); |
152 | let next_offset = end; | 155 | let next_offset = end; |
diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index 599b8e562..596f957b8 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs | |||
@@ -4,7 +4,7 @@ | |||
4 | //! get a super-set of matches. Then, we we confirm each match using precise | 4 | //! get a super-set of matches. Then, we we confirm each match using precise |
5 | //! name resolution. | 5 | //! name resolution. |
6 | 6 | ||
7 | use std::mem; | 7 | use std::{convert::TryInto, mem}; |
8 | 8 | ||
9 | use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; | 9 | use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; |
10 | use once_cell::unsync::Lazy; | 10 | use once_cell::unsync::Lazy; |
@@ -207,7 +207,7 @@ impl Definition { | |||
207 | let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); | 207 | let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); |
208 | 208 | ||
209 | for (idx, _) in text.match_indices(pat) { | 209 | for (idx, _) in text.match_indices(pat) { |
210 | let offset = TextSize::from_usize(idx); | 210 | let offset: TextSize = idx.try_into().unwrap(); |
211 | if !search_range.contains_inclusive(offset) { | 211 | if !search_range.contains_inclusive(offset) { |
212 | tested_by!(search_filters_by_range; force); | 212 | tested_by!(search_filters_by_range; force); |
213 | continue; | 213 | continue; |
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index fa9787266..bb28acfd9 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -516,7 +516,7 @@ impl TokenConvertor for Convertor { | |||
516 | fn bump(&mut self) -> Option<(Self::Token, TextRange)> { | 516 | fn bump(&mut self) -> Option<(Self::Token, TextRange)> { |
517 | if let Some((punct, offset)) = self.punct_offset.clone() { | 517 | if let Some((punct, offset)) = self.punct_offset.clone() { |
518 | if usize::from(offset) + 1 < punct.text().len() { | 518 | if usize::from(offset) + 1 < punct.text().len() { |
519 | let offset = offset + TextSize::from_usize(1); | 519 | let offset = offset + TextSize::of('.'); |
520 | let range = punct.text_range(); | 520 | let range = punct.text_range(); |
521 | self.punct_offset = Some((punct.clone(), offset)); | 521 | self.punct_offset = Some((punct.clone(), offset)); |
522 | let range = TextRange::at(range.start() + offset, TextSize::of('.')); | 522 | let range = TextRange::at(range.start() + offset, TextSize::of('.')); |
@@ -532,9 +532,9 @@ impl TokenConvertor for Convertor { | |||
532 | 532 | ||
533 | let token = if curr.kind().is_punct() { | 533 | let token = if curr.kind().is_punct() { |
534 | let range = curr.text_range(); | 534 | let range = curr.text_range(); |
535 | let range = TextRange::at(range.start(), TextSize::from_usize(1)); | 535 | let range = TextRange::at(range.start(), TextSize::of('.')); |
536 | self.punct_offset = Some((curr.clone(), TextSize::from_usize(0))); | 536 | self.punct_offset = Some((curr.clone(), 0.into())); |
537 | (SynToken::Punch(curr, TextSize::from_usize(0)), range) | 537 | (SynToken::Punch(curr, 0.into()), range) |
538 | } else { | 538 | } else { |
539 | self.punct_offset = None; | 539 | self.punct_offset = None; |
540 | let range = curr.text_range(); | 540 | let range = curr.text_range(); |
@@ -546,7 +546,7 @@ impl TokenConvertor for Convertor { | |||
546 | 546 | ||
547 | fn peek(&self) -> Option<Self::Token> { | 547 | fn peek(&self) -> Option<Self::Token> { |
548 | if let Some((punct, mut offset)) = self.punct_offset.clone() { | 548 | if let Some((punct, mut offset)) = self.punct_offset.clone() { |
549 | offset = offset + TextSize::from_usize(1); | 549 | offset = offset + TextSize::of('.'); |
550 | if usize::from(offset) < punct.text().len() { | 550 | if usize::from(offset) < punct.text().len() { |
551 | return Some(SynToken::Punch(punct, offset)); | 551 | return Some(SynToken::Punch(punct, offset)); |
552 | } | 552 | } |
@@ -558,7 +558,7 @@ impl TokenConvertor for Convertor { | |||
558 | } | 558 | } |
559 | 559 | ||
560 | let token = if curr.kind().is_punct() { | 560 | let token = if curr.kind().is_punct() { |
561 | SynToken::Punch(curr, TextSize::from_usize(0)) | 561 | SynToken::Punch(curr, 0.into()) |
562 | } else { | 562 | } else { |
563 | SynToken::Ordiniary(curr) | 563 | SynToken::Ordiniary(curr) |
564 | }; | 564 | }; |
diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index 26b8f9c36..8e04b0bbd 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | //! There are many AstNodes, but only a few tokens, so we hand-write them here. | 1 | //! There are many AstNodes, but only a few tokens, so we hand-write them here. |
2 | 2 | ||
3 | use std::convert::{TryFrom, TryInto}; | ||
4 | |||
3 | use crate::{ | 5 | use crate::{ |
4 | ast::{AstToken, Comment, RawString, String, Whitespace}, | 6 | ast::{AstToken, Comment, RawString, String, Whitespace}, |
5 | TextRange, TextSize, | 7 | TextRange, TextSize, |
@@ -95,8 +97,8 @@ impl QuoteOffsets { | |||
95 | } | 97 | } |
96 | 98 | ||
97 | let start = TextSize::from(0); | 99 | let start = TextSize::from(0); |
98 | let left_quote = TextSize::from_usize(left_quote) + TextSize::of('"'); | 100 | let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"'); |
99 | let right_quote = TextSize::from_usize(right_quote); | 101 | let right_quote = TextSize::try_from(right_quote).unwrap(); |
100 | let end = TextSize::of(literal); | 102 | let end = TextSize::of(literal); |
101 | 103 | ||
102 | let res = QuoteOffsets { | 104 | let res = QuoteOffsets { |
@@ -498,7 +500,7 @@ impl HasFormatSpecifier for String { | |||
498 | let mut res = Vec::with_capacity(text.len()); | 500 | let mut res = Vec::with_capacity(text.len()); |
499 | rustc_lexer::unescape::unescape_str(text, &mut |range, unescaped_char| { | 501 | rustc_lexer::unescape::unescape_str(text, &mut |range, unescaped_char| { |
500 | res.push(( | 502 | res.push(( |
501 | TextRange::new(TextSize::from_usize(range.start), TextSize::from_usize(range.end)) | 503 | TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap()) |
502 | + offset, | 504 | + offset, |
503 | unescaped_char, | 505 | unescaped_char, |
504 | )) | 506 | )) |
@@ -518,11 +520,7 @@ impl HasFormatSpecifier for RawString { | |||
518 | 520 | ||
519 | let mut res = Vec::with_capacity(text.len()); | 521 | let mut res = Vec::with_capacity(text.len()); |
520 | for (idx, c) in text.char_indices() { | 522 | for (idx, c) in text.char_indices() { |
521 | res.push(( | 523 | res.push((TextRange::at(idx.try_into().unwrap(), TextSize::of(c)) + offset, Ok(c))); |
522 | TextRange::new(TextSize::from_usize(idx), TextSize::from_usize(idx + c.len_utf8())) | ||
523 | + offset, | ||
524 | Ok(c), | ||
525 | )); | ||
526 | } | 524 | } |
527 | Some(res) | 525 | Some(res) |
528 | } | 526 | } |
diff --git a/crates/ra_syntax/src/fuzz.rs b/crates/ra_syntax/src/fuzz.rs index 15aad2205..10fbe3176 100644 --- a/crates/ra_syntax/src/fuzz.rs +++ b/crates/ra_syntax/src/fuzz.rs | |||
@@ -1,8 +1,13 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use crate::{validation, AstNode, SourceFile, TextRange, TextSize}; | 3 | use std::{ |
4 | convert::TryInto, | ||
5 | str::{self, FromStr}, | ||
6 | }; | ||
7 | |||
4 | use ra_text_edit::AtomTextEdit; | 8 | use ra_text_edit::AtomTextEdit; |
5 | use std::str::{self, FromStr}; | 9 | |
10 | use crate::{validation, AstNode, SourceFile, TextRange}; | ||
6 | 11 | ||
7 | fn check_file_invariants(file: &SourceFile) { | 12 | fn check_file_invariants(file: &SourceFile) { |
8 | let root = file.syntax(); | 13 | let root = file.syntax(); |
@@ -35,7 +40,7 @@ impl CheckReparse { | |||
35 | let text = format!("{}{}{}", PREFIX, text, SUFFIX); | 40 | let text = format!("{}{}{}", PREFIX, text, SUFFIX); |
36 | text.get(delete_start..delete_start.checked_add(delete_len)?)?; // make sure delete is a valid range | 41 | text.get(delete_start..delete_start.checked_add(delete_len)?)?; // make sure delete is a valid range |
37 | let delete = | 42 | let delete = |
38 | TextRange::at(TextSize::from_usize(delete_start), TextSize::from_usize(delete_len)); | 43 | TextRange::at(delete_start.try_into().unwrap(), delete_len.try_into().unwrap()); |
39 | let edited_text = | 44 | let edited_text = |
40 | format!("{}{}{}", &text[..delete_start], &insert, &text[delete_start + delete_len..]); | 45 | format!("{}{}{}", &text[..delete_start], &insert, &text[delete_start + delete_len..]); |
41 | let edit = AtomTextEdit { delete, insert }; | 46 | let edit = AtomTextEdit { delete, insert }; |
diff --git a/crates/ra_syntax/src/parsing/lexer.rs b/crates/ra_syntax/src/parsing/lexer.rs index 1fdc76d98..f450ef4a2 100644 --- a/crates/ra_syntax/src/parsing/lexer.rs +++ b/crates/ra_syntax/src/parsing/lexer.rs | |||
@@ -1,6 +1,8 @@ | |||
1 | //! Lexer analyzes raw input string and produces lexemes (tokens). | 1 | //! Lexer analyzes raw input string and produces lexemes (tokens). |
2 | //! It is just a bridge to `rustc_lexer`. | 2 | //! It is just a bridge to `rustc_lexer`. |
3 | 3 | ||
4 | use std::convert::TryInto; | ||
5 | |||
4 | use crate::{ | 6 | use crate::{ |
5 | SyntaxError, | 7 | SyntaxError, |
6 | SyntaxKind::{self, *}, | 8 | SyntaxKind::{self, *}, |
@@ -28,18 +30,19 @@ pub fn tokenize(text: &str) -> (Vec<Token>, Vec<SyntaxError>) { | |||
28 | let mut tokens = Vec::new(); | 30 | let mut tokens = Vec::new(); |
29 | let mut errors = Vec::new(); | 31 | let mut errors = Vec::new(); |
30 | 32 | ||
31 | let mut offset: usize = rustc_lexer::strip_shebang(text) | 33 | let mut offset = match rustc_lexer::strip_shebang(text) { |
32 | .map(|shebang_len| { | 34 | Some(shebang_len) => { |
33 | tokens.push(Token { kind: SHEBANG, len: TextSize::from_usize(shebang_len) }); | 35 | tokens.push(Token { kind: SHEBANG, len: shebang_len.try_into().unwrap() }); |
34 | shebang_len | 36 | shebang_len |
35 | }) | 37 | } |
36 | .unwrap_or(0); | 38 | None => 0, |
39 | }; | ||
37 | 40 | ||
38 | let text_without_shebang = &text[offset..]; | 41 | let text_without_shebang = &text[offset..]; |
39 | 42 | ||
40 | for rustc_token in rustc_lexer::tokenize(text_without_shebang) { | 43 | for rustc_token in rustc_lexer::tokenize(text_without_shebang) { |
41 | let token_len = TextSize::from_usize(rustc_token.len); | 44 | let token_len: TextSize = rustc_token.len.try_into().unwrap(); |
42 | let token_range = TextRange::at(TextSize::from_usize(offset), token_len); | 45 | let token_range = TextRange::at(offset.try_into().unwrap(), token_len); |
43 | 46 | ||
44 | let (syntax_kind, err_message) = | 47 | let (syntax_kind, err_message) = |
45 | rustc_token_kind_to_syntax_kind(&rustc_token.kind, &text[token_range]); | 48 | rustc_token_kind_to_syntax_kind(&rustc_token.kind, &text[token_range]); |
@@ -96,10 +99,9 @@ fn lex_first_token(text: &str) -> Option<(Token, Option<SyntaxError>)> { | |||
96 | let rustc_token = rustc_lexer::first_token(text); | 99 | let rustc_token = rustc_lexer::first_token(text); |
97 | let (syntax_kind, err_message) = rustc_token_kind_to_syntax_kind(&rustc_token.kind, text); | 100 | let (syntax_kind, err_message) = rustc_token_kind_to_syntax_kind(&rustc_token.kind, text); |
98 | 101 | ||
99 | let token = Token { kind: syntax_kind, len: TextSize::from_usize(rustc_token.len) }; | 102 | let token = Token { kind: syntax_kind, len: rustc_token.len.try_into().unwrap() }; |
100 | let optional_error = err_message.map(|err_message| { | 103 | let optional_error = err_message |
101 | SyntaxError::new(err_message, TextRange::new(0.into(), TextSize::of(text))) | 104 | .map(|err_message| SyntaxError::new(err_message, TextRange::up_to(TextSize::of(text)))); |
102 | }); | ||
103 | 105 | ||
104 | Some((token, optional_error)) | 106 | Some((token, optional_error)) |
105 | } | 107 | } |
diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs index 4f2b67feb..aee57db62 100644 --- a/crates/ra_syntax/src/tests.rs +++ b/crates/ra_syntax/src/tests.rs | |||
@@ -121,7 +121,7 @@ fn assert_errors_are_absent(errors: &[SyntaxError], path: &Path) { | |||
121 | 121 | ||
122 | fn dump_tokens_and_errors(tokens: &[Token], errors: &[SyntaxError], text: &str) -> String { | 122 | fn dump_tokens_and_errors(tokens: &[Token], errors: &[SyntaxError], text: &str) -> String { |
123 | let mut acc = String::new(); | 123 | let mut acc = String::new(); |
124 | let mut offset = TextSize::from_usize(0); | 124 | let mut offset: TextSize = 0.into(); |
125 | for token in tokens { | 125 | for token in tokens { |
126 | let token_len = token.len; | 126 | let token_len = token.len; |
127 | let token_text = &text[TextRange::at(offset, token.len)]; | 127 | let token_text = &text[TextRange::at(offset, token.len)]; |
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 77d7e132d..5e93895ec 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | mod block; | 3 | mod block; |
4 | 4 | ||
5 | use std::convert::TryFrom; | ||
6 | |||
5 | use rustc_lexer::unescape; | 7 | use rustc_lexer::unescape; |
6 | 8 | ||
7 | use crate::{ | 9 | use crate::{ |
@@ -112,7 +114,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) { | |||
112 | 114 | ||
113 | // FIXME: lift this lambda refactor to `fn` (https://github.com/rust-analyzer/rust-analyzer/pull/2834#discussion_r366199205) | 115 | // FIXME: lift this lambda refactor to `fn` (https://github.com/rust-analyzer/rust-analyzer/pull/2834#discussion_r366199205) |
114 | let mut push_err = |prefix_len, (off, err): (usize, unescape::EscapeError)| { | 116 | let mut push_err = |prefix_len, (off, err): (usize, unescape::EscapeError)| { |
115 | let off = token.text_range().start() + TextSize::from_usize(off + prefix_len); | 117 | let off = token.text_range().start() + TextSize::try_from(off + prefix_len).unwrap(); |
116 | acc.push(SyntaxError::new_at_offset(rustc_unescape_error_to_string(err), off)); | 118 | acc.push(SyntaxError::new_at_offset(rustc_unescape_error_to_string(err), off)); |
117 | }; | 119 | }; |
118 | 120 | ||