Skip to content

Commit

Permalink
Add static keyword to method when extracting from other static members
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Aug 25, 2024
1 parent c88b31c commit 2f8bf24
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class ExtractMethodBuilder {
private Map<PsiReference, HaxeLocalVarDeclaration> localUsedOutside;

private Map<String, String> parametersMap;
private boolean needsStaticKeyword = false;

public ExtractMethodBuilder expressions(List<HaxePsiCompositeElement> expressions) {
this.expressions = expressions;
return this;
Expand Down Expand Up @@ -247,6 +249,7 @@ private String buildMethod(String suggestedName, String block, Map<String, Strin
String parameterList = createParameterListText(parameters);
return new StringBuilder()
.append("\n")
.append(modifiers())
.append("function " + suggestedName + " (")
.append(parameterList)
.append(")")
Expand All @@ -255,6 +258,10 @@ private String buildMethod(String suggestedName, String block, Map<String, Strin
.toString();
}

private String modifiers() {
return needsStaticKeyword ? "static " : "";
}

private static String createParameterListText(Map<String, String> parameters) {
return parameters.entrySet().stream().map(entry -> {
String paramName = entry.getKey();
Expand Down Expand Up @@ -355,4 +362,7 @@ else if (!localUsedOutside.isEmpty()) {
}


public void isStatic(boolean needsStaticKeyword) {
this.needsStaticKeyword = needsStaticKeyword;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.intellij.plugins.haxe.ide.refactoring.introduceVariable.HaxeIntroduceHandler;
import com.intellij.plugins.haxe.ide.refactoring.introduceVariable.HaxeIntroduceOperation;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.model.HaxeFieldModel;
import com.intellij.plugins.haxe.model.HaxeModel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
Expand Down Expand Up @@ -96,6 +98,8 @@ protected void performAction(HaxeIntroduceOperation operation) {
.expressions(expressions);

try {

methodBuilder.isStatic(needsStaticKeyword(startElement));
methodBuilder.validateAndProcessExpressions();

boolean partOfExpression = stopElement.getParent().getParent() instanceof HaxeExpression;
Expand Down Expand Up @@ -152,6 +156,20 @@ protected void performAction(HaxeIntroduceOperation operation) {

}

private static boolean needsStaticKeyword(PsiElement startElement) {
HaxeMethod parentMethod = PsiTreeUtil.getParentOfType(startElement, HaxeMethodDeclaration.class);
if (parentMethod != null) {
return parentMethod.getModel().isStatic();
}
HaxeVarInit init = PsiTreeUtil.getParentOfType(startElement, HaxeVarInit.class);
HaxeFieldDeclaration fieldDeclaration = PsiTreeUtil.getParentOfType(startElement, HaxeFieldDeclaration.class);
if (init != null && fieldDeclaration != null) {
HaxeModel model = fieldDeclaration.getModel();
if( model instanceof HaxeFieldModel fieldModel) return fieldModel.isStatic();
}
return false;
}

private static void startRenameMethod(HaxeIntroduceOperation operation, Editor editor, PsiFile file) {
int newSelectionEnd = editor.getSelectionModel().getSelectionEnd();

Expand Down

0 comments on commit 2f8bf24

Please sign in to comment.