[Java. Terminal] Support completion for -classpath option for JavaShellCommandSpecsProvider

IDEA-359243

GitOrigin-RevId: 6e5c14bc68a81cff36588258efbca9862bbdc311
This commit is contained in:
Georgii Ustinov
2024-10-03 18:19:35 +00:00
committed by intellij-monorepo-bot
parent 6c8a7e35f4
commit 4ad4c5e655
4 changed files with 93 additions and 45 deletions
@@ -54,7 +54,7 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
description(JavaTerminalBundle.message("java.command.terminal.classpath.option.description"))
argument {
displayName(CLASSPATH_ARGUMENT_NAME)
suggestions(ShellDataGenerators.fileSuggestionsGenerator())
suggestions(JavaShellCommandUtils.classpathSuggestionsGenerator())
}
}
option("-showversion") {
@@ -116,7 +116,7 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
description(JavaTerminalBundle.message("java.command.terminal.classpath.option.description"))
argument {
displayName(CLASSPATH_ARGUMENT_NAME)
suggestions(ShellDataGenerators.fileSuggestionsGenerator())
suggestions(JavaShellCommandUtils.classpathSuggestionsGenerator())
}
}
option("--help") {
@@ -161,7 +161,7 @@ private const val MAIN_CLASS_ARGUMENT_NAME: @NlsSafe String = "mainclass"
private const val JAR_FILE_ARGUMENT_NAME: @NlsSafe String = "jar file"
private const val CLASS_GC_GNI_ARGUMENT_NAME: @NlsSafe String = "class|gc|gni"
private const val CLASS_GC_GNI_MODULE_ARGUMENT_NAME: @NlsSafe String = "$CLASS_GC_GNI_ARGUMENT_NAME|<module>"
private val CLASSPATH_ARGUMENT_NAME: @NlsSafe String = "filepath[${ShellCommandUtils.getClassPathSeparator()}filepath]"
private val CLASSPATH_ARGUMENT_NAME: @NlsSafe String = "filepath[${JavaShellCommandUtils.getClassPathSeparator()}filepath]"
private val OPTION_UI_INFO_MAP = mapOf(
"-Xms" to UIOptionInfo(separator = "", argumentName = SIZE_ARGUMENT_NAME),
@@ -0,0 +1,40 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
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
internal object JavaShellCommandUtils {
internal fun getClassPathSeparator() = when {
SystemInfo.isWindows -> ";"
else -> ":"
}
internal fun classpathSuggestionsGenerator(): ShellRuntimeDataGenerator<List<ShellCompletionSuggestion>> {
val key = "classpath generator"
return ShellRuntimeDataGenerator(
debugName = key,
getCacheKey = {
val pathInfo = getPathInfo(it)
"$key:${pathInfo.lastSeparatorIndex}:${pathInfo.typedPrefix}"
}
) { context ->
val pathInfo = getPathInfo(context)
ShellDataGenerators.getFileSuggestions(context, pathInfo.typedPrefix, false, if (pathInfo.lastSeparatorIndex == -1) 0 else pathInfo.lastSeparatorIndex + 1)
}
}
private fun getPathInfo(context: ShellRuntimeContext): PathInfo {
val separator = getClassPathSeparator()
val currentPrefix = context.typedPrefix
val index = currentPrefix.lastIndexOf(separator)
val newTypedPrefix = if (index == -1) currentPrefix else currentPrefix.substring(index + 1)
return PathInfo(newTypedPrefix, index)
}
private data class PathInfo(val typedPrefix: String, val lastSeparatorIndex: Int)
}
@@ -1,11 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.terminal
import com.intellij.openapi.util.SystemInfo
internal object ShellCommandUtils {
internal fun getClassPathSeparator() = when {
SystemInfo.isWindows -> ";"
else -> ":"
}
}