From d5095329a1c12e93653d8de4a93f0b4f5cad4c6e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 14 Jan 2021 22:43:36 +0100 Subject: Phase out SourceFileEdits in favour of a plain HashMap --- crates/ide/src/diagnostics.rs | 12 +- crates/ide/src/diagnostics/field_shorthand.rs | 6 +- crates/ide/src/diagnostics/fixes.rs | 18 +-- crates/ide/src/lib.rs | 2 +- crates/ide/src/references/rename.rs | 195 +++++++++++++------------- crates/ide/src/typing.rs | 3 +- 6 files changed, 111 insertions(+), 125 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index a43188fb0..2e5395b51 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -13,7 +13,7 @@ use hir::{ diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder}, Semantics, }; -use ide_db::{base_db::SourceDatabase, source_change::SourceFileEdits, RootDatabase}; +use ide_db::{base_db::SourceDatabase, RootDatabase}; use itertools::Itertools; use rustc_hash::FxHashSet; use syntax::{ @@ -219,7 +219,7 @@ fn check_unnecessary_braces_in_use_statement( Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string()) .with_fix(Some(Fix::new( "Remove unnecessary braces", - SourceFileEdits::from_text_edit(file_id, edit).into(), + SourceChange::from_text_edit(file_id, edit), use_range, ))), ); @@ -264,10 +264,10 @@ mod tests { .unwrap(); let fix = diagnostic.fix.unwrap(); let actual = { - let file_id = *fix.source_change.source_file_edits.edits.keys().next().unwrap(); + let file_id = *fix.source_change.source_file_edits.keys().next().unwrap(); let mut actual = analysis.file_text(file_id).unwrap().to_string(); - for edit in fix.source_change.source_file_edits.edits.values() { + for edit in fix.source_change.source_file_edits.values() { edit.apply(&mut actual); } actual @@ -613,9 +613,7 @@ fn test_fn() { Fix { label: "Create module", source_change: SourceChange { - source_file_edits: SourceFileEdits { - edits: {}, - }, + source_file_edits: {}, file_system_edits: [ CreateFile { dst: AnchoredPathBuf { diff --git a/crates/ide/src/diagnostics/field_shorthand.rs b/crates/ide/src/diagnostics/field_shorthand.rs index f4ec51b64..5c89e2170 100644 --- a/crates/ide/src/diagnostics/field_shorthand.rs +++ b/crates/ide/src/diagnostics/field_shorthand.rs @@ -1,7 +1,7 @@ //! Suggests shortening `Foo { field: field }` to `Foo { field }` in both //! expressions and patterns. -use ide_db::{base_db::FileId, source_change::SourceFileEdits}; +use ide_db::{base_db::FileId, source_change::SourceChange}; use syntax::{ast, match_ast, AstNode, SyntaxNode}; use text_edit::TextEdit; @@ -49,7 +49,7 @@ fn check_expr_field_shorthand( Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()).with_fix( Some(Fix::new( "Use struct shorthand initialization", - SourceFileEdits::from_text_edit(file_id, edit).into(), + SourceChange::from_text_edit(file_id, edit), field_range, )), ), @@ -88,7 +88,7 @@ fn check_pat_field_shorthand( acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fix( Some(Fix::new( "Use struct field shorthand", - SourceFileEdits::from_text_edit(file_id, edit).into(), + SourceChange::from_text_edit(file_id, edit), field_range, )), )); diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index b04964ccd..e4335119b 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -10,7 +10,7 @@ use hir::{ }; use ide_db::{ base_db::{AnchoredPathBuf, FileId}, - source_change::{FileSystemEdit, SourceFileEdits}, + source_change::{FileSystemEdit, SourceChange}, RootDatabase, }; use syntax::{ @@ -88,7 +88,7 @@ impl DiagnosticWithFix for MissingFields { }; Some(Fix::new( "Fill struct fields", - SourceFileEdits::from_text_edit(self.file.original_file(sema.db), edit).into(), + SourceChange::from_text_edit(self.file.original_file(sema.db), edit), sema.original_range(&field_list_parent.syntax()).range, )) } @@ -101,8 +101,7 @@ impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { let tail_expr_range = tail_expr.syntax().text_range(); let replacement = format!("{}({})", self.required, tail_expr.syntax()); let edit = TextEdit::replace(tail_expr_range, replacement); - let source_change = - SourceFileEdits::from_text_edit(self.file.original_file(sema.db), edit).into(); + let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit); let name = if self.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" }; Some(Fix::new(name, source_change, tail_expr_range)) } @@ -122,8 +121,7 @@ impl DiagnosticWithFix for RemoveThisSemicolon { .text_range(); let edit = TextEdit::delete(semicolon); - let source_change = - SourceFileEdits::from_text_edit(self.file.original_file(sema.db), edit).into(); + let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit); Some(Fix::new("Remove this semicolon", source_change, semicolon)) } @@ -204,15 +202,11 @@ fn missing_record_expr_field_fix( new_field = format!(",{}", new_field); } - let source_change = SourceFileEdits::from_text_edit( + let source_change = SourceChange::from_text_edit( def_file_id, TextEdit::insert(last_field_syntax.text_range().end(), new_field), ); - return Some(Fix::new( - "Create field", - source_change.into(), - record_expr_field.syntax().text_range(), - )); + return Some(Fix::new("Create field", source_change, record_expr_field.syntax().text_range())); fn record_field_list(field_def_list: ast::FieldList) -> Option { match field_def_list { diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 110920e58..afd552008 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -98,7 +98,7 @@ pub use ide_db::{ label::Label, line_index::{LineCol, LineIndex}, search::SearchScope, - source_change::{FileSystemEdit, SourceChange, SourceFileEdits}, + source_change::{FileSystemEdit, SourceChange}, symbol_index::Query, RootDatabase, }; diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index cd9c7c7e5..039efb26f 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -21,7 +21,7 @@ use text_edit::TextEdit; use crate::{ FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange, - SourceFileEdits, TextRange, TextSize, + TextRange, TextSize, }; type RenameResult = Result; @@ -249,8 +249,8 @@ fn rename_mod( if IdentifierKind::Ident != check_identifier(new_name)? { bail!("Invalid name `{0}`: cannot rename module to {0}", new_name); } - let mut source_file_edits = SourceFileEdits::default(); - let mut file_system_edits = Vec::new(); + + let mut source_change = SourceChange::default(); let src = module.definition_source(sema.db); let file_id = src.file_id.original_file(sema.db); @@ -264,7 +264,7 @@ fn rename_mod( }; let dst = AnchoredPathBuf { anchor: file_id, path }; let move_file = FileSystemEdit::MoveFile { src: file_id, dst }; - file_system_edits.push(move_file); + source_change.push_file_system_edit(move_file); } ModuleSource::Module(..) => {} } @@ -272,17 +272,19 @@ fn rename_mod( if let Some(src) = module.declaration_source(sema.db) { let file_id = src.file_id.original_file(sema.db); let name = src.value.name().unwrap(); - source_file_edits - .insert(file_id, TextEdit::replace(name.syntax().text_range(), new_name.into())); + source_change.insert_source_edit( + file_id, + TextEdit::replace(name.syntax().text_range(), new_name.into()), + ); } let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; let ref_edits = refs.references().iter().map(|(&file_id, references)| { source_edit_from_references(sema, file_id, references, new_name) }); - source_file_edits.extend(ref_edits); + source_change.extend(ref_edits); - Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits))) + Ok(RangeInfo::new(range, source_change)) } fn rename_to_self( @@ -331,13 +333,16 @@ fn rename_to_self( let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; - let mut edits = SourceFileEdits::default(); - edits.extend(refs.references().iter().map(|(&file_id, references)| { + let mut source_change = SourceChange::default(); + source_change.extend(refs.references().iter().map(|(&file_id, references)| { source_edit_from_references(sema, file_id, references, "self") })); - edits.insert(position.file_id, TextEdit::replace(param_range, String::from(self_param))); + source_change.insert_source_edit( + position.file_id, + TextEdit::replace(param_range, String::from(self_param)), + ); - Ok(RangeInfo::new(range, edits.into())) + Ok(RangeInfo::new(range, source_change)) } fn text_edit_from_self_param( @@ -391,7 +396,7 @@ fn rename_self_to_param( .ok_or_else(|| format_err!("No surrounding method declaration found"))?; let search_range = fn_def.syntax().text_range(); - let mut edits = SourceFileEdits::default(); + let mut source_change = SourceChange::default(); for (idx, _) in text.match_indices("self") { let offset: TextSize = idx.try_into().unwrap(); @@ -405,18 +410,18 @@ fn rename_self_to_param( } else { TextEdit::replace(usage.text_range(), String::from(new_name)) }; - edits.insert(position.file_id, edit); + source_change.insert_source_edit(position.file_id, edit); } } - if edits.len() > 1 && ident_kind == IdentifierKind::Underscore { + if source_change.source_file_edits.len() > 1 && ident_kind == IdentifierKind::Underscore { bail!("Cannot rename reference to `_` as it is being referenced multiple times"); } let range = ast::SelfParam::cast(self_token.parent()) .map_or(self_token.text_range(), |p| p.syntax().text_range()); - Ok(RangeInfo::new(range, edits.into())) + Ok(RangeInfo::new(range, source_change)) } fn rename_reference( @@ -453,12 +458,12 @@ fn rename_reference( (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident), } - let mut edits = SourceFileEdits::default(); - edits.extend(refs.into_iter().map(|(file_id, references)| { + let mut source_change = SourceChange::default(); + source_change.extend(refs.into_iter().map(|(file_id, references)| { source_edit_from_references(sema, file_id, &references, new_name) })); - Ok(RangeInfo::new(range, edits.into())) + Ok(RangeInfo::new(range, source_change)) } #[cfg(test)] @@ -480,7 +485,7 @@ mod tests { Ok(source_change) => { let mut text_edit_builder = TextEdit::builder(); let mut file_id: Option = None; - for edit in source_change.info.source_file_edits.edits { + for edit in source_change.info.source_file_edits { file_id = Some(edit.0); for indel in edit.1.into_iter() { text_edit_builder.replace(indel.delete, indel.insert); @@ -882,18 +887,16 @@ mod foo$0; RangeInfo { range: 4..7, info: SourceChange { - source_file_edits: SourceFileEdits { - edits: { - FileId( - 1, - ): TextEdit { - indels: [ - Indel { - insert: "foo2", - delete: 4..7, - }, - ], - }, + source_file_edits: { + FileId( + 1, + ): TextEdit { + indels: [ + Indel { + insert: "foo2", + delete: 4..7, + }, + ], }, }, file_system_edits: [ @@ -936,28 +939,26 @@ use crate::foo$0::FooContent; RangeInfo { range: 11..14, info: SourceChange { - source_file_edits: SourceFileEdits { - edits: { - FileId( - 0, - ): TextEdit { - indels: [ - Indel { - insert: "quux", - delete: 8..11, - }, - ], - }, - FileId( - 2, - ): TextEdit { - indels: [ - Indel { - insert: "quux", - delete: 11..14, - }, - ], - }, + source_file_edits: { + FileId( + 0, + ): TextEdit { + indels: [ + Indel { + insert: "quux", + delete: 8..11, + }, + ], + }, + FileId( + 2, + ): TextEdit { + indels: [ + Indel { + insert: "quux", + delete: 11..14, + }, + ], }, }, file_system_edits: [ @@ -994,18 +995,16 @@ mod fo$0o; RangeInfo { range: 4..7, info: SourceChange { - source_file_edits: SourceFileEdits { - edits: { - FileId( - 0, - ): TextEdit { - indels: [ - Indel { - insert: "foo2", - delete: 4..7, - }, - ], - }, + source_file_edits: { + FileId( + 0, + ): TextEdit { + indels: [ + Indel { + insert: "foo2", + delete: 4..7, + }, + ], }, }, file_system_edits: [ @@ -1043,18 +1042,16 @@ mod outer { mod fo$0o; } RangeInfo { range: 16..19, info: SourceChange { - source_file_edits: SourceFileEdits { - edits: { - FileId( - 0, - ): TextEdit { - indels: [ - Indel { - insert: "bar", - delete: 16..19, - }, - ], - }, + source_file_edits: { + FileId( + 0, + ): TextEdit { + indels: [ + Indel { + insert: "bar", + delete: 16..19, + }, + ], }, }, file_system_edits: [ @@ -1115,28 +1112,26 @@ pub mod foo$0; RangeInfo { range: 8..11, info: SourceChange { - source_file_edits: SourceFileEdits { - edits: { - FileId( - 0, - ): TextEdit { - indels: [ - Indel { - insert: "foo2", - delete: 27..30, - }, - ], - }, - FileId( - 1, - ): TextEdit { - indels: [ - Indel { - insert: "foo2", - delete: 8..11, - }, - ], - }, + source_file_edits: { + FileId( + 0, + ): TextEdit { + indels: [ + Indel { + insert: "foo2", + delete: 27..30, + }, + ], + }, + FileId( + 1, + ): TextEdit { + indels: [ + Indel { + insert: "foo2", + delete: 8..11, + }, + ], }, }, file_system_edits: [ diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs index b3fc32645..e3c3aebac 100644 --- a/crates/ide/src/typing.rs +++ b/crates/ide/src/typing.rs @@ -17,7 +17,6 @@ mod on_enter; use ide_db::{ base_db::{FilePosition, SourceDatabase}, - source_change::SourceFileEdits, RootDatabase, }; use syntax::{ @@ -59,7 +58,7 @@ pub(crate) fn on_char_typed( let file = &db.parse(position.file_id).tree(); assert_eq!(file.syntax().text().char_at(position.offset), Some(char_typed)); let edit = on_char_typed_inner(file, position.offset, char_typed)?; - Some(SourceFileEdits::from_text_edit(position.file_id, edit).into()) + Some(SourceChange::from_text_edit(position.file_id, edit)) } fn on_char_typed_inner(file: &SourceFile, offset: TextSize, char_typed: char) -> Option { -- cgit v1.2.3