Skip to content

Commit

Permalink
Fix compilation after rebase with common code action interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Nov 5, 2024
1 parent 449b189 commit 746a923
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
14 changes: 0 additions & 14 deletions metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -918,20 +918,6 @@ class Compilers(
}
}.getOrElse(Future.successful(Nil.asJava))

def convertToNamedLambdaParameters(
position: TextDocumentPositionParams,
token: CancelToken,
): Future[ju.List[TextEdit]] = {
withPCAndAdjustLsp(position) { (pc, pos, adjust) =>
pc.convertToNamedLambdaParameters(
CompilerOffsetParamsUtils.fromPos(pos, token)
).asScala
.map { edits =>
adjust.adjustTextEdits(edits)
}
}
}.getOrElse(Future.successful(Nil.asJava))

def implementAbstractMembers(
params: TextDocumentPositionParams,
token: CancelToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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}

Expand Down Expand Up @@ -43,9 +44,11 @@ class ConvertToNamedLambdaParameters(
)(implicit ec: ExecutionContext): Future[Unit] = {
val uri = data.position.getTextDocument().getUri()
for {
edits <- compilers.convertToNamedLambdaParameters(
edits <- compilers.codeAction(
data.position,
token,
CodeActionId.ConvertToNamedLambdaParameters,
null,
)
_ = logging.logErrorWhen(
edits.isEmpty(),
Expand Down Expand Up @@ -83,6 +86,11 @@ class ConvertToNamedLambdaParameters(
)
Future.successful(Seq(codeAction))
}
.filter(_ =>
compilers
.supportedCodeActions(path)
.contains(CodeActionId.ConvertToNamedLambdaParameters)
)
.getOrElse(Future.successful(Nil))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class CodeActionId {
public static final String ImportMissingSymbol = "ImportMissingSymbol";
public static final String InlineValue = "InlineValue";
public static final String InsertInferredType = "InsertInferredType";
public static final String ConvertToNamedLambdaParameters = "ConvertToNamedLambdaParameters";
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,6 @@ public CompletableFuture<List<TextEdit>> inlineValue(OffsetParams params) {
public abstract CompletableFuture<List<TextEdit>> convertToNamedArguments(OffsetParams params,
List<Integer> argIndices);

/**
* Return the text edits for converting a wildcard lambda to a named lambda.
*/
public CompletableFuture<List<TextEdit>> convertToNamedLambdaParameters(OffsetParams params) {
return CompletableFuture.supplyAsync(() -> {
throw new DisplayableException("Convert to named lambda parameters is not available in this version of Scala");
});
};

/**
* The text contents of the given file changed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ case class ScalaPresentationCompiler(
): PresentationCompiler =
copy(completionItemPriority = priority)

override def supportedCodeActions(): ju.List[String] = List(
CodeActionId.ConvertToNamedLambdaParameters
).asJava

val compilerAccess: CompilerAccess[StoreReporter, MetalsDriver] =
Scala3CompilerAccess(
config,
Expand Down Expand Up @@ -153,8 +157,22 @@ case class ScalaPresentationCompiler(
folderPath,
completionItemPriority,
).completions()
}

override def codeAction[T](
params: OffsetParams,
codeActionId: String,
codeActionPayload: ju.Optional[T]
): ju.concurrent.CompletableFuture[ju.List[l.TextEdit]] = {
(codeActionId, codeActionPayload) match {
case (CodeActionId.ConvertToNamedLambdaParameters, _) =>
convertToNamedLambdaParameters(params)
case (id, _) =>
CompletableFuture.failedFuture(
new IllegalArgumentException(s"Unsupported action id $id")
)
}
}

def definition(params: OffsetParams): CompletableFuture[DefinitionResult] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
Expand Down Expand Up @@ -364,7 +382,7 @@ case class ScalaPresentationCompiler(
}
end convertToNamedArguments

override def convertToNamedLambdaParameters(
def convertToNamedLambdaParameters(
params: OffsetParams
): ju.concurrent.CompletableFuture[ju.List[l.TextEdit]] =
val empty: Either[String, List[l.TextEdit]] = Right(List())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.net.URI
import scala.meta.internal.jdk.CollectionConverters._
import scala.meta.internal.metals.CompilerOffsetParams
import scala.meta.internal.metals.TextEdits
import scala.meta.pc.CodeActionId

import munit.Location
import munit.TestOptions
Expand Down Expand Up @@ -166,8 +167,10 @@ class ConvertToNamedLambdaParametersSuite extends BaseCodeActionSuite {
): List[l.TextEdit] = {
val (code, _, offset) = params(original)
val result = presentationCompiler
.convertToNamedLambdaParameters(
CompilerOffsetParams(URI.create(filename), code, offset, cancelToken)
.codeAction(
CompilerOffsetParams(URI.create(filename), code, offset, cancelToken),
CodeActionId.ConvertToNamedLambdaParameters,
null
)
.get()
result.asScala.toList
Expand Down

0 comments on commit 746a923

Please sign in to comment.