rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Feature request: Inline type alias

Open jplatte opened this issue 3 years ago • 9 comments

Similar to "Inline function" / "Inline into all callers", it would be nice if there was "Inline typealias" and "Inline into all uses" for type aliases.

jplatte avatar Nov 28 '21 11:11 jplatte

Was thinking about this yesterday, would’ve been nice for https://github.com/rustls/rustls/pull/895.

djc avatar Dec 30 '21 14:12 djc

I'll try to implement this.

steven-joruk avatar Mar 02 '22 16:03 steven-joruk

@Veykril It seems we can close this issue?

0xPoe avatar Jul 05 '22 13:07 0xPoe

Hey @hi-rustin, #11690 only added inlining a single instance of a type alias use, this feature also requests inlining the definition to all users.

steven-joruk avatar Jul 05 '22 16:07 steven-joruk

I'm going to try this one

sancho20021 avatar Aug 08 '22 07:08 sancho20021

Hi @steven-joruk! Currently I'm trying to implement inline_alias_uses.

You added a nice test: https://github.com/rust-lang/rust-analyzer/blob/49700e46366bfe7958204877b4edcf74987ec508/crates/ide-assists/src/handlers/inline_type_alias.rs#L385-L398

which shows that the current implementation can't inline such alias uses.

IIUC, this happens because ctx.find_node_at_offset::<ast::PathType>() invocation returns None, and we have no alias instance to work with.

Could you please explain, why ctx.find_node_at_offset behaves in such a way? This is my first issue in rust-analyzer so I have very little understanding of things :smile:

sancho20021 avatar Aug 08 '22 15:08 sancho20021

An ast::PathType specifically is an ast::Path that appreas in type position, as an example let PathPat: PathType = PathExpr;.

So to support inlining on usages, we should be checking ctx.find_note_at_offset::<ast::Path>(), then check the parent node of that path and act accordingly.

Veykril avatar Aug 08 '22 15:08 Veykril

Besides calling an associated function on an aliased type, there seems to be a lot of other usages. (Enum variant, Record expression, etc)

What is more important that needs to be done first, cover as much usages as possible in inline_type_alias or implementing a basic version of inline_type_alias_uses that works only for PathType?

By the way, I'm not sure I understand, what does Path represent?

sancho20021 avatar Aug 11 '22 18:08 sancho20021

A Path is just something like foo::bar::Baz or foo on its own as well. PathType, PathPat, PathExpr etc just wrap a Path then depending on whether it is a type, pattern expression

It doesnt really matter what you improve herefirst, thats up to you

Veykril avatar Aug 11 '22 18:08 Veykril