Search in sources :

Example 6 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class ErrorAnalyzer method analyzeErrors.

/**
   * @param editor if null, no quick fixes are created. This is used in form to source compiler.
   */
public static void analyzeErrors(@NotNull final Module module, @NotNull final VirtualFile formFile, @Nullable final GuiEditor editor, @NotNull final IRootContainer rootContainer, @Nullable final ProgressIndicator progress) {
    if (module.isDisposed()) {
        return;
    }
    // 1. Validate class to bind
    final String classToBind = rootContainer.getClassToBind();
    final PsiClass psiClass;
    if (classToBind != null) {
        psiClass = FormEditingUtil.findClassToBind(module, classToBind);
        if (psiClass == null) {
            final QuickFix[] fixes = editor != null ? new QuickFix[] { new CreateClassToBindFix(editor, classToBind) } : QuickFix.EMPTY_ARRAY;
            final ErrorInfo errorInfo = new ErrorInfo(null, null, UIDesignerBundle.message("error.class.does.not.exist", classToBind), HighlightDisplayLevel.ERROR, fixes);
            rootContainer.putClientProperty(CLIENT_PROP_CLASS_TO_BIND_ERROR, errorInfo);
        } else {
            rootContainer.putClientProperty(CLIENT_PROP_CLASS_TO_BIND_ERROR, null);
        }
    } else {
        rootContainer.putClientProperty(CLIENT_PROP_CLASS_TO_BIND_ERROR, null);
        psiClass = null;
    }
    // 2. Validate bindings to fields
    // field name -> error message
    // for performance reasons
    final ArrayList<String> usedBindings = new ArrayList<>();
    final Set<IButtonGroup> processedGroups = new HashSet<>();
    FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<IComponent>() {

        public boolean visit(final IComponent component) {
            if (progress != null && progress.isCanceled())
                return false;
            // Reset previous error (if any)
            component.putClientProperty(CLIENT_PROP_BINDING_ERROR, null);
            final String binding = component.getBinding();
            // a. Check that field exists and field is not static
            if (psiClass != null && binding != null) {
                if (validateFieldInClass(component, binding, component.getComponentClassName(), psiClass, editor, module))
                    return true;
            }
            // b. Check that binding is unique
            if (binding != null) {
                if (usedBindings.contains(binding)) {
                    // TODO[vova] implement
                    component.putClientProperty(CLIENT_PROP_BINDING_ERROR, new ErrorInfo(component, null, UIDesignerBundle.message("error.binding.already.exists", binding), HighlightDisplayLevel.ERROR, QuickFix.EMPTY_ARRAY));
                    return true;
                }
                usedBindings.add(binding);
            }
            IButtonGroup group = FormEditingUtil.findGroupForComponent(rootContainer, component);
            if (group != null && !processedGroups.contains(group)) {
                processedGroups.add(group);
                if (group.isBound()) {
                    validateFieldInClass(component, group.getName(), ButtonGroup.class.getName(), psiClass, editor, module);
                }
            }
            return true;
        }
    });
    if (progress != null)
        progress.checkCanceled();
    // Check that there are no panels in XY with children
    FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<IComponent>() {

        public boolean visit(final IComponent component) {
            if (progress != null && progress.isCanceled())
                return false;
            // Clear previous error (if any)
            component.putClientProperty(CLIENT_PROP_ERROR_ARRAY, null);
            if (!(component instanceof IContainer)) {
                return true;
            }
            final IContainer container = (IContainer) component;
            if (container instanceof IRootContainer) {
                final IRootContainer rootContainer = (IRootContainer) container;
                if (rootContainer.getComponentCount() > 1) {
                    // TODO[vova] implement
                    putError(component, new ErrorInfo(component, null, UIDesignerBundle.message("error.multiple.toplevel.components"), HighlightDisplayLevel.ERROR, QuickFix.EMPTY_ARRAY));
                }
            } else if (container.isXY() && container.getComponentCount() > 0) {
                // TODO[vova] implement
                putError(component, new ErrorInfo(component, null, UIDesignerBundle.message("error.panel.not.laid.out"), HighlightDisplayLevel.ERROR, QuickFix.EMPTY_ARRAY));
            }
            return true;
        }
    });
    if (progress != null)
        progress.checkCanceled();
    try {
        // Run inspections for form elements
        final PsiFile formPsiFile = PsiManager.getInstance(module.getProject()).findFile(formFile);
        if (formPsiFile != null && rootContainer instanceof RadRootContainer) {
            final List<FormInspectionTool> formInspectionTools = new ArrayList<>();
            final FormInspectionTool[] registeredFormInspections = Extensions.getExtensions(FormInspectionTool.EP_NAME);
            for (FormInspectionTool formInspectionTool : registeredFormInspections) {
                if (formInspectionTool.isActive(formPsiFile) && !rootContainer.isInspectionSuppressed(formInspectionTool.getShortName(), null)) {
                    formInspectionTools.add(formInspectionTool);
                }
            }
            if (formInspectionTools.size() > 0 && editor != null) {
                for (FormInspectionTool tool : formInspectionTools) {
                    tool.startCheckForm(rootContainer);
                }
                FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<RadComponent>() {

                    public boolean visit(final RadComponent component) {
                        if (progress != null && progress.isCanceled())
                            return false;
                        for (FormInspectionTool tool : formInspectionTools) {
                            if (rootContainer.isInspectionSuppressed(tool.getShortName(), component.getId()))
                                continue;
                            ErrorInfo[] errorInfos = tool.checkComponent(editor, component);
                            if (errorInfos != null) {
                                ArrayList<ErrorInfo> errorList = getErrorInfos(component);
                                if (errorList == null) {
                                    errorList = new ArrayList<>();
                                    component.putClientProperty(CLIENT_PROP_ERROR_ARRAY, errorList);
                                }
                                Collections.addAll(errorList, errorInfos);
                            }
                        }
                        return true;
                    }
                });
                for (FormInspectionTool tool : formInspectionTools) {
                    tool.doneCheckForm(rootContainer);
                }
            }
        }
    } catch (Exception e) {
        LOG.error(e);
    }
}
Also used : IButtonGroup(com.intellij.uiDesigner.lw.IButtonGroup) IComponent(com.intellij.uiDesigner.lw.IComponent) ArrayList(java.util.ArrayList) RadRootContainer(com.intellij.uiDesigner.radComponents.RadRootContainer) RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) FormInspectionTool(com.intellij.uiDesigner.inspections.FormInspectionTool) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IRootContainer(com.intellij.uiDesigner.lw.IRootContainer) IContainer(com.intellij.uiDesigner.lw.IContainer) HashSet(com.intellij.util.containers.HashSet)

