mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
initial version of quickfix to register extension point
This commit is contained in:
@@ -84,7 +84,7 @@
|
||||
<psi.referenceContributor implementation="org.jetbrains.idea.devkit.dom.impl.InspectionsPropertiesReferenceProviderContributor"/>
|
||||
<psi.referenceContributor implementation="org.jetbrains.idea.devkit.references.IconsReferencesContributor"/>
|
||||
<referencesSearch implementation="org.jetbrains.idea.devkit.references.IconsReferencesContributor"/>
|
||||
<unusedDeclarationFixProvider implementation="org.jetbrains.idea.devkit.inspections.quickfix.RegisterInspectionFixProvider"/>
|
||||
<unusedDeclarationFixProvider implementation="org.jetbrains.idea.devkit.inspections.quickfix.RegisterExtensionFixProvider"/>
|
||||
</extensions>
|
||||
|
||||
<module-components>
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jetbrains.idea.devkit.inspections.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.PsiNavigateUtil;
|
||||
import com.intellij.util.xml.DomFileElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.idea.devkit.dom.Extension;
|
||||
import org.jetbrains.idea.devkit.dom.Extensions;
|
||||
import org.jetbrains.idea.devkit.dom.IdeaPlugin;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class RegisterExtensionFix implements IntentionAction {
|
||||
private final PsiClass myExtensionClass;
|
||||
private final String myEPName;
|
||||
|
||||
public RegisterExtensionFix(PsiClass extensionClass, String epName) {
|
||||
myExtensionClass = extensionClass;
|
||||
myEPName = epName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return "Register extension";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
RegisterInspectionFix.choosePluginDescriptor(project, editor, file, new Consumer<DomFileElement<IdeaPlugin>>() {
|
||||
@Override
|
||||
public void consume(DomFileElement<IdeaPlugin> element) {
|
||||
doFix(element);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void doFix(final DomFileElement<IdeaPlugin> element) {
|
||||
Extension extension = new WriteCommandAction<Extension>(element.getFile().getProject(), element.getFile()) {
|
||||
@Override
|
||||
protected void run(Result<Extension> result) throws Throwable {
|
||||
Extensions extensions = RegisterInspectionFix.getExtension(element.getRootElement(), myEPName);
|
||||
Extension extension = extensions.addExtension(myEPName);
|
||||
XmlTag tag = extension.getXmlTag();
|
||||
tag.setAttribute("implementation", myExtensionClass.getQualifiedName());
|
||||
result.setResult(extension);
|
||||
}
|
||||
}.execute().throwException().getResultObject();
|
||||
PsiNavigateUtil.navigate(extension.getXmlTag());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jetbrains.idea.devkit.inspections.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.GlobalInspectionTool;
|
||||
import com.intellij.codeInspection.InspectionEP;
|
||||
import com.intellij.codeInspection.LocalInspectionEP;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.reference.UnusedDeclarationFixProvider;
|
||||
import com.intellij.ide.highlighter.XmlFileType;
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.ProjectScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Dmitry Avdeev
|
||||
* Date: 1/19/12
|
||||
*/
|
||||
public class RegisterExtensionFixProvider implements UnusedDeclarationFixProvider {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction[] getQuickFixes(PsiElement element) {
|
||||
if (!(element instanceof PsiIdentifier)) return IntentionAction.EMPTY_ARRAY;
|
||||
PsiElement parent = element.getParent();
|
||||
if (!(parent instanceof PsiClass)) return IntentionAction.EMPTY_ARRAY;
|
||||
PsiClass parentClass = (PsiClass)parent;
|
||||
if (InheritanceUtil.isInheritor(parentClass, LocalInspectionTool.class.getName())) {
|
||||
return new IntentionAction[] { new RegisterInspectionFix(parentClass, LocalInspectionEP.LOCAL_INSPECTION) };
|
||||
}
|
||||
if (InheritanceUtil.isInheritor(parentClass, GlobalInspectionTool.class.getName())) {
|
||||
return new IntentionAction[] { new RegisterInspectionFix(parentClass, InspectionEP.GLOBAL_INSPECTION) };
|
||||
}
|
||||
PsiField epField = findEPNameField(parentClass);
|
||||
if (epField != null) {
|
||||
String epName = findEPNameForClass(epField.getContainingClass());
|
||||
if (epName != null) {
|
||||
return new IntentionAction[] { new RegisterExtensionFix(parentClass, epName) };
|
||||
}
|
||||
}
|
||||
return IntentionAction.EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
private static String findEPNameForClass(PsiClass aClass) {
|
||||
GlobalSearchScope scope = GlobalSearchScope.getScopeRestrictedByFileTypes(ProjectScope.getAllScope(aClass.getProject()), XmlFileType.INSTANCE);
|
||||
for (PsiReference reference : ReferencesSearch.search(aClass, scope)) {
|
||||
XmlTag tag = PsiTreeUtil.getParentOfType(reference.getElement(), XmlTag.class);
|
||||
if (tag != null && "extensionPoint".equals(tag.getName())) {
|
||||
String qName = tag.getAttributeValue("qualifiedName");
|
||||
if (qName != null) {
|
||||
return qName;
|
||||
}
|
||||
String name = tag.getAttributeValue("name");
|
||||
if (name != null) {
|
||||
return "com.intellij." + name;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PsiField findEPNameField(PsiClass aClass) {
|
||||
for (PsiField field : aClass.getFields()) {
|
||||
if (field.getType() instanceof PsiClassType) {
|
||||
PsiClassType classType = (PsiClassType)field.getType();
|
||||
PsiClassType.ClassResolveResult resolved = classType.resolveGenerics();
|
||||
PsiClass fieldClass = resolved.getElement();
|
||||
if (fieldClass != null && ExtensionPointName.class.getName().equals(fieldClass.getQualifiedName())) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (PsiClass superClass: aClass.getSupers()) {
|
||||
PsiField epField = findEPNameField(superClass);
|
||||
if (epField != null) {
|
||||
return epField;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.PsiNavigateUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
@@ -84,6 +85,16 @@ class RegisterInspectionFix implements IntentionAction {
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
|
||||
choosePluginDescriptor(project, editor, file, new Consumer<DomFileElement<IdeaPlugin>>() {
|
||||
@Override
|
||||
public void consume(DomFileElement<IdeaPlugin> element) {
|
||||
doFix(element, project, file);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void choosePluginDescriptor(final Project project, Editor editor, final PsiFile file,
|
||||
final Consumer<DomFileElement<IdeaPlugin>> consumer) {
|
||||
Module module = ModuleUtil.findModuleForPsiElement(file);
|
||||
assert module != null;
|
||||
List<DomFileElement<IdeaPlugin>> elements =
|
||||
@@ -103,7 +114,7 @@ class RegisterInspectionFix implements IntentionAction {
|
||||
}
|
||||
|
||||
if (elements.size() == 1) {
|
||||
doFix(elements.get(0), project, file);
|
||||
consumer.consume(elements.get(0));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -127,7 +138,7 @@ class RegisterInspectionFix implements IntentionAction {
|
||||
|
||||
@Override
|
||||
public PopupStep onChosen(DomFileElement<IdeaPlugin> selectedValue, boolean finalChoice) {
|
||||
doFix(selectedValue, project, file);
|
||||
consumer.consume(selectedValue);
|
||||
return FINAL_CHOICE;
|
||||
}
|
||||
};
|
||||
@@ -137,12 +148,11 @@ class RegisterInspectionFix implements IntentionAction {
|
||||
|
||||
private void doFix(DomFileElement<IdeaPlugin> selectedValue, final Project project, final PsiFile file) {
|
||||
final IdeaPlugin plugin = selectedValue.getRootElement();
|
||||
final List<Extensions> extensionsList = plugin.getExtensions();
|
||||
Extension extension = new WriteCommandAction<Extension>(project, file) {
|
||||
|
||||
@Override
|
||||
protected void run(Result<Extension> result) throws Throwable {
|
||||
final Extensions extensions = getExtension(plugin, extensionsList);
|
||||
final Extensions extensions = getExtension(plugin, myEp.getName());
|
||||
Extension extension = extensions.addExtension(myEp.getName());
|
||||
XmlTag tag = extension.getXmlTag();
|
||||
tag.setAttribute("implementationClass", myPsiClass.getQualifiedName());
|
||||
@@ -152,11 +162,12 @@ class RegisterInspectionFix implements IntentionAction {
|
||||
PsiNavigateUtil.navigate(extension.getXmlTag());
|
||||
}
|
||||
|
||||
private Extensions getExtension(IdeaPlugin plugin, List<Extensions> extensionsList) {
|
||||
public static Extensions getExtension(IdeaPlugin plugin, String epName) {
|
||||
final List<Extensions> extensionsList = plugin.getExtensions();
|
||||
Extensions extensions = null;
|
||||
for (Extensions e : extensionsList) {
|
||||
String s = e.getDefaultExtensionNs().getStringValue();
|
||||
if (s != null && myEp.getName().startsWith(s)) {
|
||||
if (s != null && epName.startsWith(s)) {
|
||||
extensions = e;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jetbrains.idea.devkit.inspections.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.GlobalInspectionTool;
|
||||
import com.intellij.codeInspection.InspectionEP;
|
||||
import com.intellij.codeInspection.LocalInspectionEP;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.reference.UnusedDeclarationFixProvider;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiIdentifier;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Dmitry Avdeev
|
||||
* Date: 1/19/12
|
||||
*/
|
||||
public class RegisterInspectionFixProvider implements UnusedDeclarationFixProvider {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction[] getQuickFixes(PsiElement element) {
|
||||
if (!(element instanceof PsiIdentifier)) return IntentionAction.EMPTY_ARRAY;
|
||||
PsiElement parent = element.getParent();
|
||||
if (!(parent instanceof PsiClass)) return IntentionAction.EMPTY_ARRAY;
|
||||
if (InheritanceUtil.isInheritor((PsiClass)parent, LocalInspectionTool.class.getName())) {
|
||||
return new IntentionAction[] { new RegisterInspectionFix((PsiClass)parent, LocalInspectionEP.LOCAL_INSPECTION) };
|
||||
}
|
||||
if (InheritanceUtil.isInheritor((PsiClass)parent, GlobalInspectionTool.class.getName())) {
|
||||
return new IntentionAction[] { new RegisterInspectionFix((PsiClass)parent, InspectionEP.GLOBAL_INSPECTION) };
|
||||
}
|
||||
return IntentionAction.EMPTY_ARRAY;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user