From a0cd7fe242f1f58a1570164ec595f94b8b64e1b5 Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Thu, 27 May 2021 17:36:13 -0400 Subject: only advertise range formatting support if enabled --- crates/rust-analyzer/src/bin/main.rs | 84 ++++++++++++++++-------------------- crates/rust-analyzer/src/caps.rs | 12 ++++-- crates/rust-analyzer/src/config.rs | 2 +- 3 files changed, 47 insertions(+), 51 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 6c883dd58..2b842d393 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs @@ -158,7 +158,24 @@ fn run_server() -> Result<()> { let initialize_params = from_json::("InitializeParams", initialize_params)?; - let server_capabilities = rust_analyzer::server_capabilities(&initialize_params.capabilities); + let root_path = match initialize_params + .root_uri + .and_then(|it| it.to_file_path().ok()) + .and_then(|it| AbsPathBuf::try_from(it).ok()) + { + Some(it) => it, + None => { + let cwd = env::current_dir()?; + AbsPathBuf::assert(cwd) + } + }; + + let mut config = Config::new(root_path, initialize_params.capabilities); + if let Some(json) = initialize_params.initialization_options { + config.update(json); + } + + let server_capabilities = rust_analyzer::server_capabilities(&config); let initialize_result = lsp_types::InitializeResult { capabilities: server_capabilities, @@ -166,11 +183,7 @@ fn run_server() -> Result<()> { name: String::from("rust-analyzer"), version: Some(String::from(env!("REV"))), }), - offset_encoding: if supports_utf8(&initialize_params.capabilities) { - Some("utf-8".to_string()) - } else { - None - }, + offset_encoding: if supports_utf8(&config.caps) { Some("utf-8".to_string()) } else { None }, }; let initialize_result = serde_json::to_value(initialize_result).unwrap(); @@ -181,47 +194,26 @@ fn run_server() -> Result<()> { log::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default()); } - let config = { - let root_path = match initialize_params - .root_uri - .and_then(|it| it.to_file_path().ok()) - .and_then(|it| AbsPathBuf::try_from(it).ok()) - { - Some(it) => it, - None => { - let cwd = env::current_dir()?; - AbsPathBuf::assert(cwd) - } - }; - - let mut config = Config::new(root_path, initialize_params.capabilities); - if let Some(json) = initialize_params.initialization_options { - config.update(json); - } - - if config.linked_projects().is_empty() && config.detached_files().is_empty() { - let workspace_roots = initialize_params - .workspace_folders - .map(|workspaces| { - workspaces - .into_iter() - .filter_map(|it| it.uri.to_file_path().ok()) - .filter_map(|it| AbsPathBuf::try_from(it).ok()) - .collect::>() - }) - .filter(|workspaces| !workspaces.is_empty()) - .unwrap_or_else(|| vec![config.root_path.clone()]); - - let discovered = ProjectManifest::discover_all(&workspace_roots); - log::info!("discovered projects: {:?}", discovered); - if discovered.is_empty() { - log::error!("failed to find any projects in {:?}", workspace_roots); - } - config.discovered_projects = Some(discovered); + if config.linked_projects().is_empty() && config.detached_files().is_empty() { + let workspace_roots = initialize_params + .workspace_folders + .map(|workspaces| { + workspaces + .into_iter() + .filter_map(|it| it.uri.to_file_path().ok()) + .filter_map(|it| AbsPathBuf::try_from(it).ok()) + .collect::>() + }) + .filter(|workspaces| !workspaces.is_empty()) + .unwrap_or_else(|| vec![config.root_path.clone()]); + + let discovered = ProjectManifest::discover_all(&workspace_roots); + log::info!("discovered projects: {:?}", discovered); + if discovered.is_empty() { + log::error!("failed to find any projects in {:?}", workspace_roots); } - - config - }; + config.discovered_projects = Some(discovered); + } rust_analyzer::main_loop(config, connection)?; diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs index 4d88932ca..6b9753521 100644 --- a/crates/rust-analyzer/src/caps.rs +++ b/crates/rust-analyzer/src/caps.rs @@ -15,9 +15,10 @@ use lsp_types::{ }; use serde_json::json; +use crate::config::{Config, RustfmtConfig}; use crate::semantic_tokens; -pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabilities { +pub fn server_capabilities(config: &Config) -> ServerCapabilities { ServerCapabilities { text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions { open_close: Some(true), @@ -32,7 +33,7 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti })), hover_provider: Some(HoverProviderCapability::Simple(true)), completion_provider: Some(CompletionOptions { - resolve_provider: completions_resolve_provider(client_caps), + resolve_provider: completions_resolve_provider(&config.caps), trigger_characters: Some(vec![":".to_string(), ".".to_string(), "'".to_string()]), all_commit_characters: None, completion_item: None, @@ -51,10 +52,13 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti document_highlight_provider: Some(OneOf::Left(true)), document_symbol_provider: Some(OneOf::Left(true)), workspace_symbol_provider: Some(OneOf::Left(true)), - code_action_provider: Some(code_action_capabilities(client_caps)), + code_action_provider: Some(code_action_capabilities(&config.caps)), code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(true) }), document_formatting_provider: Some(OneOf::Left(true)), - document_range_formatting_provider: Some(OneOf::Left(true)), + document_range_formatting_provider: match config.rustfmt() { + RustfmtConfig::Rustfmt { enable_range_formatting: true, .. } => Some(OneOf::Left(true)), + _ => Some(OneOf::Left(false)), + }, document_on_type_formatting_provider: Some(DocumentOnTypeFormattingOptions { first_trigger_character: "=".to_string(), more_trigger_character: Some(vec![".".to_string(), ">".to_string(), "{".to_string()]), diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 7620a2fe1..b6a1124a5 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -238,7 +238,7 @@ impl Default for ConfigData { #[derive(Debug, Clone)] pub struct Config { - caps: lsp_types::ClientCapabilities, + pub caps: lsp_types::ClientCapabilities, data: ConfigData, detached_files: Vec, pub discovered_projects: Option>, -- cgit v1.2.3