Introduce ActiveParameter

This commit is contained in:
Aleksey Kladov 2020-04-24 01:46:00 +02:00
parent 4b8e9d5483
commit 09a4b78775
5 changed files with 35 additions and 30 deletions

View 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);

View 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,

View 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;
};

View 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 {

View 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()?;