[Parameter Name Hints] in context menu show dialog on "Add current method to blacklist" with preselected item

This commit is contained in:
Yaroslav Lepenkin
2016-11-02 02:14:19 +03:00
parent d70d3360fd
commit f9d0b8dadf
5 changed files with 96 additions and 53 deletions
@@ -17,4 +17,11 @@ package com.intellij.codeInsight.hints
class InlayInfo(val text: String, val offset: Int)
class MethodInfo(val fullyQualifiedName: String, val paramNames: List<String>)
open class MethodInfo(val fullyQualifiedName: String, val paramNames: List<String>) {
open fun getMethodName(): String {
val start = fullyQualifiedName.lastIndexOf('.') + 1
return fullyQualifiedName.substring(start)
}
}
@@ -40,34 +40,30 @@ import com.intellij.psi.util.PsiTreeUtil
private fun String.capitalize() = StringUtil.capitalizeWords(this, true)
class ShowParameterHintsSettings : AnAction() {
class ShowSettingsWithAddedPattern : AnAction() {
init {
val presentation = templatePresentation
presentation.text = CodeInsightBundle.message("inlay.hints.show.settings").capitalize()
presentation.description = CodeInsightBundle.message("inlay.hints.show.settings.description")
templatePresentation.description = CodeInsightBundle.message("inlay.hints.show.settings.description")
}
override fun update(e: AnActionEvent) {
val file = CommonDataKeys.PSI_FILE.getData(e.dataContext) ?: return
val editor = CommonDataKeys.EDITOR.getData(e.dataContext) ?: return
val info = getMethodInfoAtOffset(editor, file) ?: return
val name = info.getMethodName()
e.presentation.text = CodeInsightBundle.message("inlay.hints.show.settings", name)
}
override fun actionPerformed(e: AnActionEvent) {
val project = CommonDataKeys.PROJECT.getData(e.dataContext) ?: return
val file = CommonDataKeys.PSI_FILE.getData(e.dataContext) ?: return
val hintExtension = InlayParameterHintsExtension.forLanguage(file.language) ?: return
val dialog = ParameterNameHintsConfigurable(project, hintExtension.defaultBlackList, file.language)
dialog.show()
}
}
class BlacklistCurrentMethodAction : AnAction() {
init {
val presentation = templatePresentation
presentation.text = CodeInsightBundle.message("inlay.hints.blacklist.method").capitalize()
presentation.description = CodeInsightBundle.message("inlay.hints.blacklist.method.description")
}
override fun actionPerformed(e: AnActionEvent) {
val editor = CommonDataKeys.EDITOR.getData(e.dataContext) ?: return
val file = CommonDataKeys.PSI_FILE.getData(e.dataContext) ?: return
val hintExtension = InlayParameterHintsExtension.forLanguage(file.language) ?: return
val info = getMethodInfoAtOffset(editor, file) ?: return
val dialog = ParameterNameHintsConfigurable(project, hintExtension.defaultBlackList, file.language, info.toPattern())
addMethodAtCaretToBlackList(editor, file)
dialog.show()
}
}
@@ -160,18 +156,19 @@ private fun refreshAllOpenEditors() {
}
}
private fun addMethodAtCaretToBlackList(editor: Editor, file: PsiFile) {
private fun getMethodInfoAtOffset(editor: Editor, file: PsiFile): MethodInfo? {
val offset = editor.caretModel.offset
val element = file.findElementAt(offset)
val hintsProvider = InlayParameterHintsExtension.forLanguage(file.language) ?: return
val method = PsiTreeUtil.findFirstParent(element, { e -> hintsProvider.getMethodInfo(e) != null }) ?: return
val info = hintsProvider.getMethodInfo(method) ?: return
val pattern = info.fullyQualifiedName + '(' + info.paramNames.joinToString(",") + ')'
ParameterNameHintsSettings.getInstance().addIgnorePattern(file.language, pattern)
val hintsProvider = InlayParameterHintsExtension.forLanguage(file.language) ?: return null
val method = PsiTreeUtil.findFirstParent(element, { e -> hintsProvider.getMethodInfo(e) != null }) ?: return null
return hintsProvider.getMethodInfo(method)
}
private fun addMethodAtCaretToBlackList(editor: Editor, file: PsiFile) {
val info = getMethodInfoAtOffset(editor, file) ?: return
ParameterNameHintsSettings.getInstance().addIgnorePattern(file.language, info.toPattern())
refreshAllOpenEditors()
}
@@ -189,3 +186,5 @@ fun isPossibleHintNearOffset(file: PsiFile, offset: Int): Boolean {
return false
}
fun MethodInfo.toPattern() = this.fullyQualifiedName + '(' + this.paramNames.joinToString(",") + ')'
@@ -17,37 +17,49 @@ package com.intellij.codeInsight.hints.settings;
import com.intellij.codeInsight.hints.filtering.MatcherConstructor;
import com.intellij.lang.Language;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.fileTypes.PlainTextLanguage;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.EditorTextFieldProvider;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class ParameterNameHintsConfigurable extends DialogWrapper {
public JPanel myConfigurable;
private EditorTextField myEditorTextField;
private final Project myProject;
private final Set<String> myDefaultBlackList;
private final Language myLanguage;
private final String myNewPreselectedItem;
private final Project myProject;
public ParameterNameHintsConfigurable(@NotNull Project project,
public ParameterNameHintsConfigurable(@NotNull Project project,
@NotNull Set<String> defaultBlackList,
@NotNull Language language) {
this(project, defaultBlackList, language, null);
}
public ParameterNameHintsConfigurable(@NotNull Project project,
@NotNull Set<String> defaultBlackList,
@NotNull Language language,
@Nullable String newPreselectedPattern) {
super(project);
myProject = project;
myDefaultBlackList = defaultBlackList;
myLanguage = language;
myDefaultBlackList = defaultBlackList;
myNewPreselectedItem = newPreselectedPattern;
setTitle("Configure Parameter Name Hints Blacklist");
init();
}
@@ -84,18 +96,11 @@ public class ParameterNameHintsConfigurable extends DialogWrapper {
return myConfigurable;
}
public JPanel myConfigurable;
private EditorTextField myEditorTextField;
private void createUIComponents() {
EditorTextFieldProvider service = ServiceManager.getService(myProject, EditorTextFieldProvider.class);
myEditorTextField = service.getEditorField(PlainTextLanguage.INSTANCE, myProject, ContainerUtil.emptyIterable());
Diff diff = ParameterNameHintsSettings.getInstance().getBlackListDiff(myLanguage);
Set<String> blacklist = diff.applyOn(myDefaultBlackList);
String text = StringUtil.join(blacklist, "\n");
myEditorTextField.setText(text);
myEditorTextField = createEditor(blacklist, myNewPreselectedItem);
myEditorTextField.addDocumentListener(new DocumentAdapter() {
@Override
public void documentChanged(DocumentEvent e) {
@@ -103,7 +108,40 @@ public class ParameterNameHintsConfigurable extends DialogWrapper {
}
});
}
private EditorTextField createEditor(@NotNull Set<String> blacklist, @Nullable String newPreselectedItem) {
String text = StringUtil.join(blacklist, "\n");
final TextRange range;
if (newPreselectedItem != null) {
text += "\n";
final int startOffset = text.length();
text += newPreselectedItem;
range = new TextRange(startOffset, text.length());
}
else {
range = null;
}
return createEditorField(text, range);
}
@NotNull
private EditorTextField createEditorField(@NotNull String text, @Nullable TextRange rangeToSelect) {
Document document = EditorFactory.getInstance().createDocument(text);
EditorTextField field = new EditorTextField(document, myProject, FileTypes.PLAIN_TEXT, false, false);
field.setPreferredSize(new Dimension(200, 350));
field.addSettingsProvider(editor -> {
editor.setVerticalScrollbarVisible(true);
editor.setHorizontalScrollbarVisible(true);
editor.getSettings().setAdditionalLinesCount(2);
if (rangeToSelect != null) {
editor.getCaretModel().moveToOffset(rangeToSelect.getStartOffset());
editor.getScrollingModel().scrollVertically(document.getTextLength() - 1);
editor.getSelectionModel().setSelection(rangeToSelect.getStartOffset(), rangeToSelect.getEndOffset());
}
});
return field;
}
}
@@ -528,8 +528,8 @@ highlight.imported.members.chooser.title=Choose Imported Members to Highlight
javadoc.resolved.value=Resolved value\:
javadoc.error.resolving.url=Couldn''t resolve URL <i>{0}</i> <p>Configuring paths to API docs in <a href="open://Project Settings">project settings</a> might help
inlay.hints.show.settings=Show parameter name hints settings...
inlay.hints.show.settings.description=Show dialog with parameter name hints settings
inlay.hints.show.settings=Add Method ''{0}'' to Blacklist...
inlay.hints.show.settings.description=Open parameter name hints settings
inlay.hints.blacklist.method=Do not show hints for current method
inlay.hints.blacklist.method.description=Adds current method to parameter name hints blacklist
inlay.hints.intention.family.name=Parameter Name Hints
@@ -300,8 +300,7 @@
</group>
<group id="ParameterNameHints" popup="true">
<action id="BlacklistCurrentMethodAction" class="com.intellij.codeInsight.hints.BlacklistCurrentMethodAction"/>
<action id="ShowParameterHintsSettings" class="com.intellij.codeInsight.hints.ShowParameterHintsSettings"/>
<action id="ShowSettingsWithAddedPattern" class="com.intellij.codeInsight.hints.ShowSettingsWithAddedPattern"/>
</group>
<action id="ToggleInlineHintsAction" class="com.intellij.codeInsight.hints.ToggleInlineHintsAction">