diff --git a/java/java-impl/src/com/intellij/codeInsight/hints/JavaInlayParameterHintsProvider.kt b/java/java-impl/src/com/intellij/codeInsight/hints/JavaInlayParameterHintsProvider.kt new file mode 100644 index 000000000000..b72fb1ec4bae --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/hints/JavaInlayParameterHintsProvider.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.hints + +import com.intellij.psi.PsiCallExpression +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiMethod + +class JavaInlayParameterHintsProvider : InlayParameterHintsProvider { + + override fun getMethodInfo(element: PsiElement): MethodInfo? { + if (element is PsiCallExpression) { + val resolvedElement = element.resolveMethodGenerics().element + if (resolvedElement is PsiMethod) { + return getMethodInfo(resolvedElement) + } + } + return null + } + + override fun getParameterHints(element: PsiElement): List { + if (element is PsiCallExpression) { + return JavaParameterHintManager(element).descriptors + } + return emptyList() + } + + private fun getMethodInfo(method: PsiMethod): MethodInfo { + val qualifier = method.containingClass?.qualifiedName ?: "" + val fullMethodName = qualifier + "." + method.name + + val paramNames: List = method.parameterList.parameters.map { it.name ?: "" } + return MethodInfo(fullMethodName, paramNames) + } + + +} \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterNameHintsManager.java b/java/java-impl/src/com/intellij/codeInsight/hints/JavaParameterHintManager.java similarity index 82% rename from java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterNameHintsManager.java rename to java/java-impl/src/com/intellij/codeInsight/hints/JavaParameterHintManager.java index a73b6c1d0561..ef19b296623c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterNameHintsManager.java +++ b/java/java-impl/src/com/intellij/codeInsight/hints/JavaParameterHintManager.java @@ -13,9 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.codeInsight.daemon.impl; +package com.intellij.codeInsight.hints; -import com.intellij.codeInsight.hints.filtering.Matcher; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.TypeConversionUtil; @@ -26,15 +25,12 @@ import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.List; -public class ParameterNameHintsManager { +public class JavaParameterHintManager { @NotNull private final List myDescriptors; - private final List myBlackListMatchers; - public ParameterNameHintsManager(@NotNull PsiCallExpression callExpression, List blackListMatchers) { - myBlackListMatchers = blackListMatchers; - + public JavaParameterHintManager(@NotNull PsiCallExpression callExpression) { PsiExpression[] callArguments = getArguments(callExpression); JavaResolveResult resolveResult = callExpression.resolveMethodGenerics(); @@ -51,14 +47,11 @@ public class ParameterNameHintsManager { myDescriptors = descriptors; } - private boolean isMethodToShowParams(@NotNull PsiCallExpression callExpression, @NotNull JavaResolveResult resolveResult) { + private static boolean isMethodToShowParams(@NotNull PsiCallExpression callExpression, @NotNull JavaResolveResult resolveResult) { PsiElement element = resolveResult.getElement(); if (element instanceof PsiMethod) { PsiMethod method = (PsiMethod)element; - if (isSetter(method) || isBuilder(callExpression, method)) { - return false; - } - return !isBlackListed(method); + return !isSetter(method) && !isBuilder(callExpression, method); } return false; } @@ -80,21 +73,6 @@ public class ParameterNameHintsManager { return method.getParameterList().getParametersCount() == 1; } - public static MethodInfo getMethodInfo(PsiMethod method) { - PsiClass aClass = method.getContainingClass(); - String qualifier = aClass != null ? aClass.getQualifiedName() : ""; - String fullMethodName = qualifier + "." + method.getName(); - - PsiParameter[] params = method.getParameterList().getParameters(); - List paramNames = ContainerUtil.map(params, (e) -> e.getName()); - - return new MethodInfo(fullMethodName, paramNames); - } - - private boolean isBlackListed(PsiMethod method) { - MethodInfo info = getMethodInfo(method); - return myBlackListMatchers.stream().anyMatch((e) -> e.isMatching(info.getFullyQualifiedName(), info.getParamNames())); - } private static boolean isSetter(PsiMethod method) { String methodName = method.getName(); @@ -156,7 +134,7 @@ public class ParameterNameHintsManager { @NotNull private static InlayInfo createInlayInfo(@NotNull PsiExpression callArgument, @NotNull PsiParameter methodParam) { String paramName = ((methodParam.getType() instanceof PsiEllipsisType) ? "..." : "") + methodParam.getName(); - return new InlayInfo(paramName, callArgument.getTextRange().getStartOffset(), callArgument); + return new InlayInfo(paramName, callArgument.getTextRange().getStartOffset()); } private static boolean shouldInlineParameterName(@NotNull PsiExpression argument, diff --git a/platform/lang-api/src/com/intellij/codeInsight/hints/InlayParameterHintsProvider.kt b/platform/lang-api/src/com/intellij/codeInsight/hints/InlayParameterHintsProvider.kt new file mode 100644 index 000000000000..c19f44d4c54b --- /dev/null +++ b/platform/lang-api/src/com/intellij/codeInsight/hints/InlayParameterHintsProvider.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.hints + +import com.intellij.lang.LanguageExtension +import com.intellij.psi.PsiElement + + +object InlayParameterHintsExtension: LanguageExtension("com.intellij.parameterHintsProvider") + +interface InlayParameterHintsProvider { + + fun getParameterHints(element: PsiElement): List { + return emptyList() + } + + fun getMethodInfo(element: PsiElement): MethodInfo? { + return null + } + +} \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterNameInlays.kt b/platform/lang-api/src/com/intellij/codeInsight/hints/ParameterNameInlays.kt similarity index 80% rename from java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterNameInlays.kt rename to platform/lang-api/src/com/intellij/codeInsight/hints/ParameterNameInlays.kt index f7a8d795048d..aa83001db198 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterNameInlays.kt +++ b/platform/lang-api/src/com/intellij/codeInsight/hints/ParameterNameInlays.kt @@ -13,10 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.codeInsight.daemon.impl +package com.intellij.codeInsight.hints -import com.intellij.psi.PsiExpression - -class InlayInfo(val text: String, val offset: Int, val argument: PsiExpression) +class InlayInfo(val text: String, val offset: Int) class MethodInfo(val fullyQualifiedName: String, val paramNames: List) \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterHintsPassFactory.java b/platform/lang-impl/src/com/intellij/codeInsight/hints/ParameterHintsPassFactory.java similarity index 83% rename from java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterHintsPassFactory.java rename to platform/lang-impl/src/com/intellij/codeInsight/hints/ParameterHintsPassFactory.java index d9ff57b6dc47..dcb1d7b7ce18 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterHintsPassFactory.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/hints/ParameterHintsPassFactory.java @@ -13,12 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.codeInsight.daemon.impl; +package com.intellij.codeInsight.hints; import com.intellij.codeHighlighting.EditorBoundHighlightingPass; import com.intellij.codeHighlighting.TextEditorHighlightingPass; import com.intellij.codeHighlighting.TextEditorHighlightingPassFactory; import com.intellij.codeHighlighting.TextEditorHighlightingPassRegistrar; +import com.intellij.codeInsight.daemon.impl.ParameterHintsPresentationManager; import com.intellij.codeInsight.hints.filtering.Matcher; import com.intellij.codeInsight.hints.filtering.MatcherConstructor; import com.intellij.codeInsight.hints.settings.ParameterNameHintsSettings; @@ -31,7 +32,9 @@ import com.intellij.openapi.editor.ex.EditorSettingsExternalizable; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; -import com.intellij.psi.*; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.SyntaxTraverser; import com.intellij.util.containers.HashSet; import gnu.trove.TIntObjectHashMap; import org.jetbrains.annotations.NotNull; @@ -65,8 +68,10 @@ public class ParameterHintsPassFactory extends AbstractProjectComponent implemen public void doCollectInformation(@NotNull ProgressIndicator progress) { assert myDocument != null; myAnnotations.clear(); - if (!isEnabled() || !(myFile instanceof PsiJavaFile)) return; - PsiJavaFile file = (PsiJavaFile) myFile; + if (!isEnabled()) return; + + InlayParameterHintsProvider provider = InlayParameterHintsExtension.INSTANCE.forLanguage(myFile.getLanguage()); + if (provider == null) return; List matchers = ParameterNameHintsSettings .getInstance() @@ -75,20 +80,21 @@ public class ParameterHintsPassFactory extends AbstractProjectComponent implemen .map((item) -> MatcherConstructor.INSTANCE.createMatcher(item)) .collect(Collectors.toList()); - SyntaxTraverser.psiTraverser(file).forEach(element -> process(element, matchers)); + SyntaxTraverser.psiTraverser(myFile).forEach(element -> process(element, provider, matchers)); } private static boolean isEnabled() { return EditorSettingsExternalizable.getInstance().isShowParameterNameHints(); } - private void process(PsiElement child, List matchers) { - if (child instanceof PsiCallExpression) { - PsiCallExpression callExpression = (PsiCallExpression)child; - ParameterNameHintsManager manager = new ParameterNameHintsManager(callExpression, matchers); - for (InlayInfo info : manager.getDescriptors()) { - myAnnotations.put(info.getOffset(), info.getText()); - } + private static boolean isMatchedByAny(MethodInfo info, List matchers) { + return matchers.stream().anyMatch((e) -> e.isMatching(info.getFullyQualifiedName(), info.getParamNames())); + } + + private void process(PsiElement element, InlayParameterHintsProvider provider, List blackListMatchers) { + MethodInfo info = provider.getMethodInfo(element); + if (info != null && !isMatchedByAny(info, blackListMatchers)) { + provider.getParameterHints(element).forEach((h) -> myAnnotations.put(h.getOffset(), h.getText())); } } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/ShowParameterHintsSettings.kt b/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt similarity index 77% rename from java/java-impl/src/com/intellij/codeInsight/daemon/impl/ShowParameterHintsSettings.kt rename to platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt index 5f84a753bff8..443d13327603 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/ShowParameterHintsSettings.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt @@ -13,15 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.codeInsight.daemon.impl +package com.intellij.codeInsight.hints import com.intellij.codeInsight.hints.settings.ParameterNameHintsConfigurable import com.intellij.codeInsight.hints.settings.ParameterNameHintsSettings import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys -import com.intellij.psi.PsiCallExpression -import com.intellij.psi.PsiMethod import com.intellij.psi.util.PsiTreeUtil class ShowParameterHintsSettings : AnAction() { @@ -31,7 +29,7 @@ class ShowParameterHintsSettings : AnAction() { presentation.text = "Show Settings" presentation.description = "Show Parameter Name Hints Settings" } - + override fun actionPerformed(e: AnActionEvent) { val project = CommonDataKeys.PROJECT.getData(e.dataContext) ?: return val dialog = ParameterNameHintsConfigurable(project) @@ -55,14 +53,13 @@ class BlacklistCurrentMethodAction : AnAction() { val offset = editor.caretModel.offset val element = file.findElementAt(offset) - val callExpression = PsiTreeUtil.getParentOfType(element, PsiCallExpression::class.java) + val hintsProvider = InlayParameterHintsExtension.forLanguage(file.language) ?: return - val result = callExpression?.resolveMethodGenerics()?.element ?: return - if (result is PsiMethod) { - val info = ParameterNameHintsManager.getMethodInfo(result) - val pattern = info.fullyQualifiedName + '(' + info.paramNames.joinToString(",") + ')' - ParameterNameHintsSettings.getInstance().addIgnorePattern(pattern) - } + 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(pattern) } - -} \ No newline at end of file + +} diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index 072d47e317ee..4ac7053380b8 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -576,6 +576,9 @@ + + + diff --git a/platform/platform-resources/src/idea/LangActions.xml b/platform/platform-resources/src/idea/LangActions.xml index 3a56b2bdc9e5..c0188785cb7c 100644 --- a/platform/platform-resources/src/idea/LangActions.xml +++ b/platform/platform-resources/src/idea/LangActions.xml @@ -300,8 +300,8 @@ - - + + diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 51ecafc90b9a..2484030200b8 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -71,7 +71,7 @@ com.intellij.execution.scratch.JavaScratchCompilationSupport - com.intellij.codeInsight.daemon.impl.ParameterHintsPassFactory + com.intellij.codeInsight.hints.ParameterHintsPassFactory @@ -1149,6 +1149,7 @@ +