From dc2151085e9b117bc87307bf47edf3d17a170b49 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Apr 2020 00:17:50 +0200 Subject: Cleanups --- crates/ra_ide/src/syntax_highlighting/html.rs | 13 +++++-------- crates/ra_ide_db/src/line_index.rs | 24 ++++++++++-------------- crates/ra_ide_db/src/search.rs | 3 ++- crates/rust-analyzer/src/main_loop/handlers.rs | 2 +- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 4f17d1040..010db4017 100644 --- a/crates/ra_ide/src/syntax_highlighting/html.rs +++ b/crates/ra_ide/src/syntax_highlighting/html.rs @@ -1,11 +1,9 @@ //! Renders a bit of code as HTML. use ra_db::SourceDatabase; -use ra_syntax::{AstNode, TextSize}; +use ra_syntax::{AstNode, TextRange, TextSize}; -use crate::{FileId, RootDatabase}; - -use super::highlight; +use crate::{syntax_highlighting::highlight, FileId, RootDatabase}; pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String { let parse = db.parse(file_id); @@ -27,14 +25,13 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo let mut buf = String::new(); buf.push_str(&STYLE); buf.push_str("
");
-    // TODO: unusize
     for range in &ranges {
         if range.range.start() > prev_pos {
-            let curr = &text[usize::from(prev_pos)..usize::from(range.range.start())];
+            let curr = &text[TextRange::new(prev_pos, range.range.start())];
             let text = html_escape(curr);
             buf.push_str(&text);
         }
-        let curr = &text[usize::from(range.range.start())..usize::from(range.range.end())];
+        let curr = &text[TextRange::new(range.range.start(), range.range.end())];
 
         let class = range.highlight.to_string().replace('.', " ");
         let color = match (rainbow, range.binding_hash) {
@@ -48,7 +45,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
         prev_pos = range.range.end();
     }
     // Add the remaining (non-highlighted) text
-    let curr = &text[usize::from(prev_pos)..];
+    let curr = &text[TextRange::new(prev_pos, TextSize::of(&text))];
     let text = html_escape(curr);
     buf.push_str(&text);
     buf.push_str("
"); diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs index 7794dc9fd..81eebc711 100644 --- a/crates/ra_ide_db/src/line_index.rs +++ b/crates/ra_ide_db/src/line_index.rs @@ -1,9 +1,8 @@ //! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` //! representation. -use std::iter; -// TODO: un TextSize use ra_syntax::{TextRange, TextSize}; use rustc_hash::FxHashMap; +use std::iter; use superslice::Ext; #[derive(Clone, Debug, PartialEq, Eq)] @@ -42,7 +41,8 @@ impl LineIndex { let mut curr_col = 0.into(); let mut line = 0; for c in text.chars() { - curr_row += TextSize::of(c); + let c_len = TextSize::of(c); + curr_row += c_len; if c == '\n' { newlines.push(curr_row); @@ -58,12 +58,11 @@ impl LineIndex { continue; } - let char_len = TextSize::of(c); - if char_len > TextSize::from_usize(1) { - utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len }); + if !c.is_ascii() { + utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + c_len }); } - curr_col += char_len; + curr_col += c_len; } // Save any utf-16 characters seen in the last line @@ -102,22 +101,19 @@ impl LineIndex { } fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize { + let mut res: usize = col.into(); if let Some(utf16_chars) = self.utf16_lines.get(&line) { - let mut correction = 0; for c in utf16_chars { - if col >= c.end { - correction += usize::from(c.len()) - 1; + if c.end <= col { + res -= usize::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 break; } } - - usize::from(col) - correction - } else { - usize::from(col) } + res } fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize { diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index c66de4f42..599b8e562 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs @@ -200,7 +200,8 @@ impl Definition { for (file_id, search_range) in search_scope { let text = db.file_text(file_id); - let search_range = search_range.unwrap_or(TextRange::up_to(TextSize::of(&text))); + let search_range = + search_range.unwrap_or(TextRange::up_to(TextSize::of(text.as_str()))); let sema = Semantics::new(db); let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 381f37f16..2303ebfdb 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -592,7 +592,7 @@ pub fn handle_formatting( let crate_ids = world.analysis().crate_for(file_id)?; let file_line_index = world.analysis().file_line_index(file_id)?; - let end_position = TextSize::of(&file).conv_with(&file_line_index); + let end_position = TextSize::of(file.as_str()).conv_with(&file_line_index); let mut rustfmt = match &world.config.rustfmt { RustfmtConfig::Rustfmt { extra_args } => { -- cgit v1.2.3