Skip to content

Commit 1a894cb

Browse files
committed
Fixing create method intents so that private/public / static keywords are correct when the method created is in a different class.
1 parent 377bb55 commit 1a894cb

File tree

9 files changed

+122
-3
lines changed

9 files changed

+122
-3
lines changed

src/main/java/com/intellij/plugins/haxe/ide/inspections/intentions/HaxeIntroduceMethodIntention.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ private PsiElement generateDeclaration(@NotNull Project project) {
6767
String returnType = guessReturnElementType();
6868
String returnStatement = determineReturnStatement(returnType);
6969
String optionalStaticKeyword = needsToBeStatic() ? "static" : "";
70+
String privateKeyword = needsToBePublic() ? "public" : "private";
7071
String function = """
71-
private %s function %s (%s):%s {
72+
%s %s function %s (%s):%s {
7273
%s
7374
}
7475
"""
75-
.formatted(optionalStaticKeyword, methodName, generateParameterList(), returnType, returnStatement);
76+
.formatted(privateKeyword, optionalStaticKeyword, methodName, generateParameterList(), returnType, returnStatement);
7677

7778
return HaxeElementGenerator.createMethodDeclaration(project, function);
7879
}

src/main/java/com/intellij/plugins/haxe/ide/inspections/intentions/HaxeUnresolvedSymbolIntentionBase.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.intellij.openapi.project.Project;
88
import com.intellij.plugins.haxe.HaxeFileType;
99
import com.intellij.plugins.haxe.lang.psi.*;
10+
import com.intellij.plugins.haxe.lang.psi.impl.HaxeReferenceImpl;
1011
import com.intellij.plugins.haxe.model.HaxeFieldModel;
1112
import com.intellij.plugins.haxe.model.evaluator.HaxeExpressionEvaluator;
1213
import com.intellij.plugins.haxe.model.evaluator.HaxeExpressionEvaluatorContext;
@@ -23,6 +24,8 @@
2324

2425
import java.util.List;
2526

27+
import static com.intellij.plugins.haxe.util.HaxeResolveUtil.getLeftReference;
28+
2629
public abstract class HaxeUnresolvedSymbolIntentionBase<T extends PsiElement> extends LocalQuickFixAndIntentionActionOnPsiElement {
2730

2831
protected final @NotNull SmartPsiElementPointer<T> myPsiElementPointer;
@@ -98,6 +101,7 @@ public void invoke(@NotNull Project project,
98101

99102

100103
protected boolean needsToBeStatic() {
104+
if(hasClassReferenceCallie()) return true;
101105
HaxeMethodDeclaration type = PsiTreeUtil.getParentOfType(myPsiElementPointer.getElement(), HaxeMethodDeclaration.class);
102106
if (type != null) {
103107
return type.getModel().isStatic();
@@ -110,6 +114,51 @@ protected boolean needsToBeStatic() {
110114
return false;
111115
}
112116

117+
protected boolean needsToBePublic() {
118+
return hasClassReferenceCallie() || callieIsDifferentClass();
119+
}
120+
121+
private boolean hasClassReferenceCallie() {
122+
if(myPsiElementPointer.getElement() instanceof HaxeCallExpression callExpression) {
123+
HaxeExpression expression = callExpression.getExpression();
124+
if(expression != null) {
125+
HaxeReference leftReference = getLeftReference(expression);
126+
if(leftReference instanceof HaxeReferenceImpl reference) {
127+
ResultHolder result = HaxeExpressionEvaluator.evaluate(leftReference).result;
128+
SpecificHaxeClassReference classType = result.getClassType();
129+
if(!result.isUnknown() && classType != null) {
130+
HaxeClass haxeClass = classType.getHaxeClass();
131+
if(haxeClass != null) {
132+
String name = haxeClass.getName();
133+
return name != null && reference.textMatches(name);
134+
}
135+
}
136+
}
137+
}
138+
}
139+
return false;
140+
}
141+
private boolean callieIsDifferentClass() {
142+
if(myPsiElementPointer.getElement() instanceof HaxeCallExpression callExpression) {
143+
HaxeExpression expression = callExpression.getExpression();
144+
if(expression != null) {
145+
HaxeReference leftReference = getLeftReference(expression);
146+
if(leftReference != null) {
147+
ResultHolder result = HaxeExpressionEvaluator.evaluate(leftReference).result;
148+
SpecificHaxeClassReference classType = result.getClassType();
149+
if(!result.isUnknown() && classType != null) {
150+
HaxeClass haxeClass = classType.getHaxeClass();
151+
if(haxeClass != null) {
152+
HaxeClass currentClass = PsiTreeUtil.getParentOfType(callExpression, HaxeClass.class);
153+
return currentClass != haxeClass;
154+
}
155+
}
156+
}
157+
}
158+
}
159+
return false;
160+
}
161+
113162
protected <T extends PsiElement> T copyFileAndReturnClonedPsiElement(T psiElement) {
114163
PsiFile originalFile = psiElement.getContainingFile();
115164
PsiFile fileCopy = (PsiFile)originalFile.copy();

src/test/java/com/intellij/plugins/haxe/ide/quickfix/unresolved/UnresolvedMethodQuickFixTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@ public class UnresolvedMethodQuickFixTest extends HaxeQuickFixTestBase {
1111
return "/unresolved/method";
1212
}
1313

14-
1514
public void testCreateMethodAssign() {
1615
doSingleTest("_create_method_assign.hx");
1716
}
1817

1918
public void testCreateMethodInIf() {
2019
doSingleTest("_create_method_if.hx");
2120
}
21+
2222
public void testCreateMethodVoid() {
2323
doSingleTest("_create_method_void.hx");
2424
}
25+
26+
public void testCreateMethodOtherClass() {
27+
doSingleTest("_create_method_other_class.hx");
28+
}
29+
30+
public void testCreateMethodOtherClassRef() {
31+
doSingleTest("_create_method_other_class_ref.hx");
32+
}
2533
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// "Create method 'testMethod'" "true-preview"
2+
class OtherClass {
3+
public static function testMethod():Void {
4+
5+
}
6+
}
7+
class Test {
8+
function test() {
9+
OtherClass.testMethod(();
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// "Create method 'testMethod'" "true-preview"
2+
class OtherClass {
3+
public function testMethod():Void {
4+
5+
}
6+
}
7+
class Test {
8+
function test() {
9+
var ref:OtherClass = null;
10+
ref.testMethod();
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// "Create method 'testMethod'" "true-preview"
2+
class OtherClass {}
3+
class Test {
4+
function test() {
5+
OtherClass.testMethod<caret>(();
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// "Create method 'testMethod'" "true-preview"
2+
class OtherClass {}
3+
class Test {
4+
function test() {
5+
var ref:OtherClass = null;
6+
ref.testMethod<caret>();
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// "Create method 'testMethod'" "true-preview"
2+
class OtherClass {
3+
public static function testMethod():Void {
4+
5+
}
6+
}
7+
class Test {
8+
function test() {
9+
OtherClass.testMethod(();
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// "Create method 'testMethod'" "true-preview"
2+
class OtherClass {
3+
public function testMethod():Void {
4+
5+
}
6+
}
7+
class Test {
8+
function test() {
9+
var ref:OtherClass = null;
10+
ref.testMethod();
11+
}
12+
}

0 commit comments

Comments
 (0)