From 020ca6695f4d58f651984c4fbe2227d891896bb3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 16:39:11 +0200 Subject: Simplify --- crates/ra_assists/src/assist_ctx.rs | 22 +++++++++------------- crates/ra_assists/src/handlers/add_function.rs | 14 +++++++------- crates/ra_assists/src/lib.rs | 14 +------------- 3 files changed, 17 insertions(+), 33 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index af6756d17..871671de2 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -13,7 +13,7 @@ use ra_syntax::{ }; 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); @@ -107,7 +107,7 @@ impl<'a> AssistCtx<'a> { 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 @@ impl<'a> AssistGroup<'a> { 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 @@ impl<'a> AssistGroup<'a> { pub(crate) struct ActionBuilder<'a, 'b> { edit: TextEditBuilder, cursor_position: Option, - file: AssistFile, + file: FileId, ctx: &'a AssistCtx<'b>, } @@ -195,7 +195,7 @@ impl<'a, 'b> ActionBuilder<'a, 'b> { Self { edit: TextEditBuilder::default(), cursor_position: None, - file: AssistFile::default(), + file: ctx.frange.file_id, ctx, } } @@ -254,20 +254,16 @@ impl<'a, 'b> ActionBuilder<'a, 'b> { 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) } } diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index 1d9d4e638..278079665 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs @@ -3,9 +3,10 @@ use ra_syntax::{ 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, params: ast::ParamList, - file: AssistFile, + file: FileId, needs_pub: bool, } @@ -92,7 +93,7 @@ impl FunctionBuilder { target_module: Option>, ) -> Option { 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, -) -> 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)] diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 6e81ea8ee..13ea45ec7 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -18,7 +18,7 @@ pub mod utils; 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 -- cgit v1.2.3