]> git.lizzy.rs Git - rust.git/commitdiff
Introduce ActiveParameter
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 23 Apr 2020 23:46:00 +0000 (01:46 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 23 Apr 2020 23:46:00 +0000 (01:46 +0200)
crates/ra_ide/src/call_info.rs
crates/ra_ide/src/completion/completion_context.rs
crates/ra_ide/src/completion/presentation.rs
crates/ra_ide/src/lib.rs
crates/ra_ide/src/syntax_highlighting.rs

index f95b6baf3d47a677f14c40eb62deb1af5c570e07..5da254a6e9a95a1bcf84ef2358069c2a069827d7 100644 (file)
@@ -19,10 +19,24 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
     call_info_for_token(&sema, token)
 }
 
-pub(crate) fn call_info_for_token(
-    sema: &Semantics<RootDatabase>,
-    token: SyntaxToken,
-) -> Option<CallInfo> {
+#[derive(Debug)]
+pub(crate) struct ActiveParameter {
+    /// FIXME: should be `Type` and `Name
+    pub(crate) ty: String,
+    pub(crate) name: String,
+}
+
+impl ActiveParameter {
+    pub(crate) fn at(db: &RootDatabase, position: FilePosition) -> Option<Self> {
+        call_info(db, position)?.into_active_parameter()
+    }
+
+    pub(crate) fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> {
+        call_info_for_token(sema, token)?.into_active_parameter()
+    }
+}
+
+fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<CallInfo> {
     // Find the calling expression and it's NameRef
     let calling_node = FnCallNode::with_node(&token.parent())?;
 
@@ -160,6 +174,14 @@ fn arg_list(&self) -> Option<ast::ArgList> {
 }
 
 impl CallInfo {
+    fn into_active_parameter(self) -> Option<ActiveParameter> {
+        let idx = self.active_parameter?;
+        let ty = self.signature.parameter_types.get(idx)?.clone();
+        let name = self.signature.parameter_names.get(idx)?.clone();
+        let res = ActiveParameter { ty, name };
+        Some(res)
+    }
+
     fn with_fn(db: &RootDatabase, function: hir::Function) -> Self {
         let signature = FunctionSignature::from_hir(db, function);
 
index dd7c8a873dbda7fba857a32ebaa03715603c2fa1..a76d1ce450d2435fbca0140a883a206b15d13f38 100644 (file)
@@ -11,7 +11,7 @@
 };
 use ra_text_edit::AtomTextEdit;
 
-use crate::{completion::CompletionConfig, FilePosition};
+use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition};
 
 /// `CompletionContext` is created early during completion to figure out, where
 /// exactly is the cursor, syntax-wise.
@@ -21,7 +21,6 @@ pub(crate) struct CompletionContext<'a> {
     pub(super) db: &'a RootDatabase,
     pub(super) config: &'a CompletionConfig,
     pub(super) offset: TextUnit,
-    pub(super) file_position: FilePosition,
     /// The token before the cursor, in the original file.
     pub(super) original_token: SyntaxToken,
     /// The token before the cursor, in the macro-expanded file.
@@ -34,6 +33,8 @@ pub(crate) struct CompletionContext<'a> {
     pub(super) record_pat_syntax: Option<ast::RecordPat>,
     pub(super) record_field_syntax: Option<ast::RecordField>,
     pub(super) impl_def: Option<ast::ImplDef>,
+    /// FIXME: `ActiveParameter` is string-based, which is very wrong
+    pub(super) active_parameter: Option<ActiveParameter>,
     pub(super) is_param: bool,
     /// If a name-binding or reference to a const in a pattern.
     /// Irrefutable patterns (like let) are excluded.
@@ -90,7 +91,6 @@ pub(super) fn new(
             original_token,
             token,
             offset: position.offset,
-            file_position: position,
             krate,
             name_ref_syntax: None,
             function_syntax: None,
@@ -99,6 +99,7 @@ pub(super) fn new(
             record_pat_syntax: None,
             record_field_syntax: None,
             impl_def: None,
+            active_parameter: ActiveParameter::at(db, position),
             is_param: false,
             is_pat_binding_or_const: false,
             is_trivial_path: false,
index ae15f91de055a88f8c8077e7691688d63691bec5..6c0e32408dc82062185fc9d54ce6d8d132738422 100644 (file)
@@ -6,7 +6,6 @@
 use test_utils::tested_by;
 
 use crate::{
-    call_info::call_info,
     completion::{
         completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
         CompletionKind, Completions,
@@ -317,8 +316,8 @@ pub(crate) fn compute_score(
             struct_field.name(ctx.db).to_string(),
             struct_field.signature_ty(ctx.db).display(ctx.db).to_string(),
         )
-    } else if let Some(call_info) = call_info(ctx.db, ctx.file_position) {
-        (call_info.active_parameter_name()?, call_info.active_parameter_type()?)
+    } else if let Some(active_parameter) = &ctx.active_parameter {
+        (active_parameter.name.clone(), active_parameter.ty.clone())
     } else {
         return None;
     };
index ddaa30a1606b2643aa017e0e96f8b5c48a214e47..f692fbaa2eb654cd5a1e7339ed44a2024bb8dc8e 100644 (file)
@@ -129,21 +129,6 @@ pub struct CallInfo {
     pub active_parameter: Option<usize>,
 }
 
-impl CallInfo {
-    pub fn active_parameter_type(&self) -> Option<String> {
-        if let Some(id) = self.active_parameter {
-            return self.signature.parameter_types.get(id).map(|param_ty| param_ty.clone());
-        }
-        None
-    }
-    pub fn active_parameter_name(&self) -> Option<String> {
-        if let Some(id) = self.active_parameter {
-            return self.signature.parameter_names.get(id).map(|param_ty| param_ty.clone());
-        }
-        None
-    }
-}
-
 /// `AnalysisHost` stores the current state of the world.
 #[derive(Debug)]
 pub struct AnalysisHost {
index 93d50287561281e579c758937fa9944ae421c40d..d9912155b99ccd456e686e8f2f0865a5a13d514d 100644 (file)
@@ -19,7 +19,7 @@
 };
 use rustc_hash::FxHashMap;
 
-use crate::{call_info::call_info_for_token, Analysis, FileId};
+use crate::{call_info::ActiveParameter, Analysis, FileId};
 
 pub(crate) use html::highlight_as_html;
 pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag};
@@ -364,10 +364,8 @@ fn highlight_injection(
     literal: ast::RawString,
     expanded: SyntaxToken,
 ) -> Option<()> {
-    let call_info = call_info_for_token(&sema, expanded)?;
-    let idx = call_info.active_parameter?;
-    let name = call_info.signature.parameter_names.get(idx)?;
-    if !name.starts_with("ra_fixture") {
+    let active_parameter = ActiveParameter::at_token(&sema, expanded)?;
+    if !active_parameter.name.starts_with("ra_fixture") {
         return None;
     }
     let value = literal.value()?;