diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-02-05 17:18:02 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-02-05 17:18:02 +0000 |
commit | 80ab753d7e0bf59b81df317d6ddda43cb919ec83 (patch) | |
tree | 934a7ae2d8b549cd9d5d82a137156c0a71220973 /crates/assists/src/utils.rs | |
parent | 941f9157f5561868c4683d54fdf98378383f64b8 (diff) | |
parent | 2cf161266941eac300ae66a633ead26f5109ea16 (diff) |
Merge #7572
7572: Add `find_or_create_impl_block` to assist utils r=matklad a=yoshuawuyts
This is another continuation of https://github.com/rust-analyzer/rust-analyzer/pull/7562, introducing a small util to either find an `impl` block, or create a new one if none exists. I copied this code from the `generate_new` assist into https://github.com/rust-analyzer/rust-analyzer/pull/7562, and this unifies both into a helper.
It doesn't feel super polished in its current state, but my hope is that this is enough of a starting point that it can be expanded on later. For example something that would be useful would be a flag which either returns the index of the start of the block, or the end of the block.
Anyway, I hope this is useful. Thanks!
Co-authored-by: Yoshua Wuyts <[email protected]>
Diffstat (limited to 'crates/assists/src/utils.rs')
-rw-r--r-- | crates/assists/src/utils.rs | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 3842558d8..8045aac40 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs | |||
@@ -274,10 +274,11 @@ pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool { | |||
274 | // Uses a syntax-driven approach to find any impl blocks for the struct that | 274 | // Uses a syntax-driven approach to find any impl blocks for the struct that |
275 | // exist within the module/file | 275 | // exist within the module/file |
276 | // | 276 | // |
277 | // Returns `None` if we've found an existing `new` fn | 277 | // Returns `None` if we've found an existing fn |
278 | // | 278 | // |
279 | // FIXME: change the new fn checking to a more semantic approach when that's more | 279 | // FIXME: change the new fn checking to a more semantic approach when that's more |
280 | // viable (e.g. we process proc macros, etc) | 280 | // viable (e.g. we process proc macros, etc) |
281 | // FIXME: this partially overlaps with `find_impl_block` | ||
281 | pub(crate) fn find_struct_impl( | 282 | pub(crate) fn find_struct_impl( |
282 | ctx: &AssistContext, | 283 | ctx: &AssistContext, |
283 | strukt: &ast::AdtDef, | 284 | strukt: &ast::AdtDef, |
@@ -338,3 +339,18 @@ fn has_fn(imp: &ast::Impl, rhs_name: &str) -> bool { | |||
338 | 339 | ||
339 | false | 340 | false |
340 | } | 341 | } |
342 | |||
343 | /// Find the start of the `impl` block for the given `ast::Impl`. | ||
344 | // | ||
345 | // FIXME: add a way to find the end of the `impl` block. | ||
346 | // FIXME: this partially overlaps with `find_struct_impl` | ||
347 | pub(crate) fn find_impl_block(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize> { | ||
348 | buf.push('\n'); | ||
349 | let start = impl_def | ||
350 | .syntax() | ||
351 | .descendants_with_tokens() | ||
352 | .find(|t| t.kind() == T!['{'])? | ||
353 | .text_range() | ||
354 | .end(); | ||
355 | Some(start) | ||
356 | } | ||