Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow crate authors to control completion of their things #19375

Merged
merged 1 commit into from
Mar 28, 2025

Conversation

ChayimFriedman2
Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 commented Mar 16, 2025

Via the new #[rust_analyzer::completions(...)] attribute.

Closes #17477.

Also fix a bug with existing settings for that where the paths wouldn't resolve correctly. @Veykril it seems you just blessed the tests incorrectly in #18179?

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 16, 2025
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this infesting all of hir-def and the like with ide specific things (though I do like the changing of flags to all use bitflags).

Given this only affects flyimport (with the exception of traits), I feel like this would be more fitting to be recorded as a flag in the ImportMap ImportInfo entries and then either be filtered immediately in search_for_imports or have that flag propagate out of search_for_imports.

@@ -6343,3 +6483,42 @@ where
self(item)
}
}

pub fn resolve_absolute_path(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method does not make sense, a crate name only exists as a name of a dependency edge. That is we can only resolve paths from within the context of some crate.

Comment on lines -799 to +804
scope
.resolve_mod_path(&ModPath::from_segments(
hir::PathKind::Plain,
path.split("::").map(Symbol::intern).map(Name::new_symbol_root),
))
.find_map(|it| match it {
hir::resolve_absolute_path(db, path.split("::").map(Symbol::intern)).find_map(
|it| match it {
hir::ItemInNs::Types(ModuleDef::Trait(t)) => Some(t),
_ => None,
})
},
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, that is probably regarding

Also fix a bug with existing settings for that where the paths wouldn't resolve correctly. @Veykril it seems you just blessed the tests incorrectly in #18179?

No that was intentional, we need to resolve the paths from the given scope. Though I suppose that hir::PathKind::Plain really should've been hir::PathKind::Absolute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test blessing was probably not intentional, as it rendered those tests meaningless.

About

This method does not make sense, a crate name only exists as a name of a dependency edge. That is we can only resolve paths from within the context of some crate.

Generally I agree, but this is a global setting, so it doesn't depend at the current crate. At the extreme what could happen is that in different crates it will resolve to different things. More problematic, this prevents marking an item from the crate itself, as seen by the tests (without hacks). Also, if some crate reexports another crate, you will have to either write both or let the setting be ignored in some places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough

@ChayimFriedman2
Copy link
Contributor Author

Given this only affects flyimport (with the exception of traits), I feel like this would be more fitting to be recorded as a flag in the ImportMap ImportInfo entries and then either be filtered immediately in search_for_imports or have that flag propagate out of search_for_imports.

What about traits? Should they remain part of hir-def or should we lookup the attribute in hir?

@Veykril
Copy link
Member

Veykril commented Mar 16, 2025

We should probably lookup the attribute in hir

@ChayimFriedman2
Copy link
Contributor Author

I changed this to check the attribute in the import and symbols map. We can't filter it in search_for_imports() as other things are using it (e.g. qualify assist) that we don't want to filter that in.

@Veykril
Copy link
Member

Veykril commented Mar 17, 2025

Design wise, I wonder if we should go for something more generic like rust_analyzer::completions(relevant stuff here)?

@ChayimFriedman2
Copy link
Contributor Author

Fixed the multi crates problem and removed legacy macros.

@davidbarsky
Copy link
Contributor

Maybe something more generic would be appropriate. Two questions:

  1. How should RustRover handle this? Pretend to be rust-analyzer?
  2. We already ignore #[doc(hidden)] items in completions, right?

@ChayimFriedman2
Copy link
Contributor Author

ChayimFriedman2 commented Mar 17, 2025

  1. How should RustRover handle this? Pretend to be rust-analyzer?

I guess yes, if they want? We don't have a #[rustrover::] or an #[ide::] tool namespace, but we do have a #[rust_analyzer::] tool namespace.

  1. We already ignore #[doc(hidden)] items in completions, right?

Yes, but e.g. owo_colors will not want to use that, because they do want the methods to show up in documentation. We need something more specific.

@ChayimFriedman2
Copy link
Contributor Author

I'm not objected to a more general attribute, but I also think we shouldn't hang this indefinitely on the perfect design choice. At worst, we can declare no stability.

Via the new `#[rust_analyzer::completions(...)]` attribute.

Also fix a bug with existing settings for that where the paths wouldn't resolve correctly.
@Veykril Veykril added this pull request to the merge queue Mar 28, 2025
@Veykril
Copy link
Member

Veykril commented Mar 28, 2025

Forgot to hit the button.
Either way nice, thanks!

Merged via the queue into rust-lang:master with commit 2e1ff25 Mar 28, 2025
10 checks passed
@ChayimFriedman2 ChayimFriedman2 deleted the do-not-complete branch March 28, 2025 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto-import of modules plus inability to order completions leads to pathalogical cases
4 participants