Search in sources :

Example 41 with JavaCodeStyleManager

use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.

the class OptimizeImportsTask method performOperation.

@Override
public void performOperation(final Project project, final Set<PsiJavaFile> javaFiles) {
    CodeStyleManager.getInstance(project).performActionWithFormatterDisabled(new Runnable() {

        @Override
        public void run() {
            PsiDocumentManager.getInstance(project).commitAllDocuments();
        }
    });
    final List<SmartPsiElementPointer<PsiImportStatementBase>> redundants = new ArrayList<>();
    final Runnable findRedundantImports = () -> ReadAction.run(() -> {
        final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
        final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
        final SmartPointerManager pointerManager = SmartPointerManager.getInstance(project);
        int i = 0;
        final int fileCount = javaFiles.size();
        for (PsiJavaFile file : javaFiles) {
            if (file.isValid()) {
                final VirtualFile virtualFile = file.getVirtualFile();
                if (virtualFile != null) {
                    if (progressIndicator != null) {
                        progressIndicator.setText2(virtualFile.getPresentableUrl());
                        progressIndicator.setFraction((double) i++ / fileCount);
                    }
                    final Collection<PsiImportStatementBase> perFile = styleManager.findRedundantImports(file);
                    if (perFile != null) {
                        for (PsiImportStatementBase redundant : perFile) {
                            redundants.add(pointerManager.createSmartPsiElementPointer(redundant));
                        }
                    }
                }
            }
        }
    });
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(findRedundantImports, REMOVING_REDUNDANT_IMPORTS_TITLE, false, project))
        return;
    ApplicationManager.getApplication().runWriteAction(() -> {
        final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, REMOVING_REDUNDANT_IMPORTS_TITLE, false);
        progressTask.setMinIterationTime(200);
        progressTask.setTask(new OptimizeImportsTask(progressTask, redundants));
        ProgressManager.getInstance().run(progressTask);
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SequentialModalProgressTask(com.intellij.util.SequentialModalProgressTask) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 42 with JavaCodeStyleManager

use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.

the class JavaReplaceHandler method postProcess.

@Override
public void postProcess(PsiElement affectedElement, ReplaceOptions options) {
    if (!affectedElement.isValid()) {
        return;
    }
    if (options.isToUseStaticImport()) {
        shortenWithStaticImports(affectedElement, 0, affectedElement.getTextLength());
    }
    if (options.isToShortenFQN()) {
        final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(affectedElement.getProject());
        codeStyleManager.shortenClassReferences(affectedElement, 0, affectedElement.getTextLength());
    }
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 43 with JavaCodeStyleManager

use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.

the class ClassMappingNameConverter method getVariants.

@NotNull
@Override
public Collection<String> getVariants(ConvertContext context) {
    DomElement parent = context.getInvocationElement().getParent();
    assert parent != null;
    List<DomElement> children = DomUtil.getDefinedChildren(parent, true, true);
    DomElement classElement = ContainerUtil.find(children, domElement -> domElement.getAnnotation(MappingClass.class) != null);
    if (classElement == null)
        return Collections.emptyList();
    Object value = ((GenericDomValue) classElement).getValue();
    if (value == null)
        return Collections.emptyList();
    PsiType type;
    if (value instanceof PsiType) {
        type = (PsiType) value;
    } else if (value instanceof PsiClass) {
        type = PsiTypesUtil.getClassType((PsiClass) value);
    } else {
        LOG.error("wrong type: " + value.getClass());
        return Collections.emptyList();
    }
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
    SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type);
    return Arrays.asList(info.names);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) PsiClass(com.intellij.psi.PsiClass) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 44 with JavaCodeStyleManager

use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-plugins by JetBrains.

the class CreateActionMethodQuickFix method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull final PsiFile psiFile, @Nullable("is null when called from inspection") final Editor editor, @NotNull final PsiElement startPsiElement, @NotNull final PsiElement endPsiElement) {
    final PsiClass actionClass = (PsiClass) startPsiElement;
    final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
    PsiMethod actionMethod = elementFactory.createMethodFromText("public java.lang.String " + methodName + "() throws java.lang.Exception { return \"success\"; }", actionClass);
    final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
    actionMethod = (PsiMethod) javaCodeStyleManager.shortenClassReferences(actionMethod);
    final CodeStyleManager codestylemanager = CodeStyleManager.getInstance(project);
    actionMethod = (PsiMethod) codestylemanager.reformat(actionMethod);
    final PsiMethod element = (PsiMethod) actionClass.add(actionMethod);
    //noinspection ConstantConditions
    OpenSourceUtil.navigate((Navigatable) element.getBody().getNavigationElement());
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 45 with JavaCodeStyleManager

use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project android-butterknife-zelezny by avast.

the class InjectWriter method run.

@Override
public void run() throws Throwable {
    final IButterKnife butterKnife = ButterKnifeFactory.findButterKnifeForPsiElement(mProject, mFile);
    if (butterKnife == null) {
        // Butterknife library is not available for project
        return;
    }
    if (mCreateHolder) {
        generateAdapter(butterKnife);
    } else {
        if (Utils.getInjectCount(mElements) > 0) {
            generateFields(butterKnife);
        }
        generateInjects(butterKnife);
        if (Utils.getClickCount(mElements) > 0) {
            generateClick();
        }
        Utils.showInfoNotification(mProject, String.valueOf(Utils.getInjectCount(mElements)) + " injections and " + String.valueOf(Utils.getClickCount(mElements)) + " onClick added to " + mFile.getName());
    }
    // reformat class
    JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(mProject);
    styleManager.optimizeImports(mFile);
    styleManager.shortenClassReferences(mClass);
    new ReformatCodeProcessor(mProject, mClass.getContainingFile(), null, false).runWithoutProgress();
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IButterKnife(com.avast.android.butterknifezelezny.butterknife.IButterKnife) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Aggregations

JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)96 Project (com.intellij.openapi.project.Project)27 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)21 VariableKind (com.intellij.psi.codeStyle.VariableKind)19 IncorrectOperationException (com.intellij.util.IncorrectOperationException)15 NotNull (org.jetbrains.annotations.NotNull)13 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)12 Nullable (org.jetbrains.annotations.Nullable)12 ArrayList (java.util.ArrayList)10 NonNls (org.jetbrains.annotations.NonNls)8 StringUtil (com.intellij.openapi.util.text.StringUtil)3 UniqueNameGenerator (com.intellij.util.text.UniqueNameGenerator)3 HashSet (java.util.HashSet)3 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Editor (com.intellij.openapi.editor.Editor)2 Comparing (com.intellij.openapi.util.Comparing)2 com.intellij.psi (com.intellij.psi)2 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)2 FunctionalInterfaceParameterizationUtil (com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil)2