mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Simplify
This commit is contained in:
parent
4d50709a96
commit
020ca6695f
3 changed files with 17 additions and 33 deletions
|
@ -13,7 +13,7 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
use ra_text_edit::TextEditBuilder;
|
use ra_text_edit::TextEditBuilder;
|
||||||
|
|
||||||
use crate::{AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
|
use crate::{AssistId, AssistLabel, GroupLabel, ResolvedAssist};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
|
pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
|
||||||
|
@ -107,7 +107,7 @@ impl<'a> AssistCtx<'a> {
|
||||||
let source_change = {
|
let source_change = {
|
||||||
let mut edit = ActionBuilder::new(&self);
|
let mut edit = ActionBuilder::new(&self);
|
||||||
f(&mut edit);
|
f(&mut edit);
|
||||||
edit.build(change_label, self.frange.file_id)
|
edit.build(change_label)
|
||||||
};
|
};
|
||||||
info = info.resolved(source_change)
|
info = info.resolved(source_change)
|
||||||
};
|
};
|
||||||
|
@ -166,7 +166,7 @@ impl<'a> AssistGroup<'a> {
|
||||||
let source_change = {
|
let source_change = {
|
||||||
let mut edit = ActionBuilder::new(&self.ctx);
|
let mut edit = ActionBuilder::new(&self.ctx);
|
||||||
f(&mut edit);
|
f(&mut edit);
|
||||||
edit.build(change_label, self.ctx.frange.file_id)
|
edit.build(change_label)
|
||||||
};
|
};
|
||||||
info = info.resolved(source_change)
|
info = info.resolved(source_change)
|
||||||
};
|
};
|
||||||
|
@ -186,7 +186,7 @@ impl<'a> AssistGroup<'a> {
|
||||||
pub(crate) struct ActionBuilder<'a, 'b> {
|
pub(crate) struct ActionBuilder<'a, 'b> {
|
||||||
edit: TextEditBuilder,
|
edit: TextEditBuilder,
|
||||||
cursor_position: Option<TextSize>,
|
cursor_position: Option<TextSize>,
|
||||||
file: AssistFile,
|
file: FileId,
|
||||||
ctx: &'a AssistCtx<'b>,
|
ctx: &'a AssistCtx<'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,7 +195,7 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
|
||||||
Self {
|
Self {
|
||||||
edit: TextEditBuilder::default(),
|
edit: TextEditBuilder::default(),
|
||||||
cursor_position: None,
|
cursor_position: None,
|
||||||
file: AssistFile::default(),
|
file: ctx.frange.file_id,
|
||||||
ctx,
|
ctx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -254,20 +254,16 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
|
||||||
algo::diff(&node, &new).into_text_edit(&mut self.edit)
|
algo::diff(&node, &new).into_text_edit(&mut self.edit)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_file(&mut self, assist_file: AssistFile) {
|
pub(crate) fn set_file(&mut self, assist_file: FileId) {
|
||||||
self.file = assist_file
|
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();
|
let edit = self.edit.finish();
|
||||||
if edit.is_empty() && self.cursor_position.is_none() {
|
if edit.is_empty() && self.cursor_position.is_none() {
|
||||||
panic!("Only call `add_assist` if the assist can be applied")
|
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 }
|
SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
|
||||||
.into_source_change(file)
|
.into_source_change(self.file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,10 @@ use ra_syntax::{
|
||||||
SyntaxKind, SyntaxNode, TextSize,
|
SyntaxKind, SyntaxNode, TextSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistFile, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
|
use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
|
||||||
use hir::HirDisplay;
|
use hir::HirDisplay;
|
||||||
|
use ra_db::FileId;
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
|
||||||
// Assist: add_function
|
// Assist: add_function
|
||||||
|
@ -70,7 +71,7 @@ struct FunctionTemplate {
|
||||||
insert_offset: TextSize,
|
insert_offset: TextSize,
|
||||||
cursor_offset: TextSize,
|
cursor_offset: TextSize,
|
||||||
fn_def: ast::SourceFile,
|
fn_def: ast::SourceFile,
|
||||||
file: AssistFile,
|
file: FileId,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FunctionBuilder {
|
struct FunctionBuilder {
|
||||||
|
@ -78,7 +79,7 @@ struct FunctionBuilder {
|
||||||
fn_name: ast::Name,
|
fn_name: ast::Name,
|
||||||
type_params: Option<ast::TypeParamList>,
|
type_params: Option<ast::TypeParamList>,
|
||||||
params: ast::ParamList,
|
params: ast::ParamList,
|
||||||
file: AssistFile,
|
file: FileId,
|
||||||
needs_pub: bool,
|
needs_pub: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +93,7 @@ impl FunctionBuilder {
|
||||||
target_module: Option<hir::InFile<hir::ModuleSource>>,
|
target_module: Option<hir::InFile<hir::ModuleSource>>,
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
let needs_pub = target_module.is_some();
|
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 target = if let Some(target_module) = target_module {
|
||||||
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?;
|
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?;
|
||||||
file = in_file;
|
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(
|
fn next_space_for_fn_in_module(
|
||||||
db: &dyn hir::db::AstDatabase,
|
db: &dyn hir::db::AstDatabase,
|
||||||
module: hir::InFile<hir::ModuleSource>,
|
module: hir::InFile<hir::ModuleSource>,
|
||||||
) -> Option<(AssistFile, GeneratedFunctionTarget)> {
|
) -> Option<(FileId, GeneratedFunctionTarget)> {
|
||||||
let file = module.file_id.original_file(db);
|
let file = module.file_id.original_file(db);
|
||||||
let assist_file = AssistFile::TargetFile(file);
|
|
||||||
let assist_item = match module.value {
|
let assist_item = match module.value {
|
||||||
hir::ModuleSource::SourceFile(it) => {
|
hir::ModuleSource::SourceFile(it) => {
|
||||||
if let Some(last_item) = it.items().last() {
|
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)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub mod utils;
|
||||||
pub mod ast_transform;
|
pub mod ast_transform;
|
||||||
|
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ra_db::{FileId, FileRange};
|
use ra_db::FileRange;
|
||||||
use ra_ide_db::{source_change::SourceChange, RootDatabase};
|
use ra_ide_db::{source_change::SourceChange, RootDatabase};
|
||||||
use ra_syntax::TextRange;
|
use ra_syntax::TextRange;
|
||||||
|
|
||||||
|
@ -62,18 +62,6 @@ pub struct ResolvedAssist {
|
||||||
pub source_change: SourceChange,
|
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.
|
/// Return all the assists applicable at the given position.
|
||||||
///
|
///
|
||||||
/// Assists are returned in the "unresolved" state, that is only labels are
|
/// Assists are returned in the "unresolved" state, that is only labels are
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue