about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-12 17:11:25 +0000
committerGitHub <noreply@github.com>2020-03-12 17:11:25 +0000
commitfab40db8bdfdac5cde5789f74d1ec28e60a8bb7e (patch)
tree5ec90c43aa979d20a21aabd54c801d4bc39b69fd
parentd98a5fab46c5850a484349c50dda7cb823cc179a (diff)
parentddfac09363fa778e3830cdb7a9ce6de3d779072e (diff)
downloadrust-fab40db8bdfdac5cde5789f74d1ec28e60a8bb7e.tar.gz
rust-fab40db8bdfdac5cde5789f74d1ec28e60a8bb7e.zip
Merge #3566
3566: Fix docs r=matklad a=matklad



bors r+
đŸ¤–

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
-rw-r--r--crates/ra_ide/src/completion.rs4
-rw-r--r--crates/ra_ide/src/inlay_hints.rs52
-rw-r--r--crates/ra_ide/src/lib.rs4
-rw-r--r--crates/rust-analyzer/src/config.rs13
-rw-r--r--crates/rust-analyzer/src/conv.rs18
-rw-r--r--crates/rust-analyzer/src/main_loop.rs8
-rw-r--r--crates/rust-analyzer/src/main_loop/handlers.rs6
-rw-r--r--crates/rust-analyzer/src/req.rs14
-rw-r--r--crates/rust-analyzer/src/world.rs4
-rw-r--r--docs/user/features.md5
-rw-r--r--editors/code/src/client.ts7
11 files changed, 73 insertions, 62 deletions
diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs
index 93e53c92109..cd0757be5f1 100644
--- a/crates/ra_ide/src/completion.rs
+++ b/crates/ra_ide/src/completion.rs
@@ -75,9 +75,9 @@ impl Default for CompletionOptions {
 pub(crate) fn completions(
     db: &RootDatabase,
     position: FilePosition,
-    opts: &CompletionOptions,
+    options: &CompletionOptions,
 ) -> Option<Completions> {
-    let ctx = CompletionContext::new(db, position, opts)?;
+    let ctx = CompletionContext::new(db, position, options)?;
 
     let mut acc = Completions::default();
 
diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs
index 59922e14c4d..ecd615cf4c0 100644
--- a/crates/ra_ide/src/inlay_hints.rs
+++ b/crates/ra_ide/src/inlay_hints.rs
@@ -11,13 +11,13 @@ use ra_syntax::{
 use crate::{FileId, FunctionSignature};
 
 #[derive(Clone, Debug, PartialEq, Eq)]
-pub struct InlayConfig {
+pub struct InlayHintsOptions {
     pub type_hints: bool,
     pub parameter_hints: bool,
     pub max_length: Option<usize>,
 }
 
-impl Default for InlayConfig {
+impl Default for InlayHintsOptions {
     fn default() -> Self {
         Self { type_hints: true, parameter_hints: true, max_length: None }
     }
@@ -39,7 +39,7 @@ pub struct InlayHint {
 pub(crate) fn inlay_hints(
     db: &RootDatabase,
     file_id: FileId,
-    inlay_hint_opts: &InlayConfig,
+    options: &InlayHintsOptions,
 ) -> Vec<InlayHint> {
     let _p = profile("inlay_hints");
     let sema = Semantics::new(db);
@@ -49,9 +49,9 @@ pub(crate) fn inlay_hints(
     for node in file.syntax().descendants() {
         match_ast! {
             match node {
-                ast::CallExpr(it) => { get_param_name_hints(&mut res, &sema, inlay_hint_opts, ast::Expr::from(it)); },
-                ast::MethodCallExpr(it) => { get_param_name_hints(&mut res, &sema, inlay_hint_opts, ast::Expr::from(it)); },
-                ast::BindPat(it) => { get_bind_pat_hints(&mut res, &sema, inlay_hint_opts, it); },
+                ast::CallExpr(it) => { get_param_name_hints(&mut res, &sema, options, ast::Expr::from(it)); },
+                ast::MethodCallExpr(it) => { get_param_name_hints(&mut res, &sema, options, ast::Expr::from(it)); },
+                ast::BindPat(it) => { get_bind_pat_hints(&mut res, &sema, options, it); },
                 _ => (),
             }
         }
@@ -62,10 +62,10 @@ pub(crate) fn inlay_hints(
 fn get_param_name_hints(
     acc: &mut Vec<InlayHint>,
     sema: &Semantics<RootDatabase>,
-    inlay_hint_opts: &InlayConfig,
+    options: &InlayHintsOptions,
     expr: ast::Expr,
 ) -> Option<()> {
-    if !inlay_hint_opts.parameter_hints {
+    if !options.parameter_hints {
         return None;
     }
 
@@ -102,10 +102,10 @@ fn get_param_name_hints(
 fn get_bind_pat_hints(
     acc: &mut Vec<InlayHint>,
     sema: &Semantics<RootDatabase>,
-    inlay_hint_opts: &InlayConfig,
+    options: &InlayHintsOptions,
     pat: ast::BindPat,
 ) -> Option<()> {
-    if !inlay_hint_opts.type_hints {
+    if !options.type_hints {
         return None;
     }
 
@@ -118,7 +118,7 @@ fn get_bind_pat_hints(
     acc.push(InlayHint {
         range: pat.syntax().text_range(),
         kind: InlayKind::TypeHint,
-        label: ty.display_truncated(sema.db, inlay_hint_opts.max_length).to_string().into(),
+        label: ty.display_truncated(sema.db, options.max_length).to_string().into(),
     });
     Some(())
 }
@@ -224,7 +224,7 @@ fn get_fn_signature(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<
 
 #[cfg(test)]
 mod tests {
-    use crate::inlay_hints::InlayConfig;
+    use crate::inlay_hints::InlayHintsOptions;
     use insta::assert_debug_snapshot;
 
     use crate::mock_analysis::single_file;
@@ -238,7 +238,7 @@ mod tests {
                 let _x = foo(4, 4);
             }"#,
         );
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ parameter_hints: true, type_hints: false, max_length: None}).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions{ parameter_hints: true, type_hints: false, max_length: None}).unwrap(), @r###"
         [
             InlayHint {
                 range: [106; 107),
@@ -262,7 +262,7 @@ mod tests {
                 let _x = foo(4, 4);
             }"#,
         );
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ type_hints: false, parameter_hints: false, max_length: None}).unwrap(), @r###"[]"###);
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions{ type_hints: false, parameter_hints: false, max_length: None}).unwrap(), @r###"[]"###);
     }
 
     #[test]
@@ -274,7 +274,7 @@ mod tests {
                 let _x = foo(4, 4);
             }"#,
         );
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ type_hints: true, parameter_hints: false, max_length: None}).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions{ type_hints: true, parameter_hints: false, max_length: None}).unwrap(), @r###"
         [
             InlayHint {
                 range: [97; 99),
@@ -298,7 +298,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions::default()).unwrap(), @r###"
         [
             InlayHint {
                 range: [69; 71),
@@ -355,7 +355,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions::default()).unwrap(), @r###"
         [
             InlayHint {
                 range: [193; 197),
@@ -435,7 +435,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions::default()).unwrap(), @r###"
         [
             InlayHint {
                 range: [21; 30),
@@ -499,7 +499,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions::default()).unwrap(), @r###"
         [
             InlayHint {
                 range: [21; 30),
@@ -549,7 +549,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions::default()).unwrap(), @r###"
         [
             InlayHint {
                 range: [188; 192),
@@ -644,7 +644,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions::default()).unwrap(), @r###"
         [
             InlayHint {
                 range: [188; 192),
@@ -739,7 +739,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions::default()).unwrap(), @r###"
         [
             InlayHint {
                 range: [252; 256),
@@ -811,7 +811,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
         [
             InlayHint {
                 range: [74; 75),
@@ -899,7 +899,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions::default()).unwrap(), @r###"
         [
             InlayHint {
                 range: [798; 809),
@@ -1021,7 +1021,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
         []
         "###
         );
@@ -1047,7 +1047,7 @@ fn main() {
 }"#,
         );
 
-        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
+        assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsOptions { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
         []
         "###
         );
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 922e4caa8a4..e9af80b6c4d 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -68,7 +68,7 @@ pub use crate::{
     expand_macro::ExpandedMacro,
     folding_ranges::{Fold, FoldKind},
     hover::HoverResult,
-    inlay_hints::{InlayConfig, InlayHint, InlayKind},
+    inlay_hints::{InlayHint, InlayHintsOptions, InlayKind},
     references::{Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult},
     runnables::{Runnable, RunnableKind, TestId},
     source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
@@ -319,7 +319,7 @@ impl Analysis {
     pub fn inlay_hints(
         &self,
         file_id: FileId,
-        inlay_hint_opts: &InlayConfig,
+        inlay_hint_opts: &InlayHintsOptions,
     ) -> Cancelable<Vec<InlayHint>> {
         self.with_db(|db| inlay_hints::inlay_hints(db, file_id, inlay_hint_opts))
     }
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index bd5904db001..084e17b040c 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -7,8 +7,6 @@
 //! configure the server itself, feature flags are passed into analysis, and
 //! tweak things like automatic insertion of `()` in completions.
 
-use crate::req::InlayConfigDef;
-use ra_ide::InlayConfig;
 use rustc_hash::FxHashMap;
 
 use ra_project_model::CargoFeatures;
@@ -32,8 +30,11 @@ pub struct ServerConfig {
 
     pub lru_capacity: Option<usize>,
 
-    #[serde(with = "InlayConfigDef")]
-    pub inlay_hints: InlayConfig,
+    #[serde(deserialize_with = "nullable_bool_true")]
+    pub inlay_hints_type: bool,
+    #[serde(deserialize_with = "nullable_bool_true")]
+    pub inlay_hints_parameter: bool,
+    pub inlay_hints_max_length: Option<usize>,
 
     pub cargo_watch_enable: bool,
     pub cargo_watch_args: Vec<String>,
@@ -63,7 +64,9 @@ impl Default for ServerConfig {
             exclude_globs: Vec::new(),
             use_client_watching: false,
             lru_capacity: None,
-            inlay_hints: Default::default(),
+            inlay_hints_type: true,
+            inlay_hints_parameter: true,
+            inlay_hints_max_length: None,
             cargo_watch_enable: true,
             cargo_watch_args: Vec::new(),
             cargo_watch_command: "check".to_string(),
diff --git a/crates/rust-analyzer/src/conv.rs b/crates/rust-analyzer/src/conv.rs
index a2d68c344e4..fd4657d7e50 100644
--- a/crates/rust-analyzer/src/conv.rs
+++ b/crates/rust-analyzer/src/conv.rs
@@ -11,8 +11,8 @@ use lsp_types::{
 use ra_ide::{
     translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition,
     FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag,
-    InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo, ReferenceAccess, Severity,
-    SourceChange, SourceFileEdit,
+    InlayHint, InlayKind, InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo,
+    ReferenceAccess, Severity, SourceChange, SourceFileEdit,
 };
 use ra_syntax::{SyntaxKind, TextRange, TextUnit};
 use ra_text_edit::{AtomTextEdit, TextEdit};
@@ -323,6 +323,20 @@ impl ConvWith<&FoldConvCtx<'_>> for Fold {
     }
 }
 
+impl ConvWith<&LineIndex> for InlayHint {
+    type Output = req::InlayHint;
+    fn conv_with(self, line_index: &LineIndex) -> Self::Output {
+        req::InlayHint {
+            label: self.label.to_string(),
+            range: self.range.conv_with(line_index),
+            kind: match self.kind {
+                InlayKind::ParameterHint => req::InlayKind::ParameterHint,
+                InlayKind::TypeHint => req::InlayKind::TypeHint,
+            },
+        }
+    }
+}
+
 impl Conv for Highlight {
     type Output = (u32, u32);
 
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index 495056da3e1..2b3b16d356b 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -18,7 +18,7 @@ use crossbeam_channel::{select, unbounded, RecvError, Sender};
 use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response};
 use lsp_types::{ClientCapabilities, NumberOrString};
 use ra_cargo_watch::{url_from_path_with_drive_lowercasing, CheckOptions, CheckTask};
-use ra_ide::{Canceled, FileId, LibraryData, SourceRootId};
+use ra_ide::{Canceled, FileId, InlayHintsOptions, LibraryData, SourceRootId};
 use ra_prof::profile;
 use ra_vfs::{VfsFile, VfsTask, Watch};
 use relative_path::RelativePathBuf;
@@ -177,7 +177,11 @@ pub fn main_loop(
                     .and_then(|it| it.folding_range.as_ref())
                     .and_then(|it| it.line_folding_only)
                     .unwrap_or(false),
-                inlay_hints: config.inlay_hints,
+                inlay_hints: InlayHintsOptions {
+                    type_hints: config.inlay_hints_type,
+                    parameter_hints: config.inlay_hints_parameter,
+                    max_length: config.inlay_hints_max_length,
+                },
                 cargo_watch: CheckOptions {
                     enable: config.cargo_watch_enable,
                     args: config.cargo_watch_args,
diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs
index 921990da030..6482f3b771a 100644
--- a/crates/rust-analyzer/src/main_loop/handlers.rs
+++ b/crates/rust-analyzer/src/main_loop/handlers.rs
@@ -999,11 +999,7 @@ pub fn handle_inlay_hints(
     Ok(analysis
         .inlay_hints(file_id, &world.options.inlay_hints)?
         .into_iter()
-        .map(|api_type| InlayHint {
-            label: api_type.label.to_string(),
-            range: api_type.range.conv_with(&line_index),
-            kind: api_type.kind,
-        })
+        .map_conv_with(&line_index)
         .collect())
 }
 
diff --git a/crates/rust-analyzer/src/req.rs b/crates/rust-analyzer/src/req.rs
index 1dcab2703e7..a3efe3b9feb 100644
--- a/crates/rust-analyzer/src/req.rs
+++ b/crates/rust-analyzer/src/req.rs
@@ -4,8 +4,6 @@ use lsp_types::{Location, Position, Range, TextDocumentIdentifier, Url};
 use rustc_hash::FxHashMap;
 use serde::{Deserialize, Serialize};
 
-use ra_ide::{InlayConfig, InlayKind};
-
 pub use lsp_types::{
     notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CodeLens,
     CodeLensParams, CompletionParams, CompletionResponse, DiagnosticTag,
@@ -198,24 +196,14 @@ pub struct InlayHintsParams {
 }
 
 #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
-#[serde(remote = "InlayKind")]
-pub enum InlayKindDef {
+pub enum InlayKind {
     TypeHint,
     ParameterHint,
 }
 
-#[derive(Deserialize)]
-#[serde(remote = "InlayConfig", rename_all = "camelCase")]
-pub struct InlayConfigDef {
-    pub type_hints: bool,
-    pub parameter_hints: bool,
-    pub max_length: Option<usize>,
-}
-
 #[derive(Debug, Deserialize, Serialize)]
 pub struct InlayHint {
     pub range: Range,
-    #[serde(with = "InlayKindDef")]
     pub kind: InlayKind,
     pub label: String,
 }
diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs
index d358f6b4792..058ce2af8a8 100644
--- a/crates/rust-analyzer/src/world.rs
+++ b/crates/rust-analyzer/src/world.rs
@@ -13,7 +13,7 @@ use lsp_types::Url;
 use parking_lot::RwLock;
 use ra_cargo_watch::{url_from_path_with_drive_lowercasing, CheckOptions, CheckWatcher};
 use ra_ide::{
-    Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, InlayConfig, LibraryData,
+    Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, InlayHintsOptions, LibraryData,
     SourceRootId,
 };
 use ra_project_model::{get_rustc_cfg_options, ProjectWorkspace};
@@ -35,7 +35,7 @@ pub struct Options {
     pub publish_decorations: bool,
     pub supports_location_link: bool,
     pub line_folding_only: bool,
-    pub inlay_hints: InlayConfig,
+    pub inlay_hints: InlayHintsOptions,
     pub rustfmt_args: Vec<String>,
     pub cargo_watch: CheckOptions,
 }
diff --git a/docs/user/features.md b/docs/user/features.md
index 06bc7ded556..45360c63398 100644
--- a/docs/user/features.md
+++ b/docs/user/features.md
@@ -191,8 +191,9 @@ Two types of inlay hints are displayed currently:
 
 In VS Code, the following settings can be used to configure the inlay hints:
 
-* `rust-analyzer.inlayHintOpts.displayType` configure which types of inlay hints are shown.
-* `rust-analyzer.inlayHintOpts.maxLength` — shortens the hints if their length exceeds the value specified. If no value is specified (`null`), no shortening is applied.
+* `rust-analyzer.inlayHints.typeHints` - enable hints for inferred types.
+* `rust-analyzer.inlayHints.parameterHints` - enable hints for function parameters.
+* `rust-analyzer.inlayHints.maxLength` — shortens the hints if their length exceeds the value specified. If no value is specified (`null`), no shortening is applied.
 
 **Note:** VS Code does not have native support for inlay hints [yet](https://github.com/microsoft/vscode/issues/16221) and the hints are implemented using decorations.
 This approach has limitations, the caret movement and bracket highlighting near the edges of the hint may be weird:
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts
index e9f261c2419..b2c830b309a 100644
--- a/editors/code/src/client.ts
+++ b/editors/code/src/client.ts
@@ -29,11 +29,16 @@ export async function createClient(config: Config, serverPath: string): Promise<
         initializationOptions: {
             publishDecorations: !config.highlightingSemanticTokens,
             lruCapacity: config.lruCapacity,
-            inlayHints: config.inlayHints,
+
+            inlayHintsType: config.inlayHints.typeHints,
+            inlayHintsParameter: config.inlayHints.parameterHints,
+            inlayHintsMaxLength: config.inlayHints.maxLength,
+
             cargoWatchEnable: cargoWatchOpts.enable,
             cargoWatchArgs: cargoWatchOpts.arguments,
             cargoWatchCommand: cargoWatchOpts.command,
             cargoWatchAllTargets: cargoWatchOpts.allTargets,
+
             excludeGlobs: config.excludeGlobs,
             useClientWatching: config.useClientWatching,
             featureFlags: config.featureFlags,