Skip to content

Commit 8d3c8ca

Browse files
committed
refactor: Use codeAction interface for most code actions
1 parent 3ea209b commit 8d3c8ca

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

presentation-compiler/src/main/dotty/tools/pc/ScalaPresentationCompiler.scala

+51-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ case class ScalaPresentationCompiler(
5555
completionItemPriority: CompletionItemPriority = (_: String) => 0,
5656
) extends PresentationCompiler:
5757

58+
override def supportedCodeActions(): ju.List[String] = List(
59+
CodeActionId.ConvertToNamedArguments,
60+
CodeActionId.ImplementAbstractMembers,
61+
CodeActionId.ExtractMethod,
62+
CodeActionId.InlineValue,
63+
CodeActionId.InsertInferredType
64+
).asJava
65+
5866
def this() = this("", None, Nil, Nil)
5967

6068
val scalaVersion = BuildInfo.scalaVersion
@@ -67,6 +75,42 @@ case class ScalaPresentationCompiler(
6775
.map(StdReportContext(_, _ => buildTargetName, reportsLevel))
6876
.getOrElse(EmptyReportContext)
6977

78+
override def codeAction[T](
79+
params: OffsetParams,
80+
codeActionId: String,
81+
codeActionPayload: Optional[T]
82+
): CompletableFuture[ju.List[TextEdit]] = {
83+
(codeActionId, codeActionPayload.asScala) match {
84+
case (
85+
CodeActionId.ConvertToNamedArguments,
86+
Some(argIndices: ju.List[_])
87+
) =>
88+
val payload = argIndices.asScala.collect { case i: Integer =>
89+
i.toInt
90+
}.toSet
91+
convertToNamedArguments(params, payload)
92+
case (CodeActionId.ImplementAbstractMembers, _) =>
93+
implementAbstractMembers(params)
94+
case (CodeActionId.InsertInferredType, _) =>
95+
insertInferredType(params)
96+
case (CodeActionId.InlineValue, _) =>
97+
inlineValue(params)
98+
case (CodeActionId.ExtractMethod, Some(extractionPos: OffsetParams)) =>
99+
params match {
100+
case range: RangeParams =>
101+
extractMethod(range, extractionPos)
102+
case _ =>
103+
CompletableFuture.failedFuture(
104+
new IllegalArgumentException(s"Expected range parameters")
105+
)
106+
}
107+
case (id, _) =>
108+
CompletableFuture.failedFuture(
109+
new IllegalArgumentException(s"Unsupported action id $id")
110+
)
111+
}
112+
}
113+
70114
override def withCompletionItemPriority(
71115
priority: CompletionItemPriority
72116
): PresentationCompiler =
@@ -348,14 +392,20 @@ case class ScalaPresentationCompiler(
348392
override def convertToNamedArguments(
349393
params: OffsetParams,
350394
argIndices: ju.List[Integer]
395+
): CompletableFuture[ju.List[l.TextEdit]] =
396+
convertToNamedArguments(params, argIndices.asScala.toSet.map(_.toInt))
397+
398+
def convertToNamedArguments(
399+
params: OffsetParams,
400+
argIndices: Set[Int]
351401
): CompletableFuture[ju.List[l.TextEdit]] =
352402
val empty: Either[String, List[l.TextEdit]] = Right(List())
353403
(compilerAccess
354404
.withNonInterruptableCompiler(Some(params))(empty, params.token()) { pc =>
355405
new ConvertToNamedArgumentsProvider(
356406
pc.compiler(),
357407
params,
358-
argIndices.asScala.map(_.toInt).toSet
408+
argIndices
359409
).convertToNamedArguments
360410
})
361411
.thenApplyAsync {

0 commit comments

Comments
 (0)