Example 7 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class FormEditingUtil method deleteComponents.

public static void deleteComponents(final Collection<? extends RadComponent> selection, boolean deleteEmptyCells) {
    if (selection.size() == 0) {
        return;
    }
    final RadRootContainer rootContainer = (RadRootContainer) getRoot(selection.iterator().next());
    final Set<String> deletedComponentIds = new HashSet<>();
    for (final RadComponent component : selection) {
        boolean wasSelected = component.isSelected();
        final RadContainer parent = component.getParent();
        boolean wasPackedHorz = false;
        boolean wasPackedVert = false;
        if (parent.getParent() != null && parent.getParent().isXY()) {
            final Dimension minSize = parent.getMinimumSize();
            wasPackedHorz = parent.getWidth() == minSize.width;
            wasPackedVert = parent.getHeight() == minSize.height;
        }
        iterate(component, new ComponentVisitor() {

            public boolean visit(final IComponent c) {
                RadComponent rc = (RadComponent) c;
                BindingProperty.checkRemoveUnusedField(rootContainer, rc.getBinding(), null);
                deletedComponentIds.add(rc.getId());
                return true;
            }
        });
        GridConstraints delConstraints = parent.getLayoutManager().isGrid() ? component.getConstraints() : null;
        int index = parent.indexOfComponent(component);
        parent.removeComponent(component);
        if (wasSelected) {
            if (parent.getComponentCount() > index) {
                parent.getComponent(index).setSelected(true);
            } else if (index > 0 && parent.getComponentCount() == index) {
                parent.getComponent(index - 1).setSelected(true);
            } else {
                parent.setSelected(true);
            }
        }
        if (delConstraints != null && deleteEmptyCells) {
            deleteEmptyGridCells(parent, delConstraints);
        }
        if (wasPackedHorz || wasPackedVert) {
            final Dimension minSize = parent.getMinimumSize();
            Dimension newSize = new Dimension(parent.getWidth(), parent.getHeight());
            if (wasPackedHorz) {
                newSize.width = minSize.width;
            }
            if (wasPackedVert) {
                newSize.height = minSize.height;
            }
            parent.setSize(newSize);
        }
    }
    iterate(rootContainer, new ComponentVisitor() {

        public boolean visit(final IComponent component) {
            RadComponent rc = (RadComponent) component;
            for (IProperty p : component.getModifiedProperties()) {
                if (p instanceof IntroComponentProperty) {
                    IntroComponentProperty icp = (IntroComponentProperty) p;
                    final String value = icp.getValue(rc);
                    if (deletedComponentIds.contains(value)) {
                        try {
                            icp.resetValue(rc);
                        } catch (Exception e) {
                        // ignore
                        }
                    }
                }
            }
            return true;
        }
    });
}
Also used : RadRootContainer(com.intellij.uiDesigner.radComponents.RadRootContainer) RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) RelativePoint(com.intellij.ui.awt.RelativePoint) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IntroComponentProperty(com.intellij.uiDesigner.propertyInspector.properties.IntroComponentProperty) GridConstraints(com.intellij.uiDesigner.core.GridConstraints) RadContainer(com.intellij.uiDesigner.radComponents.RadContainer) HashSet(com.intellij.util.containers.HashSet)

Example 8 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class FormEditingUtil method collectUsedLocales.

