diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 89e7bef7de8..68756f78a81 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -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, diff --git a/crates/ide_db/src/line_index.rs b/crates/ide_db/src/line_index.rs index 41226305eaa..490c172e3e8 100644 --- a/crates/ide_db/src/line_index.rs +++ b/crates/ide_db/src/line_index.rs @@ -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 } diff --git a/crates/ide_db/src/line_index/tests.rs b/crates/ide_db/src/line_index/tests.rs index 05f7484e8fe..1a109654e4c 100644 --- a/crates/ide_db/src/line_index/tests.rs +++ b/crates/ide_db/src/line_index/tests.rs @@ -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] diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs index 6735b6388cd..edf12faa4d6 100644 --- a/crates/rust-analyzer/src/cli/analysis_bench.rs +++ b/crates/rust-analyzer/src/cli/analysis_bench.rs @@ -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 { diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 3417af687d5..6d6f398f482 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -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) )); diff --git a/crates/rust-analyzer/src/from_proto.rs b/crates/rust-analyzer/src/from_proto.rs index 6676eebf401..82e7cfa8183 100644 --- a/crates/rust-analyzer/src/from_proto.rs +++ b/crates/rust-analyzer/src/from_proto.rs @@ -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 { } 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) } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 8a2b4d9bd99..599b5207c1e 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -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 {