Search in sources :

Example 26 with TIntArrayList

use of gnu.trove.TIntArrayList in project intellij-community by JetBrains.

the class ByLine method getBigLines.

@NotNull
private static Pair<List<Line>, TIntArrayList> getBigLines(@NotNull List<Line> lines, int threshold) {
    List<Line> bigLines = new ArrayList<>(lines.size());
    TIntArrayList indexes = new TIntArrayList(lines.size());
    for (int i = 0; i < lines.size(); i++) {
        Line line = lines.get(i);
        if (line.getNonSpaceChars() > threshold) {
            bigLines.add(line);
            indexes.add(i);
        }
    }
    return Pair.create(bigLines, indexes);
}
Also used : ArrayList(java.util.ArrayList) TIntArrayList(gnu.trove.TIntArrayList) TIntArrayList(gnu.trove.TIntArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with TIntArrayList

use of gnu.trove.TIntArrayList in project intellij-community by JetBrains.

the class AddOnDemandStaticImportAction method invoke.

public static void invoke(final Project project, PsiFile file, final Editor editor, PsiElement element) {
    final PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement) element.getParent();
    final PsiClass aClass = (PsiClass) refExpr.resolve();
    if (aClass == null) {
        return;
    }
    final PsiClass containingClass = PsiUtil.getTopLevelClass(refExpr);
    if (aClass != containingClass) {
        PsiImportList importList = ((PsiJavaFile) file).getImportList();
        if (importList == null) {
            return;
        }
        boolean alreadyImported = false;
        for (PsiImportStaticStatement statement : importList.getImportStaticStatements()) {
            if (!statement.isOnDemand())
                continue;
            PsiClass staticResolve = statement.resolveTargetClass();
            if (aClass == staticResolve) {
                alreadyImported = true;
                break;
            }
        }
        if (!alreadyImported) {
            PsiImportStaticStatement importStaticStatement = JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createImportStaticStatement(aClass, "*");
            importList.add(importStaticStatement);
        }
    }
    List<PsiFile> roots = file.getViewProvider().getAllFiles();
    for (final PsiFile root : roots) {
        PsiElement copy = root.copy();
        final PsiManager manager = root.getManager();
        final TIntArrayList expressionToDequalifyOffsets = new TIntArrayList();
        copy.accept(new JavaRecursiveElementWalkingVisitor() {

            int delta;

            @Override
            public void visitReferenceElement(PsiJavaCodeReferenceElement expression) {
                if (isParameterizedReference(expression))
                    return;
                PsiElement qualifierExpression = expression.getQualifier();
                if (qualifierExpression instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement) qualifierExpression).isReferenceTo(aClass)) {
                    try {
                        PsiElement resolved = expression.resolve();
                        int end = expression.getTextRange().getEndOffset();
                        qualifierExpression.delete();
                        delta += end - expression.getTextRange().getEndOffset();
                        PsiElement after = expression.resolve();
                        if (manager.areElementsEquivalent(after, resolved)) {
                            expressionToDequalifyOffsets.add(expression.getTextRange().getStartOffset() + delta);
                        }
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                    }
                }
                super.visitElement(expression);
            }
        });
        expressionToDequalifyOffsets.forEachDescending(new TIntProcedure() {

            @Override
            public boolean execute(int offset) {
                PsiJavaCodeReferenceElement expression = PsiTreeUtil.findElementOfClassAtOffset(root, offset, PsiJavaCodeReferenceElement.class, false);
                if (expression == null) {
                    return false;
                }
                PsiElement qualifierExpression = expression.getQualifier();
                if (qualifierExpression instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement) qualifierExpression).isReferenceTo(aClass)) {
                    qualifierExpression.delete();
                    if (editor != null) {
                        HighlightManager.getInstance(project).addRangeHighlight(editor, expression.getTextRange().getStartOffset(), expression.getTextRange().getEndOffset(), EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES), false, null);
                    }
                }
                return true;
            }
        });
    }
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) TIntArrayList(gnu.trove.TIntArrayList) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 28 with TIntArrayList

use of gnu.trove.TIntArrayList in project intellij-community by JetBrains.

the class ControlFlowAnalyzer method getEmptyIntArray.

