mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
[Java. Terminal] Add support for completion in jar files for java command in new terminal
IDEA-364892 GitOrigin-RevId: c79b5d4e370a2d77fcc5960e19d414d4cf1ed3a6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
4c202ca663
commit
9fe79cd05b
@@ -53,6 +53,7 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
|
||||
exclusiveOn = listOf("--class-path")
|
||||
description(JavaTerminalBundle.message("java.command.terminal.classpath.option.description"))
|
||||
argument {
|
||||
suggestions(JavaShellCommandUtils.classpathSuggestionsGenerator())
|
||||
displayName(CLASSPATH_ARGUMENT_NAME)
|
||||
}
|
||||
}
|
||||
@@ -114,6 +115,7 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
|
||||
option("--class-path") {
|
||||
description(JavaTerminalBundle.message("java.command.terminal.classpath.option.description"))
|
||||
argument {
|
||||
suggestions(JavaShellCommandUtils.classpathSuggestionsGenerator())
|
||||
displayName(CLASSPATH_ARGUMENT_NAME)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,48 @@
|
||||
package com.intellij.java.terminal
|
||||
|
||||
import com.intellij.openapi.util.SystemInfo
|
||||
import com.intellij.terminal.completion.spec.ShellCompletionSuggestion
|
||||
import com.intellij.terminal.completion.spec.ShellRuntimeContext
|
||||
import com.intellij.terminal.completion.spec.ShellRuntimeDataGenerator
|
||||
import org.jetbrains.plugins.terminal.block.completion.spec.ShellDataGenerators
|
||||
import org.jetbrains.plugins.terminal.block.completion.spec.ShellRuntimeDataGenerator
|
||||
|
||||
object JavaShellCommandUtils {
|
||||
fun getClassPathSeparator() = when {
|
||||
private const val SEPARATOR_NOT_FOUND_INDEX = -1
|
||||
|
||||
fun getClassPathSeparator(): String = when {
|
||||
SystemInfo.isWindows -> ";"
|
||||
else -> ":"
|
||||
}
|
||||
|
||||
fun classpathSuggestionsGenerator(): ShellRuntimeDataGenerator<List<ShellCompletionSuggestion>> {
|
||||
val key = "classpath generator"
|
||||
return ShellRuntimeDataGenerator(
|
||||
debugName = key,
|
||||
getCacheKey = {
|
||||
val pathInfo = getPathInfo(it)
|
||||
"$key:${pathInfo.typedPrefix}:${pathInfo.replacementIndexDelta}"
|
||||
}
|
||||
) { context ->
|
||||
val pathInfo = getPathInfo(context)
|
||||
ShellDataGenerators.getFileSuggestions(context, pathInfo.typedPrefix, false, pathInfo.replacementIndexDelta)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getPathInfo(context: ShellRuntimeContext): PathInfo {
|
||||
val separator = getClassPathSeparator()
|
||||
val typedPrefix = context.typedPrefix
|
||||
val separatorIndex = typedPrefix.lastIndexOf(separator)
|
||||
val isStartWithQuote = ShellDataGenerators.isStartWithQuote(typedPrefix)
|
||||
val quoteOffset = if (isStartWithQuote) 1 else 0
|
||||
|
||||
return if (separatorIndex == SEPARATOR_NOT_FOUND_INDEX) {
|
||||
PathInfo(typedPrefix.substring(quoteOffset))
|
||||
} else {
|
||||
val adjustedSeparatorIndex = if (isStartWithQuote) separatorIndex else separatorIndex + 1
|
||||
PathInfo(typedPrefix.substring(separatorIndex + 1), adjustedSeparatorIndex)
|
||||
}
|
||||
}
|
||||
|
||||
private data class PathInfo(val typedPrefix: String, val replacementIndexDelta: Int = 0)
|
||||
}
|
||||
+73
-2
@@ -3,6 +3,7 @@ package com.intellij.java.terminal.completion
|
||||
|
||||
import com.intellij.execution.vmOptions.*
|
||||
import com.intellij.java.terminal.JavaShellCommandContext
|
||||
import com.intellij.java.terminal.JavaShellCommandUtils
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.terminal.completion.spec.ShellCommandResult
|
||||
import com.intellij.terminal.completion.spec.ShellCompletionSuggestion
|
||||
@@ -55,19 +56,89 @@ class JavaShellCommandSpecsProviderTest : BasePlatformTestCase() {
|
||||
UsefulTestCase.assertDoesntContain(fixture.getCompletionNames(), listOf("--add-experimental-exports", "--add-diagnostic-exports", "-XXadvanced"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `classpath suggestion generator with single quote`() = runBlocking {
|
||||
val fixture = createFixture()
|
||||
val completion = fixture.getCompletions("java -cp '")
|
||||
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
|
||||
assertTrue(completion.all { it.prefixReplacementIndex == 1})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `classpath suggestion generator single quote and after separator`() = runBlocking {
|
||||
val separator = JavaShellCommandUtils.getClassPathSeparator()
|
||||
val fixture = createFixture()
|
||||
val argument = "'file1.jar$separator"
|
||||
val completion = fixture.getCompletions("java -cp $argument")
|
||||
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
|
||||
assertTrue(completion.all { it.prefixReplacementIndex == argument.length })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `classpath suggestion generator with double quote`() = runBlocking {
|
||||
val fixture = createFixture()
|
||||
val completion = fixture.getCompletions("java -cp \"")
|
||||
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
|
||||
assertTrue(completion.all { it.prefixReplacementIndex == 1})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `classpath suggestion generator with double quote and after separator`() = runBlocking {
|
||||
val separator = JavaShellCommandUtils.getClassPathSeparator()
|
||||
val fixture = createFixture()
|
||||
val argument = "\"file1.jar$separator"
|
||||
val completion = fixture.getCompletions("java -cp $argument")
|
||||
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
|
||||
assertTrue(completion.all { it.prefixReplacementIndex == argument.length })
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `classpath suggestion generator simple`() = runBlocking {
|
||||
val fixture = createFixture()
|
||||
val completion = fixture.getCompletions("java -cp ")
|
||||
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
|
||||
assertTrue(completion.all { it.prefixReplacementIndex == 0})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `classpath suggestion generator after separator`() = runBlocking {
|
||||
val separator = JavaShellCommandUtils.getClassPathSeparator()
|
||||
val fixture = createFixture()
|
||||
val argument = "file1.jar$separator"
|
||||
val completion = fixture.getCompletions("java -cp $argument")
|
||||
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
|
||||
assertTrue(completion.all { it.prefixReplacementIndex == argument.length})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `classpath suggestion generator after double separator`() = runBlocking {
|
||||
val separator = JavaShellCommandUtils.getClassPathSeparator()
|
||||
val fixture = createFixture()
|
||||
val argument = "file1.jar$separator$separator"
|
||||
val completion = fixture.getCompletions("java -cp $argument")
|
||||
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
|
||||
assertTrue(completion.all { it.prefixReplacementIndex == argument.length})
|
||||
}
|
||||
|
||||
private fun createFixture(javaVersion: Int = 11): ShellCompletionTestFixture {
|
||||
ApplicationManager.getApplication().replaceService(VMOptionsService::class.java, MockVMOptionsService(), testRootDisposable)
|
||||
val fixture = ShellCompletionTestFixture.builder(project).mockShellCommandResults { command ->
|
||||
if (command == JavaShellCommandContext.JAVA_SHOW_SETTINGS_PROPERTIES_VERSION_COMMAND) {
|
||||
return@mockShellCommandResults ShellCommandResult.create("java.home = /jre/home\njava.version = ${javaVersion}", exitCode = 0)
|
||||
}
|
||||
|
||||
if (command.startsWith("__jetbrains_intellij_get_directory_files")) {
|
||||
return@mockShellCommandResults ShellCommandResult.create("file1.jar\nfile2.jar\ndir1/", exitCode = 0)
|
||||
}
|
||||
|
||||
return@mockShellCommandResults ShellCommandResult.create("", exitCode = 1)
|
||||
}.build()
|
||||
return fixture
|
||||
}
|
||||
|
||||
private suspend fun ShellCompletionTestFixture.getCompletionNames(): List<String> {
|
||||
val actual: List<ShellCompletionSuggestion> = getCompletions("java ")
|
||||
private suspend fun ShellCompletionTestFixture.getCompletionNames(command: String = "java "): List<String> {
|
||||
val actual: List<ShellCompletionSuggestion> = getCompletions(command)
|
||||
return actual.map { it.name }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user