Skip to content

Commit 09a4b78

Browse files
committed
Introduce ActiveParameter
1 parent 4b8e9d5 commit 09a4b78

File tree

5 files changed

+35
-30
lines changed

5 files changed

+35
-30
lines changed

crates/ra_ide/src/call_info.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
1919
call_info_for_token(&sema, token)
2020
}
2121

22-
pub(crate) fn call_info_for_token(
23-
sema: &Semantics<RootDatabase>,
24-
token: SyntaxToken,
25-
) -> Option<CallInfo> {
22+
#[derive(Debug)]
23+
pub(crate) struct ActiveParameter {
24+
/// FIXME: should be `Type` and `Name
25+
pub(crate) ty: String,
26+
pub(crate) name: String,
27+
}
28+
29+
impl ActiveParameter {
30+
pub(crate) fn at(db: &RootDatabase, position: FilePosition) -> Option<Self> {
31+
call_info(db, position)?.into_active_parameter()
32+
}
33+
34+
pub(crate) fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> {
35+
call_info_for_token(sema, token)?.into_active_parameter()
36+
}
37+
}
38+
39+
fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<CallInfo> {
2640
// Find the calling expression and it's NameRef
2741
let calling_node = FnCallNode::with_node(&token.parent())?;
2842

@@ -160,6 +174,14 @@ impl FnCallNode {
160174
}
161175

162176
impl CallInfo {
177+
fn into_active_parameter(self) -> Option<ActiveParameter> {
178+
let idx = self.active_parameter?;
179+
let ty = self.signature.parameter_types.get(idx)?.clone();
180+
let name = self.signature.parameter_names.get(idx)?.clone();
181+
let res = ActiveParameter { ty, name };
182+
Some(res)
183+
}
184+
163185
fn with_fn(db: &RootDatabase, function: hir::Function) -> Self {
164186
let signature = FunctionSignature::from_hir(db, function);
165187

crates/ra_ide/src/completion/completion_context.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ra_syntax::{
1111
};
1212
use ra_text_edit::AtomTextEdit;
1313

14-
use crate::{completion::CompletionConfig, FilePosition};
14+
use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition};
1515

1616
/// `CompletionContext` is created early during completion to figure out, where
1717
/// exactly is the cursor, syntax-wise.
@@ -21,7 +21,6 @@ pub(crate) struct CompletionContext<'a> {
2121
pub(super) db: &'a RootDatabase,
2222
pub(super) config: &'a CompletionConfig,
2323
pub(super) offset: TextUnit,
24-
pub(super) file_position: FilePosition,
2524
/// The token before the cursor, in the original file.
2625
pub(super) original_token: SyntaxToken,
2726
/// The token before the cursor, in the macro-expanded file.
@@ -34,6 +33,8 @@ pub(crate) struct CompletionContext<'a> {
3433
pub(super) record_pat_syntax: Option<ast::RecordPat>,
3534
pub(super) record_field_syntax: Option<ast::RecordField>,
3635
pub(super) impl_def: Option<ast::ImplDef>,
36+
/// FIXME: `ActiveParameter` is string-based, which is very wrong
37+
pub(super) active_parameter: Option<ActiveParameter>,
3738
pub(super) is_param: bool,
3839
/// If a name-binding or reference to a const in a pattern.
3940
/// Irrefutable patterns (like let) are excluded.
@@ -90,7 +91,6 @@ impl<'a> CompletionContext<'a> {
9091
original_token,
9192
token,
9293
offset: position.offset,
93-
file_position: position,
9494
krate,
9595
name_ref_syntax: None,
9696
function_syntax: None,
@@ -99,6 +99,7 @@ impl<'a> CompletionContext<'a> {
9999
record_pat_syntax: None,
100100
record_field_syntax: None,
101101
impl_def: None,
102+
active_parameter: ActiveParameter::at(db, position),
102103
is_param: false,
103104
is_pat_binding_or_const: false,
104105
is_trivial_path: false,

crates/ra_ide/src/completion/presentation.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use stdx::SepBy;
66
use test_utils::tested_by;
77

88
use crate::{
9-
call_info::call_info,
109
completion::{
1110
completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
1211
CompletionKind, Completions,
@@ -317,8 +316,8 @@ pub(crate) fn compute_score(
317316
struct_field.name(ctx.db).to_string(),
318317
struct_field.signature_ty(ctx.db).display(ctx.db).to_string(),
319318
)
320-
} else if let Some(call_info) = call_info(ctx.db, ctx.file_position) {
321-
(call_info.active_parameter_name()?, call_info.active_parameter_type()?)
319+
} else if let Some(active_parameter) = &ctx.active_parameter {
320+
(active_parameter.name.clone(), active_parameter.ty.clone())
322321
} else {
323322
return None;
324323
};

crates/ra_ide/src/lib.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,6 @@ pub struct CallInfo {
129129
pub active_parameter: Option<usize>,
130130
}
131131

132-
impl CallInfo {
133-
pub fn active_parameter_type(&self) -> Option<String> {
134-
if let Some(id) = self.active_parameter {
135-
return self.signature.parameter_types.get(id).map(|param_ty| param_ty.clone());
136-
}
137-
None
138-
}
139-
pub fn active_parameter_name(&self) -> Option<String> {
140-
if let Some(id) = self.active_parameter {
141-
return self.signature.parameter_names.get(id).map(|param_ty| param_ty.clone());
142-
}
143-
None
144-
}
145-
}
146-
147132
/// `AnalysisHost` stores the current state of the world.
148133
#[derive(Debug)]
149134
pub struct AnalysisHost {

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use ra_syntax::{
1919
};
2020
use rustc_hash::FxHashMap;
2121

22-
use crate::{call_info::call_info_for_token, Analysis, FileId};
22+
use crate::{call_info::ActiveParameter, Analysis, FileId};
2323

2424
pub(crate) use html::highlight_as_html;
2525
pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag};
@@ -364,10 +364,8 @@ fn highlight_injection(
364364
literal: ast::RawString,
365365
expanded: SyntaxToken,
366366
) -> Option<()> {
367-
let call_info = call_info_for_token(&sema, expanded)?;
368-
let idx = call_info.active_parameter?;
369-
let name = call_info.signature.parameter_names.get(idx)?;
370-
if !name.starts_with("ra_fixture") {
367+
let active_parameter = ActiveParameter::at_token(&sema, expanded)?;
368+
if !active_parameter.name.starts_with("ra_fixture") {
371369
return None;
372370
}
373371
let value = literal.value()?;

0 commit comments

Comments
 (0)