mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IG: remove WriteCommandAction.Simple and simplify
This commit is contained in:
-2
@@ -1823,9 +1823,7 @@ package.dot.html.may.be.package.info.display.name='package.html' may be converte
|
||||
package.dot.html.may.be.package.info.exists.problem.descriptor=<code>package.html</code> is ignored because <code>package-info.java</code> exists
|
||||
package.dot.html.may.be.package.info.problem.descriptor=<code>package.html</code> may be converted to <code>package-info.java</code>
|
||||
package.dot.html.may.be.package.info.delete.quickfix=Delete 'package.html'
|
||||
package.dot.html.delete.command=package.html deletion
|
||||
package.dot.html.may.be.package.info.convert.quickfix=Convert to 'package-info.java'
|
||||
package.dot.html.convert.command=package.html to package-info.java conversion
|
||||
choose.super.class.to.ignore=Choose class
|
||||
ignore.anonymous.inner.classes=Ignore anonymous inner classes
|
||||
try.with.identical.catches.display.name=Identical 'catch' branches in 'try' statement
|
||||
|
||||
+50
-103
@@ -16,28 +16,18 @@
|
||||
package com.siyeh.ig.javadoc;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.command.UndoConfirmationPolicy;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.AsyncResult;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.html.HtmlTag;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
import com.intellij.psi.xml.XmlFile;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.psi.xml.XmlTagValue;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PackageDotHtmlMayBePackageInfoInspection extends PackageDotHtmlMayBePackageInfoInspectionBase {
|
||||
|
||||
@@ -59,29 +49,13 @@ public class PackageDotHtmlMayBePackageInfoInspection extends PackageDotHtmlMayB
|
||||
return InspectionGadgetsBundle.message("package.dot.html.may.be.package.info.delete.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
if (!(element instanceof XmlFile)) {
|
||||
return;
|
||||
}
|
||||
final XmlFile xmlFile = (XmlFile)element;
|
||||
new WriteCommandAction.Simple(project, InspectionGadgetsBundle.message("package.dot.html.delete.command"), xmlFile) {
|
||||
@Override
|
||||
protected void run() throws Throwable {
|
||||
element.delete();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
|
||||
return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
|
||||
}
|
||||
}.execute();
|
||||
element.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,11 +73,6 @@ public class PackageDotHtmlMayBePackageInfoInspection extends PackageDotHtmlMayB
|
||||
return InspectionGadgetsBundle.message("package.dot.html.may.be.package.info.convert.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(final Project project, ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
@@ -115,86 +84,64 @@ public class PackageDotHtmlMayBePackageInfoInspection extends PackageDotHtmlMayB
|
||||
if (directory == null) {
|
||||
return;
|
||||
}
|
||||
final PsiFile file = directory.findFile("package-info.java");
|
||||
final PsiFile file = directory.findFile(PsiPackage.PACKAGE_INFO_FILE);
|
||||
if (file != null) {
|
||||
return;
|
||||
}
|
||||
new WriteCommandAction.Simple(project, InspectionGadgetsBundle.message("package.dot.html.convert.command"), xmlFile) {
|
||||
@Override
|
||||
protected void run() throws Throwable {
|
||||
final PsiJavaFile packageInfoFile = (PsiJavaFile)directory.createFile("package-info.java");
|
||||
CommandProcessor.getInstance().addAffectedFiles(project, packageInfoFile.getVirtualFile());
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
|
||||
String packageInfoText = getPackageInfoText(xmlFile);
|
||||
if (packageInfoText == null) {
|
||||
packageInfoText = xmlFile.getText();
|
||||
}
|
||||
final StringBuilder commentText = new StringBuilder("/**\n");
|
||||
final String[] lines = StringUtil.splitByLines(packageInfoText);
|
||||
boolean appended = false;
|
||||
for (String line : lines) {
|
||||
if (!appended && line.isEmpty()) {
|
||||
// skip empty lines at the beginning
|
||||
continue;
|
||||
}
|
||||
commentText.append(" * ").append(line).append('\n');
|
||||
appended = true;
|
||||
}
|
||||
commentText.append("*/");
|
||||
final PsiDocComment comment = elementFactory.createDocCommentFromText(commentText.toString());
|
||||
if (!aPackage.isEmpty()) {
|
||||
final PsiPackageStatement packageStatement = elementFactory.createPackageStatement(aPackage);
|
||||
final PsiElement addedElement = packageInfoFile.add(packageStatement);
|
||||
packageInfoFile.addBefore(comment, addedElement);
|
||||
}
|
||||
else {
|
||||
packageInfoFile.add(comment);
|
||||
}
|
||||
xmlFile.delete();
|
||||
if (!isOnTheFly()) {
|
||||
return;
|
||||
}
|
||||
final AsyncResult<DataContext> dataContextFromFocus = DataManager.getInstance().getDataContextFromFocus();
|
||||
dataContextFromFocus.doWhenDone(new Consumer<DataContext>() {
|
||||
@Override
|
||||
public void consume(DataContext dataContext) {
|
||||
final FileEditorManager editorManager = FileEditorManager.getInstance(project);
|
||||
final VirtualFile virtualFile = packageInfoFile.getVirtualFile();
|
||||
if (virtualFile == null) {
|
||||
return;
|
||||
}
|
||||
editorManager.openFile(virtualFile, true);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
|
||||
return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
|
||||
}
|
||||
}.execute();
|
||||
final String packageInfoText = getPackageInfoText(xmlFile);
|
||||
final PsiJavaFile packageInfoFile = (PsiJavaFile)directory.createFile(PsiPackage.PACKAGE_INFO_FILE);
|
||||
final String commentText = buildCommentText(packageInfoText);
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
|
||||
final PsiDocComment comment = elementFactory.createDocCommentFromText(commentText);
|
||||
if (!aPackage.isEmpty()) {
|
||||
final PsiPackageStatement packageStatement = elementFactory.createPackageStatement(aPackage);
|
||||
final PsiElement addedElement = packageInfoFile.add(packageStatement);
|
||||
packageInfoFile.addBefore(comment, addedElement);
|
||||
}
|
||||
else {
|
||||
packageInfoFile.add(comment);
|
||||
}
|
||||
xmlFile.delete();
|
||||
if (isOnTheFly()) {
|
||||
packageInfoFile.navigate(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static String getPackageInfoText(XmlFile xmlFile) {
|
||||
final XmlTag rootTag = xmlFile.getRootTag();
|
||||
if (rootTag == null) {
|
||||
return null;
|
||||
}
|
||||
final PsiElement[] children = rootTag.getChildren();
|
||||
for (PsiElement child : children) {
|
||||
if (!(child instanceof HtmlTag)) {
|
||||
@NotNull
|
||||
private static String buildCommentText(String packageInfoText) {
|
||||
final StringBuilder commentText = new StringBuilder("/**\n");
|
||||
final String[] lines = StringUtil.splitByLines(packageInfoText);
|
||||
boolean appended = false;
|
||||
for (String line : lines) {
|
||||
if (!appended && line.isEmpty()) {
|
||||
// skip empty lines at the beginning
|
||||
continue;
|
||||
}
|
||||
final HtmlTag htmlTag = (HtmlTag)child;
|
||||
@NonNls final String name = htmlTag.getName();
|
||||
if ("body".equalsIgnoreCase(name)) {
|
||||
final XmlTagValue value = htmlTag.getValue();
|
||||
return value.getText();
|
||||
commentText.append(" * ").append(line).append('\n');
|
||||
appended = true;
|
||||
}
|
||||
commentText.append("*/");
|
||||
return commentText.toString();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static String getPackageInfoText(XmlFile xmlFile) {
|
||||
final XmlTag rootTag = xmlFile.getRootTag();
|
||||
if (rootTag != null) {
|
||||
final PsiElement[] children = rootTag.getChildren();
|
||||
for (PsiElement child : children) {
|
||||
if (!(child instanceof HtmlTag)) {
|
||||
continue;
|
||||
}
|
||||
final HtmlTag htmlTag = (HtmlTag)child;
|
||||
@NonNls final String name = htmlTag.getName();
|
||||
if ("body".equalsIgnoreCase(name)) {
|
||||
final XmlTagValue value = htmlTag.getValue();
|
||||
return value.getText();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return xmlFile.getText();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user