"Make Public" intention

This commit is contained in:
Bas Leijdekkers
2012-03-27 20:26:21 +02:00
parent c80bd20e46
commit 33a16efc51
7 changed files with 143 additions and 0 deletions
@@ -313,6 +313,13 @@
</intentionAction>
-->
<!-- Modifiers -->
<intentionAction>
<className>com.siyeh.ipp.modifiers.MakePublicIntention</className>
<categoryKey>intention.category.modifiers</categoryKey>
</intentionAction>
<!-- Other -->
<intentionAction>
<className>com.siyeh.ipp.expression.FlipExpressionIntention</className>
@@ -154,6 +154,8 @@ obscure.thrown.exceptions.intention.family.name=Replace Exceptions in Throws Cla
add.array.creation.expression.intention.family.name=Add Array Creation Expression
replace.diamond.with.explicit.type.arguments.intention.name=Replace '<>' with explicit type arguments
replace.diamond.with.explicit.type.arguments.intention.family.name=Replace Diamond with Explicit Type Arguments
make.public.intention.name=Make 'public'
make.public.intention.family.name=Make Public
#hand made
demorgans.intention.name1=Replace '\\&\\&' with '||'
@@ -201,4 +203,5 @@ intention.category.imports=Imports
intention.category.comments=Comments
intention.category.control.flow=Control Flow
intention.category.strings=Strings
intention.category.modifiers=Modifiers
intention.category.other=Other
@@ -0,0 +1,74 @@
/*
* 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 com.siyeh.ipp.modifiers;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.*;
import com.intellij.refactoring.ui.ConflictsDialog;
import com.intellij.refactoring.util.RefactoringUIUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.MultiMap;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class MakePublicIntention extends Intention {
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new MakePublicPredicate();
}
@Override
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
final PsiModifierListOwner owner = (PsiModifierListOwner)element.getParent();
final PsiModifierList modifierList = owner.getModifierList();
if (modifierList == null) {
return;
}
if (!checkForConflicts(owner)) return;
modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
}
private static boolean checkForConflicts(PsiModifierListOwner owner) {
if (!(owner instanceof PsiClass)) {
return true;
}
final PsiClass aClass = (PsiClass)owner;
final PsiElement parent = aClass.getParent();
if (!(parent instanceof PsiJavaFile)) {
return true;
}
final PsiJavaFile javaFile = (PsiJavaFile)parent;
final String name = FileUtil.getNameWithoutExtension(javaFile.getName());
final String className = aClass.getName();
if (name.equals(className)) {
return true;
}
final MultiMap<PsiElement, String> conflicts = new MultiMap();
conflicts.putValue(aClass, "The " + RefactoringUIUtil.getDescription(aClass, false) + " is declared in " +
RefactoringUIUtil.getDescription(javaFile, false) +
" but when public should be declared in a file named '" + className + '\'');
final ConflictsDialog conflictsDialog = new ConflictsDialog(owner.getProject(), conflicts);
conflictsDialog.show();
return conflictsDialog.isOK();
}
}
@@ -0,0 +1,50 @@
/*
* 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 com.siyeh.ipp.modifiers;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.siyeh.ipp.base.PsiElementPredicate;
/**
* @author Bas Leijdekkers
*/
public class MakePublicPredicate implements PsiElementPredicate {
@Override
public boolean satisfiedBy(PsiElement element) {
final PsiElement parent = element.getParent();
if (!(parent instanceof PsiClass || parent instanceof PsiField || parent instanceof PsiMethod)) {
return false;
}
if (element instanceof PsiDocComment || element instanceof PsiCodeBlock) {
return false;
}
if (parent instanceof PsiClass) {
final PsiClass aClass = (PsiClass)parent;
final PsiElement brace = aClass.getLBrace();
if (brace != null && brace.getTextOffset() < element.getTextOffset()) {
return false;
}
}
final PsiModifierListOwner owner = (PsiModifierListOwner)parent;
final PsiModifierList modifierList = owner.getModifierList();
if (modifierList == null || modifierList.hasModifierProperty(PsiModifier.PUBLIC)) {
return false;
}
return true;
}
}
@@ -0,0 +1,2 @@
public class X {
}
@@ -0,0 +1,2 @@
<spot>class X </spot>{
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention makes a class, method or field public.
</body>
</html>