don't show parameter hints on completion of blacklisted methods with one parameter

This commit is contained in:
Dmitry Batrak
2017-04-19 14:36:37 +03:00
parent af08eeeeb9
commit 9a70984732
4 changed files with 45 additions and 18 deletions
@@ -21,6 +21,10 @@ import com.intellij.codeInsight.daemon.impl.ParameterHintsPresentationManager;
import com.intellij.codeInsight.hint.ParameterInfoController;
import com.intellij.codeInsight.hint.ShowParameterInfoContext;
import com.intellij.codeInsight.hint.api.impls.MethodParameterInfoHandler;
import com.intellij.codeInsight.hints.HintInfo;
import com.intellij.codeInsight.hints.JavaInlayParameterHintsProvider;
import com.intellij.codeInsight.hints.ParameterHintsPassFactory;
import com.intellij.codeInsight.hints.filtering.Matcher;
import com.intellij.codeInsight.lookup.*;
import com.intellij.codeInsight.lookup.impl.JavaElementLookupRenderer;
import com.intellij.codeInsight.template.*;
@@ -29,6 +33,7 @@ import com.intellij.codeInsight.template.impl.TemplateImpl;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
@@ -300,18 +305,29 @@ public class JavaMethodCallElement extends LookupItem<PsiMethod> implements Type
return;
}
boolean showHints = true;
if (parametersCount == 1) {
HintInfo.MethodInfo methodInfo = JavaInlayParameterHintsProvider.Companion.getInstance().getMethodInfo(method);
if (methodInfo != null) {
List<Matcher> matchers = ParameterHintsPassFactory.getBlackListMatchers(JavaLanguage.INSTANCE);
showHints = matchers.stream().noneMatch(m -> m.isMatching(methodInfo.getFullyQualifiedName(), methodInfo.getParamNames()));
}
}
Editor editor = context.getEditor();
CaretModel caretModel = editor.getCaretModel();
int offset = caretModel.getOffset();
caretModel.moveToOffset(offset - 1); // avoid caret impact on hints location
editor.getDocument().insertString(offset, StringUtil.repeat(", ", parametersCount - 1));
List<Inlay> addedHints = new ArrayList<>(parametersCount);
for (PsiParameter parameter : parameterList.getParameters()) {
String name = parameter.getName();
if (name != null) {
addedHints.add(ParameterHintsPresentationManager.getInstance().addHint(editor, offset, name + ":", false, true));
if (showHints) {
for (PsiParameter parameter : parameterList.getParameters()) {
String name = parameter.getName();
if (name != null) {
addedHints.add(ParameterHintsPresentationManager.getInstance().addHint(editor, offset, name + ":", false, true));
}
offset += 2;
}
offset += 2;
}
int braceOffset = caretModel.getOffset();
caretModel.moveToLogicalPosition(editor.offsetToLogicalPosition(braceOffset + 1).leanForward(true));
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -45,7 +45,7 @@ class JavaInlayParameterHintsProvider : InlayParameterHintsProvider {
return emptyList()
}
private fun getMethodInfo(method: PsiMethod): MethodInfo? {
fun getMethodInfo(method: PsiMethod): MethodInfo? {
val containingClass = method.containingClass ?: return null
val fullMethodName = StringUtil.getQualifiedName(containingClass.qualifiedName, method.name)
@@ -152,6 +152,12 @@ public class CompletionHintsTest extends LightFixtureCompletionTestCase {
myFixture.checkResult("class C { void m() { System.setProperty(System.getProperty(, ), )<caret> } }");
}
public void testNoHintsForMethodWithOneParameterFromBlackList() {
myFixture.configureByText(JavaFileType.INSTANCE, "class C { void m() { System.getPro<caret> } }");
complete("getProperty(String key)");
myFixture.checkResultWithInlays("class C { void m() { System.getProperty() } }");
}
private void showParameterInfo() {
myFixture.performEditorAction("ParameterInfo");
UIUtil.dispatchAllInvocationEvents();
@@ -59,6 +59,21 @@ public class ParameterHintsPassFactory extends AbstractProjectComponent implemen
return new ParameterHintsPass(file, editor);
}
public static List<Matcher> getBlackListMatchers(Language language) {
InlayParameterHintsProvider provider = InlayParameterHintsExtension.INSTANCE.forLanguage(language);
Set<String> blackList = ParameterHintsPass.getBlackList(language);
Language dependentLanguage = provider.getBlackListDependencyLanguage();
if (dependentLanguage != null) {
blackList.addAll(ParameterHintsPass.getBlackList(dependentLanguage));
}
return blackList
.stream()
.map((item) -> MatcherConstructor.INSTANCE.createMatcher(item))
.filter((e) -> e != null)
.collect(Collectors.toList());
}
private static class ParameterHintsPass extends EditorBoundHighlightingPass {
private final Map<Integer, String> myHints = new HashMap<>();
private final Map<Integer, String> myShowOnlyIfExistedBeforeHints = new HashMap<>();
@@ -77,17 +92,7 @@ public class ParameterHintsPassFactory extends AbstractProjectComponent implemen
InlayParameterHintsProvider provider = InlayParameterHintsExtension.INSTANCE.forLanguage(language);
if (provider == null) return;
Set<String> blackList = getBlackList(language);
Language dependentLanguage = provider.getBlackListDependencyLanguage();
if (dependentLanguage != null) {
blackList.addAll(getBlackList(dependentLanguage));
}
List<Matcher> matchers = blackList
.stream()
.map((item) -> MatcherConstructor.INSTANCE.createMatcher(item))
.filter((e) -> e != null)
.collect(Collectors.toList());
List<Matcher> matchers = getBlackListMatchers(language);
SyntaxTraverser.psiTraverser(myFile).forEach(element -> process(element, provider, matchers));
}