Skip to content

Commit

Permalink
Merge pull request #42033 from mindula/fix-40525
Browse files Browse the repository at this point in the history
Fix rename position in `Extract to local variable` and `Extract to constant` code actions
  • Loading branch information
KavinduZoysa authored Jan 31, 2024
2 parents dbcd5fd + 4ba5b8d commit a7a0e92
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public List<CodeAction> getCodeActions(CodeActionContext context,
CodeAction codeAction = CodeActionUtil.createCodeAction(CommandConstants.EXTRACT_TO_CONSTANT,
textEdits, context.fileUri(), CodeActionKind.RefactorExtract);
CodeActionUtil.addRenamePopup(context, codeAction, CommandConstants.RENAME_COMMAND_TITLE_FOR_CONSTANT,
getRenamePosition(textEdits.get(1).getRange().getStart(), addNewLineAtStart));
getRenamePosition(textEdits.get(1).getRange(), addNewLineAtStart));
return Collections.singletonList(codeAction);
}

Expand All @@ -127,7 +127,7 @@ public List<CodeAction> getCodeActions(CodeActionContext context,
CodeAction codeAction = CodeActionUtil.createCodeAction(CommandConstants.EXTRACT_TO_CONSTANT,
textEdits, context.fileUri(), CodeActionKind.RefactorExtract);
CodeActionUtil.addRenamePopup(context, codeAction, CommandConstants.RENAME_COMMAND_TITLE_FOR_CONSTANT,
getRenamePosition(textEdits.get(1).getRange().getStart(), addNewLineAtStart));
getRenamePosition(textEdits.get(1).getRange(), addNewLineAtStart));
return Collections.singletonList(codeAction);
}

Expand All @@ -141,7 +141,7 @@ public List<CodeAction> getCodeActions(CodeActionContext context,
LinkedHashMap<String, Position> renamePositionMap = new LinkedHashMap<>();
nodeList.forEach(extractableNode ->
renamePositionMap.put(extractableNode.toSourceCode().strip(),
getRenamePosition(PositionUtil.toRange(extractableNode.lineRange()).getStart(),
getRenamePosition(PositionUtil.toRange(extractableNode.lineRange()),
addNewLineAtStart)));
return Collections.singletonList(
CodeActionUtil.createCodeAction(CommandConstants.EXTRACT_TO_CONSTANT,
Expand Down Expand Up @@ -171,13 +171,13 @@ private boolean isExpressionNode(Node node) {
return node instanceof StatementNode || node instanceof ModuleMemberDeclarationNode;
}

private Position getRenamePosition(Position replacePosition, boolean addNewLineAtStart) {
private Position getRenamePosition(Range range, boolean addNewLineAtStart) {
// line position will increment by one due to const declaration statement
int line = replacePosition.getLine() + 1;
int line = range.getEnd().getLine() + 1;
if (addNewLineAtStart) {
line += 1;
}
return new Position(line, replacePosition.getCharacter());
return new Position(line, range.getStart().getCharacter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public List<CodeAction> getCodeActions(CodeActionContext context,
CodeAction codeAction = CodeActionUtil.createCodeAction(CommandConstants.EXTRACT_TO_VARIABLE,
textEdits, context.fileUri(), CodeActionKind.RefactorExtract);
CodeActionUtil.addRenamePopup(context, codeAction, CommandConstants.RENAME_COMMAND_TITLE_FOR_VARIABLE,
getRenamePosition(textEdits.get(1).getRange().getStart()));
getRenamePosition(textEdits.get(1).getRange()));
return Collections.singletonList(codeAction);
}

Expand All @@ -165,7 +165,7 @@ public List<CodeAction> getCodeActions(CodeActionContext context,
String key = extractableNode.toSourceCode().strip();
textEditMap.put(key,
getTextEdits(context, tSymbol.get(), extractableNode, statementNode));
renamePositionMap.put(key, getRenamePosition(PositionUtil.toRange(extractableNode.lineRange()).getStart()));
renamePositionMap.put(key, getRenamePosition(PositionUtil.toRange(extractableNode.lineRange())));
});

if (lsClientCapabilities.getInitializationOptions().isPositionalRefactorRenameSupported()) {
Expand Down Expand Up @@ -198,8 +198,8 @@ private List<TextEdit> getTextEdits(CodeActionContext context, TypeSymbol typeSy
return List.of(varDeclEdit, replaceEdit);
}

private Position getRenamePosition(Position position) {
return new Position(position.getLine() + 1, position.getCharacter());
private Position getRenamePosition(Range range) {
return new Position(range.getEnd().getLine() + 1, range.getStart().getCharacter());
}

private static List<Node> getPossibleExpressionNodes(Node node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public Object[][] dataProvider() {
{"extractNumericLiteralInUnaryExprToConstant.json"},
{"extractNumericLiteralInUnaryExprToConstant2.json"},
{"extractExprToConstant1.json"},
{"extractExprToConstant2.json"}
{"extractExprToConstant2.json"},
{"extractTwoLinesToConstant.json"}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"range": {
"start": {
"line": 26,
"character": 13
},
"end": {
"line": 27,
"character": 18
}
},
"source": "extractToConstant.bal",
"expected": [
{
"title": "Extract to constant",
"kind": "refactor.extract",
"edits": [
{
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 0
}
},
"newText": "const string CONST = \"abc\"\n + \"def\";\n"
},
{
"range": {
"start": {
"line": 26,
"character": 13
},
"end": {
"line": 27,
"character": 18
}
},
"newText": "CONST"
}
],
"command": {
"title": "Rename constant",
"command": "ballerina.action.positional.rename",
"arguments": [
"extractToConstant.bal",
{
"line": 28,
"character": 13
}
]
},
"resolvable": false
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ const int CONST1 = 10 + 20;

int unaryNumericExpr = -100;
boolean unaryLogicalExpr = !true;

string str = "abc"
+ "def";
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"arguments": [
"extractToVariableInQueryExpression.bal",
{
"line": 3,
"line": 4,
"character": 24
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"arguments": [
"extractToVariableInTableConstructor.bal",
{
"line": 6,
"line": 9,
"character": 30
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"arguments": [
"extractToVariableInTableConstructor.bal",
{
"line": 13,
"line": 16,
"character": 21
}
]
Expand Down

0 comments on commit a7a0e92

Please sign in to comment.