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

Disable OrganizeImports commands if they are not available #6595

Merged
merged 3 commits into from
Aug 19, 2024

Conversation

wjoel
Copy link
Contributor

@wjoel wjoel commented Jul 15, 2024

Rather than having Metals show a warning message, leave it to the client to decide on what to do with the reason for why they are disabled, and if/how it should be displayed.

Fixes scalameta/metals-zed#14

Then it looks like this in VS Code:
organize-imports-disabled-vscode

Rather than having Metals show a warning message, leave it to the
client to decide on what to do with the reason for why they are
disabled, and if/how it should be displayed.
val hasUnused = params
.getContext()
.getDiagnostics()
.asScala
.collect { case ScalacDiagnostic.UnusedImport(name) => name }
.nonEmpty

if (hasUnused && !diagnostics.hasDiagnosticError(file)) {
true
if (!hasUnused) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should arguably also check if (!isScalaOrSbt(file)), as the other one does?

Copy link
Contributor

Choose a reason for hiding this comment

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

That would make sense, though I don't think Metals will be invoked for anything else than Scala and Java files. Maybe we should be explicit about not doing organize import on Java

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this! I wasn't aware we had the disabled flag before.

val hasUnused = params
.getContext()
.getDiagnostics()
.asScala
.collect { case ScalacDiagnostic.UnusedImport(name) => name }
.nonEmpty

if (hasUnused && !diagnostics.hasDiagnosticError(file)) {
true
if (!hasUnused) {
Copy link
Contributor

Choose a reason for hiding this comment

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

That would make sense, though I don't think Metals will be invoked for anything else than Scala and Java files. Maybe we should be explicit about not doing organize import on Java

Some("Only supported for Scala and sbt files")
} else if (diagnostics.hasDiagnosticError(file)) {
scribe.info(canNotOrganizeImportsWithErrors)
Some(canNotOrganizeImportsWithErrors)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

We are now missing one of the conditions isSourceOrganizeImportCalled(params) and organize imports action is returned everywhere. We want to return it on unused imports and also when explicitly called. Maybe we should still keep isCallAllowed for that and also check that flag?

@wjoel
Copy link
Contributor Author

wjoel commented Aug 5, 2024

It'll be a week or two before I'll have time to get back to this, so if anyone else wants to address the comments, please feel free to do so.

Otherwise I will get it done (and Zed is being a bit annoying even with those changes, but otherwise it's quite fun overall, so I have some strong personal motivations), but definitely not this week.

@tgodzik
Copy link
Contributor

tgodzik commented Aug 6, 2024

It'll be a week or two before I'll have time to get back to this, so if anyone else wants to address the comments, please feel free to do so.

Otherwise I will get it done (and Zed is being a bit annoying even with those changes, but otherwise it's quite fun overall, so I have some strong personal motivations), but definitely not this week.

There is no hurry! Just let us know if you have any problems or are unable to finish it.

@wjoel wjoel requested a review from tgodzik August 18, 2024 20:11
Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Thanks! I pushed a minor fix to the tests to include information if an action is disabled

@tgodzik tgodzik merged commit ead4e2e into scalameta:main Aug 19, 2024
21 of 22 checks passed
@wjoel wjoel deleted the disable-organize-imports-on-errors branch August 19, 2024 20:09
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.

Fix before trying to organize your imports never goes away
2 participants