mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Cleanups
This commit is contained in:
parent
8843588fca
commit
dc2151085e
4 changed files with 18 additions and 24 deletions
|
@ -1,11 +1,9 @@
|
||||||
//! Renders a bit of code as HTML.
|
//! Renders a bit of code as HTML.
|
||||||
|
|
||||||
use ra_db::SourceDatabase;
|
use ra_db::SourceDatabase;
|
||||||
use ra_syntax::{AstNode, TextSize};
|
use ra_syntax::{AstNode, TextRange, TextSize};
|
||||||
|
|
||||||
use crate::{FileId, RootDatabase};
|
use crate::{syntax_highlighting::highlight, FileId, RootDatabase};
|
||||||
|
|
||||||
use super::highlight;
|
|
||||||
|
|
||||||
pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
|
pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
|
||||||
let parse = db.parse(file_id);
|
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();
|
let mut buf = String::new();
|
||||||
buf.push_str(&STYLE);
|
buf.push_str(&STYLE);
|
||||||
buf.push_str("<pre><code>");
|
buf.push_str("<pre><code>");
|
||||||
// TODO: unusize
|
|
||||||
for range in &ranges {
|
for range in &ranges {
|
||||||
if range.range.start() > prev_pos {
|
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);
|
let text = html_escape(curr);
|
||||||
buf.push_str(&text);
|
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 class = range.highlight.to_string().replace('.', " ");
|
||||||
let color = match (rainbow, range.binding_hash) {
|
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();
|
prev_pos = range.range.end();
|
||||||
}
|
}
|
||||||
// Add the remaining (non-highlighted) text
|
// 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);
|
let text = html_escape(curr);
|
||||||
buf.push_str(&text);
|
buf.push_str(&text);
|
||||||
buf.push_str("</code></pre>");
|
buf.push_str("</code></pre>");
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)`
|
//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)`
|
||||||
//! representation.
|
//! representation.
|
||||||
use std::iter;
|
|
||||||
// TODO: un TextSize
|
|
||||||
use ra_syntax::{TextRange, TextSize};
|
use ra_syntax::{TextRange, TextSize};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
use std::iter;
|
||||||
use superslice::Ext;
|
use superslice::Ext;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
@ -42,7 +41,8 @@ impl LineIndex {
|
||||||
let mut curr_col = 0.into();
|
let mut curr_col = 0.into();
|
||||||
let mut line = 0;
|
let mut line = 0;
|
||||||
for c in text.chars() {
|
for c in text.chars() {
|
||||||
curr_row += TextSize::of(c);
|
let c_len = TextSize::of(c);
|
||||||
|
curr_row += c_len;
|
||||||
if c == '\n' {
|
if c == '\n' {
|
||||||
newlines.push(curr_row);
|
newlines.push(curr_row);
|
||||||
|
|
||||||
|
@ -58,12 +58,11 @@ impl LineIndex {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let char_len = TextSize::of(c);
|
if !c.is_ascii() {
|
||||||
if char_len > TextSize::from_usize(1) {
|
utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + c_len });
|
||||||
utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
curr_col += char_len;
|
curr_col += c_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save any utf-16 characters seen in the last line
|
// 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 {
|
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) {
|
if let Some(utf16_chars) = self.utf16_lines.get(&line) {
|
||||||
let mut correction = 0;
|
|
||||||
for c in utf16_chars {
|
for c in utf16_chars {
|
||||||
if col >= c.end {
|
if c.end <= col {
|
||||||
correction += usize::from(c.len()) - 1;
|
res -= usize::from(c.len()) - 1;
|
||||||
} else {
|
} else {
|
||||||
// From here on, all utf16 characters come *after* the character we are mapping,
|
// From here on, all utf16 characters come *after* the character we are mapping,
|
||||||
// so we don't need to take them into account
|
// so we don't need to take them into account
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
usize::from(col) - correction
|
|
||||||
} else {
|
|
||||||
usize::from(col)
|
|
||||||
}
|
}
|
||||||
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize {
|
fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize {
|
||||||
|
|
|
@ -200,7 +200,8 @@ impl Definition {
|
||||||
|
|
||||||
for (file_id, search_range) in search_scope {
|
for (file_id, search_range) in search_scope {
|
||||||
let text = db.file_text(file_id);
|
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 sema = Semantics::new(db);
|
||||||
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
|
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
|
||||||
|
|
|
@ -592,7 +592,7 @@ pub fn handle_formatting(
|
||||||
let crate_ids = world.analysis().crate_for(file_id)?;
|
let crate_ids = world.analysis().crate_for(file_id)?;
|
||||||
|
|
||||||
let file_line_index = world.analysis().file_line_index(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 {
|
let mut rustfmt = match &world.config.rustfmt {
|
||||||
RustfmtConfig::Rustfmt { extra_args } => {
|
RustfmtConfig::Rustfmt { extra_args } => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue