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

Make compiler hint to import unqualified types/values #4304

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

mine-tech-oficial
Copy link

@mine-tech-oficial mine-tech-oficial commented Mar 5, 2025

This PR closes #4297

  • Show hint to import unqualified types
  • Show hint to import unqualified values
  • Consider the type's arity
  • Consider the value's arity

Currently the way it decides which module to hint is by preferring imported modules over importable ones, and sorting by name length.

@mine-tech-oficial
Copy link
Author

The hints should be done now, although I don't if its possible to find the arity (suppose someone wrote Dict, mentioning dict.Dict, but didn't put the types, which the code would recognize as "nullary"). Also, the current code for suggesting modules to import also doesn't take the arity into account.

@mine-tech-oficial mine-tech-oficial marked this pull request as ready for review March 6, 2025 13:22
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you! I've added some comments inline, and snapshot tests will need to be added to test all the new paths through the code.

It would be great to also check the arity of the constructors. Suggesting a zero-arity Wibble when it is being used in a way that takes 2 arguments would be unhelpful.

if is_type {
format!("Did you mean to import `{module}.{{type {name}}}`?")
} else {
format!("Did you mean to import `{module}.{{{name}}}`")
Copy link
Member

@lpil lpil Mar 10, 2025

Choose a reason for hiding this comment

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

I don't want to encourage unqualified importing of values. Perhaps we should only make these suggestions for types and record constructors.

Copy link
Author

Choose a reason for hiding this comment

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

Okay! Tying with my point below, we could write the "generic" value case as "Did you mean to reference module.{value}" and, in case it isn't already imported, "Did you mean to import module and reference module.{value}". What do you think?

@@ -2326,7 +2331,7 @@ but no type in scope with that name."
Diagnostic {
title: "Unknown variable".into(),
text,
hint: None,
hint: suggestions.first().map(|suggestion| suggestion.suggestion(name, false)),
Copy link
Member

Choose a reason for hiding this comment

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

There's a vector of suggestions, but only the first one is used. Seems like only one should be given in that case.

Copy link
Author

Choose a reason for hiding this comment

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

I thought this was fine, as that is what the current code for module suggestions does. Anyway, I'll change it!

Copy link
Author

Choose a reason for hiding this comment

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

Actually, having a vector of suggestions allows the LS to give all possible quick fixes when a code action to do so is done (I have one basically ready, will do a PR after this one is pushed)

@lpil lpil marked this pull request as draft March 10, 2025 12:58
@mine-tech-oficial
Copy link
Author

The code should now consider arity, but it is an Option as I don't think it is possible to find the arity in all cases.

@mine-tech-oficial mine-tech-oficial marked this pull request as ready for review March 10, 2025 23:13
@mine-tech-oficial mine-tech-oficial requested a review from lpil March 14, 2025 20:41
@mine-tech-oficial
Copy link
Author

Hey @lpil, I've answered some of your questions, waiting on clarifications!

@mine-tech-oficial
Copy link
Author

I have implemented the code actions locally. Should I push them to this PR or wait for a future one?

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you! Looking good. I've left some comments inline, and the tests still need to be added for each possible path through the code.

@lpil lpil marked this pull request as draft March 17, 2025 11:54
@mine-tech-oficial mine-tech-oficial marked this pull request as ready for review March 20, 2025 00:16
@mine-tech-oficial
Copy link
Author

mine-tech-oficial commented Mar 20, 2025

@lpil, I think I have addressed all of your points! Right now I just need to comment and write tests, and I'm waiting on some clarifications.

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you

@mine-tech-oficial
Copy link
Author

Should be all done now!

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you! Nearly there! Few small things around the wording

match self {
ModuleSuggestion::Importable(module) => match layer {
Layer::Type => format!("Did you mean to import `{module}.{{type {name}}}`?"),
Layer::Value => format!("Did you mean to import `{module}.{{{name}}}`?"),
Copy link
Member

Choose a reason for hiding this comment

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

This still has the confusing syntax that is easy to misread. Perhaps it can avoid using Gleam syntax altogether and copy the format used in the existing function.

Copy link
Author

Choose a reason for hiding this comment

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

What about "Did you mean to import module.name"?

Copy link
Member

Choose a reason for hiding this comment

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

That's not Gleam syntax either. It should either be Gleam syntax, or it should not look like code.

Let's use a sentence without any code-like bits, similar to the existing module error suggestion

Copy link
Author

Choose a reason for hiding this comment

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

What do you think about "Did you mean to import {module} and add {name} as a unqualified import" then?

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking something like "did you mean to import the {value} value from the {module} module?"

Copy link
Author

Choose a reason for hiding this comment

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

Oh, this is great! Thanks for the suggestion!

@mine-tech-oficial
Copy link
Author

Now it should be ready! Also, the way I've refactored, it allows for the normal module suggestion to consider arity in the future

@mine-tech-oficial mine-tech-oficial requested a review from lpil March 24, 2025 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show hint to import unqualified types/values
3 participants