A FnDescriptor shouldn't exist without a name

This commit is contained in:
Jeremy A. Kolb 2018-10-12 07:54:57 -04:00
parent 3ac5199755
commit c9909f42ba
3 changed files with 44 additions and 46 deletions

View file

@ -4,7 +4,7 @@ use std::{
use relative_path::RelativePathBuf;
use ra_syntax::{
SmolStr,
ast::{self, NameOwner, AstNode, TypeParamsOwner},
ast::{self, NameOwner, AstNode},
text_utils::is_subrange
};
use {
@ -222,15 +222,15 @@ fn resolve_submodule(
#[derive(Debug, Clone)]
pub struct FnDescriptor {
pub name: Option<String>,
pub name: String,
pub label : String,
pub ret_type: Option<String>,
pub params: Vec<String>,
}
impl FnDescriptor {
pub fn new(node: ast::FnDef) -> Self {
let name = node.name().map(|name| name.text().to_string());
pub fn new(node: ast::FnDef) -> Option<Self> {
let name = node.name()?.text().to_string();
// Strip the body out for the label.
let label : String = if let Some(body) = node.body() {
@ -247,12 +247,12 @@ impl FnDescriptor {
let params = FnDescriptor::param_list(node);
let ret_type = node.ret_type().map(|r| r.syntax().text().to_string());
FnDescriptor {
Some(FnDescriptor {
name,
ret_type,
params,
label
}
})
}
fn param_list(node: ast::FnDef) -> Vec<String> {

View file

@ -321,47 +321,45 @@ impl AnalysisImpl {
for (_, fs) in file_symbols {
if fs.kind == FN_DEF {
if let Some(fn_def) = find_node_at_offset(syntax, fs.node_range.start()) {
let descriptor = FnDescriptor::new(fn_def);
if let Some(descriptor) = FnDescriptor::new(fn_def) {
// If we have a calling expression let's find which argument we are on
let mut current_parameter = None;
// If we have a calling expression let's find which argument we are on
let mut current_parameter = None;
let num_params = descriptor.params.len();
let has_self = fn_def.param_list()
.and_then(|l| l.self_param())
.is_some();
let num_params = descriptor.params.len();
let has_self = fn_def.param_list()
.and_then(|l| l.self_param())
.is_some();
if num_params == 1 {
if !has_self {
current_parameter = Some(1);
}
}
else if num_params > 1 {
// Count how many parameters into the call we are.
// TODO: This is best effort for now and should be fixed at some point.
// It may be better to see where we are in the arg_list and then check
// where offset is in that list (or beyond).
// Revisit this after we get documentation comments in.
if let Some(ref arg_list) = calling_node.arg_list() {
let start = arg_list.syntax().range().start();
let range_search = TextRange::from_to(start, offset);
let mut commas : usize = arg_list.syntax().text()
.slice(range_search).to_string()
.matches(",")
.count();
// If we have a method call eat the first param since it's just self.
if has_self {
commas = commas + 1;
if num_params == 1 {
if !has_self {
current_parameter = Some(1);
}
} else if num_params > 1 {
// Count how many parameters into the call we are.
// TODO: This is best effort for now and should be fixed at some point.
// It may be better to see where we are in the arg_list and then check
// where offset is in that list (or beyond).
// Revisit this after we get documentation comments in.
if let Some(ref arg_list) = calling_node.arg_list() {
let start = arg_list.syntax().range().start();
current_parameter = Some(commas);
let range_search = TextRange::from_to(start, offset);
let mut commas: usize = arg_list.syntax().text()
.slice(range_search).to_string()
.matches(",")
.count();
// If we have a method call eat the first param since it's just self.
if has_self {
commas = commas + 1;
}
current_parameter = Some(commas);
}
}
}
return Some((descriptor, current_parameter));
return Some((descriptor, current_parameter));
}
}
}
}