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

refactor: Add a common codeAction method to the presentation compiler together with a supportedCodeActions method #6737

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,34 @@ class Compilers(
}
}.getOrElse(Future.successful(Nil.asJava))

def codeAction(
params: TextDocumentPositionParams,
token: CancelToken,
codeActionId: String,
codeActionPayload: Object,
KacperFKorban marked this conversation as resolved.
Show resolved Hide resolved
): Future[ju.List[TextEdit]] = {
withPCAndAdjustLsp(params) { (pc, pos, adjust) =>
pc.codeAction(
CompilerOffsetParamsUtils.fromPos(
pos,
token,
outlineFilesProvider.getOutlineFiles(pc.buildTargetId()),
),
codeActionId,
codeActionPayload,
).asScala
.map { edits =>
adjust.adjustTextEdits(edits)
}
}
}.getOrElse(Future.successful(Nil.asJava))

def supportedCodeActions(path: AbsolutePath): ju.List[String] = {
loadCompiler(path).map { pc =>
pc.supportedCodeActions()
}
}.getOrElse(Nil.asJava)

def hover(
params: HoverExtParams,
token: CancelToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ trait CodeAction {
*/
def kind: String

/**
* The CodeActionId for this code action, if applicable. CodeActionId is only
* used for code actions that require the use of the presentation compiler.
*/
def maybeCodeActionId: Option[String] = None

type CommandData
type ActionCommand = ParametrizedCommand[CommandData]
def command: Option[ActionCommand] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.jdk.CollectionConverters._

import scala.meta.internal.metals.MetalsEnrichments.XtensionString
import scala.meta.internal.metals._
import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.internal.metals.codeactions.CodeAction
Expand Down Expand Up @@ -32,34 +33,28 @@ final class CodeActionProvider(
new ActionableDiagnostic(),
new StringActions(buffers),
extractMemberAction,
new SourceOrganizeImports(
scalafixProvider,
buildTargets,
diagnostics,
),
new OrganizeImportsQuickFix(
scalafixProvider,
buildTargets,
diagnostics,
),
new SourceOrganizeImports(scalafixProvider, buildTargets, diagnostics),
new OrganizeImportsQuickFix(scalafixProvider, buildTargets, diagnostics),
new InsertInferredType(trees, compilers, languageClient),
new PatternMatchRefactor(trees),
new RewriteBracesParensCodeAction(trees),
new ExtractValueCodeAction(trees, buffers),
new CreateCompanionObjectCodeAction(trees, buffers),
new ExtractMethodCodeAction(trees, compilers, languageClient),
new InlineValueCodeAction(
trees,
compilers,
languageClient,
),
new InlineValueCodeAction(trees, compilers, languageClient),
new ConvertToNamedArguments(trees, compilers, languageClient),
new FlatMapToForComprehensionCodeAction(trees, buffers),
new MillifyDependencyCodeAction(buffers),
new MillifyScalaCliDependencyCodeAction(buffers),
new ConvertCommentCodeAction(buffers),
)

def actionsForParams(params: l.CodeActionParams): List[CodeAction] = {
val path = params.getTextDocument.getUri.toAbsolutePath
val supportedCodeActions = compilers.supportedCodeActions(path)
allActions.filter(_.maybeCodeActionId.forall(supportedCodeActions.contains))
}

def codeActions(
params: l.CodeActionParams,
token: CancelToken,
Expand All @@ -73,7 +68,7 @@ final class CodeActionProvider(
case None => true
}

val actions = allActions.collect {
val actions = actionsForParams(params).collect {
case action if isRequestedKind(action) =>
action.contribute(params, token)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class ConvertToNamedArguments(
import ConvertToNamedArguments._
override val kind: String = l.CodeActionKind.RefactorRewrite

override val maybeCodeActionId: Option[String] = Some(
"ConvertToNamedArguments"
)

override type CommandData = ServerCommands.ConvertToNamedArgsRequest

override def command: Option[ActionCommand] = Some(
Expand Down Expand Up @@ -57,7 +61,7 @@ class ConvertToNamedArguments(
} yield ()
}

def getTermWithArgs(
private def getTermWithArgs(
apply: Tree,
args: List[Tree],
nameEnd: Int,
Expand All @@ -80,7 +84,7 @@ class ConvertToNamedArguments(
}
}

def firstApplyWithUnnamedArgs(
private def firstApplyWithUnnamedArgs(
term: Option[Tree]
): Option[ApplyTermWithArgIndices] = {
term match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class ExtractMethodCodeAction(
)
override def kind: String = l.CodeActionKind.RefactorExtract

override val maybeCodeActionId: Option[String] = Some(
"ExtractMethod"
)

override def handleCommand(
data: ServerCommands.ExtractMethodParams,
token: CancelToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class ImplementAbstractMembers(compilers: Compilers) extends CodeAction {

override def kind: String = l.CodeActionKind.QuickFix

override val maybeCodeActionId: Option[String] = Some(
"ImplementAbstractMembers"
)

override def contribute(
params: l.CodeActionParams,
token: CancelToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class ImportMissingSymbol(compilers: Compilers, buildTargets: BuildTargets)

override def kind: String = l.CodeActionKind.QuickFix

override val maybeCodeActionId: Option[String] = Some(
"ImportMissingSymbol"
)

override def contribute(
params: l.CodeActionParams,
token: CancelToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class InlineValueCodeAction(

override def kind: String = l.CodeActionKind.RefactorInline

override val maybeCodeActionId: Option[String] = Some(
"InlineValue"
)

override def contribute(params: l.CodeActionParams, token: CancelToken)(
implicit ec: ExecutionContext
): Future[Seq[l.CodeAction]] = Future {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class InsertInferredType(
import InsertInferredType._
override def kind: String = l.CodeActionKind.QuickFix

override val maybeCodeActionId: Option[String] = Some(
"InsertInferredType"
)

override def handleCommand(
textDocumentParams: l.TextDocumentPositionParams,
token: CancelToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.net.URI;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.List;
Expand Down Expand Up @@ -111,6 +112,27 @@ public CompletableFuture<java.util.List<ReferencesResult>> references(References
return CompletableFuture.completedFuture(Collections.emptyList());
}

/**
* Execute the given code action
*/
public CompletableFuture<List<TextEdit>> codeAction(OffsetParams params, String codeActionId, Object codeActionPayload) {
return CompletableFuture.completedFuture(Collections.emptyList());
}

/**
* Returns the list of code actions supported by the current presentation compiler.
*/
public List<String> supportedCodeActions() {
return Arrays.asList(
"ConvertToNamedArguments",
"ExtractMethod",
"ImplementAbstractMembers",
"ImportMissingSymbol",
"InlineValue",
"InsertInferredType"
);
}

/**
* Return decoded and pretty printed TASTy content for .scala or .tasty file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ case class ScalaPresentationCompiler(
case Right(edits: List[l.TextEdit]) => edits.asJava
}
end convertToNamedArguments

override def selectionRange(
params: ju.List[OffsetParams]
): CompletableFuture[ju.List[l.SelectionRange]] =
Expand Down
Loading