-
Notifications
You must be signed in to change notification settings - Fork 399
feat: implement 'convert to named lambda parameters' code action #6669
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
Merged
tgodzik
merged 6 commits into
scalameta:main
from
KacperFKorban:convert-to-named-lambda-params
May 6, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f863d17
feat: implement 'convert to named lambda parameters' code action
KacperFKorban 8196bf4
Fix compilation after rebase with common code action interface
KacperFKorban 9ae0a02
Remove pc parts
KacperFKorban 0d0d81d
Merge branch 'main' into convert-to-named-lambda-params
KacperFKorban e86fe29
Use resolveCodeAction instead of codeAction; unignore test case for n…
KacperFKorban 0091b56
Fix code action resolution
KacperFKorban File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...rc/main/scala/scala/meta/internal/metals/codeactions/ConvertToNamedLambdaParameters.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package scala.meta.internal.metals.codeactions | ||
|
|
||
| import scala.concurrent.ExecutionContext | ||
| import scala.concurrent.Future | ||
|
|
||
| import scala.meta.Term | ||
| import scala.meta.internal.metals.Compilers | ||
| import scala.meta.internal.metals.MetalsEnrichments._ | ||
| import scala.meta.internal.metals.ServerCommands | ||
| import scala.meta.internal.metals.clients.language.MetalsLanguageClient | ||
| import scala.meta.internal.metals.codeactions.CodeAction | ||
| import scala.meta.internal.metals.codeactions.CodeActionBuilder | ||
| import scala.meta.internal.metals.logging | ||
| import scala.meta.internal.parsing.Trees | ||
| import scala.meta.pc.CancelToken | ||
| import scala.meta.pc.CodeActionId | ||
|
|
||
| import org.eclipse.{lsp4j => l} | ||
|
|
||
| /** | ||
| * Code action to convert a wildcard lambda to a lambda with named parameters | ||
| * e.g. | ||
| * | ||
| * List(1, 2).map(<<_>> + 1) => List(1, 2).map(i => i + 1) | ||
| */ | ||
| class ConvertToNamedLambdaParameters( | ||
| trees: Trees, | ||
| compilers: Compilers, | ||
| languageClient: MetalsLanguageClient, | ||
| ) extends CodeAction { | ||
|
|
||
| override val kind: String = l.CodeActionKind.RefactorRewrite | ||
|
|
||
| override type CommandData = | ||
| ServerCommands.ConvertToNamedLambdaParametersRequest | ||
|
|
||
| override def command: Option[ActionCommand] = Some( | ||
| ServerCommands.ConvertToNamedLambdaParameters | ||
| ) | ||
|
|
||
| override def handleCommand( | ||
| data: ServerCommands.ConvertToNamedLambdaParametersRequest, | ||
| token: CancelToken, | ||
| )(implicit ec: ExecutionContext): Future[Unit] = { | ||
| val uri = data.position.getTextDocument().getUri() | ||
| for { | ||
| edits <- compilers.codeAction( | ||
| data.position, | ||
| token, | ||
| CodeActionId.ConvertToNamedLambdaParameters, | ||
| None, | ||
| ) | ||
| _ = logging.logErrorWhen( | ||
| edits.isEmpty(), | ||
| s"Could not convert lambda at position ${data.position} to named lambda", | ||
| ) | ||
| workspaceEdit = new l.WorkspaceEdit(Map(uri -> edits).asJava) | ||
| _ <- languageClient | ||
| .applyEdit(new l.ApplyWorkspaceEditParams(workspaceEdit)) | ||
| .asScala | ||
| } yield () | ||
| } | ||
|
|
||
| override def contribute( | ||
| params: l.CodeActionParams, | ||
| token: CancelToken, | ||
| )(implicit ec: ExecutionContext): Future[Seq[l.CodeAction]] = { | ||
| val path = params.getTextDocument().getUri().toAbsolutePath | ||
| val range = params.getRange() | ||
| val maybeLambda = | ||
| trees.findLastEnclosingAt[Term.AnonymousFunction](path, range.getStart()) | ||
| maybeLambda | ||
| .map { lambda => | ||
| val position = new l.TextDocumentPositionParams( | ||
| params.getTextDocument(), | ||
| new l.Position(lambda.pos.startLine, lambda.pos.startColumn), | ||
| ) | ||
| val command = | ||
| ServerCommands.ConvertToNamedLambdaParameters.toLsp( | ||
| ServerCommands.ConvertToNamedLambdaParametersRequest(position) | ||
| ) | ||
| val codeAction = CodeActionBuilder.build( | ||
| title = ConvertToNamedLambdaParameters.title, | ||
| kind = kind, | ||
| command = Some(command), | ||
| ) | ||
| Future.successful(Seq(codeAction)) | ||
| } | ||
| .filter(_ => | ||
| compilers | ||
| .supportedCodeActions(path) | ||
| .contains(CodeActionId.ConvertToNamedLambdaParameters) | ||
| ) | ||
| .getOrElse(Future.successful(Nil)) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| object ConvertToNamedLambdaParameters { | ||
| def title: String = "Convert to named lambda parameters" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.