mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 22:51:17 +07:00
[terminal] IJPL-159651 Extract enum of provided integration functions
GitOrigin-RevId: d28367fc695bd935d1eaf64fe5e2fa56db471280
This commit is contained in:
committed by
intellij-monorepo-bot
parent
e555a9d95a
commit
70d51863b8
@@ -18,6 +18,7 @@ import git4idea.repo.GitRepositoryManager
|
|||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.assertj.core.api.SoftAssertions
|
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.jetbrains.plugins.terminal.block.util.ShellCompletionTestFixture
|
||||||
import org.mockito.Mockito.*
|
import org.mockito.Mockito.*
|
||||||
|
|
||||||
@@ -177,7 +178,7 @@ class GitShellCommandOverrideSpecTest : BasePlatformTestCase() {
|
|||||||
remotes: List<GitRemote> = ALL_REMOTES
|
remotes: List<GitRemote> = ALL_REMOTES
|
||||||
): ShellCompletionTestFixture = ShellCompletionTestFixture.builder(project)
|
): ShellCompletionTestFixture = ShellCompletionTestFixture.builder(project)
|
||||||
.mockShellCommandResults { command ->
|
.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)
|
return@mockShellCommandResults ShellCommandResult.create("file1\nfile2", exitCode = 0)
|
||||||
}
|
}
|
||||||
if (!command.startsWith("git")) error("Unknown command: $command")
|
if (!command.startsWith("git")) error("Unknown command: $command")
|
||||||
|
|||||||
@@ -15,13 +15,14 @@ import kotlinx.serialization.encoding.Encoder
|
|||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import org.jetbrains.plugins.terminal.block.completion.spec.ShellRuntimeDataGenerator
|
import org.jetbrains.plugins.terminal.block.completion.spec.ShellRuntimeDataGenerator
|
||||||
import org.jetbrains.plugins.terminal.block.session.ShellCommandExecutionManagerImpl
|
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> {
|
internal fun powerShellCompletionGenerator(command: String, caretOffset: Int): ShellRuntimeDataGenerator<CompletionResult> {
|
||||||
return ShellRuntimeDataGenerator(debugName = "powershell completion") { context ->
|
return ShellRuntimeDataGenerator(debugName = "powershell completion") { context ->
|
||||||
assert(context.shellName.isPowerShell())
|
assert(context.shellName.isPowerShell())
|
||||||
|
|
||||||
val escapedCommand = ShellCommandExecutionManagerImpl.escapePowerShellParameter(command)
|
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) {
|
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}")
|
logger<PowerShellCompletionContributor>().warn("PowerShell completion generator for command '$command' at offset $caretOffset failed with exit code ${commandResult.exitCode}, output: ${commandResult.output}")
|
||||||
return@ShellRuntimeDataGenerator emptyCompletionResult()
|
return@ShellRuntimeDataGenerator emptyCompletionResult()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.openapi.util.Key
|
import com.intellij.openapi.util.Key
|
||||||
import com.intellij.terminal.completion.spec.ShellRuntimeContext
|
import com.intellij.terminal.completion.spec.ShellRuntimeContext
|
||||||
import org.jetbrains.annotations.ApiStatus
|
import org.jetbrains.annotations.ApiStatus
|
||||||
|
import org.jetbrains.plugins.terminal.block.session.ShellIntegrationFunctions.GET_DIRECTORY_FILES
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@get:ApiStatus.Experimental
|
@get:ApiStatus.Experimental
|
||||||
@@ -27,7 +28,7 @@ suspend fun ShellRuntimeContext.getChildFiles(
|
|||||||
onlyDirectories: Boolean = false
|
onlyDirectories: Boolean = false
|
||||||
): List<String> {
|
): List<String> {
|
||||||
val adjustedPath = path.ifEmpty { "." }
|
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) {
|
if (result.exitCode != 0) {
|
||||||
logger<ShellRuntimeContext>().warn("Get files command for path '$adjustedPath' failed with exit code ${result.exitCode}, output: ${result.output}")
|
logger<ShellRuntimeContext>().warn("Get files command for path '$adjustedPath' failed with exit code ${result.exitCode}, output: ${result.output}")
|
||||||
return emptyList()
|
return emptyList()
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import kotlinx.serialization.Serializable
|
|||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import org.jetbrains.annotations.VisibleForTesting
|
import org.jetbrains.annotations.VisibleForTesting
|
||||||
import org.jetbrains.plugins.terminal.block.completion.spec.ShellRuntimeDataGenerator
|
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
|
import org.jetbrains.plugins.terminal.exp.completion.TerminalShellSupport
|
||||||
|
|
||||||
internal object ShellEnvBasedGenerators {
|
internal object ShellEnvBasedGenerators {
|
||||||
@@ -26,7 +28,7 @@ internal object ShellEnvBasedGenerators {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getShellEnv(context: ShellRuntimeContext): ShellEnvironment? {
|
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) {
|
if (result.exitCode != 0) {
|
||||||
LOG.error("Get shell environment command failed with exit code ${result.exitCode}, output: ${result.output}")
|
LOG.error("Get shell environment command failed with exit code ${result.exitCode}, output: ${result.output}")
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ package org.jetbrains.plugins.terminal.block.completion
|
|||||||
import com.intellij.terminal.completion.spec.ShellCommandResult
|
import com.intellij.terminal.completion.spec.ShellCommandResult
|
||||||
import com.intellij.testFramework.fixtures.BasePlatformTestCase
|
import com.intellij.testFramework.fixtures.BasePlatformTestCase
|
||||||
import kotlinx.coroutines.runBlocking
|
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.jetbrains.plugins.terminal.block.util.ShellCompletionTestFixture
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
@@ -37,7 +38,7 @@ internal class ShellCdCommandTest : BasePlatformTestCase() {
|
|||||||
private fun createFixture(files: List<String>, expectedPath: String): ShellCompletionTestFixture {
|
private fun createFixture(files: List<String>, expectedPath: String): ShellCompletionTestFixture {
|
||||||
return ShellCompletionTestFixture.builder(project)
|
return ShellCompletionTestFixture.builder(project)
|
||||||
.mockShellCommandResults { command ->
|
.mockShellCommandResults { command ->
|
||||||
if (command == "__jetbrains_intellij_get_directory_files $expectedPath") {
|
if (command == "${GET_DIRECTORY_FILES.functionName} $expectedPath") {
|
||||||
ShellCommandResult.create(files.joinToString("\n"), exitCode = 0)
|
ShellCommandResult.create(files.joinToString("\n"), exitCode = 0)
|
||||||
}
|
}
|
||||||
else error("Unknown command: $command")
|
else error("Unknown command: $command")
|
||||||
|
|||||||
@@ -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.ShellAliasSuggestion
|
||||||
import org.jetbrains.plugins.terminal.block.completion.spec.ShellCommandSpec
|
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.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.TestCommandSpecsManager
|
||||||
import org.jetbrains.plugins.terminal.block.util.TestGeneratorsExecutor
|
import org.jetbrains.plugins.terminal.block.util.TestGeneratorsExecutor
|
||||||
import org.jetbrains.plugins.terminal.block.util.TestRuntimeContextProvider
|
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) {
|
private suspend fun doTestImpl(arguments: List<String>, assertions: ShellCommandTreeAssertions.() -> Unit) {
|
||||||
// Mock fileSuggestionsGenerator result
|
// Mock fileSuggestionsGenerator result
|
||||||
val generatorCommandsRunner = ShellCommandExecutor { command ->
|
val generatorCommandsRunner = ShellCommandExecutor { command ->
|
||||||
if (command.startsWith("__jetbrains_intellij_get_directory_files")) {
|
if (command.startsWith(GET_DIRECTORY_FILES.functionName)) {
|
||||||
val path = command.removePrefix("__jetbrains_intellij_get_directory_files").trim()
|
val path = command.removePrefix(GET_DIRECTORY_FILES.functionName).trim()
|
||||||
val files = filePathSuggestions[path]
|
val files = filePathSuggestions[path]
|
||||||
if (files != null) {
|
if (files != null) {
|
||||||
ShellCommandResult.create(files.joinToString("\n"), exitCode = 0)
|
ShellCommandResult.create(files.joinToString("\n"), exitCode = 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user