Refactor assists API to be more convenient for adding new assists

It now duplicates completion API in its shape.
This commit is contained in:
Aleksey Kladov 2020-05-06 18:45:35 +02:00
parent f4cd75ac06
commit 4867968d22
38 changed files with 525 additions and 597 deletions

View file

@ -1,14 +1,13 @@
use ra_syntax::{
ast::{self, AstNode},
SyntaxKind, SyntaxNode, TextSize,
};
use crate::{Assist, AssistCtx, AssistId};
use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
use hir::HirDisplay;
use ra_db::FileId;
use ra_syntax::{
ast::{self, edit::IndentLevel, ArgListOwner, AstNode, ModuleItemOwner},
SyntaxKind, SyntaxNode, TextSize,
};
use rustc_hash::{FxHashMap, FxHashSet};
use crate::{AssistContext, AssistId, Assists};
// Assist: add_function
//
// Adds a stub function with a signature matching the function under the cursor.
@ -34,7 +33,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
// }
//
// ```
pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
let path = path_expr.path()?;
@ -59,7 +58,7 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
let target = call.syntax().text_range();
ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| {
acc.add(AssistId("add_function"), "Add function", target, |edit| {
let function_template = function_builder.render();
edit.set_file(function_template.file);
edit.set_cursor(function_template.cursor_offset);
@ -87,7 +86,7 @@ impl FunctionBuilder {
/// Prepares a generated function that matches `call` in `generate_in`
/// (or as close to `call` as possible, if `generate_in` is `None`)
fn from_call(
ctx: &AssistCtx,
ctx: &AssistContext,
call: &ast::CallExpr,
path: &ast::Path,
target_module: Option<hir::InFile<hir::ModuleSource>>,
@ -152,7 +151,7 @@ fn fn_name(call: &ast::Path) -> Option<ast::Name> {
/// Computes the type variables and arguments required for the generated function
fn fn_args(
ctx: &AssistCtx,
ctx: &AssistContext,
call: &ast::CallExpr,
) -> Option<(Option<ast::TypeParamList>, ast::ParamList)> {
let mut arg_names = Vec::new();
@ -219,7 +218,7 @@ fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
}
}
fn fn_arg_type(ctx: &AssistCtx, fn_arg: &ast::Expr) -> Option<String> {
fn fn_arg_type(ctx: &AssistContext, fn_arg: &ast::Expr) -> Option<String> {
let ty = ctx.sema.type_of_expr(fn_arg)?;
if ty.is_unknown() {
return None;