[kotlin] prohibit kotlin scratches creation

^KTIJ-32088 Fixed


(cherry picked from commit eafbbe789be89b4787ab284e11c5011064dd3fae)

KT-MR-18854

GitOrigin-RevId: a04d8c5f0bb3b4d9e4b2317e2ecb804cad355eba
This commit is contained in:
Vlad Koshkin
2024-11-08 16:05:26 +01:00
committed by intellij-monorepo-bot
parent 30236d0cf3
commit 087bbdc0bd
6 changed files with 28 additions and 1 deletions

View File

@@ -11364,6 +11364,7 @@ a:com.intellij.ide.scratch.LRUPopupBuilder
- withExtraMiddleValue(java.lang.Object,java.lang.String,javax.swing.Icon):com.intellij.ide.scratch.LRUPopupBuilder
- withExtraSpeedSearchNamer(java.util.function.Function):com.intellij.ide.scratch.LRUPopupBuilder
- withExtraTopValue(java.lang.Object,java.lang.String,javax.swing.Icon):com.intellij.ide.scratch.LRUPopupBuilder
- withFilter(java.util.function.Predicate):com.intellij.ide.scratch.LRUPopupBuilder
- withSelection(java.lang.Object):com.intellij.ide.scratch.LRUPopupBuilder
f:com.intellij.ide.scratch.ScratchFileActions
- <init>():V
@@ -11383,6 +11384,7 @@ a:com.intellij.ide.scratch.ScratchFileCreationHelper
- sf:EXTENSION:com.intellij.lang.LanguageExtension
- <init>():V
- beforeCreate(com.intellij.openapi.project.Project,com.intellij.ide.scratch.ScratchFileCreationHelper$Context):V
- newScratchAllowed():Z
- s:parseHeader(com.intellij.openapi.project.Project,com.intellij.lang.Language,java.lang.String):com.intellij.psi.PsiFile
- prepareText(com.intellij.openapi.project.Project,com.intellij.ide.scratch.ScratchFileCreationHelper$Context,com.intellij.openapi.actionSystem.DataContext):Z
- s:reformat(com.intellij.openapi.project.Project,com.intellij.lang.Language,java.lang.String):java.lang.String

View File

@@ -26,6 +26,7 @@ import java.awt.*;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* @author gregsh
@@ -41,6 +42,7 @@ public abstract class LRUPopupBuilder<T> {
private T mySelection;
private Consumer<? super T> myOnChosen;
private Comparator<? super T> myComparator;
private Predicate<? super T> myFilter = it -> true;
private Iterable<? extends T> myValues;
private JBIterable<T> myTopValues = JBIterable.empty();
private JBIterable<T> myMiddleValues = JBIterable.empty();
@@ -129,6 +131,11 @@ public abstract class LRUPopupBuilder<T> {
return this;
}
public @NotNull LRUPopupBuilder<T> withFilter(@Nullable Predicate<? super T> filter) {
myFilter = filter;
return this;
}
public @NotNull LRUPopupBuilder<T> withExtraSpeedSearchNamer(@Nullable Function<? super T, String> function) {
myExtraSpeedSearchNamer = function;
return this;
@@ -153,7 +160,7 @@ public abstract class LRUPopupBuilder<T> {
if (!lru.isEmpty()) {
lru.sort(Comparator.comparingInt(o -> ids.indexOf(getStorageId(o))));
}
List<T> combinedItems = ContainerUtil.concat(topItems, lru, middleItems, items, bottomItems);
List<T> combinedItems = ContainerUtil.concat(topItems, lru, middleItems, items, bottomItems).stream().filter(myFilter).toList();
T sep1 = ContainerUtil.getOrElse(combinedItems, topItems.size() + lru.size() + middleItems.size(), null);
T sep2 = ContainerUtil.getOrElse(combinedItems, topItems.size() + lru.size() + middleItems.size() + items.size(), null);

View File

@@ -33,6 +33,13 @@ public abstract class ScratchFileCreationHelper {
public boolean prepareText(@NotNull Project project, @NotNull Context context, @NotNull DataContext dataContext) {
return false;
}
/**
* Return false if it's prohibited to create new scratch file for particular {@link Context#language}
*/
public boolean newScratchAllowed() {
return true;
}
public void beforeCreate(@NotNull Project project, @NotNull Context context) {
}

View File

@@ -256,6 +256,7 @@ final class ScratchImplUtil {
}
}
.forValues(items)
.withFilter(it -> it.language == null || ScratchFileCreationHelper.EXTENSION.forLanguage(it.language).newScratchAllowed())
.withComparator(comparator)
.withExtraSpeedSearchNamer(o -> o.fileExtension)
.withSelection(null);

View File

@@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.scratch
import com.intellij.ide.scratch.ScratchFileCreationHelper
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginModeProvider
import org.jetbrains.kotlin.idea.statistics.KotlinCreateFileFUSCollector
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
@@ -16,4 +17,11 @@ class KtScratchFileCreationHelper : ScratchFileCreationHelper() {
return super.prepareText(project, context, dataContext)
}
override fun newScratchAllowed(): Boolean =
if (KotlinPluginModeProvider.isK2Mode()) {
false
} else {
super.newScratchAllowed()
}
}

View File

@@ -61,6 +61,8 @@
<projectService serviceInterface="org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinCompilerPluginsProvider"
serviceImplementation="org.jetbrains.kotlin.idea.fir.extensions.KtCompilerPluginsProviderIdeImpl"/>
<compilableFileTypesProvider implementation="org.jetbrains.kotlin.idea.compiler.KotlinCompilableFileTypesProvider" />
<scratch.creationHelper language="kotlin"
implementationClass="org.jetbrains.kotlin.idea.scratch.KtScratchFileCreationHelper"/>
<!-- fir-specific -->
<applicationService serviceInterface="org.jetbrains.kotlin.idea.base.plugin.KotlinPluginModeProvider"
serviceImplementation="org.jetbrains.kotlin.idea.fir.plugin.K2KotlinPluginModeProvider"/>