aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/to_proto.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-02-12 21:55:27 +0000
committerAleksey Kladov <[email protected]>2021-02-16 16:17:32 +0000
commitc8b9ec8e62d9f560a6557496bc4b579019ccb509 (patch)
treee3fdf968a8fcfb018319c94f524b0cb1b9ba3e67 /crates/rust-analyzer/src/to_proto.rs
parent0025836f262ee410050ba79b6ea09d75f76449ac (diff)
Implement utf8 offsets
Diffstat (limited to 'crates/rust-analyzer/src/to_proto.rs')
-rw-r--r--crates/rust-analyzer/src/to_proto.rs25
1 files changed, 18 insertions, 7 deletions
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 43e29ef04..368d916e7 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -17,14 +17,19 @@ use serde_json::to_value;
17use crate::{ 17use crate::{
18 cargo_target_spec::CargoTargetSpec, 18 cargo_target_spec::CargoTargetSpec,
19 global_state::GlobalStateSnapshot, 19 global_state::GlobalStateSnapshot,
20 line_endings::{LineEndings, LineIndex}, 20 line_endings::{LineEndings, LineIndex, OffsetEncoding},
21 lsp_ext, semantic_tokens, Result, 21 lsp_ext, semantic_tokens, Result,
22}; 22};
23 23
24pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position { 24pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position {
25 let line_col = line_index.index.line_col(offset); 25 let line_col = line_index.index.line_col(offset);
26 let line_col = line_index.index.to_utf16(line_col); 26 match line_index.encoding {
27 lsp_types::Position::new(line_col.line, line_col.col) 27 OffsetEncoding::Utf8 => lsp_types::Position::new(line_col.line, line_col.col),
28 OffsetEncoding::Utf16 => {
29 let line_col = line_index.index.to_utf16(line_col);
30 lsp_types::Position::new(line_col.line, line_col.col)
31 }
32 }
28} 33}
29 34
30pub(crate) fn range(line_index: &LineIndex, range: TextRange) -> lsp_types::Range { 35pub(crate) fn range(line_index: &LineIndex, range: TextRange) -> lsp_types::Range {
@@ -1068,8 +1073,11 @@ mod tests {
1068 }"#; 1073 }"#;
1069 1074
1070 let (offset, text) = test_utils::extract_offset(fixture); 1075 let (offset, text) = test_utils::extract_offset(fixture);
1071 let line_index = 1076 let line_index = LineIndex {
1072 LineIndex { index: Arc::new(ide::LineIndex::new(&text)), endings: LineEndings::Unix }; 1077 index: Arc::new(ide::LineIndex::new(&text)),
1078 endings: LineEndings::Unix,
1079 encoding: OffsetEncoding::Utf16,
1080 };
1073 let (analysis, file_id) = Analysis::from_single_file(text); 1081 let (analysis, file_id) = Analysis::from_single_file(text);
1074 let completions: Vec<(String, Option<String>)> = analysis 1082 let completions: Vec<(String, Option<String>)> = analysis
1075 .completions( 1083 .completions(
@@ -1125,8 +1133,11 @@ fn main() {
1125 let folds = analysis.folding_ranges(file_id).unwrap(); 1133 let folds = analysis.folding_ranges(file_id).unwrap();
1126 assert_eq!(folds.len(), 4); 1134 assert_eq!(folds.len(), 4);
1127 1135
1128 let line_index = 1136 let line_index = LineIndex {
1129 LineIndex { index: Arc::new(ide::LineIndex::new(&text)), endings: LineEndings::Unix }; 1137 index: Arc::new(ide::LineIndex::new(&text)),
1138 endings: LineEndings::Unix,
1139 encoding: OffsetEncoding::Utf16,
1140 };
1130 let converted: Vec<lsp_types::FoldingRange> = 1141 let converted: Vec<lsp_types::FoldingRange> =
1131 folds.into_iter().map(|it| folding_range(&text, &line_index, true, it)).collect(); 1142 folds.into_iter().map(|it| folding_range(&text, &line_index, true, it)).collect();
1132 1143