@NotNull
private TIntArrayList getEmptyIntArray() {
    if (intArrayPool.isEmpty()) {
        return new TIntArrayList(1);
    }
    TIntArrayList list = intArrayPool.pop();
    list.clear();
    return list;
}
Also used : TIntArrayList(gnu.trove.TIntArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with TIntArrayList

use of gnu.trove.TIntArrayList in project intellij-community by JetBrains.

the class Util method findParametersToRemove.

// returns parameters that are used solely in specified expression
@NotNull
public static TIntArrayList findParametersToRemove(@NotNull PsiMethod method, @NotNull final PsiExpression expr, @Nullable final PsiExpression[] occurences) {
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    if (parameters.length == 0)
        return new TIntArrayList();
    PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    final PsiMethod[] allMethods = ArrayUtil.append(overridingMethods, method);
    final TIntHashSet suspects = new TIntHashSet();
    expr.accept(new JavaRecursiveElementWalkingVisitor() {

        @Override
        public void visitReferenceExpression(final PsiReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            PsiElement resolved = expression.resolve();
            if (resolved instanceof PsiParameter) {
                int i = ArrayUtil.find(parameters, resolved);
                if (i != -1) {
                    suspects.add(i);
                }
            }
        }
    });
    final TIntIterator iterator = suspects.iterator();
    while (iterator.hasNext()) {
        final int paramNum = iterator.next();
        for (PsiMethod psiMethod : allMethods) {
            PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
            if (paramNum >= psiParameters.length)
                continue;
            PsiParameter parameter = psiParameters[paramNum];
            if (!ReferencesSearch.search(parameter, parameter.getResolveScope(), false).forEach(reference -> {
                PsiElement element = reference.getElement();
                boolean stillCanBeRemoved = false;
                if (element != null) {
                    stillCanBeRemoved = isAncestor(expr, element, false) || PsiUtil.isInsideJavadocComment(getPhysical(element));
                    if (!stillCanBeRemoved && occurences != null) {
                        for (PsiExpression occurence : occurences) {
                            if (isAncestor(occurence, element, false)) {
                                stillCanBeRemoved = true;
                                break;
                            }
                        }
                    }
                }
                if (!stillCanBeRemoved) {
                    iterator.remove();
                    return false;
                }
                return true;
            }))
                break;
        }
    }
    return new TIntArrayList(suspects.toArray());
}
Also used : ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) TIntHashSet(gnu.trove.TIntHashSet) ArrayUtil(com.intellij.util.ArrayUtil) TIntIterator(gnu.trove.TIntIterator) OverridingMethodsSearch(com.intellij.psi.search.searches.OverridingMethodsSearch) ElementToWorkOn(com.intellij.refactoring.introduceField.ElementToWorkOn) UsageInfo(com.intellij.usageView.UsageInfo) GenerateMembersUtil(com.intellij.codeInsight.generation.GenerateMembersUtil) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) List(java.util.List) Processor(com.intellij.util.Processor) PsiUtil(com.intellij.psi.util.PsiUtil) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) TIntArrayList(gnu.trove.TIntArrayList) TIntIterator(gnu.trove.TIntIterator) TIntArrayList(gnu.trove.TIntArrayList) TIntHashSet(gnu.trove.TIntHashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with TIntArrayList

use of gnu.trove.TIntArrayList in project intellij-community by JetBrains.

the class InplaceIntroduceParameterPopup method performIntroduce.

protected void performIntroduce() {
    boolean isDeleteLocalVariable = false;
    PsiExpression parameterInitializer = myExpr;
    if (getLocalVariable() != null) {
        if (myPanel.isUseInitializer()) {
            parameterInitializer = getLocalVariable().getInitializer();
        }
        isDeleteLocalVariable = myPanel.isDeleteLocalVariable();
    }
    final TIntArrayList parametersToRemove = myPanel.getParametersToRemove();
    final IntroduceParameterProcessor processor = new IntroduceParameterProcessor(myProject, myMethod, myMethodToSearchFor, parameterInitializer, myExpr, (PsiLocalVariable) getLocalVariable(), isDeleteLocalVariable, getInputName(), myPanel.isReplaceAllOccurences(), myPanel.getReplaceFieldsWithGetters(), myMustBeFinal || myPanel.isGenerateFinal(), isGenerateDelegate(), getType(), parametersToRemove);
    final Runnable runnable = () -> {
        final Runnable performRefactoring = () -> {
            processor.setPrepareSuccessfulSwingThreadCallback(() -> {
            });
            processor.run();
            normalizeParameterIdxAccordingToRemovedParams(parametersToRemove);
            final PsiParameter parameter = getParameter();
            if (parameter != null) {
                super.saveSettings(parameter);
            }
        };
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            performRefactoring.run();
        } else {
            TransactionGuard.getInstance().submitTransactionLater(myProject, performRefactoring);
        }
    };
    CommandProcessor.getInstance().executeCommand(myProject, runnable, getCommandName(), null);
}
Also used : TIntArrayList(gnu.trove.TIntArrayList)

Aggregations

TIntArrayList (gnu.trove.TIntArrayList)104 NotNull (org.jetbrains.annotations.NotNull)34 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Nullable (org.jetbrains.annotations.Nullable)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ArrangementMatchingRulesControl (com.intellij.application.options.codeStyle.arrangement.match.ArrangementMatchingRulesControl)3 StringSearcher (com.intellij.util.text.StringSearcher)3 TIntHashSet (gnu.trove.TIntHashSet)3 TIntProcedure (gnu.trove.TIntProcedure)3 IOException (java.io.IOException)3 IDevice (com.android.ddmlib.IDevice)2 ArrangementMatchingRulesModel (com.intellij.application.options.codeStyle.arrangement.match.ArrangementMatchingRulesModel)2 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 ElementToWorkOn (com.intellij.refactoring.introduceField.ElementToWorkOn)2 IntroduceParameterProcessor (com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor)2 RelativePoint (com.intellij.ui.awt.RelativePoint)2 RadComponent (com.intellij.uiDesigner.radComponents.RadComponent)2