diff options
-rw-r--r-- | crates/ide/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ide_db/src/line_index.rs | 25 | ||||
-rw-r--r-- | crates/ide_db/src/line_index/tests.rs | 4 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cli/analysis_bench.rs | 4 | ||||
-rw-r--r-- | crates/rust-analyzer/src/from_proto.rs | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/to_proto.rs | 1 |
6 files changed, 27 insertions, 10 deletions
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 68756f78a..a2c8db505 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs | |||
@@ -95,7 +95,7 @@ pub use ide_db::{ | |||
95 | }, | 95 | }, |
96 | call_info::CallInfo, | 96 | call_info::CallInfo, |
97 | label::Label, | 97 | label::Label, |
98 | line_index::{LineColUtf16, LineIndex}, | 98 | line_index::{LineCol, LineColUtf16, LineIndex}, |
99 | search::{ReferenceAccess, SearchScope}, | 99 | search::{ReferenceAccess, SearchScope}, |
100 | source_change::{FileSystemEdit, SourceChange}, | 100 | source_change::{FileSystemEdit, SourceChange}, |
101 | symbol_index::Query, | 101 | symbol_index::Query, |
diff --git a/crates/ide_db/src/line_index.rs b/crates/ide_db/src/line_index.rs index 490c172e3..8e9d8cca2 100644 --- a/crates/ide_db/src/line_index.rs +++ b/crates/ide_db/src/line_index.rs | |||
@@ -22,6 +22,14 @@ pub struct LineColUtf16 { | |||
22 | pub col: u32, | 22 | pub col: u32, |
23 | } | 23 | } |
24 | 24 | ||
25 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
26 | pub struct LineCol { | ||
27 | /// Zero-based | ||
28 | pub line: u32, | ||
29 | /// Zero-based utf8 offset | ||
30 | pub col: u32, | ||
31 | } | ||
32 | |||
25 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | 33 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] |
26 | pub(crate) struct Utf16Char { | 34 | pub(crate) struct Utf16Char { |
27 | /// Start offset of a character inside a line, zero-based | 35 | /// Start offset of a character inside a line, zero-based |
@@ -88,18 +96,25 @@ impl LineIndex { | |||
88 | LineIndex { newlines, utf16_lines } | 96 | LineIndex { newlines, utf16_lines } |
89 | } | 97 | } |
90 | 98 | ||
91 | pub fn line_col(&self, offset: TextSize) -> LineColUtf16 { | 99 | pub fn line_col(&self, offset: TextSize) -> LineCol { |
92 | let line = partition_point(&self.newlines, |&it| it <= offset) - 1; | 100 | let line = partition_point(&self.newlines, |&it| it <= offset) - 1; |
93 | let line_start_offset = self.newlines[line]; | 101 | let line_start_offset = self.newlines[line]; |
94 | let col = offset - line_start_offset; | 102 | let col = offset - line_start_offset; |
103 | LineCol { line: line as u32, col: col.into() } | ||
104 | } | ||
105 | |||
106 | pub fn offset(&self, line_col: LineCol) -> TextSize { | ||
107 | self.newlines[line_col.line as usize] + TextSize::from(line_col.col) | ||
108 | } | ||
95 | 109 | ||
96 | LineColUtf16 { line: line as u32, col: self.utf8_to_utf16_col(line as u32, col) as u32 } | 110 | pub fn to_utf16(&self, line_col: LineCol) -> LineColUtf16 { |
111 | let col = self.utf8_to_utf16_col(line_col.line, line_col.col.into()); | ||
112 | LineColUtf16 { line: line_col.line, col: col as u32 } | ||
97 | } | 113 | } |
98 | 114 | ||
99 | pub fn offset(&self, line_col: LineColUtf16) -> TextSize { | 115 | pub fn to_utf8(&self, line_col: LineColUtf16) -> LineCol { |
100 | //FIXME: return Result | ||
101 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col); | 116 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col); |
102 | self.newlines[line_col.line as usize] + col | 117 | LineCol { line: line_col.line, col: col.into() } |
103 | } | 118 | } |
104 | 119 | ||
105 | pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { | 120 | pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { |
diff --git a/crates/ide_db/src/line_index/tests.rs b/crates/ide_db/src/line_index/tests.rs index af51d7348..09f3bca62 100644 --- a/crates/ide_db/src/line_index/tests.rs +++ b/crates/ide_db/src/line_index/tests.rs | |||
@@ -17,14 +17,14 @@ fn test_line_index() { | |||
17 | 17 | ||
18 | let index = LineIndex::new(text); | 18 | let index = LineIndex::new(text); |
19 | for &(offset, line, col) in &table { | 19 | for &(offset, line, col) in &table { |
20 | assert_eq!(index.line_col(offset.into()), LineColUtf16 { line, col }); | 20 | assert_eq!(index.line_col(offset.into()), LineCol { line, col }); |
21 | } | 21 | } |
22 | 22 | ||
23 | let text = "\nhello\nworld"; | 23 | let text = "\nhello\nworld"; |
24 | let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)]; | 24 | let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)]; |
25 | let index = LineIndex::new(text); | 25 | let index = LineIndex::new(text); |
26 | for &(offset, line, col) in &table { | 26 | for &(offset, line, col) in &table { |
27 | assert_eq!(index.line_col(offset.into()), LineColUtf16 { line, col }); | 27 | assert_eq!(index.line_col(offset.into()), LineCol { line, col }); |
28 | } | 28 | } |
29 | } | 29 | } |
30 | 30 | ||
diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs index edf12faa4..877abd12b 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}; | |||
5 | use anyhow::{bail, format_err, Result}; | 5 | use anyhow::{bail, format_err, Result}; |
6 | use hir::PrefixKind; | 6 | use hir::PrefixKind; |
7 | use ide::{ | 7 | use ide::{ |
8 | Analysis, AnalysisHost, Change, CompletionConfig, DiagnosticsConfig, FilePosition, LineColUtf16, | 8 | Analysis, AnalysisHost, Change, CompletionConfig, DiagnosticsConfig, FilePosition, LineCol, |
9 | }; | 9 | }; |
10 | use ide_db::{ | 10 | use ide_db::{ |
11 | base_db::{ | 11 | base_db::{ |
@@ -97,7 +97,7 @@ impl BenchCmd { | |||
97 | let offset = host | 97 | let offset = host |
98 | .analysis() | 98 | .analysis() |
99 | .file_line_index(file_id)? | 99 | .file_line_index(file_id)? |
100 | .offset(LineColUtf16 { line: pos.line - 1, col: pos.column }); | 100 | .offset(LineCol { line: pos.line - 1, col: pos.column }); |
101 | let file_position = FilePosition { file_id, offset }; | 101 | let file_position = FilePosition { file_id, offset }; |
102 | 102 | ||
103 | if is_completion { | 103 | if is_completion { |
diff --git a/crates/rust-analyzer/src/from_proto.rs b/crates/rust-analyzer/src/from_proto.rs index 82e7cfa81..de995412c 100644 --- a/crates/rust-analyzer/src/from_proto.rs +++ b/crates/rust-analyzer/src/from_proto.rs | |||
@@ -19,6 +19,7 @@ pub(crate) fn vfs_path(url: &lsp_types::Url) -> Result<vfs::VfsPath> { | |||
19 | 19 | ||
20 | pub(crate) fn offset(line_index: &LineIndex, position: lsp_types::Position) -> TextSize { | 20 | pub(crate) fn offset(line_index: &LineIndex, position: lsp_types::Position) -> TextSize { |
21 | let line_col = LineColUtf16 { line: position.line as u32, col: position.character as u32 }; | 21 | let line_col = LineColUtf16 { line: position.line as u32, col: position.character as u32 }; |
22 | let line_col = line_index.to_utf8(line_col); | ||
22 | line_index.offset(line_col) | 23 | line_index.offset(line_col) |
23 | } | 24 | } |
24 | 25 | ||
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 599b5207c..ec5e8aa73 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs | |||
@@ -22,6 +22,7 @@ use crate::{ | |||
22 | 22 | ||
23 | pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position { | 23 | pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position { |
24 | let line_col = line_index.line_col(offset); | 24 | let line_col = line_index.line_col(offset); |
25 | let line_col = line_index.to_utf16(line_col); | ||
25 | lsp_types::Position::new(line_col.line, line_col.col) | 26 | lsp_types::Position::new(line_col.line, line_col.col) |
26 | } | 27 | } |
27 | 28 | ||