static imports suggestions: allow to disable

This commit is contained in:
Anna Kozlova
2016-01-22 20:05:16 +03:00
parent 5206f93264
commit 05db80e8d4
6 changed files with 34 additions and 14 deletions
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.application.options.editor.JavaAutoImportOptions">
<grid id="27dc6" binding="myWholePanel" layout-manager="GridLayoutManager" row-count="6" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="myWholePanel" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="445" height="411"/>
@@ -29,6 +29,14 @@
<text resource-bundle="messages/ApplicationBundle" key="checkbox.add.unambiguous.imports.on.the.fly"/>
</properties>
</component>
<component id="90edc" class="javax.swing.JCheckBox" binding="myCbAddMethodImports">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Show import suggestions for static methods and fields"/>
</properties>
</component>
<grid id="2ef17" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="5" bottom="0" right="0"/>
<constraints>
@@ -61,7 +69,7 @@
</grid>
<grid id="e67c2" binding="myExcludeFromImportAndCompletionPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="400" height="150"/>
</grid>
</constraints>
@@ -71,7 +79,7 @@
</grid>
<component id="64f08" class="com.intellij.ui.TitledSeparator">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/ApplicationBundle" key="exclude.from.completion.group"/>
@@ -39,6 +39,7 @@ public class JavaAutoImportOptions implements AutoImportOptionsProvider {
private JCheckBox myCbShowImportPopup;
private JPanel myWholePanel;
private JCheckBox myCbAddUnambiguousImports;
private JCheckBox myCbAddMethodImports;
private JCheckBox myCbOptimizeImports;
private JPanel myExcludeFromImportAndCompletionPanel;
private final ExcludeTable myExcludePackagesTable;
@@ -78,6 +79,7 @@ public class JavaAutoImportOptions implements AutoImportOptionsProvider {
myCbShowImportPopup.setSelected(daemonSettings.isImportHintEnabled());
myCbOptimizeImports.setSelected(codeInsightSettings.OPTIMIZE_IMPORTS_ON_THE_FLY);
myCbAddUnambiguousImports.setSelected(codeInsightSettings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY);
myCbAddMethodImports.setSelected(codeInsightSettings.ADD_MEMBER_IMPORTS_ON_THE_FLY);
myExcludePackagesTable.reset();
}
@@ -94,6 +96,7 @@ public class JavaAutoImportOptions implements AutoImportOptionsProvider {
daemonSettings.setImportHintEnabled(myCbShowImportPopup.isSelected());
codeInsightSettings.OPTIMIZE_IMPORTS_ON_THE_FLY = myCbOptimizeImports.isSelected();
codeInsightSettings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = myCbAddUnambiguousImports.isSelected();
codeInsightSettings.ADD_MEMBER_IMPORTS_ON_THE_FLY = myCbAddMethodImports.isSelected();
myExcludePackagesTable.apply();
@@ -113,6 +116,7 @@ public class JavaAutoImportOptions implements AutoImportOptionsProvider {
boolean isModified = isModified(myCbShowImportPopup, daemonSettings.isImportHintEnabled());
isModified |= isModified(myCbOptimizeImports, codeInsightSettings.OPTIMIZE_IMPORTS_ON_THE_FLY);
isModified |= isModified(myCbAddUnambiguousImports, codeInsightSettings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY);
isModified |= isModified(myCbAddMethodImports, codeInsightSettings.ADD_MEMBER_IMPORTS_ON_THE_FLY);
isModified |= getSmartPasteValue() != codeInsightSettings.ADD_IMPORTS_ON_PASTE;
isModified |= myExcludePackagesTable.isModified();
@@ -317,7 +317,11 @@ public abstract class ImportClassFixBase<T extends PsiElement, R extends PsiRefe
if (classes.length == 1 &&
(canImportHere = canImportHere(allowCaretNearRef, editor, psiFile, classes[0].getName())) &&
canAddUnambiguousImport(psiFile) &&
(FileTypeUtils.isInServerPageFile(psiFile) ?
CodeInsightSettings.getInstance().JSP_ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY :
CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY) &&
(ApplicationManager.getApplication().isUnitTestMode() || DaemonListeners.canChangeFileSilently(psiFile)) &&
!LaterInvocator.isInModalContext() &&
!autoImportWillInsertUnexpectedCharacters(classes[0])
) {
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@@ -340,14 +344,6 @@ public abstract class ImportClassFixBase<T extends PsiElement, R extends PsiRefe
return Result.POPUP_NOT_SHOWN;
}
public static boolean canAddUnambiguousImport(PsiFile psiFile) {
return (FileTypeUtils.isInServerPageFile(psiFile) ?
CodeInsightSettings.getInstance().JSP_ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY :
CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY) &&
(ApplicationManager.getApplication().isUnitTestMode() || DaemonListeners.canChangeFileSilently(psiFile)) &&
!LaterInvocator.isInModalContext();
}
protected int getStartOffset(T element, R ref) {
return element.getTextOffset();
}
@@ -15,13 +15,16 @@
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.daemon.impl.DaemonListeners;
import com.intellij.codeInsight.daemon.impl.ShowAutoImportPass;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.hint.QuestionAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.HintAction;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.impl.LaterInvocator;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
@@ -95,6 +98,9 @@ public abstract class StaticImportMemberFix<T extends PsiMember> implements Inte
}
private ImportClassFixBase.Result doFix(Editor editor) {
if (!CodeInsightSettings.getInstance().ADD_MEMBER_IMPORTS_ON_THE_FLY) {
return ImportClassFixBase.Result.POPUP_NOT_SHOWN;
}
final List<T> candidates = getMembersToImport(true);
if (candidates.isEmpty()) {
return ImportClassFixBase.Result.POPUP_NOT_SHOWN;
@@ -106,7 +112,10 @@ public abstract class StaticImportMemberFix<T extends PsiMember> implements Inte
}
final QuestionAction action = createQuestionAction(candidates, element.getProject(), editor);
if (candidates.size() == 1 && ImportClassFixBase.canAddUnambiguousImport(element.getContainingFile())) {
PsiFile psiFile = element.getContainingFile();
if (candidates.size() == 1 &&
(ApplicationManager.getApplication().isUnitTestMode() || DaemonListeners.canChangeFileSilently(psiFile)) &&
!LaterInvocator.isInModalContext()) {
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@Override
public void run() {
@@ -135,6 +135,7 @@ public class CodeInsightSettings implements PersistentStateComponent<Element>, C
public boolean OPTIMIZE_IMPORTS_ON_THE_FLY = false;
public boolean ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = false;
public boolean ADD_MEMBER_IMPORTS_ON_THE_FLY = true;
public boolean JSP_ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = false;
/**
@@ -90,7 +90,9 @@ public class ShowAutoImportPass extends TextEditorHighlightingPass {
private void importUnambiguousImports(final int caretOffset) {
if (!DaemonCodeAnalyzerSettings.getInstance().isImportHintEnabled()) return;
if (!DaemonCodeAnalyzer.getInstance(myProject).isImportHintsEnabled(myFile)) return;
if (!CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY) return;
final CodeInsightSettings codeInsightSettings = CodeInsightSettings.getInstance();
if (!codeInsightSettings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY &&
!codeInsightSettings.ADD_MEMBER_IMPORTS_ON_THE_FLY) return;
Document document = getDocument();
final List<HighlightInfo> infos = new ArrayList<HighlightInfo>();