mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-25 13:24:22 +00:00
Prepare for utf-8 offsets
This commit is contained in:
parent
cc49502ab4
commit
2cb4ac9eb4
@ -95,7 +95,7 @@ pub use ide_db::{
|
||||
},
|
||||
call_info::CallInfo,
|
||||
label::Label,
|
||||
line_index::{LineCol, LineIndex},
|
||||
line_index::{LineColUtf16, LineIndex},
|
||||
search::{ReferenceAccess, SearchScope},
|
||||
source_change::{FileSystemEdit, SourceChange},
|
||||
symbol_index::Query,
|
||||
|
@ -15,11 +15,11 @@ pub struct LineIndex {
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct LineCol {
|
||||
pub struct LineColUtf16 {
|
||||
/// Zero-based
|
||||
pub line: u32,
|
||||
/// Zero-based
|
||||
pub col_utf16: u32,
|
||||
pub col: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
@ -88,17 +88,17 @@ impl LineIndex {
|
||||
LineIndex { newlines, utf16_lines }
|
||||
}
|
||||
|
||||
pub fn line_col(&self, offset: TextSize) -> LineCol {
|
||||
pub fn line_col(&self, offset: TextSize) -> LineColUtf16 {
|
||||
let line = partition_point(&self.newlines, |&it| it <= offset) - 1;
|
||||
let line_start_offset = self.newlines[line];
|
||||
let col = offset - line_start_offset;
|
||||
|
||||
LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 }
|
||||
LineColUtf16 { line: line as u32, col: self.utf8_to_utf16_col(line as u32, col) as u32 }
|
||||
}
|
||||
|
||||
pub fn offset(&self, line_col: LineCol) -> TextSize {
|
||||
pub fn offset(&self, line_col: LineColUtf16) -> TextSize {
|
||||
//FIXME: return Result
|
||||
let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16);
|
||||
let col = self.utf16_to_utf8_col(line_col.line, line_col.col);
|
||||
self.newlines[line_col.line as usize] + col
|
||||
}
|
||||
|
||||
|
@ -4,23 +4,23 @@ use super::*;
|
||||
fn test_line_index() {
|
||||
let text = "hello\nworld";
|
||||
let index = LineIndex::new(text);
|
||||
assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 });
|
||||
assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 });
|
||||
assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 });
|
||||
assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 });
|
||||
assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 });
|
||||
assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 });
|
||||
assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 });
|
||||
assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 });
|
||||
assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 });
|
||||
assert_eq!(index.line_col(0.into()), LineColUtf16 { line: 0, col: 0 });
|
||||
assert_eq!(index.line_col(1.into()), LineColUtf16 { line: 0, col: 1 });
|
||||
assert_eq!(index.line_col(5.into()), LineColUtf16 { line: 0, col: 5 });
|
||||
assert_eq!(index.line_col(6.into()), LineColUtf16 { line: 1, col: 0 });
|
||||
assert_eq!(index.line_col(7.into()), LineColUtf16 { line: 1, col: 1 });
|
||||
assert_eq!(index.line_col(8.into()), LineColUtf16 { line: 1, col: 2 });
|
||||
assert_eq!(index.line_col(10.into()), LineColUtf16 { line: 1, col: 4 });
|
||||
assert_eq!(index.line_col(11.into()), LineColUtf16 { line: 1, col: 5 });
|
||||
assert_eq!(index.line_col(12.into()), LineColUtf16 { line: 1, col: 6 });
|
||||
|
||||
let text = "\nhello\nworld";
|
||||
let index = LineIndex::new(text);
|
||||
assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 });
|
||||
assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 });
|
||||
assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 });
|
||||
assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 });
|
||||
assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 });
|
||||
assert_eq!(index.line_col(0.into()), LineColUtf16 { line: 0, col: 0 });
|
||||
assert_eq!(index.line_col(1.into()), LineColUtf16 { line: 1, col: 0 });
|
||||
assert_eq!(index.line_col(2.into()), LineColUtf16 { line: 1, col: 1 });
|
||||
assert_eq!(index.line_col(6.into()), LineColUtf16 { line: 1, col: 5 });
|
||||
assert_eq!(index.line_col(7.into()), LineColUtf16 { line: 2, col: 0 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -5,7 +5,7 @@ use std::{env, path::PathBuf, str::FromStr, sync::Arc, time::Instant};
|
||||
use anyhow::{bail, format_err, Result};
|
||||
use hir::PrefixKind;
|
||||
use ide::{
|
||||
Analysis, AnalysisHost, Change, CompletionConfig, DiagnosticsConfig, FilePosition, LineCol,
|
||||
Analysis, AnalysisHost, Change, CompletionConfig, DiagnosticsConfig, FilePosition, LineColUtf16,
|
||||
};
|
||||
use ide_db::{
|
||||
base_db::{
|
||||
@ -97,7 +97,7 @@ impl BenchCmd {
|
||||
let offset = host
|
||||
.analysis()
|
||||
.file_line_index(file_id)?
|
||||
.offset(LineCol { line: pos.line - 1, col_utf16: pos.column });
|
||||
.offset(LineColUtf16 { line: pos.line - 1, col: pos.column });
|
||||
let file_position = FilePosition { file_id, offset };
|
||||
|
||||
if is_completion {
|
||||
|
@ -218,9 +218,9 @@ impl AnalysisStatsCmd {
|
||||
bar.println(format!(
|
||||
"{}:{}-{}:{}: {}",
|
||||
start.line + 1,
|
||||
start.col_utf16,
|
||||
start.col,
|
||||
end.line + 1,
|
||||
end.col_utf16,
|
||||
end.col,
|
||||
ty.display(db)
|
||||
));
|
||||
} else {
|
||||
@ -250,9 +250,9 @@ impl AnalysisStatsCmd {
|
||||
"{} {}:{}-{}:{}: Expected {}, got {}",
|
||||
path,
|
||||
start.line + 1,
|
||||
start.col_utf16,
|
||||
start.col,
|
||||
end.line + 1,
|
||||
end.col_utf16,
|
||||
end.col,
|
||||
mismatch.expected.display(db),
|
||||
mismatch.actual.display(db)
|
||||
));
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Conversion lsp_types types to rust-analyzer specific ones.
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use ide::{Annotation, AnnotationKind, AssistKind, LineCol, LineIndex};
|
||||
use ide::{Annotation, AnnotationKind, AssistKind, LineColUtf16, LineIndex};
|
||||
use ide_db::base_db::{FileId, FilePosition, FileRange};
|
||||
use syntax::{TextRange, TextSize};
|
||||
use vfs::AbsPathBuf;
|
||||
@ -18,7 +18,7 @@ pub(crate) fn vfs_path(url: &lsp_types::Url) -> Result<vfs::VfsPath> {
|
||||
}
|
||||
|
||||
pub(crate) fn offset(line_index: &LineIndex, position: lsp_types::Position) -> TextSize {
|
||||
let line_col = LineCol { line: position.line as u32, col_utf16: position.character as u32 };
|
||||
let line_col = LineColUtf16 { line: position.line as u32, col: position.character as u32 };
|
||||
line_index.offset(line_col)
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ use crate::{
|
||||
|
||||
pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position {
|
||||
let line_col = line_index.line_col(offset);
|
||||
lsp_types::Position::new(line_col.line, line_col.col_utf16)
|
||||
lsp_types::Position::new(line_col.line, line_col.col)
|
||||
}
|
||||
|
||||
pub(crate) fn range(line_index: &LineIndex, range: TextRange) -> lsp_types::Range {
|
||||
|
Loading…
Reference in New Issue
Block a user