private void addImport(final PsiReference ref, final PsiClass targetClass) { StatisticsManager.getInstance().incUseCount(JavaStatisticsManager.createInfo(null, targetClass)); CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { @Override public void run() { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { DumbService.getInstance(myProject).withAlternativeResolveEnabled(new Runnable() { @Override public void run() { _addImport(ref, targetClass); } }); } }); } }, QuickFixBundle.message("add.import"), null); }
private static int compareMembers(PsiMember m1, PsiMember m2, PsiExpression context) { ProgressManager.checkCanceled(); int result = JavaStatisticsManager.createInfo(null, m2).getUseCount() - JavaStatisticsManager.createInfo(null, m1).getUseCount(); if (result != 0) return result; final PsiClass aClass = m1.getContainingClass(); final PsiClass bClass = m2.getContainingClass(); if (aClass != null && bClass != null) { result = JavaStatisticsManager.createInfo(null, bClass).getUseCount() - JavaStatisticsManager.createInfo(null, aClass).getUseCount(); if (result != 0) return result; } WeighingComparable<PsiElement,ProximityLocation> proximity1 = PsiProximityComparator.getProximity(m1, context); WeighingComparable<PsiElement,ProximityLocation> proximity2 = PsiProximityComparator.getProximity(m2, context); if (proximity1 != null && proximity2 != null) { result = proximity2.compareTo(proximity1); if (result != 0) return result; } String name1 = PsiUtil.getMemberQualifiedName(m1); String name2 = PsiUtil.getMemberQualifiedName(m2); return Comparing.compare(name1, name2); }
@Override public SuggestedNameInfo getSuggestedNames(final PsiElement element, @Nullable PsiElement nameSuggestionContext, Set<String> result) { if (nameSuggestionContext == null) nameSuggestionContext = element; if (element instanceof GrVariable && nameSuggestionContext instanceof GroovyPsiElement) { final PsiType type = ((GrVariable)element).getTypeGroovy(); if (type != null) { final String[] names = GroovyNameSuggestionUtil .suggestVariableNameByType(type, new DefaultGroovyVariableNameValidator((GroovyPsiElement)nameSuggestionContext)); result.addAll(Arrays.asList(names)); return new SuggestedNameInfo(names) { @Override public void nameChosen(String name) { JavaStatisticsManager .incVariableNameUseCount(name, JavaCodeStyleManager.getInstance(element.getProject()).getVariableKind((GrVariable)element), ((GrVariable)element).getName(), type); } }; } } return null; }
private static int compareMembers(PsiMember m1, PsiMember m2, PsiExpression context) { ProgressManager.checkCanceled(); int result = JavaStatisticsManager.createInfo(null, m2).getUseCount() - JavaStatisticsManager.createInfo(null, m1).getUseCount(); if (result != 0) return result; final PsiClass aClass = m1.getContainingClass(); final PsiClass bClass = m2.getContainingClass(); if (aClass != null && bClass != null) { result = JavaStatisticsManager.createInfo(null, bClass).getUseCount() - JavaStatisticsManager.createInfo(null, aClass).getUseCount(); if (result != 0) return result; } WeighingComparable<PsiElement,ProximityLocation> proximity1 = PsiProximityComparator.getProximity(m1, context); WeighingComparable<PsiElement,ProximityLocation> proximity2 = PsiProximityComparator.getProximity(m2, context); if (proximity1 != null && proximity2 != null) { result = proximity2.compareTo(proximity1); if (result != 0) return result; } String name1 = PsiUtil.getMemberQualifiedName(m1); String name2 = PsiUtil.getMemberQualifiedName(m2); return name1 == null || name2 == null ? 0 : name1.compareTo(name2); }
private static boolean processMostProbableInheritors(CompletionParameters parameters, Collection<PsiClassType> expectedClassTypes, Consumer<PsiType> consumer) { PsiFile file = parameters.getOriginalFile(); for (final PsiClassType type : expectedClassTypes) { consumer.consume(type); final PsiClassType.ClassResolveResult baseResult = JavaCompletionUtil.originalize(type).resolveGenerics(); final PsiClass baseClass = baseResult.getElement(); if (baseClass == null) return false; final PsiSubstitutor baseSubstitutor = baseResult.getSubstitutor(); final Processor<PsiClass> processor = CodeInsightUtil.createInheritorsProcessor(parameters.getPosition(), type, 0, false, consumer, baseClass, baseSubstitutor); final StatisticsInfo[] stats = StatisticsManager.getInstance().getAllValues(JavaStatisticsManager.getAfterNewKey(type)); for (final StatisticsInfo statisticsInfo : stats) { final String value = statisticsInfo.getValue(); if (value.startsWith(JavaStatisticsManager.CLASS_PREFIX)) { final String qname = value.substring(JavaStatisticsManager.CLASS_PREFIX.length()); final PsiClass psiClass = JavaPsiFacade.getInstance(file.getProject()).findClass(qname, file.getResolveScope()); if (psiClass != null && !PsiTreeUtil.isAncestor(file, psiClass, true) && !processor.process(psiClass)) break; } } } return true; }
private static void sortVariableNameSuggestions(@NotNull String[] names, @NotNull final VariableKind variableKind, @Nullable final String propertyName, @Nullable final PsiType type) { if( names.length <= 1 ) { return; } if (LOG.isDebugEnabled()) { LOG.debug("sorting names:" + variableKind); if (propertyName != null) { LOG.debug("propertyName:" + propertyName); } if (type != null) { LOG.debug("type:" + type); } for (String name : names) { int count = JavaStatisticsManager.getVariableNameUseCount(name, variableKind, propertyName, type); LOG.debug(name + " : " + count); } } Comparator<String> comparator = new Comparator<String>() { @Override public int compare(@NotNull String s1, @NotNull String s2) { int count1 = JavaStatisticsManager.getVariableNameUseCount(s1, variableKind, propertyName, type); int count2 = JavaStatisticsManager.getVariableNameUseCount(s2, variableKind, propertyName, type); return count2 - count1; } }; Arrays.sort(names, comparator); }
private void addImport(final PsiReference ref, final PsiClass targetClass) { StatisticsManager.getInstance().incUseCount(JavaStatisticsManager.createInfo(null, targetClass)); CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { @Override public void run() { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { _addImport(ref, targetClass); } }); } }, QuickFixBundle.message("add.import"), null); }
private static void sortVariableNameSuggestions(String[] names, final VariableKind variableKind, @Nullable final String propertyName, @Nullable final PsiType type) { if( names.length <= 1 ) { return; } if (LOG.isDebugEnabled()) { LOG.debug("sorting names:" + variableKind); if (propertyName != null) { LOG.debug("propertyName:" + propertyName); } if (type != null) { LOG.debug("type:" + type); } for (String name : names) { int count = JavaStatisticsManager.getVariableNameUseCount(name, variableKind, propertyName, type); LOG.debug(name + " : " + count); } } Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String s1, String s2) { int count1 = JavaStatisticsManager.getVariableNameUseCount(s1, variableKind, propertyName, type); int count2 = JavaStatisticsManager.getVariableNameUseCount(s2, variableKind, propertyName, type); return count2 - count1; } }; Arrays.sort(names, comparator); }
private static boolean processMostProbableInheritors(PsiFile contextFile, PsiElement context, Collection<PsiClassType> expectedClassTypes, Consumer<PsiType> consumer) { for(final PsiClassType type : expectedClassTypes) { consumer.consume(type); final PsiClassType.ClassResolveResult baseResult = JavaCompletionUtil.originalize(type).resolveGenerics(); final PsiClass baseClass = baseResult.getElement(); if(baseClass == null) { return false; } final PsiSubstitutor baseSubstitutor = baseResult.getSubstitutor(); final Processor<PsiClass> processor = CodeInsightUtil.createInheritorsProcessor(context, type, 0, false, consumer, baseClass, baseSubstitutor); final StatisticsInfo[] stats = StatisticsManager.getInstance().getAllValues(JavaStatisticsManager.getAfterNewKey(type)); for(final StatisticsInfo statisticsInfo : stats) { final String value = statisticsInfo.getValue(); if(value.startsWith(JavaStatisticsManager.CLASS_PREFIX)) { final String qname = value.substring(JavaStatisticsManager.CLASS_PREFIX.length()); final PsiClass psiClass = JavaPsiFacade.getInstance(contextFile.getProject()).findClass(qname, contextFile.getResolveScope()); if(psiClass != null && !PsiTreeUtil.isAncestor(contextFile, psiClass, true) && !processor.process(psiClass)) { break; } } } } return true; }
private void addImport(final PsiReference ref, final PsiClass targetClass) { if(!ref.getElement().isValid() || !targetClass.isValid() || ref.resolve() == targetClass) { return; } if(!FileModificationService.getInstance().preparePsiElementForWrite(ref.getElement())) { return; } StatisticsManager.getInstance().incUseCount(JavaStatisticsManager.createInfo(null, targetClass)); CommandProcessor.getInstance().executeCommand(myProject, () -> ApplicationManager.getApplication().runWriteAction(() -> DumbService.getInstance(myProject).withAlternativeResolveEnabled(() -> _addImport(ref, targetClass))), JavaQuickFixBundle.message("add.import"), null); }
private static void sortVariableNameSuggestions(@NotNull String[] names, @NotNull final VariableKind variableKind, @Nullable final String propertyName, @Nullable final PsiType type) { if(names.length <= 1) { return; } if(LOG.isDebugEnabled()) { LOG.debug("sorting names:" + variableKind); if(propertyName != null) { LOG.debug("propertyName:" + propertyName); } if(type != null) { LOG.debug("type:" + type); } for(String name : names) { int count = JavaStatisticsManager.getVariableNameUseCount(name, variableKind, propertyName, type); LOG.debug(name + " : " + count); } } Comparator<String> comparator = new Comparator<String>() { @Override public int compare(@NotNull String s1, @NotNull String s2) { int count1 = JavaStatisticsManager.getVariableNameUseCount(s1, variableKind, propertyName, type); int count2 = JavaStatisticsManager.getVariableNameUseCount(s2, variableKind, propertyName, type); return count2 - count1; } }; Arrays.sort(names, comparator); }
@Override public StatisticsInfo serialize(final PsiElement element, final ProximityLocation location) { return element instanceof PsiMember ? JavaStatisticsManager.createInfo(null, (PsiMember)element) : null; }
@Override public StatisticsInfo serialize(final LookupElement element, final CompletionLocation location) { Object o = element.getObject(); if (o instanceof PsiLocalVariable || o instanceof PsiParameter || o instanceof PsiThisExpression || o instanceof PsiKeyword) { return StatisticsInfo.EMPTY; } PsiElement position = location.getCompletionParameters().getPosition(); if (SUPER_CALL.accepts(position) || ReferenceExpressionCompletionContributor.IN_SWITCH_LABEL.accepts(position)) { return StatisticsInfo.EMPTY; } LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY); if (item == null) return null; PsiType qualifierType = JavaCompletionUtil.getQualifierType(item); if (o instanceof PsiMember) { final ExpectedTypeInfo[] infos = JavaCompletionUtil.EXPECTED_TYPES.getValue(location); final ExpectedTypeInfo firstInfo = infos != null && infos.length > 0 ? infos[0] : null; String key2 = JavaStatisticsManager.getMemberUseKey2((PsiMember)o); if (o instanceof PsiClass) { PsiType expectedType = firstInfo != null ? firstInfo.getDefaultType() : null; return new StatisticsInfo(JavaStatisticsManager.getAfterNewKey(expectedType), key2); } PsiClass containingClass = ((PsiMember)o).getContainingClass(); if (containingClass != null) { String expectedName = firstInfo instanceof ExpectedTypeInfoImpl ? ((ExpectedTypeInfoImpl)firstInfo).getExpectedName() : null; String contextPrefix = expectedName == null ? "" : "expectedName=" + expectedName + "###"; String context = contextPrefix + JavaStatisticsManager.getMemberUseKey2(containingClass); if (o instanceof PsiMethod) { String memberValue = JavaStatisticsManager.getMemberUseKey2(RecursionWeigher.findDeepestSuper((PsiMethod)o)); List<StatisticsInfo> superMethodInfos = ContainerUtil.newArrayList(new StatisticsInfo(contextPrefix + context, memberValue)); for (PsiClass superClass : InheritanceUtil.getSuperClasses(containingClass)) { superMethodInfos.add(new StatisticsInfo(contextPrefix + JavaStatisticsManager.getMemberUseKey2(superClass), memberValue)); } return StatisticsInfo.createComposite(superMethodInfos); } return new StatisticsInfo(context, key2); } } if (qualifierType != null) return StatisticsInfo.EMPTY; return null; }
@NotNull @Override public SuggestedNameInfo suggestVariableName(@NotNull final VariableKind kind, @Nullable final String propertyName, @Nullable final PsiExpression expr, @Nullable PsiType type, final boolean correctKeywords) { LinkedHashSet<String> names = new LinkedHashSet<String>(); if (expr != null && type == null) { type = expr.getType(); } if (propertyName != null) { String[] namesByName = getSuggestionsByName(propertyName, kind, false, correctKeywords); sortVariableNameSuggestions(namesByName, kind, propertyName, null); ContainerUtil.addAll(names, namesByName); } final NamesByExprInfo namesByExpr; if (expr != null) { namesByExpr = suggestVariableNameByExpression(expr, kind, correctKeywords); if (namesByExpr.propertyName != null) { sortVariableNameSuggestions(namesByExpr.names, kind, namesByExpr.propertyName, null); } ContainerUtil.addAll(names, namesByExpr.names); } else { namesByExpr = null; } if (type != null) { String[] namesByType = suggestVariableNameByType(type, kind, correctKeywords); sortVariableNameSuggestions(namesByType, kind, null, type); ContainerUtil.addAll(names, namesByType); } final String _propertyName; if (propertyName != null) { _propertyName = propertyName; } else { _propertyName = namesByExpr != null ? namesByExpr.propertyName : null; } addNamesFromStatistics(names, kind, _propertyName, type); String[] namesArray = ArrayUtil.toStringArray(names); sortVariableNameSuggestions(namesArray, kind, _propertyName, type); final PsiType _type = type; return new SuggestedNameInfo(namesArray) { @Override public void nameChosen(String name) { if (_propertyName != null || _type != null && _type.isValid()) { JavaStatisticsManager.incVariableNameUseCount(name, kind, _propertyName, _type); } } }; }
@Override public StatisticsInfo serialize(final LookupElement element, final CompletionLocation location) { Object o = element.getObject(); if (o instanceof PsiLocalVariable || o instanceof PsiParameter || o instanceof PsiThisExpression || o instanceof PsiKeyword) { return StatisticsInfo.EMPTY; } LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY); if (item == null) return null; PsiType qualifierType = JavaCompletionUtil.getQualifierType(item); if (o instanceof PsiMember) { final ExpectedTypeInfo[] infos = JavaCompletionUtil.EXPECTED_TYPES.getValue(location); final ExpectedTypeInfo firstInfo = infos != null && infos.length > 0 ? infos[0] : null; String key2 = JavaStatisticsManager.getMemberUseKey2((PsiMember)o); if (o instanceof PsiClass) { PsiType expectedType = firstInfo != null ? firstInfo.getDefaultType() : null; return new StatisticsInfo(JavaStatisticsManager.getAfterNewKey(expectedType), key2); } PsiClass containingClass = ((PsiMember)o).getContainingClass(); if (containingClass != null) { String expectedName = firstInfo instanceof ExpectedTypeInfoImpl ? ((ExpectedTypeInfoImpl)firstInfo).expectedName.compute() : null; String contextPrefix = expectedName == null ? "" : "expectedName=" + expectedName + "###"; String context = contextPrefix + JavaStatisticsManager.getMemberUseKey2(containingClass); if (o instanceof PsiMethod) { String memberValue = JavaStatisticsManager.getMemberUseKey2(RecursionWeigher.findDeepestSuper((PsiMethod)o)); List<StatisticsInfo> superMethodInfos = ContainerUtil.newArrayList(new StatisticsInfo(contextPrefix + context, memberValue)); for (PsiClass superClass : InheritanceUtil.getSuperClasses(containingClass)) { superMethodInfos.add(new StatisticsInfo(contextPrefix + JavaStatisticsManager.getMemberUseKey2(superClass), memberValue)); } return StatisticsInfo.createComposite(superMethodInfos); } return new StatisticsInfo(context, key2); } } if (qualifierType != null) return StatisticsInfo.EMPTY; return null; }
@Override public SuggestedNameInfo suggestVariableName(@NotNull final VariableKind kind, @Nullable final String propertyName, @Nullable final PsiExpression expr, @Nullable PsiType type, final boolean correctKeywords) { LinkedHashSet<String> names = new LinkedHashSet<String>(); if (expr != null && type == null) { type = expr.getType(); } if (propertyName != null) { String[] namesByName = getSuggestionsByName(propertyName, kind, false, correctKeywords); sortVariableNameSuggestions(namesByName, kind, propertyName, null); ContainerUtil.addAll(names, namesByName); } final NamesByExprInfo namesByExpr; if (expr != null) { namesByExpr = suggestVariableNameByExpression(expr, kind, correctKeywords); if (namesByExpr.propertyName != null) { sortVariableNameSuggestions(namesByExpr.names, kind, namesByExpr.propertyName, null); } ContainerUtil.addAll(names, namesByExpr.names); } else { namesByExpr = null; } if (type != null) { String[] namesByType = suggestVariableNameByType(type, kind, correctKeywords); sortVariableNameSuggestions(namesByType, kind, null, type); ContainerUtil.addAll(names, namesByType); } final String _propertyName; if (propertyName != null) { _propertyName = propertyName; } else { _propertyName = namesByExpr != null ? namesByExpr.propertyName : null; } addNamesFromStatistics(names, kind, _propertyName, type); String[] namesArray = ArrayUtil.toStringArray(names); sortVariableNameSuggestions(namesArray, kind, _propertyName, type); final PsiType _type = type; return new SuggestedNameInfo(namesArray) { @Override public void nameChosen(String name) { if (_propertyName != null || _type != null && _type.isValid()) { JavaStatisticsManager.incVariableNameUseCount(name, kind, _propertyName, _type); } } }; }
@Override public StatisticsInfo serialize(final LookupElement element, final CompletionLocation location) { Object o = element.getObject(); if(o instanceof PsiLocalVariable || o instanceof PsiParameter || o instanceof PsiThisExpression || o instanceof PsiKeyword) { return StatisticsInfo.EMPTY; } LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY); if(item == null) { return null; } PsiType qualifierType = JavaCompletionUtil.getQualifierType(item); if(o instanceof PsiMember) { final ExpectedTypeInfo[] infos = JavaCompletionUtil.EXPECTED_TYPES.getValue(location); final ExpectedTypeInfo firstInfo = infos != null && infos.length > 0 ? infos[0] : null; String key2 = JavaStatisticsManager.getMemberUseKey2((PsiMember) o); if(o instanceof PsiClass) { PsiType expectedType = firstInfo != null ? firstInfo.getDefaultType() : null; return new StatisticsInfo(JavaStatisticsManager.getAfterNewKey(expectedType), key2); } PsiClass containingClass = ((PsiMember) o).getContainingClass(); if(containingClass != null) { String expectedName = firstInfo instanceof ExpectedTypeInfoImpl ? ((ExpectedTypeInfoImpl) firstInfo).getExpectedName() : null; String contextPrefix = expectedName == null ? "" : "expectedName=" + expectedName + "###"; String context = contextPrefix + JavaStatisticsManager.getMemberUseKey2(containingClass); if(o instanceof PsiMethod) { String memberValue = JavaStatisticsManager.getMemberUseKey2(RecursionWeigher.findDeepestSuper((PsiMethod) o)); List<StatisticsInfo> superMethodInfos = ContainerUtil.newArrayList(new StatisticsInfo(contextPrefix + context, memberValue)); for(PsiClass superClass : InheritanceUtil.getSuperClasses(containingClass)) { superMethodInfos.add(new StatisticsInfo(contextPrefix + JavaStatisticsManager.getMemberUseKey2(superClass), memberValue)); } return StatisticsInfo.createComposite(superMethodInfos); } return new StatisticsInfo(context, key2); } } if(qualifierType != null) { return StatisticsInfo.EMPTY; } return null; }
@NotNull @Override public SuggestedNameInfo suggestVariableName(@NotNull final VariableKind kind, @Nullable final String propertyName, @Nullable final PsiExpression expr, @Nullable PsiType type, final boolean correctKeywords) { LinkedHashSet<String> names = new LinkedHashSet<String>(); if(expr != null && type == null) { type = expr.getType(); } if(propertyName != null) { String[] namesByName = getSuggestionsByName(propertyName, kind, false, correctKeywords); sortVariableNameSuggestions(namesByName, kind, propertyName, null); ContainerUtil.addAll(names, namesByName); } final NamesByExprInfo namesByExpr; if(expr != null) { namesByExpr = suggestVariableNameByExpression(expr, kind, correctKeywords); if(namesByExpr.propertyName != null) { sortVariableNameSuggestions(namesByExpr.names, kind, namesByExpr.propertyName, null); } ContainerUtil.addAll(names, namesByExpr.names); } else { namesByExpr = null; } if(type != null) { String[] namesByType = suggestVariableNameByType(type, kind, correctKeywords); sortVariableNameSuggestions(namesByType, kind, null, type); ContainerUtil.addAll(names, namesByType); } final String _propertyName; if(propertyName != null) { _propertyName = propertyName; } else { _propertyName = namesByExpr != null ? namesByExpr.propertyName : null; } addNamesFromStatistics(names, kind, _propertyName, type); String[] namesArray = ArrayUtil.toStringArray(names); sortVariableNameSuggestions(namesArray, kind, _propertyName, type); final PsiType _type = type; return new SuggestedNameInfo(namesArray) { @Override public void nameChosen(String name) { if(_propertyName != null || _type != null && _type.isValid()) { JavaStatisticsManager.incVariableNameUseCount(name, kind, _propertyName, _type); } } }; }