diff --git a/java/execution/impl/src/com/intellij/execution/scratch/JavaScratchConfigurable.java b/java/execution/impl/src/com/intellij/execution/scratch/JavaScratchConfigurable.java index ea2eaca3da5c..55df9b8851ad 100644 --- a/java/execution/impl/src/com/intellij/execution/scratch/JavaScratchConfigurable.java +++ b/java/execution/impl/src/com/intellij/execution/scratch/JavaScratchConfigurable.java @@ -15,22 +15,27 @@ */ package com.intellij.execution.scratch; -import com.intellij.execution.application.ApplicationConfigurable; -import com.intellij.execution.application.ApplicationConfiguration; +import com.intellij.application.options.ModulesComboBox; +import com.intellij.execution.ui.CommonJavaParametersPanel; +import com.intellij.execution.ui.ConfigurationModuleSelector; +import com.intellij.execution.ui.DefaultJreSelector; +import com.intellij.execution.ui.JrePathEditor; import com.intellij.ide.scratch.ScratchFileService; import com.intellij.ide.scratch.ScratchRootType; import com.intellij.openapi.fileChooser.FileChooser; import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; import com.intellij.openapi.options.ConfigurationException; +import com.intellij.openapi.options.SettingsEditor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.LabeledComponent; import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileWithId; -import com.intellij.ui.components.JBLabel; -import com.intellij.util.ui.components.BorderLayoutPanel; +import com.intellij.ui.PanelWithAnchor; +import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -43,13 +48,28 @@ import java.awt.event.ActionListener; * @author Eugene Zhuravlev * Date: 30-Sep-15 */ -public class JavaScratchConfigurable extends ApplicationConfigurable{ +public class JavaScratchConfigurable extends SettingsEditor implements PanelWithAnchor { - private final TextFieldWithBrowseButton myScratchPathField; + private final CommonJavaParametersPanel myCommonProgramParameters; + private final LabeledComponent myMainClass; + private final LabeledComponent myScratchPathField; + private final LabeledComponent myModule; + private JPanel myWholePanel; + + private final ConfigurationModuleSelector myModuleSelector; + private JrePathEditor myJrePathEditor; + private JComponent myAnchor; public JavaScratchConfigurable(final Project project) { - super(project); - myScratchPathField = new TextFieldWithBrowseButton(new ActionListener() { + myMainClass = new LabeledComponent(); + myMainClass.setLabelLocation(BorderLayout.WEST); + myMainClass.setText("Main &class:"); + myMainClass.setComponent(new JTextField()); + + myScratchPathField = new LabeledComponent(); + myScratchPathField.setLabelLocation(BorderLayout.WEST); + myScratchPathField.setText("&Path to scratch file:"); + myScratchPathField.setComponent(new TextFieldWithBrowseButton(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { VirtualFile toSelect = getVFileFromEditor(); @@ -63,33 +83,64 @@ public class JavaScratchConfigurable extends ApplicationConfigurable{ setVFileToEditor(file); } } - }, this); + }, this)); + + myModule = new LabeledComponent(); + myModule.setLabelLocation(BorderLayout.WEST); + myModule.setComponent(new ModulesComboBox()); + myModule.setText("Use classpath of &module:"); + myModuleSelector = new ConfigurationModuleSelector(project, myModule.getComponent()); + + myCommonProgramParameters = new CommonJavaParametersPanel(); + myCommonProgramParameters.setModuleContext(myModuleSelector.getModule()); + myModule.getComponent().addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + myCommonProgramParameters.setModuleContext(myModuleSelector.getModule()); + } + }); + myJrePathEditor = new JrePathEditor(); + myJrePathEditor.setDefaultJreSelector(DefaultJreSelector.projectSdk(project)); + + myWholePanel = new JPanel(new GridBagLayout()); + myWholePanel.add(myMainClass, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0),0, 0 )); + myWholePanel.add(myScratchPathField, new GridBagConstraints(GridBagConstraints.RELATIVE, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0),0, 0 )); + myWholePanel.add(myCommonProgramParameters, new GridBagConstraints(GridBagConstraints.RELATIVE, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(12, 0, 12, 0),0, 0 )); + myWholePanel.add(myModule, new GridBagConstraints(GridBagConstraints.RELATIVE, 3, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),0, 0 )); + myWholePanel.add(myJrePathEditor, new GridBagConstraints(GridBagConstraints.RELATIVE, 4, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0),0, 0 )); + + myAnchor = UIUtil.mergeComponentsWithAnchor(myMainClass, myScratchPathField, myCommonProgramParameters, myJrePathEditor, myModule); } @Override - public void applyEditorTo(ApplicationConfiguration configuration) throws ConfigurationException { - super.applyEditorTo(configuration); + public void applyEditorTo(JavaScratchConfiguration configuration) throws ConfigurationException { + myCommonProgramParameters.applyTo(configuration); + myModuleSelector.applyTo(configuration); + configuration.MAIN_CLASS_NAME = myMainClass.getComponent().getText().trim(); + configuration.ALTERNATIVE_JRE_PATH = myJrePathEditor.getJrePathOrName(); + configuration.ALTERNATIVE_JRE_PATH_ENABLED = myJrePathEditor.isAlternativeJreSelected(); + final VirtualFile vFile = getVFileFromEditor(); - ((JavaScratchConfiguration)configuration).SCRATCH_FILE_ID = vFile instanceof VirtualFileWithId ? ((VirtualFileWithId)vFile).getId() : 0; + configuration.SCRATCH_FILE_ID = vFile instanceof VirtualFileWithId ? ((VirtualFileWithId)vFile).getId() : 0; } @Nullable private VirtualFile getVFileFromEditor() { - final String path = FileUtil.toSystemIndependentName(myScratchPathField.getText()); + final String path = FileUtil.toSystemIndependentName(myScratchPathField.getComponent().getText().trim()); return !StringUtil.isEmpty(path) ? LocalFileSystem.getInstance().findFileByPath(path) : null; } @Override - public void resetEditorFrom(ApplicationConfiguration configuration) { - super.resetEditorFrom(configuration); - final JavaScratchConfiguration scratchConfig = (JavaScratchConfiguration)configuration; - final VirtualFile file = scratchConfig.getScratchVirtualFile(); - setVFileToEditor(file); + public void resetEditorFrom(JavaScratchConfiguration configuration) { + myCommonProgramParameters.reset(configuration); + myModuleSelector.reset(configuration); + myMainClass.getComponent().setText(configuration.MAIN_CLASS_NAME != null ? configuration.MAIN_CLASS_NAME.replaceAll("\\$", "\\.") : ""); + myJrePathEditor.setPathOrName(configuration.ALTERNATIVE_JRE_PATH, configuration.ALTERNATIVE_JRE_PATH_ENABLED); + setVFileToEditor(configuration.getScratchVirtualFile()); } private void setVFileToEditor(VirtualFile file) { if (file != null) { - myScratchPathField.setText(FileUtil.toSystemDependentName(file.getPath())); + myScratchPathField.getComponent().setText(FileUtil.toSystemDependentName(file.getPath())); } else { myScratchPathField.setText(""); @@ -99,9 +150,21 @@ public class JavaScratchConfigurable extends ApplicationConfigurable{ @NotNull @Override public JComponent createEditor() { - final JPanel panel = new JPanel(new GridBagLayout()); - panel.add(new JBLabel("Path to scratch file: "), new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(20,0,0,10), 0, 0)); - panel.add(myScratchPathField, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(20, 0, 0, 0), 0, 0)); - return new BorderLayoutPanel().addToCenter(super.createEditor()).addToBottom(panel); + return myWholePanel; + } + + @Override + public JComponent getAnchor() { + return myAnchor; + } + + @Override + public void setAnchor(@Nullable JComponent anchor) { + myAnchor = anchor; + myMainClass.setAnchor(anchor); + myScratchPathField.setAnchor(anchor); + myCommonProgramParameters.setAnchor(anchor); + myJrePathEditor.setAnchor(anchor); + myModule.setAnchor(anchor); } } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java index 12b91dc196ea..51adfb34bf76 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java @@ -84,9 +84,8 @@ public class JavaResolveCache { PsiType type = isOverloadCheck ? null : myCalculatedTypes.get(expr); if (type == null) { final RecursionGuard.StackStamp dStackStamp = PsiDiamondType.ourDiamondGuard.markStack(); - final RecursionGuard.StackStamp gStackStamp = PsiResolveHelper.ourGraphGuard.markStack(); type = f.fun(expr); - if (!dStackStamp.mayCacheNow() || !gStackStamp.mayCacheNow() || isOverloadCheck) { + if (!dStackStamp.mayCacheNow() || isOverloadCheck) { return type; } if (type == null) type = TypeConversionUtil.NULL_TYPE; diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java index 20d18262f9d9..6121d6530c8b 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java @@ -1195,7 +1195,9 @@ public class InferenceSession { } } finally { - LambdaUtil.ourFunctionTypes.set(null); + if (formula instanceof InputOutputConstraintFormula) { + LambdaUtil.getFunctionalTypeMap().remove(((InputOutputConstraintFormula)formula).getExpression()); + } } return true; } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/CheckedExceptionCompatibilityConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/CheckedExceptionCompatibilityConstraint.java index f03a796350d8..bd7549957c7b 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/CheckedExceptionCompatibilityConstraint.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/CheckedExceptionCompatibilityConstraint.java @@ -157,7 +157,7 @@ public class CheckedExceptionCompatibilityConstraint extends InputOutputConstrai } @Override - protected PsiExpression getExpression() { + public PsiExpression getExpression() { return myExpression; } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/InputOutputConstraintFormula.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/InputOutputConstraintFormula.java index 8efff6b10f71..0fb9e7f41d52 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/InputOutputConstraintFormula.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/InputOutputConstraintFormula.java @@ -30,7 +30,7 @@ import java.util.*; */ public abstract class InputOutputConstraintFormula implements ConstraintFormula { - protected abstract PsiExpression getExpression(); + public abstract PsiExpression getExpression(); protected abstract PsiType getT(); protected abstract void setT(PsiType t); protected abstract InputOutputConstraintFormula createSelfConstraint(PsiType type, PsiExpression expression); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/diamond/IDEA140686.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/diamond/IDEA140686.java new file mode 100644 index 000000000000..eae6d376024f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/diamond/IDEA140686.java @@ -0,0 +1,50 @@ +package nise; + +import java.util.function.BiFunction; +import java.util.function.Predicate; +import java.util.function.Supplier; + + + +class TypeTest { + public static void main(String[] args) { + assertThat(id(new Combiner<>(echo("A"), echo("B"), (str1, str2) -> + new StringBuilder(str1.length() + str2.length()) + .append(str1) + .append(str2))).get(), + hasSameContentAs("AB")); + } + + private static Supplier id(Supplier s) { + return s; + } + + private static Supplier echo(T s) { + return () -> s; + } + + private static Predicate hasSameContentAs(CharSequence seq) { + return charSequence -> charSequence.toString().equals(seq.toString()); + } + + private static void assertThat(T actual, Predicate matcher) { + if (!matcher.test(actual)) throw new AssertionError(); + } + + private static class Combiner implements Supplier { + private final Supplier s1; + private final Supplier s2; + private final BiFunction f; + + public Combiner(Supplier s1, Supplier s2, BiFunction f) { + this.s1 = s1; + this.s2 = s2; + this.f = f; + } + + @Override + public R get() { + return f.apply(s1.get(), s2.get()); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/expressions/ProoveThatInferenceInsideLambdaBodyDontInfluenceOuterCallInference.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/expressions/ProoveThatInferenceInsideLambdaBodyDontInfluenceOuterCallInference.java new file mode 100644 index 000000000000..c34eae729fbb --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/expressions/ProoveThatInferenceInsideLambdaBodyDontInfluenceOuterCallInference.java @@ -0,0 +1,25 @@ + +import java.util.List; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +class Test { + void f(Stream stream1) { + final Supplier> revisionDiffItemDTOs = () -> stream1.map(record -> { + foo(null); + final Object stream = record.getAllNodes().stream(); + return ""; + }).collect(Collectors.toList()); + } + + + private void foo(T t) throws RuntimeException {} + + private class ReviewDiffRecord { + List getAllNodes() { + return null; + } + } + +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/Diamond8HighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/Diamond8HighlightingTest.java index 1990221b5e5f..fb31edf8d404 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/Diamond8HighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/Diamond8HighlightingTest.java @@ -46,6 +46,10 @@ public class Diamond8HighlightingTest extends LightDaemonAnalyzerTestCase { public void testWithConstructorRefInside() throws Exception { doTest(); } + + public void testIDEA140686() throws Exception { + doTest(); + } private void doTest() throws Exception { doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false); diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/Java8ExpressionsCheckTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/Java8ExpressionsCheckTest.java index 99b4a647fe11..a325f0e32552 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/Java8ExpressionsCheckTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/Java8ExpressionsCheckTest.java @@ -16,17 +16,13 @@ package com.intellij.codeInsight.daemon.lambda; import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.testFramework.IdeaTestUtil; import org.jetbrains.annotations.NonNls; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.List; public class Java8ExpressionsCheckTest extends LightDaemonAnalyzerTestCase { @NonNls static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/lambda/expressions"; @@ -47,6 +43,10 @@ public class Java8ExpressionsCheckTest extends LightDaemonAnalyzerTestCase { doTestAllMethodCallExpressions(); } + public void testProoveThatInferenceInsideLambdaBodyDontInfluenceOuterCallInference() throws Exception { + doTestAllMethodCallExpressions(); + } + public void testCachedUnresolvedMethods() throws Exception { doTestCachedUnresolved(); } diff --git a/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreviewController.java b/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreviewController.java index 00346aeb29bd..4ea787e5a498 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreviewController.java +++ b/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreviewController.java @@ -274,7 +274,6 @@ public class LivePreviewController implements LivePreview.Delegate, FindUtil.Rep public void dispose() { if (myDisposed) return; - myLivePreview.cleanUp(); off(); mySearchResults.dispose(); diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileService.java b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileService.java index ab518d001af7..f81ed3fbf2ea 100644 --- a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileService.java +++ b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileService.java @@ -46,4 +46,8 @@ public abstract class ScratchFileService { @NotNull public abstract PerFileMappings getScratchesMapping(); + + public static boolean isInScratchRoot(@Nullable VirtualFile file) { + return getInstance().getRootType(file) != null; + } } diff --git a/platform/lang-impl/src/com/intellij/refactoring/move/moveFilesOrDirectories/MoveFilesOrDirectoriesHandler.java b/platform/lang-impl/src/com/intellij/refactoring/move/moveFilesOrDirectories/MoveFilesOrDirectoriesHandler.java index 2aa855f52711..5ede26109afd 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/move/moveFilesOrDirectories/MoveFilesOrDirectoriesHandler.java +++ b/platform/lang-impl/src/com/intellij/refactoring/move/moveFilesOrDirectories/MoveFilesOrDirectoriesHandler.java @@ -22,9 +22,9 @@ import com.intellij.openapi.actionSystem.LangDataKeys; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.PsiUtilCore; import com.intellij.refactoring.move.MoveCallback; import com.intellij.refactoring.move.MoveHandlerDelegate; import org.jetbrains.annotations.Nullable; @@ -62,13 +62,7 @@ public class MoveFilesOrDirectoriesHandler extends MoveHandlerDelegate { public static boolean isValidTarget(PsiElement psiElement) { if (!(psiElement instanceof PsiDirectory || psiElement instanceof PsiDirectoryContainer)) return false; - return psiElement.getManager().isInProject(psiElement) || isInScratches(psiElement); - } - - protected static boolean isInScratches(PsiElement psiElement) { - VirtualFile virtualFile = psiElement instanceof PsiFileSystemItem ? ((PsiFileSystemItem)psiElement).getVirtualFile() : null; - if (virtualFile != null && ScratchFileService.getInstance().getRootType(virtualFile) != null) return true; - return false; + return psiElement.getManager().isInProject(psiElement) || ScratchFileService.isInScratchRoot(PsiUtilCore.getVirtualFile(psiElement)); } public void doMove(final PsiElement[] elements, final PsiElement targetContainer) { diff --git a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameProcessor.java b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameProcessor.java index 06d812127d6f..ef76ae11eabb 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameProcessor.java +++ b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameProcessor.java @@ -300,7 +300,10 @@ public class RenameProcessor extends BaseRefactoringProcessor { //noinspection ForLoopReplaceableByForEach for (int i = 0; i < elements.size(); i++) { PsiElement element = elements.get(i); - LOG.assertTrue(element != null); + if (element == null) { + LOG.error("primary: " + myPrimaryElement + "; renamers: " + myRenamers); + continue; + } final String newName = myAllRenames.get(element); final UsageInfo[] usages = RenameUtil.findUsages(element, newName, mySearchInComments, mySearchTextOccurrences, myAllRenames); final List usagesList = Arrays.asList(usages); diff --git a/platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java b/platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java index 7881b8a7c96a..bf9e8a783adc 100644 --- a/platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java +++ b/platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java @@ -32,6 +32,7 @@ import com.intellij.ui.HyperlinkLabel; import com.intellij.ui.TextFieldWithHistory; import com.intellij.ui.TextFieldWithHistoryWithBrowseButton; import com.intellij.util.NotNullProducer; +import com.intellij.util.ObjectUtils; import com.intellij.util.PlatformIcons; import com.intellij.util.containers.ComparatorUtil; import com.intellij.util.containers.ContainerUtil; @@ -295,7 +296,7 @@ public class SwingHelper { boolean mergeWithPrevHistory) { Set newHistorySet = ContainerUtil.newHashSet(history); List prevHistory = textFieldWithHistory.getHistory(); - List mergedHistory = ContainerUtil.newArrayList(); + List mergedHistory = ContainerUtil.newArrayListWithCapacity(history.size()); if (mergeWithPrevHistory) { for (String item : prevHistory) { if (!newHistorySet.contains(item)) { @@ -303,15 +304,20 @@ public class SwingHelper { } } } - else { - String currentText = textFieldWithHistory.getText(); - if (StringUtil.isNotEmpty(currentText) && !newHistorySet.contains(currentText)) { - mergedHistory.add(currentText); - } - } mergedHistory.addAll(history); + String oldText = StringUtil.notNullize(textFieldWithHistory.getText()); + String oldSelectedItem = ObjectUtils.tryCast(textFieldWithHistory.getSelectedItem(), String.class); + if (!mergedHistory.contains(oldSelectedItem)) { + oldSelectedItem = null; + } textFieldWithHistory.setHistory(mergedHistory); setLongestAsPrototype(textFieldWithHistory, mergedHistory); + if (oldSelectedItem != null) { + textFieldWithHistory.setSelectedItem(oldSelectedItem); + } + if (!oldText.equals(oldSelectedItem)) { + textFieldWithHistory.setText(oldText); + } } private static void setLongestAsPrototype(@NotNull JComboBox comboBox, @NotNull List variants) { diff --git a/python/src/com/jetbrains/python/psi/PyUtil.java b/python/src/com/jetbrains/python/psi/PyUtil.java index 31bdabd31f6d..87d9be86fe81 100644 --- a/python/src/com/jetbrains/python/psi/PyUtil.java +++ b/python/src/com/jetbrains/python/psi/PyUtil.java @@ -23,7 +23,7 @@ import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.ide.fileTemplates.FileTemplate; import com.intellij.ide.fileTemplates.FileTemplateManager; -import com.intellij.ide.scratch.ScratchRootType; +import com.intellij.ide.scratch.ScratchFileService; import com.intellij.injected.editor.VirtualFileWindow; import com.intellij.lang.ASTFactory; import com.intellij.lang.ASTNode; @@ -55,6 +55,7 @@ import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.stubs.StubElement; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.PsiUtilCore; import com.intellij.psi.util.QualifiedName; import com.intellij.ui.awt.RelativePoint; import com.intellij.util.*; @@ -1832,8 +1833,7 @@ public class PyUtil { } public static boolean isInScratchFile(@NotNull PsiElement element) { - PsiFile file = element.getContainingFile(); - return file != null && ScratchRootType.getInstance().isScratchFile(file.getVirtualFile()); + return ScratchFileService.isInScratchRoot(PsiUtilCore.getVirtualFile(element)); } /**