aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-02-12 22:26:01 +0000
committerAleksey Kladov <[email protected]>2021-02-16 16:17:32 +0000
commitf3d56b89c54110aaafab14eeaa4c803abe10b7df (patch)
tree2425411886ff7b647cc8938d5b7c6e3105989d99 /crates
parentc8b9ec8e62d9f560a6557496bc4b579019ccb509 (diff)
Enable offset-encoding capability
Diffstat (limited to 'crates')
-rw-r--r--crates/rust-analyzer/src/bin/main.rs8
-rw-r--r--crates/rust-analyzer/src/config.rs12
-rw-r--r--crates/rust-analyzer/src/global_state.rs4
-rw-r--r--crates/rust-analyzer/src/line_endings.rs3
-rw-r--r--crates/rust-analyzer/src/lsp_ext.rs4
5 files changed, 24 insertions, 7 deletions
diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs
index 93d0ad4ec..89482b952 100644
--- a/crates/rust-analyzer/src/bin/main.rs
+++ b/crates/rust-analyzer/src/bin/main.rs
@@ -8,7 +8,7 @@ use std::{convert::TryFrom, env, fs, path::PathBuf, process};
8 8
9use lsp_server::Connection; 9use lsp_server::Connection;
10use project_model::ProjectManifest; 10use project_model::ProjectManifest;
11use rust_analyzer::{cli, config::Config, from_json, Result}; 11use rust_analyzer::{cli, config::Config, from_json, lsp_ext::supports_utf8, Result};
12use vfs::AbsPathBuf; 12use vfs::AbsPathBuf;
13 13
14#[cfg(all(feature = "mimalloc"))] 14#[cfg(all(feature = "mimalloc"))]
@@ -127,7 +127,11 @@ fn run_server() -> Result<()> {
127 name: String::from("rust-analyzer"), 127 name: String::from("rust-analyzer"),
128 version: Some(String::from(env!("REV"))), 128 version: Some(String::from(env!("REV"))),
129 }), 129 }),
130 offset_encoding: None, 130 offset_encoding: if supports_utf8(&initialize_params.capabilities) {
131 Some("utf-8".to_string())
132 } else {
133 None
134 },
131 }; 135 };
132 136
133 let initialize_result = serde_json::to_value(initialize_result).unwrap(); 137 let initialize_result = serde_json::to_value(initialize_result).unwrap();
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index f9098968a..425ef145c 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -23,7 +23,10 @@ use rustc_hash::FxHashSet;
23use serde::{de::DeserializeOwned, Deserialize}; 23use serde::{de::DeserializeOwned, Deserialize};
24use vfs::AbsPathBuf; 24use vfs::AbsPathBuf;
25 25
26use crate::{caps::completion_item_edit_resolve, diagnostics::DiagnosticsMapConfig}; 26use crate::{
27 caps::completion_item_edit_resolve, diagnostics::DiagnosticsMapConfig,
28 line_endings::OffsetEncoding, lsp_ext::supports_utf8,
29};
27 30
28config_data! { 31config_data! {
29 struct ConfigData { 32 struct ConfigData {
@@ -415,6 +418,13 @@ impl Config {
415 false 418 false
416 ) 419 )
417 } 420 }
421 pub fn offset_encoding(&self) -> OffsetEncoding {
422 if supports_utf8(&self.caps) {
423 OffsetEncoding::Utf8
424 } else {
425 OffsetEncoding::Utf16
426 }
427 }
418 428
419 fn experimental(&self, index: &'static str) -> bool { 429 fn experimental(&self, index: &'static str) -> bool {
420 try_or!(self.caps.experimental.as_ref()?.get(index)?.as_bool()?, false) 430 try_or!(self.caps.experimental.as_ref()?.get(index)?.as_bool()?, false)
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index d26e5ef48..85c87645c 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -22,7 +22,7 @@ use crate::{
22 diagnostics::{CheckFixes, DiagnosticCollection}, 22 diagnostics::{CheckFixes, DiagnosticCollection},
23 document::DocumentData, 23 document::DocumentData,
24 from_proto, 24 from_proto,
25 line_endings::{LineEndings, LineIndex, OffsetEncoding}, 25 line_endings::{LineEndings, LineIndex},
26 main_loop::Task, 26 main_loop::Task,
27 op_queue::OpQueue, 27 op_queue::OpQueue,
28 reload::SourceRootConfig, 28 reload::SourceRootConfig,
@@ -274,7 +274,7 @@ impl GlobalStateSnapshot {
274 pub(crate) fn file_line_index(&self, file_id: FileId) -> Cancelable<LineIndex> { 274 pub(crate) fn file_line_index(&self, file_id: FileId) -> Cancelable<LineIndex> {
275 let endings = self.vfs.read().1[&file_id]; 275 let endings = self.vfs.read().1[&file_id];
276 let index = self.analysis.file_line_index(file_id)?; 276 let index = self.analysis.file_line_index(file_id)?;
277 let res = LineIndex { index, endings, encoding: OffsetEncoding::Utf16 }; 277 let res = LineIndex { index, endings, encoding: self.config.offset_encoding() };
278 Ok(res) 278 Ok(res)
279 } 279 }
280 280
diff --git a/crates/rust-analyzer/src/line_endings.rs b/crates/rust-analyzer/src/line_endings.rs
index 7b6cba43e..dd8901152 100644
--- a/crates/rust-analyzer/src/line_endings.rs
+++ b/crates/rust-analyzer/src/line_endings.rs
@@ -4,8 +4,7 @@
4 4
5use std::sync::Arc; 5use std::sync::Arc;
6 6
7pub(crate) enum OffsetEncoding { 7pub enum OffsetEncoding {
8 #[allow(unused)]
9 Utf8, 8 Utf8,
10 Utf16, 9 Utf16,
11} 10}
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs
index a1ad855c3..0d2c8f7ff 100644
--- a/crates/rust-analyzer/src/lsp_ext.rs
+++ b/crates/rust-analyzer/src/lsp_ext.rs
@@ -385,3 +385,7 @@ pub(crate) enum CodeLensResolveData {
385 Impls(lsp_types::request::GotoImplementationParams), 385 Impls(lsp_types::request::GotoImplementationParams),
386 References(lsp_types::TextDocumentPositionParams), 386 References(lsp_types::TextDocumentPositionParams),
387} 387}
388
389pub fn supports_utf8(caps: &lsp_types::ClientCapabilities) -> bool {
390 caps.offset_encoding.as_deref().unwrap_or_default().iter().any(|it| it == "utf-8")
391}