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);
});
}
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());
}
}
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);
}
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());
}
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();
}
Aggregations