[terminal] IJPL-159651 Extract enum of provided integration functions

GitOrigin-RevId: d28367fc695bd935d1eaf64fe5e2fa56db471280
This commit is contained in:
Vladimir Shefer
2024-08-14 13:55:14 +02:00
committed by intellij-monorepo-bot
parent e555a9d95a
commit 70d51863b8
7 changed files with 45 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ import git4idea.repo.GitRepositoryManager
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.SoftAssertions
import org.jetbrains.plugins.terminal.block.session.ShellIntegrationFunctions.GET_DIRECTORY_FILES
import org.jetbrains.plugins.terminal.block.util.ShellCompletionTestFixture
import org.mockito.Mockito.*
@@ -177,7 +178,7 @@ class GitShellCommandOverrideSpecTest : BasePlatformTestCase() {
remotes: List<GitRemote> = ALL_REMOTES
): ShellCompletionTestFixture = ShellCompletionTestFixture.builder(project)
.mockShellCommandResults { command ->
if (command.startsWith("__jetbrains_intellij_get_directory_files")) {
if (command.startsWith(GET_DIRECTORY_FILES.functionName)) {
return@mockShellCommandResults ShellCommandResult.create("file1\nfile2", exitCode = 0)
}
if (!command.startsWith("git")) error("Unknown command: $command")

View File

@@ -15,13 +15,14 @@ import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import org.jetbrains.plugins.terminal.block.completion.spec.ShellRuntimeDataGenerator
import org.jetbrains.plugins.terminal.block.session.ShellCommandExecutionManagerImpl
import org.jetbrains.plugins.terminal.block.session.ShellIntegrationFunctions.GET_COMPLETIONS
internal fun powerShellCompletionGenerator(command: String, caretOffset: Int): ShellRuntimeDataGenerator<CompletionResult> {
return ShellRuntimeDataGenerator(debugName = "powershell completion") { context ->
assert(context.shellName.isPowerShell())
val escapedCommand = ShellCommandExecutionManagerImpl.escapePowerShellParameter(command)
val commandResult = context.runShellCommand("""__JetBrainsIntellijGetCompletions "$escapedCommand" $caretOffset""")
val commandResult = context.runShellCommand("""${GET_COMPLETIONS.functionName} "$escapedCommand" $caretOffset""")
if (commandResult.exitCode != 0) {
logger<PowerShellCompletionContributor>().warn("PowerShell completion generator for command '$command' at offset $caretOffset failed with exit code ${commandResult.exitCode}, output: ${commandResult.output}")
return@ShellRuntimeDataGenerator emptyCompletionResult()

View File

@@ -6,6 +6,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.terminal.completion.spec.ShellRuntimeContext
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.plugins.terminal.block.session.ShellIntegrationFunctions.GET_DIRECTORY_FILES
import java.io.File
@get:ApiStatus.Experimental
@@ -27,7 +28,7 @@ suspend fun ShellRuntimeContext.getChildFiles(
onlyDirectories: Boolean = false
): List<String> {
val adjustedPath = path.ifEmpty { "." }
val result = runShellCommand("__jetbrains_intellij_get_directory_files $adjustedPath")
val result = runShellCommand("${GET_DIRECTORY_FILES.functionName} $adjustedPath")
if (result.exitCode != 0) {
logger<ShellRuntimeContext>().warn("Get files command for path '$adjustedPath' failed with exit code ${result.exitCode}, output: ${result.output}")
return emptyList()

View File

@@ -9,6 +9,8 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.jetbrains.annotations.VisibleForTesting
import org.jetbrains.plugins.terminal.block.completion.spec.ShellRuntimeDataGenerator
import org.jetbrains.plugins.terminal.block.session.ShellIntegrationFunctions
import org.jetbrains.plugins.terminal.block.shellintegration.ShellIntegrationGenerators
import org.jetbrains.plugins.terminal.exp.completion.TerminalShellSupport
internal object ShellEnvBasedGenerators {
@@ -26,7 +28,7 @@ internal object ShellEnvBasedGenerators {
}
suspend fun getShellEnv(context: ShellRuntimeContext): ShellEnvironment? {
val result = context.runShellCommand("__jetbrains_intellij_get_environment")
val result = context.runShellCommand(ShellIntegrationFunctions.GET_ENVIRONMENT.functionName)
if (result.exitCode != 0) {
LOG.error("Get shell environment command failed with exit code ${result.exitCode}, output: ${result.output}")
return null

View File

@@ -0,0 +1,31 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.terminal.block.session;
import org.jetbrains.annotations.ApiStatus;
/**
* The Shell functions, provided by shell integrations (i.e. command-block-support)
*/
@ApiStatus.Internal
public enum ShellIntegrationFunctions {
GET_ENVIRONMENT("__jetbrains_intellij_get_environment"),
GET_DIRECTORY_FILES("__jetbrains_intellij_get_directory_files"),
/**
* POWERSHELL ONLY
* argument: 1. escapedCommand
* argument: 2. caretOffset
*/
GET_COMPLETIONS("__JetBrainsIntellijGetCompletions")
;
ShellIntegrationFunctions(String functionName) {
myFunctionName = functionName;
}
private final String myFunctionName;
public String getFunctionName() {
return myFunctionName;
}
}

View File

@@ -4,6 +4,7 @@ package org.jetbrains.plugins.terminal.block.completion
import com.intellij.terminal.completion.spec.ShellCommandResult
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.plugins.terminal.block.session.ShellIntegrationFunctions.GET_DIRECTORY_FILES
import org.jetbrains.plugins.terminal.block.util.ShellCompletionTestFixture
import org.junit.Test
import org.junit.runner.RunWith
@@ -37,7 +38,7 @@ internal class ShellCdCommandTest : BasePlatformTestCase() {
private fun createFixture(files: List<String>, expectedPath: String): ShellCompletionTestFixture {
return ShellCompletionTestFixture.builder(project)
.mockShellCommandResults { command ->
if (command == "__jetbrains_intellij_get_directory_files $expectedPath") {
if (command == "${GET_DIRECTORY_FILES.functionName} $expectedPath") {
ShellCommandResult.create(files.joinToString("\n"), exitCode = 0)
}
else error("Unknown command: $command")

View File

@@ -11,6 +11,7 @@ import kotlinx.coroutines.runBlocking
import org.jetbrains.plugins.terminal.block.completion.spec.ShellAliasSuggestion
import org.jetbrains.plugins.terminal.block.completion.spec.ShellCommandSpec
import org.jetbrains.plugins.terminal.block.completion.spec.ShellDataGenerators.fileSuggestionsGenerator
import org.jetbrains.plugins.terminal.block.session.ShellIntegrationFunctions.GET_DIRECTORY_FILES
import org.jetbrains.plugins.terminal.block.util.TestCommandSpecsManager
import org.jetbrains.plugins.terminal.block.util.TestGeneratorsExecutor
import org.jetbrains.plugins.terminal.block.util.TestRuntimeContextProvider
@@ -367,8 +368,8 @@ internal class ShellCommandTreeBuilderTest {
private suspend fun doTestImpl(arguments: List<String>, assertions: ShellCommandTreeAssertions.() -> Unit) {
// Mock fileSuggestionsGenerator result
val generatorCommandsRunner = ShellCommandExecutor { command ->
if (command.startsWith("__jetbrains_intellij_get_directory_files")) {
val path = command.removePrefix("__jetbrains_intellij_get_directory_files").trim()
if (command.startsWith(GET_DIRECTORY_FILES.functionName)) {
val path = command.removePrefix(GET_DIRECTORY_FILES.functionName).trim()
val files = filePathSuggestions[path]
if (files != null) {
ShellCommandResult.create(files.joinToString("\n"), exitCode = 0)