From b1d5817dd18b7b5fc102a63b084b1ee7ff4f9996 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Apr 2020 23:40:41 +0200 Subject: Convert code to text-size --- crates/ra_syntax/src/ast/tokens.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'crates/ra_syntax/src/ast') diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index aa34b682d..26b8f9c36 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs @@ -2,7 +2,7 @@ use crate::{ ast::{AstToken, Comment, RawString, String, Whitespace}, - TextRange, TextUnit, + TextRange, TextSize, }; impl Comment { @@ -94,14 +94,14 @@ impl QuoteOffsets { return None; } - let start = TextUnit::from(0); - let left_quote = TextUnit::from_usize(left_quote) + TextUnit::of_char('"'); - let right_quote = TextUnit::from_usize(right_quote); - let end = TextUnit::of_str(literal); + let start = TextSize::from(0); + let left_quote = TextSize::from_usize(left_quote) + TextSize::of('"'); + let right_quote = TextSize::from_usize(right_quote); + let end = TextSize::of(literal); let res = QuoteOffsets { - quotes: [TextRange::from_to(start, left_quote), TextRange::from_to(right_quote, end)], - contents: TextRange::from_to(left_quote, right_quote), + quotes: [TextRange::new(start, left_quote), TextRange::new(right_quote, end)], + contents: TextRange::new(left_quote, right_quote), }; Some(res) } @@ -168,7 +168,7 @@ impl HasStringValue for RawString { impl RawString { pub fn map_range_up(&self, range: TextRange) -> Option { let contents_range = self.text_range_between_quotes()?; - assert!(range.is_subrange(&TextRange::offset_len(0.into(), contents_range.len()))); + assert!(TextRange::up_to(contents_range.len()).contains_range(range)); Some(range + contents_range.start()) } } @@ -459,7 +459,7 @@ pub trait HasFormatSpecifier: AstToken { while let Some((r, Ok(next_char))) = chars.peek() { if next_char.is_ascii_digit() { chars.next(); - range = range.extend_to(r); + range = range.cover(*r); } else { break; } @@ -477,7 +477,7 @@ pub trait HasFormatSpecifier: AstToken { while let Some((r, Ok(next_char))) = chars.peek() { if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() { chars.next(); - range = range.extend_to(r); + range = range.cover(*r); } else { break; } @@ -498,10 +498,8 @@ impl HasFormatSpecifier for String { let mut res = Vec::with_capacity(text.len()); rustc_lexer::unescape::unescape_str(text, &mut |range, unescaped_char| { res.push(( - TextRange::from_to( - TextUnit::from_usize(range.start), - TextUnit::from_usize(range.end), - ) + offset, + TextRange::new(TextSize::from_usize(range.start), TextSize::from_usize(range.end)) + + offset, unescaped_char, )) }); @@ -521,10 +519,8 @@ impl HasFormatSpecifier for RawString { let mut res = Vec::with_capacity(text.len()); for (idx, c) in text.char_indices() { res.push(( - TextRange::from_to( - TextUnit::from_usize(idx), - TextUnit::from_usize(idx + c.len_utf8()), - ) + offset, + TextRange::new(TextSize::from_usize(idx), TextSize::from_usize(idx + c.len_utf8())) + + offset, Ok(c), )); } -- cgit v1.2.3 From 63a462f37ca584e1a585a69e30823ce25d4d252f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Apr 2020 00:57:47 +0200 Subject: Switch to TryFrom --- crates/ra_syntax/src/ast/tokens.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'crates/ra_syntax/src/ast') 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 @@ //! There are many AstNodes, but only a few tokens, so we hand-write them here. +use std::convert::{TryFrom, TryInto}; + use crate::{ ast::{AstToken, Comment, RawString, String, Whitespace}, TextRange, TextSize, @@ -95,8 +97,8 @@ impl QuoteOffsets { } let start = TextSize::from(0); - let left_quote = TextSize::from_usize(left_quote) + TextSize::of('"'); - let right_quote = TextSize::from_usize(right_quote); + let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"'); + let right_quote = TextSize::try_from(right_quote).unwrap(); let end = TextSize::of(literal); let res = QuoteOffsets { @@ -498,7 +500,7 @@ impl HasFormatSpecifier for String { let mut res = Vec::with_capacity(text.len()); rustc_lexer::unescape::unescape_str(text, &mut |range, unescaped_char| { res.push(( - TextRange::new(TextSize::from_usize(range.start), TextSize::from_usize(range.end)) + TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap()) + offset, unescaped_char, )) @@ -518,11 +520,7 @@ impl HasFormatSpecifier for RawString { let mut res = Vec::with_capacity(text.len()); for (idx, c) in text.char_indices() { - res.push(( - TextRange::new(TextSize::from_usize(idx), TextSize::from_usize(idx + c.len_utf8())) - + offset, - Ok(c), - )); + res.push((TextRange::at(idx.try_into().unwrap(), TextSize::of(c)) + offset, Ok(c))); } Some(res) } -- cgit v1.2.3