public static Locale[] collectUsedLocales(final Module module, final IRootContainer rootContainer) {
    final Set<Locale> locales = new HashSet<>();
    final PropertiesReferenceManager propManager = PropertiesReferenceManager.getInstance(module.getProject());
    for (String bundleName : collectUsedBundleNames(rootContainer)) {
        List<PropertiesFile> propFiles = propManager.findPropertiesFiles(module, bundleName.replace('/', '.'));
        for (PropertiesFile propFile : propFiles) {
            locales.add(propFile.getLocale());
        }
    }
    return locales.toArray(new Locale[locales.size()]);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) HashSet(com.intellij.util.containers.HashSet)

Example 9 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class ConvertClosureToMethodIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrField field;
    if (element.getParent() instanceof GrField) {
        field = (GrField) element.getParent();
    } else {
        final PsiReference ref = element.getReference();
        LOG.assertTrue(ref != null);
        PsiElement resolved = ref.resolve();
        if (resolved instanceof GrAccessorMethod) {
            resolved = ((GrAccessorMethod) resolved).getProperty();
        }
        LOG.assertTrue(resolved instanceof GrField);
        field = (GrField) resolved;
    }
    final HashSet<PsiReference> usages = new HashSet<>();
    usages.addAll(ReferencesSearch.search(field).findAll());
    final GrAccessorMethod[] getters = field.getGetters();
    for (GrAccessorMethod getter : getters) {
        usages.addAll(MethodReferencesSearch.search(getter).findAll());
    }
    final GrAccessorMethod setter = field.getSetter();
    if (setter != null) {
        usages.addAll(MethodReferencesSearch.search(setter).findAll());
    }
    final String fieldName = field.getName();
    LOG.assertTrue(fieldName != null);
    final Collection<PsiElement> fieldUsages = new HashSet<>();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    for (PsiReference usage : usages) {
        final PsiElement psiElement = usage.getElement();
        if (PsiUtil.isMethodUsage(psiElement))
            continue;
        if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
            conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
        } else {
            if (psiElement instanceof GrReferenceExpression) {
                fieldUsages.add(psiElement);
                if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
                    conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
                }
            } else if (psiElement instanceof GrArgumentLabel) {
                conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
            }
        }
    }
    final PsiClass containingClass = field.getContainingClass();
    final GrExpression initializer = field.getInitializerGroovy();
    LOG.assertTrue(initializer != null);
    final PsiType type = initializer.getType();
    LOG.assertTrue(type instanceof GrClosureType);
    final GrSignature signature = ((GrClosureType) type).getSignature();
    final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
    for (MethodSignature s : signatures) {
        final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
        if (method != null) {
            conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
        }
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
        conflictsDialog.show();
        if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
    }
    execute(field, fieldUsages);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) MethodSignature(com.intellij.psi.util.MethodSignature) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) HashSet(com.intellij.util.containers.HashSet) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Example 10 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class DissociateResourceBundleAction method extractResourceBundles.

@NotNull
private static Collection<ResourceBundle> extractResourceBundles(final AnActionEvent event) {
    final Set<ResourceBundle> targetResourceBundles = new HashSet<>();
    final ResourceBundle[] chosenResourceBundles = event.getData(ResourceBundle.ARRAY_DATA_KEY);
    if (chosenResourceBundles != null) {
        for (ResourceBundle resourceBundle : chosenResourceBundles) {
            if (resourceBundle.getPropertiesFiles().size() > 1) {
                targetResourceBundles.add(resourceBundle);
            }
        }
    }
    final PsiElement[] psiElements = event.getData(LangDataKeys.PSI_ELEMENT_ARRAY);
    if (psiElements != null) {
        for (PsiElement element : psiElements) {
            final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(element);
            if (propertiesFile != null) {
                final ResourceBundle bundle = propertiesFile.getResourceBundle();
                if (bundle.getPropertiesFiles().size() > 1) {
                    targetResourceBundles.add(bundle);
                }
            }
        }
    }
    return targetResourceBundles;
}
Also used : ResourceBundle(com.intellij.lang.properties.ResourceBundle) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

HashSet (com.intellij.util.containers.HashSet)162 NotNull (org.jetbrains.annotations.NotNull)50 VirtualFile (com.intellij.openapi.vfs.VirtualFile)31 File (java.io.File)22 PsiElement (com.intellij.psi.PsiElement)20 Module (com.intellij.openapi.module.Module)19 ArrayList (java.util.ArrayList)18 Project (com.intellij.openapi.project.Project)17 THashSet (gnu.trove.THashSet)15 Nullable (org.jetbrains.annotations.Nullable)14 HashMap (com.intellij.util.containers.HashMap)13 IOException (java.io.IOException)13 PsiFile (com.intellij.psi.PsiFile)12 UsageInfo (com.intellij.usageView.UsageInfo)12 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 MultiMap (com.intellij.util.containers.MultiMap)11 Pair (com.intellij.openapi.util.Pair)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)6 Document (com.intellij.openapi.editor.Document)5