From f9d0b8dadff3208b90bc1d6aea63d9d601e46498 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Tue, 1 Nov 2016 21:20:25 +0300 Subject: [PATCH] [Parameter Name Hints] in context menu show dialog on "Add current method to blacklist" with preselected item --- .../codeInsight/hints/ParameterNameInlays.kt | 9 ++- .../codeInsight/hints/PopupActions.kt | 57 +++++++------- .../ParameterNameHintsConfigurable.java | 76 ++++++++++++++----- .../src/messages/CodeInsightBundle.properties | 4 +- .../src/idea/LangActions.xml | 3 +- 5 files changed, 96 insertions(+), 53 deletions(-) diff --git a/platform/lang-api/src/com/intellij/codeInsight/hints/ParameterNameInlays.kt b/platform/lang-api/src/com/intellij/codeInsight/hints/ParameterNameInlays.kt index aa83001db198..c9e8f73de3fe 100644 --- a/platform/lang-api/src/com/intellij/codeInsight/hints/ParameterNameInlays.kt +++ b/platform/lang-api/src/com/intellij/codeInsight/hints/ParameterNameInlays.kt @@ -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) \ No newline at end of file +open class MethodInfo(val fullyQualifiedName: String, val paramNames: List) { + + open fun getMethodName(): String { + val start = fullyQualifiedName.lastIndexOf('.') + 1 + return fullyQualifiedName.substring(start) + } + +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt b/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt index aba36f650a9e..1c6d201f64d4 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt @@ -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(",") + ')' diff --git a/platform/lang-impl/src/com/intellij/codeInsight/hints/settings/ParameterNameHintsConfigurable.java b/platform/lang-impl/src/com/intellij/codeInsight/hints/settings/ParameterNameHintsConfigurable.java index fad2ac7c7039..a6040a30aba8 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hints/settings/ParameterNameHintsConfigurable.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/hints/settings/ParameterNameHintsConfigurable.java @@ -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 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 defaultBlackList, @NotNull Language language) { + this(project, defaultBlackList, language, null); + } + + public ParameterNameHintsConfigurable(@NotNull Project project, + @NotNull Set 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 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 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; + } } diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index 622644870ece..c094f99c6f0c 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -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 {0}

Configuring paths to API docs in project settings 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 diff --git a/platform/platform-resources/src/idea/LangActions.xml b/platform/platform-resources/src/idea/LangActions.xml index a0c79c8bff5b..13632b2e51b4 100644 --- a/platform/platform-resources/src/idea/LangActions.xml +++ b/platform/platform-resources/src/idea/LangActions.xml @@ -300,8 +300,7 @@ - - +