use of com.intellij.util.Function in project kotlin by JetBrains.
the class ResolveSession method getTopLevelClassifierDescriptors.
@Override
@NotNull
@ReadOnly
public Collection<ClassifierDescriptor> getTopLevelClassifierDescriptors(@NotNull FqName fqName, @NotNull final LookupLocation location) {
if (fqName.isRoot())
return Collections.emptyList();
PackageMemberDeclarationProvider provider = declarationProviderFactory.getPackageMemberDeclarationProvider(fqName.parent());
if (provider == null)
return Collections.emptyList();
Collection<ClassifierDescriptor> result = new SmartList<ClassifierDescriptor>();
result.addAll(ContainerUtil.mapNotNull(provider.getClassOrObjectDeclarations(fqName.shortName()), new Function<KtClassLikeInfo, ClassifierDescriptor>() {
@Override
public ClassDescriptor fun(KtClassLikeInfo classLikeInfo) {
if (classLikeInfo instanceof KtClassOrObjectInfo) {
//noinspection RedundantCast
return getClassDescriptor(((KtClassOrObjectInfo) classLikeInfo).getCorrespondingClassOrObject(), location);
} else if (classLikeInfo instanceof KtScriptInfo) {
return getScriptDescriptor(((KtScriptInfo) classLikeInfo).getScript());
} else {
throw new IllegalStateException("Unexpected " + classLikeInfo + " of type " + classLikeInfo.getClass().getName());
}
}
}));
result.addAll(ContainerUtil.map(provider.getTypeAliasDeclarations(fqName.shortName()), new Function<KtTypeAlias, ClassifierDescriptor>() {
@Override
public ClassifierDescriptor fun(KtTypeAlias alias) {
return (ClassifierDescriptor) lazyDeclarationResolver.resolveToDescriptor(alias);
}
}));
return result;
}
use of com.intellij.util.Function in project intellij-community by JetBrains.
the class PyExtractSuperclassHelper method extractSuperclass.
static void extractSuperclass(final PyClass clazz, @NotNull Collection<PyMemberInfo<PyElement>> selectedMemberInfos, final String superBaseName, final String targetFile) {
final Project project = clazz.getProject();
//We will need to change it probably while param may be read-only
//noinspection AssignmentToMethodParameter
selectedMemberInfos = new ArrayList<>(selectedMemberInfos);
final RefactoringEventData beforeData = new RefactoringEventData();
beforeData.addElements(JBIterable.from(selectedMemberInfos).transform((Function<PyMemberInfo<PyElement>, PsiElement>) info -> info.getMember()).toList());
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(getRefactoringId(), beforeData);
// PY-12171
final PyMemberInfo<PyElement> objectMember = MembersManager.findMember(selectedMemberInfos, ALLOW_OBJECT);
if (LanguageLevel.forElement(clazz).isPy3K() && !isObjectParentDeclaredExplicitly(clazz)) {
// Remove object from list if Py3
if (objectMember != null) {
selectedMemberInfos.remove(objectMember);
}
} else {
// Always add object if < Py3
if (objectMember == null) {
final PyMemberInfo<PyElement> object = MembersManager.findMember(clazz, ALLOW_OBJECT);
if (object != null) {
selectedMemberInfos.add(object);
}
}
}
final String text = "class " + superBaseName + ":\n pass" + "\n";
PyClass newClass = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyClass.class, text);
newClass = placeNewClass(project, newClass, clazz, targetFile);
MembersManager.moveAllMembers(selectedMemberInfos, clazz, newClass);
if (!newClass.getContainingFile().equals(clazz.getContainingFile())) {
// To remove unneeded imports only if user used different file
PyClassRefactoringUtil.optimizeImports(clazz.getContainingFile());
}
PyClassRefactoringUtil.addSuperclasses(project, clazz, null, newClass);
final RefactoringEventData afterData = new RefactoringEventData();
afterData.addElement(newClass);
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(getRefactoringId(), afterData);
}
use of com.intellij.util.Function in project intellij-community by JetBrains.
the class PyChangeSignatureTest method testAnnotationsForStarredParametersAreNotShownInDialog.
// PY-14774
public void testAnnotationsForStarredParametersAreNotShownInDialog() throws Exception {
runWithLanguageLevel(LanguageLevel.PYTHON30, () -> {
myFixture.configureByText(PythonFileType.INSTANCE, "def func(a, b:int, *args: tuple, c:list, d:str='foo', ** kwargs:dict):\n" + " pass");
final PyFunction function = (PyFunction) new PyChangeSignatureHandler().findTargetMember(myFixture.getFile(), myFixture.getEditor());
assertNotNull(function);
final List<String> expected = Arrays.asList("a", "b", "*args", "c", "d", "**kwargs");
final List<PyParameterInfo> parameters = new PyMethodDescriptor(function).getParameters();
assertEquals(expected, ContainerUtil.map(parameters, info -> info.getOldName()));
});
}
use of com.intellij.util.Function in project kotlin by JetBrains.
the class KtParsingTestCase method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
initApplication();
ComponentAdapter component = getApplication().getPicoContainer().getComponentAdapter(ProgressManager.class.getName());
Extensions.registerAreaClass("IDEA_PROJECT", null);
myProject = new MockProjectEx(getTestRootDisposable());
myPsiManager = new MockPsiManager(myProject);
myFileFactory = new PsiFileFactoryImpl(myPsiManager);
MutablePicoContainer appContainer = getApplication().getPicoContainer();
registerComponentInstance(appContainer, MessageBus.class, MessageBusFactory.newMessageBus(getApplication()));
registerComponentInstance(appContainer, SchemesManagerFactory.class, new MockSchemesManagerFactory());
final MockEditorFactory editorFactory = new MockEditorFactory();
registerComponentInstance(appContainer, EditorFactory.class, editorFactory);
registerComponentInstance(appContainer, FileDocumentManager.class, new MockFileDocumentManagerImpl(new Function<CharSequence, Document>() {
@Override
public Document fun(CharSequence charSequence) {
return editorFactory.createDocument(charSequence);
}
}, HARD_REF_TO_DOCUMENT_KEY));
registerComponentInstance(appContainer, PsiDocumentManager.class, new MockPsiDocumentManager());
registerApplicationService(PsiBuilderFactory.class, new PsiBuilderFactoryImpl());
registerApplicationService(DefaultASTFactory.class, new CoreASTFactory());
registerApplicationService(ReferenceProvidersRegistry.class, new ReferenceProvidersRegistryImpl());
registerApplicationService(ProgressManager.class, new CoreProgressManager());
myProject.registerService(CachedValuesManager.class, new CachedValuesManagerImpl(myProject, new PsiCachedValuesFactory(myPsiManager)));
myProject.registerService(PsiManager.class, myPsiManager);
this.registerExtensionPoint(FileTypeFactory.FILE_TYPE_FACTORY_EP, FileTypeFactory.class);
for (ParserDefinition definition : myDefinitions) {
addExplicitExtension(LanguageParserDefinitions.INSTANCE, definition.getFileNodeType().getLanguage(), definition);
}
if (myDefinitions.length > 0) {
configureFromParserDefinition(myDefinitions[0], myFileExt);
}
// That's for reparse routines
final PomModelImpl pomModel = new PomModelImpl(myProject);
myProject.registerService(PomModel.class, pomModel);
new TreeAspect(pomModel);
}
use of com.intellij.util.Function in project kotlin by JetBrains.
the class MemberMatching method typeParametersMatch.
static boolean typeParametersMatch(@NotNull KtTypeParameterListOwner typeParameterListOwner, @NotNull List<TypeParameterDescriptor> typeParameterDescriptors) {
List<KtTypeParameter> decompiledParameters = typeParameterListOwner.getTypeParameters();
if (decompiledParameters.size() != typeParameterDescriptors.size()) {
return false;
}
Multimap<Name, String> decompiledParameterToBounds = HashMultimap.create();
for (KtTypeParameter parameter : decompiledParameters) {
KtTypeReference extendsBound = parameter.getExtendsBound();
if (extendsBound != null) {
decompiledParameterToBounds.put(parameter.getNameAsName(), extendsBound.getText());
}
}
for (KtTypeConstraint typeConstraint : typeParameterListOwner.getTypeConstraints()) {
KtSimpleNameExpression typeParameterName = typeConstraint.getSubjectTypeParameterName();
assert typeParameterName != null;
KtTypeReference bound = typeConstraint.getBoundTypeReference();
assert bound != null;
decompiledParameterToBounds.put(typeParameterName.getReferencedNameAsName(), bound.getText());
}
for (int i = 0; i < decompiledParameters.size(); i++) {
KtTypeParameter decompiledParameter = decompiledParameters.get(i);
TypeParameterDescriptor descriptor = typeParameterDescriptors.get(i);
Name name = decompiledParameter.getNameAsName();
assert name != null;
if (!name.equals(descriptor.getName())) {
return false;
}
Set<String> descriptorUpperBounds = Sets.newHashSet(ContainerUtil.map(descriptor.getUpperBounds(), new Function<KotlinType, String>() {
@Override
public String fun(KotlinType type) {
return DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(type);
}
}));
KotlinBuiltIns builtIns = DescriptorUtilsKt.getBuiltIns(descriptor);
Set<String> decompiledUpperBounds = decompiledParameterToBounds.get(descriptor.getName()).isEmpty() ? Sets.newHashSet(DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(builtIns.getDefaultBound())) : Sets.newHashSet(decompiledParameterToBounds.get(descriptor.getName()));
if (!descriptorUpperBounds.equals(decompiledUpperBounds)) {
return false;
}
}
return true;
}
Aggregations