diff options
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/html.rs | 13 | ||||
-rw-r--r-- | crates/ra_ide_db/src/line_index.rs | 24 | ||||
-rw-r--r-- | crates/ra_ide_db/src/search.rs | 3 | ||||
-rw-r--r-- | 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 @@ | |||
1 | //! Renders a bit of code as HTML. | 1 | //! Renders a bit of code as HTML. |
2 | 2 | ||
3 | use ra_db::SourceDatabase; | 3 | use ra_db::SourceDatabase; |
4 | use ra_syntax::{AstNode, TextSize}; | 4 | use ra_syntax::{AstNode, TextRange, TextSize}; |
5 | 5 | ||
6 | use crate::{FileId, RootDatabase}; | 6 | use crate::{syntax_highlighting::highlight, FileId, RootDatabase}; |
7 | |||
8 | use super::highlight; | ||
9 | 7 | ||
10 | pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String { | 8 | pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String { |
11 | let parse = db.parse(file_id); | 9 | let parse = db.parse(file_id); |
@@ -27,14 +25,13 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
27 | let mut buf = String::new(); | 25 | let mut buf = String::new(); |
28 | buf.push_str(&STYLE); | 26 | buf.push_str(&STYLE); |
29 | buf.push_str("<pre><code>"); | 27 | buf.push_str("<pre><code>"); |
30 | // TODO: unusize | ||
31 | for range in &ranges { | 28 | for range in &ranges { |
32 | if range.range.start() > prev_pos { | 29 | if range.range.start() > prev_pos { |
33 | let curr = &text[usize::from(prev_pos)..usize::from(range.range.start())]; | 30 | let curr = &text[TextRange::new(prev_pos, range.range.start())]; |
34 | let text = html_escape(curr); | 31 | let text = html_escape(curr); |
35 | buf.push_str(&text); | 32 | buf.push_str(&text); |
36 | } | 33 | } |
37 | let curr = &text[usize::from(range.range.start())..usize::from(range.range.end())]; | 34 | let curr = &text[TextRange::new(range.range.start(), range.range.end())]; |
38 | 35 | ||
39 | let class = range.highlight.to_string().replace('.', " "); | 36 | let class = range.highlight.to_string().replace('.', " "); |
40 | let color = match (rainbow, range.binding_hash) { | 37 | let color = match (rainbow, range.binding_hash) { |
@@ -48,7 +45,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
48 | prev_pos = range.range.end(); | 45 | prev_pos = range.range.end(); |
49 | } | 46 | } |
50 | // Add the remaining (non-highlighted) text | 47 | // Add the remaining (non-highlighted) text |
51 | let curr = &text[usize::from(prev_pos)..]; | 48 | let curr = &text[TextRange::new(prev_pos, TextSize::of(&text))]; |
52 | let text = html_escape(curr); | 49 | let text = html_escape(curr); |
53 | buf.push_str(&text); | 50 | buf.push_str(&text); |
54 | buf.push_str("</code></pre>"); | 51 | buf.push_str("</code></pre>"); |
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 @@ | |||
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 | // TODO: un TextSize | ||
5 | use ra_syntax::{TextRange, TextSize}; | 3 | use ra_syntax::{TextRange, TextSize}; |
6 | use rustc_hash::FxHashMap; | 4 | use rustc_hash::FxHashMap; |
5 | use std::iter; | ||
7 | use superslice::Ext; | 6 | use superslice::Ext; |
8 | 7 | ||
9 | #[derive(Clone, Debug, PartialEq, Eq)] | 8 | #[derive(Clone, Debug, PartialEq, Eq)] |
@@ -42,7 +41,8 @@ impl LineIndex { | |||
42 | let mut curr_col = 0.into(); | 41 | let mut curr_col = 0.into(); |
43 | let mut line = 0; | 42 | let mut line = 0; |
44 | for c in text.chars() { | 43 | for c in text.chars() { |
45 | curr_row += TextSize::of(c); | 44 | let c_len = TextSize::of(c); |
45 | curr_row += c_len; | ||
46 | if c == '\n' { | 46 | if c == '\n' { |
47 | newlines.push(curr_row); | 47 | newlines.push(curr_row); |
48 | 48 | ||
@@ -58,12 +58,11 @@ impl LineIndex { | |||
58 | continue; | 58 | continue; |
59 | } | 59 | } |
60 | 60 | ||
61 | let char_len = TextSize::of(c); | 61 | if !c.is_ascii() { |
62 | if char_len > TextSize::from_usize(1) { | 62 | utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + c_len }); |
63 | utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len }); | ||
64 | } | 63 | } |
65 | 64 | ||
66 | curr_col += char_len; | 65 | curr_col += c_len; |
67 | } | 66 | } |
68 | 67 | ||
69 | // Save any utf-16 characters seen in the last line | 68 | // Save any utf-16 characters seen in the last line |
@@ -102,22 +101,19 @@ impl LineIndex { | |||
102 | } | 101 | } |
103 | 102 | ||
104 | fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize { | 103 | fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize { |
104 | let mut res: usize = col.into(); | ||
105 | if let Some(utf16_chars) = self.utf16_lines.get(&line) { | 105 | if let Some(utf16_chars) = self.utf16_lines.get(&line) { |
106 | let mut correction = 0; | ||
107 | for c in utf16_chars { | 106 | for c in utf16_chars { |
108 | if col >= c.end { | 107 | if c.end <= col { |
109 | correction += usize::from(c.len()) - 1; | 108 | res -= usize::from(c.len()) - 1; |
110 | } else { | 109 | } else { |
111 | // From here on, all utf16 characters come *after* the character we are mapping, | 110 | // From here on, all utf16 characters come *after* the character we are mapping, |
112 | // so we don't need to take them into account | 111 | // so we don't need to take them into account |
113 | break; | 112 | break; |
114 | } | 113 | } |
115 | } | 114 | } |
116 | |||
117 | usize::from(col) - correction | ||
118 | } else { | ||
119 | usize::from(col) | ||
120 | } | 115 | } |
116 | res | ||
121 | } | 117 | } |
122 | 118 | ||
123 | fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize { | 119 | 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 { | |||
200 | 200 | ||
201 | for (file_id, search_range) in search_scope { | 201 | for (file_id, search_range) in search_scope { |
202 | let text = db.file_text(file_id); | 202 | let text = db.file_text(file_id); |
203 | let search_range = search_range.unwrap_or(TextRange::up_to(TextSize::of(&text))); | 203 | let search_range = |
204 | search_range.unwrap_or(TextRange::up_to(TextSize::of(text.as_str()))); | ||
204 | 205 | ||
205 | let sema = Semantics::new(db); | 206 | let sema = Semantics::new(db); |
206 | let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); | 207 | 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( | |||
592 | let crate_ids = world.analysis().crate_for(file_id)?; | 592 | let crate_ids = world.analysis().crate_for(file_id)?; |
593 | 593 | ||
594 | let file_line_index = world.analysis().file_line_index(file_id)?; | 594 | let file_line_index = world.analysis().file_line_index(file_id)?; |
595 | let end_position = TextSize::of(&file).conv_with(&file_line_index); | 595 | let end_position = TextSize::of(file.as_str()).conv_with(&file_line_index); |
596 | 596 | ||
597 | let mut rustfmt = match &world.config.rustfmt { | 597 | let mut rustfmt = match &world.config.rustfmt { |
598 | RustfmtConfig::Rustfmt { extra_args } => { | 598 | RustfmtConfig::Rustfmt { extra_args } => { |