[terminal] Refactoring: Small fixes

1. Make `TerminalCommandSpecCompletionContributorGen2` internal
2. Move `TerminalCommandSpecCompletionContributorGen2` to the new package ` reworked.completion `, to distinguish logic
3. Remove duplicate `weighter`

GitOrigin-RevId: 3ae31e901ca93213deb31555766566325e79076d
This commit is contained in:
Yuliya Karalenka
2025-06-16 16:06:06 +00:00
committed by intellij-monorepo-bot
parent a7a54920fb
commit c0a7d20402
2 changed files with 19 additions and 21 deletions
@@ -100,17 +100,13 @@
order="first, before terminalPowerShell, after ml_context"/>
<completion.contributor language="any"
id="terminalCommandSpecGen2"
implementationClass="org.jetbrains.plugins.terminal.block.completion.TerminalCommandSpecCompletionContributorGen2"
implementationClass="org.jetbrains.plugins.terminal.block.reworked.completion.TerminalCommandSpecCompletionContributorGen2"
order="first, before terminalPowerShell, after ml_context"/>
<completion.contributor language="any"
id="terminalPowerShell"
implementationClass="org.jetbrains.plugins.terminal.block.completion.powershell.PowerShellCompletionContributor"
order="first, before shShebang, before liveTemplates, after ml_context"/>
<completion.ml.model implementation="org.jetbrains.plugins.terminal.block.completion.ml.ShMLRankingProvider"/>
<weigher key="completion"
id="terminalCommandSpecGen1"
order="before stats"
implementationClass="org.jetbrains.plugins.terminal.block.completion.ShellCommandSpecCompletionWeigher"/>
<weigher key="completion"
id="terminalCommandSpecGen2"
order="before stats"
@@ -1,5 +1,5 @@
// Copyright 2000-2025 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.completion
package org.jetbrains.plugins.terminal.block.reworked.completion
import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.lookup.LookupElement
@@ -18,13 +18,13 @@ import com.intellij.terminal.completion.ShellDataGeneratorsExecutor
import com.intellij.terminal.completion.ShellRuntimeContextProvider
import com.intellij.terminal.completion.spec.ShellCompletionSuggestion
import com.intellij.terminal.completion.spec.ShellSuggestionType
import org.jetbrains.plugins.terminal.LocalBlockTerminalRunner.Companion.BLOCK_TERMINAL_AUTOCOMPLETION
import org.jetbrains.plugins.terminal.block.completion.TerminalCompletionUtil.findIconForSuggestion
import org.jetbrains.plugins.terminal.block.completion.TerminalCompletionUtil.getNextSuggestionsString
import org.jetbrains.plugins.terminal.block.completion.spec.ShellDataGenerators.availableCommandsGenerator
import org.jetbrains.plugins.terminal.block.completion.spec.ShellDataGenerators.fileSuggestionsGenerator
import org.jetbrains.plugins.terminal.LocalBlockTerminalRunner
import org.jetbrains.plugins.terminal.block.completion.ShellCommandSpecsManagerImpl
import org.jetbrains.plugins.terminal.block.completion.TerminalCompletionScope
import org.jetbrains.plugins.terminal.block.completion.TerminalCompletionUtil
import org.jetbrains.plugins.terminal.block.completion.spec.ShellDataGenerators
import org.jetbrains.plugins.terminal.block.completion.spec.impl.ShellDataGeneratorsExecutorReworkedImpl
import org.jetbrains.plugins.terminal.block.completion.spec.impl.ShellEnvBasedGenerators.aliasesGenerator
import org.jetbrains.plugins.terminal.block.completion.spec.impl.ShellEnvBasedGenerators
import org.jetbrains.plugins.terminal.block.completion.spec.impl.ShellRuntimeContextProviderReworkedImpl
import org.jetbrains.plugins.terminal.block.reworked.TerminalBlocksModel
import org.jetbrains.plugins.terminal.block.reworked.TerminalSessionModel
@@ -34,7 +34,7 @@ import org.jetbrains.plugins.terminal.exp.completion.TerminalShellSupport
import org.jetbrains.plugins.terminal.util.ShellType
import java.io.File
class TerminalCommandSpecCompletionContributorGen2 : CompletionContributor(), DumbAware {
internal class TerminalCommandSpecCompletionContributorGen2 : CompletionContributor(), DumbAware {
val tracer = TelemetryManager.getTracer(TerminalCompletionScope)
override fun fillCompletionVariants(parameters: CompletionParameters, result: CompletionResultSet) {
@@ -49,7 +49,7 @@ class TerminalCommandSpecCompletionContributorGen2 : CompletionContributor(), Du
return
}
if (parameters.isAutoPopup && !Registry.`is`(BLOCK_TERMINAL_AUTOCOMPLETION)) {
if (parameters.isAutoPopup && !Registry.`is`(LocalBlockTerminalRunner.BLOCK_TERMINAL_AUTOCOMPLETION)) {
result.stopHere()
return
}
@@ -110,11 +110,13 @@ class TerminalCommandSpecCompletionContributorGen2 : CompletionContributor(), Du
}
val runtimeContext = context.runtimeContextProvider.getContext(tokens.last())
val completion = ShellCommandSpecCompletion(ShellCommandSpecsManagerImpl.getInstance(), context.generatorsExecutor, context.runtimeContextProvider)
val completion = ShellCommandSpecCompletion(ShellCommandSpecsManagerImpl.getInstance(), context.generatorsExecutor,
context.runtimeContextProvider)
val commandExecutable = tokens.first()
val commandArguments = tokens.subList(1, tokens.size)
val availableCommandsProvider = suspend { context.generatorsExecutor.execute(runtimeContext, availableCommandsGenerator()) }
val fileProducer = suspend { context.generatorsExecutor.execute(runtimeContext, fileSuggestionsGenerator()) }
val availableCommandsProvider = suspend { context.generatorsExecutor.execute(runtimeContext,
ShellDataGenerators.availableCommandsGenerator()) }
val fileProducer = suspend { context.generatorsExecutor.execute(runtimeContext, ShellDataGenerators.fileSuggestionsGenerator()) }
val specCompletionFunction: suspend (String) -> List<ShellCompletionSuggestion>? = { commandName ->
tracer.spanBuilder("terminal-completion-compute-completion-items").useWithScope {
completion.computeCompletionItems(commandName, commandArguments)
@@ -191,7 +193,7 @@ class TerminalCommandSpecCompletionContributorGen2 : CompletionContributor(), Du
}
// aliases generator does not requires actual typed prefix
val dummyRuntimeContext = context.runtimeContextProvider.getContext("")
val aliases: Map<String, String> = context.generatorsExecutor.execute(dummyRuntimeContext, aliasesGenerator())
val aliases: Map<String, String> = context.generatorsExecutor.execute(dummyRuntimeContext, ShellEnvBasedGenerators.aliasesGenerator())
val expandedTokens = expandAliases(tokens, aliases, context)
return expandedTokens
}
@@ -224,9 +226,9 @@ class TerminalCommandSpecCompletionContributorGen2 : CompletionContributor(), Du
}
private fun ShellCompletionSuggestion.toLookupElement(shellType: ShellType): LookupElement {
val actualIcon = icon ?: findIconForSuggestion(name, type)
val actualIcon = icon ?: TerminalCompletionUtil.findIconForSuggestion(name, type)
val realInsertValue = insertValue?.replace("{cursor}", "")
val nextSuggestions = getNextSuggestionsString(this).takeIf { it.isNotEmpty() }
val nextSuggestions = TerminalCompletionUtil.getNextSuggestionsString(this).takeIf { it.isNotEmpty() }
val escapedInsertValue = StringUtil.escapeChar(realInsertValue ?: name, ' ')
// Remove path separator from insert value, so there will be an exact match
@@ -290,4 +292,4 @@ class TerminalCommandSpecCompletionContributorGen2 : CompletionContributor(), Du
get() = parameters.editor.project!!
}
}
}