From f8a2b533045757c42c206b2596448baf4737f1f0 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Tue, 9 Oct 2018 10:08:17 -0400 Subject: Language Server: textDocument/signatureHelp Implements a pretty barebones function signature help mechanism in the language server. Users can use `Analysis::resolve_callback()` to get basic information about a call site. Fixes #102 --- crates/ra_analysis/src/descriptors.rs | 56 ++++++++++++++++- crates/ra_analysis/src/imp.rs | 114 ++++++++++++++++++++++++++++++++-- crates/ra_analysis/src/lib.rs | 6 ++ 3 files changed, 171 insertions(+), 5 deletions(-) (limited to 'crates/ra_analysis/src') diff --git a/crates/ra_analysis/src/descriptors.rs b/crates/ra_analysis/src/descriptors.rs index 0731b5572..4dcac1aa2 100644 --- a/crates/ra_analysis/src/descriptors.rs +++ b/crates/ra_analysis/src/descriptors.rs @@ -4,7 +4,8 @@ use std::{ use relative_path::RelativePathBuf; use ra_syntax::{ SmolStr, - ast::{self, NameOwner}, + ast::{self, NameOwner, AstNode, TypeParamsOwner}, + text_utils::is_subrange }; use { FileId, @@ -218,3 +219,56 @@ fn resolve_submodule( } (points_to, problem) } + +#[derive(Debug, Clone)] +pub struct FnDescriptor { + pub name: Option, + pub label : String, + pub ret_type: Option, + pub params: Vec, +} + +impl FnDescriptor { + pub fn new(node: ast::FnDef) -> Self { + let name = node.name().map(|name| name.text().to_string()); + + // Strip the body out for the label. + let label : String = if let Some(body) = node.body() { + let body_range = body.syntax().range(); + let label : String = node.syntax().children() + .filter(|child| !is_subrange(body_range, child.range())) + .map(|node| node.text().to_string()) + .collect(); + label + } else { + node.syntax().text().to_string() + }; + + let params = FnDescriptor::param_list(node); + let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); + + FnDescriptor { + name, + ret_type, + params, + label + } + } + + fn param_list(node: ast::FnDef) -> Vec { + let mut res = vec![]; + if let Some(param_list) = node.param_list() { + if let Some(self_param) = param_list.self_param() { + res.push(self_param.syntax().text().to_string()) + } + + // Maybe use param.pat here? See if we can just extract the name? + //res.extend(param_list.params().map(|p| p.syntax().text().to_string())); + res.extend(param_list.params() + .filter_map(|p| p.pat()) + .map(|pat| pat.syntax().text().to_string()) + ); + } + res + } +} \ No newline at end of file diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 47bc0032b..9e3ae2b03 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -12,19 +12,18 @@ use relative_path::RelativePath; use rustc_hash::FxHashSet; use ra_editor::{self, FileSymbol, LineIndex, find_node_at_offset, LocalEdit, resolve_local_name}; use ra_syntax::{ - TextUnit, TextRange, SmolStr, File, AstNode, + TextUnit, TextRange, SmolStr, File, AstNode, SyntaxNodeRef, SyntaxKind::*, - ast::{self, NameOwner}, + ast::{self, NameOwner, ArgListOwner, Expr}, }; use { FileId, FileResolver, Query, Diagnostic, SourceChange, SourceFileEdit, Position, FileSystemEdit, JobToken, CrateGraph, CrateId, roots::{SourceRoot, ReadonlySourceRoot, WritableSourceRoot}, - descriptors::{ModuleTreeDescriptor, Problem}, + descriptors::{FnDescriptor, ModuleTreeDescriptor, Problem}, }; - #[derive(Clone, Debug)] pub(crate) struct FileResolverImp { inner: Arc @@ -306,6 +305,70 @@ impl AnalysisImpl { .collect() } + pub fn resolve_callable(&self, file_id: FileId, offset: TextUnit, token: &JobToken) + -> Option<(FnDescriptor, Option)> { + + let root = self.root(file_id); + let file = root.syntax(file_id); + let syntax = file.syntax(); + + // Find the calling expression and it's NameRef + let calling_node = FnCallNode::with_node(syntax, offset)?; + let name_ref = calling_node.name_ref()?; + + // Resolve the function's NameRef (NOTE: this isn't entirely accurate). + let file_symbols = self.index_resolve(name_ref, token); + 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 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(); + + + 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; + } + + current_parameter = Some(commas); + } + } + + return Some((descriptor, current_parameter)); + } + } + } + + None + } + fn index_resolve(&self, name_ref: ast::NameRef, token: &JobToken) -> Vec<(FileId, FileSymbol)> { let name = name_ref.text(); let mut query = Query::new(name.to_string()); @@ -355,3 +418,46 @@ impl CrateGraph { Some(crate_id) } } + +enum FnCallNode<'a> { + CallExpr(ast::CallExpr<'a>), + MethodCallExpr(ast::MethodCallExpr<'a>) +} + +impl<'a> FnCallNode<'a> { + pub fn with_node(syntax: SyntaxNodeRef, offset: TextUnit) -> Option { + if let Some(expr) = find_node_at_offset::(syntax, offset) { + return Some(FnCallNode::CallExpr(expr)); + } + if let Some(expr) = find_node_at_offset::(syntax, offset) { + return Some(FnCallNode::MethodCallExpr(expr)); + } + None + } + + pub fn name_ref(&self) -> Option { + match *self { + FnCallNode::CallExpr(call_expr) => { + Some(match call_expr.expr()? { + Expr::PathExpr(path_expr) => { + path_expr.path()?.segment()?.name_ref()? + }, + _ => return None + }) + }, + + FnCallNode::MethodCallExpr(call_expr) => { + call_expr.syntax().children() + .filter_map(ast::NameRef::cast) + .nth(0) + } + } + } + + pub fn arg_list(&self) -> Option { + match *self { + FnCallNode::CallExpr(expr) => expr.arg_list(), + FnCallNode::MethodCallExpr(expr) => expr.arg_list() + } + } +} \ No newline at end of file diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 849fd93e4..1aca72ae0 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -38,6 +38,7 @@ pub use ra_editor::{ Fold, FoldKind }; pub use job::{JobToken, JobHandle}; +pub use descriptors::FnDescriptor; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct FileId(pub u32); @@ -236,6 +237,11 @@ impl Analysis { let file = self.imp.file_syntax(file_id); ra_editor::folding_ranges(&file) } + + pub fn resolve_callable(&self, file_id: FileId, offset: TextUnit, token: &JobToken) + -> Option<(FnDescriptor, Option)> { + self.imp.resolve_callable(file_id, offset, token) + } } #[derive(Debug)] -- cgit v1.2.3