From 09a4b78775809677473b39505796785242bcee2f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Apr 2020 01:46:00 +0200 Subject: Introduce ActiveParameter --- crates/ra_ide/src/call_info.rs | 30 +++++++++++++++++++--- crates/ra_ide/src/completion/completion_context.rs | 7 ++--- crates/ra_ide/src/completion/presentation.rs | 5 ++-- crates/ra_ide/src/lib.rs | 15 ----------- crates/ra_ide/src/syntax_highlighting.rs | 8 +++--- 5 files changed, 35 insertions(+), 30 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs index f95b6baf3..5da254a6e 100644 --- a/crates/ra_ide/src/call_info.rs +++ b/crates/ra_ide/src/call_info.rs @@ -19,10 +19,24 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option, - token: SyntaxToken, -) -> Option { +#[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 { + call_info(db, position)?.into_active_parameter() + } + + pub(crate) fn at_token(sema: &Semantics, token: SyntaxToken) -> Option { + call_info_for_token(sema, token)?.into_active_parameter() + } +} + +fn call_info_for_token(sema: &Semantics, token: SyntaxToken) -> Option { // Find the calling expression and it's NameRef let calling_node = FnCallNode::with_node(&token.parent())?; @@ -160,6 +174,14 @@ impl FnCallNode { } impl CallInfo { + fn into_active_parameter(self) -> Option { + 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); diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index dd7c8a873..a76d1ce45 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -11,7 +11,7 @@ use ra_syntax::{ }; 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, pub(super) record_field_syntax: Option, pub(super) impl_def: Option, + /// FIXME: `ActiveParameter` is string-based, which is very wrong + pub(super) active_parameter: Option, 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 @@ impl<'a> CompletionContext<'a> { original_token, token, offset: position.offset, - file_position: position, krate, name_ref_syntax: None, function_syntax: None, @@ -99,6 +99,7 @@ impl<'a> CompletionContext<'a> { 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, diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index ae15f91de..6c0e32408 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -6,7 +6,6 @@ use stdx::SepBy; 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; }; diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index ddaa30a16..f692fbaa2 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -129,21 +129,6 @@ pub struct CallInfo { pub active_parameter: Option, } -impl CallInfo { - pub fn active_parameter_type(&self) -> Option { - 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 { - 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 { diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 93d502875..d9912155b 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -19,7 +19,7 @@ use ra_syntax::{ }; 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()?; -- cgit v1.2.3