Skip to content

Commit

Permalink
fixed extracting from functions with type parameters
Browse files Browse the repository at this point in the history
added testcase
  • Loading branch information
AlexHaxe committed Nov 25, 2024
1 parent d327396 commit 94e8203
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/refactor/refactor/ExtractMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class ExtractMethod {

// insert new method with function signature and body after current function
final staticModifier = extractData.isStatic ? "static " : "";
final functionDefinition:String = '${staticModifier}function ${extractData.newMethodName}($parameterList)$returnTypeHint';
final functionName = makeFunctionName(extractData, context);
final functionDefinition:String = '${staticModifier}function $functionName($parameterList)$returnTypeHint';
final body:String = codeGen.makeBody();

changelist.addChange(context.what.fileName,
Expand All @@ -83,6 +84,19 @@ class ExtractMethod {
});
}

static function makeFunctionName(extractData:ExtractMethodData, context:RefactorContext):String {
final functionName = extractData.functionToken.access().firstChild().token;
if (functionName == null) {
return extractData.newMethodName;
}
final functionTypeParameter = functionName.access().firstOf(Binop(OpLt)).token;
if (functionTypeParameter == null) {
return extractData.newMethodName;
}
final pos = functionTypeParameter.getPos();
return extractData.newMethodName + RefactorHelper.extractText(context.converter, extractData.content, pos.min, pos.max);
}

static function makeExtractMethodData(context:CanRefactorContext):Null<ExtractMethodData> {
final fileContent = context.fileReader(context.what.fileName);
var content:String;
Expand Down
17 changes: 16 additions & 1 deletion test/refactor/refactor/RefactorExtractMethodTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class RefactorExtractMethodTest extends RefactorTestBase {
var edits:Array<TestEdit> = [
makeReplaceTestEdit("testcases/methods/Container.hx", "var result = processExtract(items, converter);\n", 108, 239, true),
makeInsertTestEdit("testcases/methods/Container.hx",
"function processExtract(items:Array<T>, converter:T -> String):Array<String> {\n"
"function processExtract<T>(items:Array<T>, converter:T -> String):Array<String> {\n"
+ "var result = new Array<String>();\n"
+ " for (item in items) {\n"
+ " var converted = converter(item);\n"
Expand Down Expand Up @@ -506,4 +506,19 @@ class RefactorExtractMethodTest extends RefactorTestBase {
];
checkRefactor(RefactorExtractMethod, {fileName: "testcases/methods/Matcher.hx", posStart: 76, posEnd: 284}, edits, async);
}

function testTypeProcessor(async:Async) {
var edits:Array<TestEdit> = [
makeReplaceTestEdit("testcases/methods/TypeProcessor.hx", "processExtract(item, compare);\n", 114, 221, true),
makeInsertTestEdit("testcases/methods/TypeProcessor.hx",
"function processExtract<T:Base, U:IComparable>(item:T, compare:U) {\n"
+ "var result = item.process();\n"
+ " if (compare.compareTo(result) > 0) {\n"
+ " item.update(compare.getValue());\n"
+ " }\n"
+ "}\n",
225, true),
];
checkRefactor(RefactorExtractMethod, {fileName: "testcases/methods/TypeProcessor.hx", posStart: 113, posEnd: 221}, edits, async);
}
}
20 changes: 20 additions & 0 deletions testcases/methods/TypeProcessor.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package testcases.methods;

class TypeProcessor {
function process<T:Base, U:IComparable>(item:T, compare:U) {
var result = item.process();
if (compare.compareTo(result) > 0) {
item.update(compare.getValue());
}
}
}

interface Base {
function process():Float;
function update(value:Float):Void;
}

interface IComparable {
function compareTo(value:Float):Float;
function getValue():Float;
}

0 comments on commit 94e8203

Please sign in to comment.