This commit is contained in:
Aleksey Kladov 2020-04-25 00:17:50 +02:00
parent 8843588fca
commit dc2151085e
4 changed files with 18 additions and 24 deletions

View File

@ -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("<pre><code>");
// 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("</code></pre>");

View File

@ -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 {

View File

@ -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());

View File

@ -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 } => {