diff --git a/src/refactor/refactor/ExtractMethod.hx b/src/refactor/refactor/ExtractMethod.hx index 116a6fe..02f049d 100644 --- a/src/refactor/refactor/ExtractMethod.hx +++ b/src/refactor/refactor/ExtractMethod.hx @@ -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, @@ -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 { final fileContent = context.fileReader(context.what.fileName); var content:String; diff --git a/test/refactor/refactor/RefactorExtractMethodTest.hx b/test/refactor/refactor/RefactorExtractMethodTest.hx index 7bc797e..766d517 100644 --- a/test/refactor/refactor/RefactorExtractMethodTest.hx +++ b/test/refactor/refactor/RefactorExtractMethodTest.hx @@ -435,7 +435,7 @@ class RefactorExtractMethodTest extends RefactorTestBase { var edits:Array = [ makeReplaceTestEdit("testcases/methods/Container.hx", "var result = processExtract(items, converter);\n", 108, 239, true), makeInsertTestEdit("testcases/methods/Container.hx", - "function processExtract(items:Array, converter:T -> String):Array {\n" + "function processExtract(items:Array, converter:T -> String):Array {\n" + "var result = new Array();\n" + " for (item in items) {\n" + " var converted = converter(item);\n" @@ -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 = [ + makeReplaceTestEdit("testcases/methods/TypeProcessor.hx", "processExtract(item, compare);\n", 114, 221, true), + makeInsertTestEdit("testcases/methods/TypeProcessor.hx", + "function processExtract(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); + } } diff --git a/testcases/methods/TypeProcessor.hx b/testcases/methods/TypeProcessor.hx new file mode 100644 index 0000000..c856cee --- /dev/null +++ b/testcases/methods/TypeProcessor.hx @@ -0,0 +1,20 @@ +package testcases.methods; + +class TypeProcessor { + function process(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; +}