Skip to content

Commit 1ed82b2

Browse files
author
Ivan Scherbak
committed
improve unimport intention. Invoke action inside php doc comments
1 parent a5aeb15 commit 1ed82b2

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
]]></description>
1111

1212
<change-notes><![CDATA[
13+
<h4>[unreleased]</h4>
14+
- [Unimport class] invoke action inside php doc comments
15+
1316
<h4>0.0.2</h4>
1417
- [Find magic methods] run task in background
1518
- [Find magic methods] Find magic properties

src/com/funivan/phpstorm/refactoring/UnimportClass/UnimportClassIntention.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
1111
import com.jetbrains.php.lang.psi.elements.ClassReference;
1212
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
13+
import com.jetbrains.php.lang.psi.elements.PhpReference;
1314
import com.jetbrains.php.lang.psi.elements.PhpUse;
1415
import com.jetbrains.php.lang.psi.visitors.PhpRecursiveElementVisitor;
1516
import org.jetbrains.annotations.NotNull;
1617
import org.jetbrains.annotations.Nullable;
1718

1819
/**
19-
* Created by ivan on 19.01.16.
20+
* Created by funivan
2021
*/
2122
public class UnimportClassIntention extends PsiElementBaseIntentionAction {
2223

@@ -56,17 +57,23 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable Ps
5657
}
5758

5859
PsiElement baseElement = element.getParent();
59-
if (!(baseElement instanceof ClassReference)) {
60+
61+
if (!(baseElement instanceof PhpReference)) {
62+
return false;
63+
}
64+
PhpReference ref = (PhpReference) baseElement;
65+
66+
if (ref.isAbsolute()) {
6067
return false;
6168
}
6269

63-
String fqn = ((ClassReference) baseElement).getFQN();
70+
String fqn = ref.getFQN();
6471

65-
if (fqn.equals(baseElement.getText())) {
72+
if (fqn == null) {
6673
return false;
6774
}
6875

69-
return true;
76+
return !fqn.equals(baseElement.getText());
7077
}
7178

7279
/**
@@ -78,14 +85,22 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable Ps
7885
*/
7986
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
8087
element = element.getParent();
81-
ClassReference classReference = (ClassReference) element;
88+
if (!(element instanceof PhpReference)) {
89+
return;
90+
}
91+
PhpReference classReference = (PhpReference) element;
8292

8393
PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(classReference);
8494

85-
assert scopeForUseOperator != null;
95+
if (scopeForUseOperator == null) {
96+
return;
97+
}
8698

8799

88100
String fqn = classReference.getFQN();
101+
if (fqn == null) {
102+
return;
103+
}
89104
ClassReference newClassRef = PhpPsiElementFactory.createClassReference(project, fqn);
90105

91106

@@ -99,7 +114,7 @@ public void visitPhpElement(PhpPsiElement element) {
99114

100115
public void visitPhpDocType(PhpDocType phpDocType) {
101116

102-
if (phpDocType.isAbsolute() == true) {
117+
if (phpDocType.isAbsolute()) {
103118
return;
104119
}
105120

0 commit comments

Comments
 (0)