This commit is contained in:
Aleksey Kladov 2020-05-06 16:39:11 +02:00
parent 4d50709a96
commit 020ca6695f
3 changed files with 17 additions and 33 deletions

View File

@ -13,7 +13,7 @@
};
use ra_text_edit::TextEditBuilder;
use crate::{AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
use crate::{AssistId, AssistLabel, GroupLabel, ResolvedAssist};
#[derive(Clone, Debug)]
pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
@ -107,7 +107,7 @@ pub(crate) fn add_assist(
let source_change = {
let mut edit = ActionBuilder::new(&self);
f(&mut edit);
edit.build(change_label, self.frange.file_id)
edit.build(change_label)
};
info = info.resolved(source_change)
};
@ -166,7 +166,7 @@ pub(crate) fn add_assist(
let source_change = {
let mut edit = ActionBuilder::new(&self.ctx);
f(&mut edit);
edit.build(change_label, self.ctx.frange.file_id)
edit.build(change_label)
};
info = info.resolved(source_change)
};
@ -186,7 +186,7 @@ pub(crate) fn finish(self) -> Option<Assist> {
pub(crate) struct ActionBuilder<'a, 'b> {
edit: TextEditBuilder,
cursor_position: Option<TextSize>,
file: AssistFile,
file: FileId,
ctx: &'a AssistCtx<'b>,
}
@ -195,7 +195,7 @@ fn new(ctx: &'a AssistCtx<'b>) -> Self {
Self {
edit: TextEditBuilder::default(),
cursor_position: None,
file: AssistFile::default(),
file: ctx.frange.file_id,
ctx,
}
}
@ -254,20 +254,16 @@ pub(crate) fn rewrite(&mut self, rewriter: SyntaxRewriter) {
algo::diff(&node, &new).into_text_edit(&mut self.edit)
}
pub(crate) fn set_file(&mut self, assist_file: AssistFile) {
self.file = assist_file
pub(crate) fn set_file(&mut self, assist_file: FileId) {
self.file = assist_file;
}
fn build(self, change_label: String, current_file: FileId) -> SourceChange {
fn build(self, change_label: String) -> SourceChange {
let edit = self.edit.finish();
if edit.is_empty() && self.cursor_position.is_none() {
panic!("Only call `add_assist` if the assist can be applied")
}
let file = match self.file {
AssistFile::CurrentFile => current_file,
AssistFile::TargetFile(it) => it,
};
SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
.into_source_change(file)
.into_source_change(self.file)
}
}

View File

@ -3,9 +3,10 @@
SyntaxKind, SyntaxNode, TextSize,
};
use crate::{Assist, AssistCtx, AssistFile, AssistId};
use crate::{Assist, AssistCtx, AssistId};
use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
use hir::HirDisplay;
use ra_db::FileId;
use rustc_hash::{FxHashMap, FxHashSet};
// Assist: add_function
@ -70,7 +71,7 @@ struct FunctionTemplate {
insert_offset: TextSize,
cursor_offset: TextSize,
fn_def: ast::SourceFile,
file: AssistFile,
file: FileId,
}
struct FunctionBuilder {
@ -78,7 +79,7 @@ struct FunctionBuilder {
fn_name: ast::Name,
type_params: Option<ast::TypeParamList>,
params: ast::ParamList,
file: AssistFile,
file: FileId,
needs_pub: bool,
}
@ -92,7 +93,7 @@ fn from_call(
target_module: Option<hir::InFile<hir::ModuleSource>>,
) -> Option<Self> {
let needs_pub = target_module.is_some();
let mut file = AssistFile::default();
let mut file = ctx.frange.file_id;
let target = if let Some(target_module) = target_module {
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?;
file = in_file;
@ -253,9 +254,8 @@ fn next_space_for_fn_after_call_site(expr: &ast::CallExpr) -> Option<GeneratedFu
fn next_space_for_fn_in_module(
db: &dyn hir::db::AstDatabase,
module: hir::InFile<hir::ModuleSource>,
) -> Option<(AssistFile, GeneratedFunctionTarget)> {
) -> Option<(FileId, GeneratedFunctionTarget)> {
let file = module.file_id.original_file(db);
let assist_file = AssistFile::TargetFile(file);
let assist_item = match module.value {
hir::ModuleSource::SourceFile(it) => {
if let Some(last_item) = it.items().last() {
@ -272,7 +272,7 @@ fn next_space_for_fn_in_module(
}
}
};
Some((assist_file, assist_item))
Some((file, assist_item))
}
#[cfg(test)]

View File

@ -18,7 +18,7 @@ macro_rules! eprintln {
pub mod ast_transform;
use hir::Semantics;
use ra_db::{FileId, FileRange};
use ra_db::FileRange;
use ra_ide_db::{source_change::SourceChange, RootDatabase};
use ra_syntax::TextRange;
@ -62,18 +62,6 @@ pub struct ResolvedAssist {
pub source_change: SourceChange,
}
#[derive(Debug, Clone, Copy)]
enum AssistFile {
CurrentFile,
TargetFile(FileId),
}
impl Default for AssistFile {
fn default() -> Self {
Self::CurrentFile
}
}
/// Return all the assists applicable at the given position.
///
/// Assists are returned in the "unresolved" state, that is only labels are