Skip to content

Commit

Permalink
0.0.3 [Unimport class] Add support for the class alias. Can be invoke…
Browse files Browse the repository at this point in the history
…d on the use statement
  • Loading branch information
Ivan Scherbak committed Oct 11, 2016
1 parent 3cc0ae6 commit 469aef8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 30 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/ivan.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<id>com.funivan.phpstorm.refactoring</id>
<name>Refactoring</name>
<version>0.0.2</version>
<version>0.0.3</version>
<vendor email="[email protected]" url="http://funivan.com">Funivan</vendor>

<description><![CDATA[
Expand All @@ -10,6 +10,9 @@
]]></description>

<change-notes><![CDATA[
<h4>0.0.3</h4>
- [Unimport class] Add support for the class alias. Can be invoked on the use statement
<h4>0.0.2</h4>
- [Unimport class] invoke action inside php doc comments
- [Find magic methods] run task in background
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.PhpWorkaroundUtil;
import com.jetbrains.php.codeInsight.PhpCodeInsightUtil;
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocType;
import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
import com.jetbrains.php.lang.psi.elements.ClassReference;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import com.jetbrains.php.lang.psi.elements.PhpReference;
import com.jetbrains.php.lang.psi.elements.PhpUse;
import com.jetbrains.php.lang.psi.visitors.PhpRecursiveElementVisitor;
import com.jetbrains.php.lang.psi.elements.impl.PhpUseImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;

/**
* Created by funivan
*/
Expand Down Expand Up @@ -58,22 +62,27 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable Ps

PsiElement baseElement = element.getParent();

if (!(baseElement instanceof PhpReference)) {

if (!(baseElement instanceof ClassReference)) {
return false;
}
PhpReference ref = (PhpReference) baseElement;

if (ref.isAbsolute()) {
return false;
String fqn = ((ClassReference) baseElement).getFQN();


if (baseElement.getParent() instanceof PhpUseImpl) {
return true;
}

String fqn = ref.getFQN();

if (fqn == null) {
if (baseElement.getText().equals(fqn)) {
return false;
}

return !fqn.equals(baseElement.getText());
Collection<PhpClass> classes = PhpIndex.getInstance(project).getClassesByFQN(fqn);

return classes.size() != 0;

}

/**
Expand All @@ -85,16 +94,31 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable Ps
*/
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
element = element.getParent();
if (!(element instanceof PhpReference)) {
return;


ClassReference classReference = (ClassReference) element;
String shortFqn = classReference.getText();

PsiElement parentElement = classReference.getParent();
if (parentElement instanceof PhpUseImpl) {

PhpUseImpl useStatement = (PhpUseImpl) parentElement;

String alias = useStatement.getAliasName();
if (alias != null) {
shortFqn = alias;
} else {
shortFqn = useStatement.getName();
}
}
PhpReference classReference = (PhpReference) element;


final String searchClassName = shortFqn;

PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(classReference);

if (scopeForUseOperator == null) {
return;
}

assert scopeForUseOperator != null;


String fqn = classReference.getFQN();
Expand All @@ -103,29 +127,41 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
}
ClassReference newClassRef = PhpPsiElementFactory.createClassReference(project, fqn);

final ArrayList<PsiElement> replaceElements = new ArrayList<>();


scopeForUseOperator.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {

scopeForUseOperator.acceptChildren(new PhpRecursiveElementVisitor() {
public void visitPhpElement(PhpPsiElement element) {
if (!PhpCodeInsightUtil.isScopeForUseOperator(element)) {
super.visitPhpElement(element);
if (element instanceof ClassReference) {
visitPhpClassReference((ClassReference) element);
}

if (element instanceof PhpDocType) {
visitPhpDocType((PhpDocType) element);
}

super.visitElement(element);

}

public void visitPhpDocType(PhpDocType phpDocType) {
private void visitPhpDocType(PhpDocType phpDocType) {

if (phpDocType.isAbsolute()) {
return;
}

if (phpDocType.getText().equals(classReference.getText())) {
// probably idea byg. FQN of docElement consist of Current namespace + ClassName
replace(phpDocType);

if (phpDocType.getText().equals(searchClassName)) {
collectElement(phpDocType);
}
}

public void visitPhpClassReference(ClassReference classReference) {
if (classReference.getParent() instanceof PhpUse) {
private void visitPhpClassReference(ClassReference classReference) {

// Skip import use statements
if (classReference.getParent() instanceof PhpUseImpl) {
return;
}

Expand All @@ -137,17 +173,26 @@ public void visitPhpClassReference(ClassReference classReference) {
return;
}

replace(classReference);
collectElement(classReference);
}

private void replace(PsiElement currentElement) {
private void collectElement(PsiElement currentElement) {
if (currentElement.getText().equals(newClassRef.getText())) {
return;
}
currentElement.replace(newClassRef);

replaceElements.add(currentElement);
}


});


for (PsiElement oldReference : replaceElements) {
oldReference.replace(newClassRef);
}


}

}

0 comments on commit 469aef8

Please sign in to comment.