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_ide_db/src/line_index.rs | 22 +++++++++++----------- crates/ra_ide_db/src/line_index_utils.rs | 11 +++++++---- crates/ra_ide_db/src/search.rs | 4 ++-- 3 files changed, 20 insertions(+), 17 deletions(-) (limited to 'crates/ra_ide_db/src') 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 @@ //! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` //! representation. +use std::iter; + use ra_syntax::{TextRange, TextSize}; use rustc_hash::FxHashMap; -use std::iter; use superslice::Ext; #[derive(Clone, Debug, PartialEq, Eq)] @@ -116,12 +117,11 @@ impl LineIndex { res } - fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize { - let mut col: TextSize = col.into(); + fn utf16_to_utf8_col(&self, line: u32, mut col: u32) -> TextSize { if let Some(utf16_chars) = self.utf16_lines.get(&line) { for c in utf16_chars { - if col >= c.start { - col += c.len() - TextSize::from_usize(1); + if col >= u32::from(c.start) { + col += u32::from(c.len()) - 1; } else { // From here on, all utf16 characters come *after* the character we are mapping, // so we don't need to take them into account @@ -130,12 +130,12 @@ impl LineIndex { } } - col + col.into() } } #[cfg(test)] -mod test_line_index { +mod tests { use super::*; #[test] @@ -224,12 +224,12 @@ const C: char = \"メ メ\"; assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); // UTF-16 to UTF-8 - assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from_usize(15)); + assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15)); - assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from_usize(20)); - assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from_usize(23)); + assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from(20)); + assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(23)); - assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from_usize(15)); + assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from(15)); } #[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 @@ //! Code in this module applies this "to (Line, Column) after edit" //! transformation. +use std::convert::TryInto; + use ra_syntax::{TextRange, TextSize}; use ra_text_edit::{AtomTextEdit, TextEdit}; @@ -139,14 +141,15 @@ impl Iterator for OffsetStepIter<'_> { .text .char_indices() .filter_map(|(i, c)| { + let i: TextSize = i.try_into().unwrap(); + let char_len = TextSize::of(c); if c == '\n' { - let next_offset = self.offset + TextSize::from_usize(i + 1); + let next_offset = self.offset + i + char_len; let next = Step::Newline(next_offset); Some((next, next_offset)) } else { - let char_len = TextSize::of(c); - if char_len > TextSize::from_usize(1) { - let start = self.offset + TextSize::from_usize(i); + if !c.is_ascii() { + let start = self.offset + i; let end = start + char_len; let next = Step::Utf16Char(TextRange::new(start, end)); 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 @@ //! get a super-set of matches. Then, we we confirm each match using precise //! name resolution. -use std::mem; +use std::{convert::TryInto, mem}; use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; use once_cell::unsync::Lazy; @@ -207,7 +207,7 @@ impl Definition { let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); for (idx, _) in text.match_indices(pat) { - let offset = TextSize::from_usize(idx); + let offset: TextSize = idx.try_into().unwrap(); if !search_range.contains_inclusive(offset) { tested_by!(search_filters_by_range; force); continue; -- cgit v1.2.3