From f3d56b89c54110aaafab14eeaa4c803abe10b7df Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 13 Feb 2021 01:26:01 +0300 Subject: Enable offset-encoding capability --- crates/rust-analyzer/src/bin/main.rs | 8 ++++++-- crates/rust-analyzer/src/config.rs | 12 +++++++++++- crates/rust-analyzer/src/global_state.rs | 4 ++-- crates/rust-analyzer/src/line_endings.rs | 3 +-- crates/rust-analyzer/src/lsp_ext.rs | 4 ++++ 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}; use lsp_server::Connection; use project_model::ProjectManifest; -use rust_analyzer::{cli, config::Config, from_json, Result}; +use rust_analyzer::{cli, config::Config, from_json, lsp_ext::supports_utf8, Result}; use vfs::AbsPathBuf; #[cfg(all(feature = "mimalloc"))] @@ -127,7 +127,11 @@ fn run_server() -> Result<()> { name: String::from("rust-analyzer"), version: Some(String::from(env!("REV"))), }), - offset_encoding: None, + offset_encoding: if supports_utf8(&initialize_params.capabilities) { + Some("utf-8".to_string()) + } else { + None + }, }; 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; use serde::{de::DeserializeOwned, Deserialize}; use vfs::AbsPathBuf; -use crate::{caps::completion_item_edit_resolve, diagnostics::DiagnosticsMapConfig}; +use crate::{ + caps::completion_item_edit_resolve, diagnostics::DiagnosticsMapConfig, + line_endings::OffsetEncoding, lsp_ext::supports_utf8, +}; config_data! { struct ConfigData { @@ -415,6 +418,13 @@ impl Config { false ) } + pub fn offset_encoding(&self) -> OffsetEncoding { + if supports_utf8(&self.caps) { + OffsetEncoding::Utf8 + } else { + OffsetEncoding::Utf16 + } + } fn experimental(&self, index: &'static str) -> bool { 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::{ diagnostics::{CheckFixes, DiagnosticCollection}, document::DocumentData, from_proto, - line_endings::{LineEndings, LineIndex, OffsetEncoding}, + line_endings::{LineEndings, LineIndex}, main_loop::Task, op_queue::OpQueue, reload::SourceRootConfig, @@ -274,7 +274,7 @@ impl GlobalStateSnapshot { pub(crate) fn file_line_index(&self, file_id: FileId) -> Cancelable { let endings = self.vfs.read().1[&file_id]; let index = self.analysis.file_line_index(file_id)?; - let res = LineIndex { index, endings, encoding: OffsetEncoding::Utf16 }; + let res = LineIndex { index, endings, encoding: self.config.offset_encoding() }; Ok(res) } 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 @@ use std::sync::Arc; -pub(crate) enum OffsetEncoding { - #[allow(unused)] +pub enum OffsetEncoding { Utf8, Utf16, } 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 { Impls(lsp_types::request::GotoImplementationParams), References(lsp_types::TextDocumentPositionParams), } + +pub fn supports_utf8(caps: &lsp_types::ClientCapabilities) -> bool { + caps.offset_encoding.as_deref().unwrap_or_default().iter().any(|it| it == "utf-8") +} -- cgit v1.2.3