aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/utils.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-02-27 14:29:10 +0000
committerGitHub <[email protected]>2021-02-27 14:29:10 +0000
commit4a24edd989720f1232d8f6a660d790ae77115964 (patch)
tree6efdc9d6ce4a1c9e8c886c1b3e70a17d6a19fe7b /crates/ide_assists/src/utils.rs
parent2a4076c14d0e3f7ae03908c2b9cd1a52851d401c (diff)
parent558bcf4e0bf9d94ab51238e59f6fc5c170f38c3e (diff)
Merge #7677
7677: More enum matching r=yoshuawuyts a=jDomantas * Renamed existing `generate_enum_match_method` to `generate_enum_is_variant` * Added two similar assists to generate `into_` and `as_` methods. * Made all of them general enough to work on record and tuple variants too. For `as_` method generation there's room to improve: * Right now it always returns `Option<&Field>`, even though `Option<Field>` would be nicer when `Field: Copy`. I don't know how to check if the field type implements `Copy`. If given suggestions I could try to fix this in a follow-up pr. * `&String` could be replaced with `&str`, `&Box<_>` with `&_`, and probably some more. I don't know what would be a good way to do that. Closes #7604 Co-authored-by: Domantas Jadenkus <[email protected]>
Diffstat (limited to 'crates/ide_assists/src/utils.rs')
-rw-r--r--crates/ide_assists/src/utils.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs
index 276792bc1..880ab6fe3 100644
--- a/crates/ide_assists/src/utils.rs
+++ b/crates/ide_assists/src/utils.rs
@@ -21,7 +21,7 @@ use syntax::{
21}; 21};
22 22
23use crate::{ 23use crate::{
24 assist_context::AssistContext, 24 assist_context::{AssistBuilder, AssistContext},
25 ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, 25 ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
26}; 26};
27 27
@@ -464,3 +464,25 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str
464 464
465 buf 465 buf
466} 466}
467
468pub(crate) fn add_method_to_adt(
469 builder: &mut AssistBuilder,
470 adt: &ast::Adt,
471 impl_def: Option<ast::Impl>,
472 method: &str,
473) {
474 let mut buf = String::with_capacity(method.len() + 2);
475 if impl_def.is_some() {
476 buf.push('\n');
477 }
478 buf.push_str(method);
479
480 let start_offset = impl_def
481 .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
482 .unwrap_or_else(|| {
483 buf = generate_impl_text(&adt, &buf);
484 adt.syntax().text_range().end()
485 });
486
487 builder.insert(start_offset, buf);
488}