Skip to content

Commit

Permalink
bugfixes for Extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Nov 27, 2023
1 parent 1ebf283 commit 166c227
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## 1.4.20
* Added: Allow Extract method from fields
* Bugfix: Extract method did not return value when used in var/field init expressions
* Bugfix: Extract method did not include parameters from parent method
* Bugfix: Extract method parameter list was not formatted correctly
* Bugfix: Extract method was not correctly handling trailing semicolon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ public HaxeMethodDeclaration buildExtractedMethod(@Nullable Project project) {
HaxePsiCompositeElement fistElement = expressions.get(0);
String suggestedName = getSuggestedNames(fistElement).get(0);

boolean firstStatementReturn = PsiTreeUtil.getParentOfType(fistElement, HaxeBinaryExpression.class) != null;
boolean firstStatementReturn =
PsiTreeUtil.getParentOfType(fistElement, HaxeBinaryExpression.class) != null
|| PsiTreeUtil.getParentOfType(fistElement, HaxeVarInit.class) != null;

String block = buildMethodContent(selectedText, firstStatementReturn);
String method = buildMethod(suggestedName, block, parametersMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,18 @@ protected void performAction(HaxeIntroduceOperation operation) {

final SelectionModel selectionModel = editor.getSelectionModel();

final int selectionStart = selectionModel.getSelectionStart();
final int selectionEnd = selectionModel.getSelectionEnd();
// check selection direction and invert if right to left.
int tmpSelectionStart = selectionModel.getSelectionStart();
int tmpSelectionEnd = selectionModel.getSelectionEnd();
if (tmpSelectionStart > tmpSelectionEnd) {
int realEnd = tmpSelectionStart;
tmpSelectionStart = tmpSelectionEnd;
tmpSelectionEnd = realEnd;
}

final int selectionStart = tmpSelectionStart;
final int selectionEnd = tmpSelectionEnd;


if (selectionModel.hasSelection()) {
stopElement = file.findElementAt(selectionEnd);
Expand All @@ -74,7 +84,9 @@ protected void performAction(HaxeIntroduceOperation operation) {

int startOffset = startElement.getTextRange().getStartOffset();
int endOffset = stopElement.getTextRange().getEndOffset();

if (!(startElement instanceof HaxePsiCompositeElement) || !(stopElement instanceof HaxePsiCompositeElement)) {
throw new UnableToExtractMethodException();
}
List<HaxePsiCompositeElement> expressions = collectExpressions((HaxePsiCompositeElement)startElement, (HaxePsiCompositeElement)stopElement);

ExtractMethodBuilder methodBuilder = new ExtractMethodBuilder()
Expand All @@ -92,17 +104,19 @@ protected void performAction(HaxeIntroduceOperation operation) {
PsiElement replacementExpression = methodBuilder.buildReplacementExpressionAndUpdateMethod(project, methodDeclaration);

Document document = editor.getDocument();
HaxeMethodDeclaration parentMethod = PsiTreeUtil.getParentOfType(startElement, HaxeMethodDeclaration.class);
if (parentMethod != null) {
HaxePsiCompositeElement parentMethod = PsiTreeUtil.getParentOfType(startElement, HaxeMethodDeclaration.class);
HaxePsiCompositeElement parentField = PsiTreeUtil.getParentOfType(startElement, HaxeFieldDeclaration.class);
HaxePsiCompositeElement parentMember = parentMethod != null ? parentMethod : parentField;
if (parentMember != null) {

WriteCommandAction.writeCommandAction(project, parentMethod.getContainingFile())
WriteCommandAction.writeCommandAction(project, parentMember.getContainingFile())
.withGroupId("Extract method")
.compute(() ->
{

parentMethod.getParent().addAfter(methodDeclaration, parentMethod);
parentMember.getParent().addAfter(methodDeclaration, parentMember);

CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(parentMethod);
CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(parentMember);

PsiDocumentManager instance = PsiDocumentManager.getInstance(project);
instance.doPostponedOperationsAndUnblockDocument(document);
Expand Down

0 comments on commit 166c227

Please sign in to comment.