Skip to content

Commit dc21510

Browse files
committed
Cleanups
1 parent 8843588 commit dc21510

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

crates/ra_ide/src/syntax_highlighting/html.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//! Renders a bit of code as HTML.
22
33
use ra_db::SourceDatabase;
4-
use ra_syntax::{AstNode, TextSize};
4+
use ra_syntax::{AstNode, TextRange, TextSize};
55

6-
use crate::{FileId, RootDatabase};
7-
8-
use super::highlight;
6+
use crate::{syntax_highlighting::highlight, FileId, RootDatabase};
97

108
pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
119
let parse = db.parse(file_id);
@@ -27,14 +25,13 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
2725
let mut buf = String::new();
2826
buf.push_str(&STYLE);
2927
buf.push_str("<pre><code>");
30-
// TODO: unusize
3128
for range in &ranges {
3229
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())];
3431
let text = html_escape(curr);
3532
buf.push_str(&text);
3633
}
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())];
3835

3936
let class = range.highlight.to_string().replace('.', " ");
4037
let color = match (rainbow, range.binding_hash) {
@@ -48,7 +45,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
4845
prev_pos = range.range.end();
4946
}
5047
// 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))];
5249
let text = html_escape(curr);
5350
buf.push_str(&text);
5451
buf.push_str("</code></pre>");

crates/ra_ide_db/src/line_index.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)`
22
//! representation.
3-
use std::iter;
4-
// TODO: un TextSize
53
use ra_syntax::{TextRange, TextSize};
64
use rustc_hash::FxHashMap;
5+
use std::iter;
76
use superslice::Ext;
87

98
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -42,7 +41,8 @@ impl LineIndex {
4241
let mut curr_col = 0.into();
4342
let mut line = 0;
4443
for c in text.chars() {
45-
curr_row += TextSize::of(c);
44+
let c_len = TextSize::of(c);
45+
curr_row += c_len;
4646
if c == '\n' {
4747
newlines.push(curr_row);
4848

@@ -58,12 +58,11 @@ impl LineIndex {
5858
continue;
5959
}
6060

61-
let char_len = TextSize::of(c);
62-
if char_len > TextSize::from_usize(1) {
63-
utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len });
61+
if !c.is_ascii() {
62+
utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + c_len });
6463
}
6564

66-
curr_col += char_len;
65+
curr_col += c_len;
6766
}
6867

6968
// Save any utf-16 characters seen in the last line
@@ -102,22 +101,19 @@ impl LineIndex {
102101
}
103102

104103
fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize {
104+
let mut res: usize = col.into();
105105
if let Some(utf16_chars) = self.utf16_lines.get(&line) {
106-
let mut correction = 0;
107106
for c in utf16_chars {
108-
if col >= c.end {
109-
correction += usize::from(c.len()) - 1;
107+
if c.end <= col {
108+
res -= usize::from(c.len()) - 1;
110109
} else {
111110
// From here on, all utf16 characters come *after* the character we are mapping,
112111
// so we don't need to take them into account
113112
break;
114113
}
115114
}
116-
117-
usize::from(col) - correction
118-
} else {
119-
usize::from(col)
120115
}
116+
res
121117
}
122118

123119
fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize {

crates/ra_ide_db/src/search.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ impl Definition {
200200

201201
for (file_id, search_range) in search_scope {
202202
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())));
204205

205206
let sema = Semantics::new(db);
206207
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ pub fn handle_formatting(
592592
let crate_ids = world.analysis().crate_for(file_id)?;
593593

594594
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);
596596

597597
let mut rustfmt = match &world.config.rustfmt {
598598
RustfmtConfig::Rustfmt { extra_args } => {

0 commit comments

Comments
 (0)