Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modified extract method to sort extracted method parameters by order …

…of decleration in original method, added space between variables, and fixed small issue when extracting from a method that is at the very top of the ruby source file.
  • Loading branch information...
commit 16e360a0440f19484fc033ecbad1c1560b98bd2a 1 parent b141919
@rossbeehler rossbeehler authored
View
60 features/extract_method.feature
@@ -35,7 +35,7 @@ Feature: Extract Method :RExtractMethod
@bar = foo
end
- def add(three,two)
+ def add(two, three)
two + three
end
@@ -44,7 +44,7 @@ Feature: Extract Method :RExtractMethod
two = 2
three = 3
four = two + two
- five = add(three,two)
+ five = add(two, three)
six = five + one
end
end
@@ -130,3 +130,59 @@ Feature: Extract Method :RExtractMethod
bowling.score.should == 0
end
end
+
+ """
+
+ @issue
+ Scenario: Parameters to extracted method should be in the order they are declared in original method when declared on separate lines.
+ Given I have the following code:
+ """
+ def originalMethod
+ x = 1
+ y = 2
+ z = x + y
+ end
+
+ """
+ When I select "x + y" and execute:
+ """
+ :RExtractMethod
+ """
+ And I fill in the parameter "add"
+ Then I should see:
+ """
+ def add(x, y)
+ x + y
+ end
+
+ def originalMethod
+ x = 1
+ y = 2
+ z = add(x, y)
+ end
+
+ """
+
+ @issue
+ Scenario: Parameters to extracted method should be in the order they are declared in original method when declared on same lines.
+ Given I have the following code:
+ """
+ def originalMethod(b, a)
+ c = a + b
+ end
+
+ """
+ When I select "a + b" and execute:
+ """
+ :RExtractMethod
+ """
+ And I fill in the parameter "add"
+ Then I should see:
+ """
+ def add(b, a)
+ a + b
+ end
+
+ def originalMethod(b, a)
+ c = add(b, a)
+ end
View
12 features/step_definitions/extract_method_steps.rb
@@ -23,3 +23,15 @@ def select_lines
add_return_key
add_to_commands command
end
+
+When /^I select \"x \+ y\" and execute:$/ do |command|
+ @commands = ":normal 4Gfxv$"
+ add_return_key
+ add_to_commands command
+end
+
+When /^I select \"a \+ b\" and execute:$/ do |command|
+ @commands = ":normal 2Gfav$"
+ add_return_key
+ add_to_commands command
+end
View
49 plugin/refactorings/general/extractmethod.vim
@@ -28,6 +28,8 @@ function! ExtractMethod() range
call insert(parameters,var)
endfor
+ let parameters = s:sort_parameters_by_declaration(parameters)
+
for var in selection_variables[0]
if index(post_selection_variables[1], var) != -1
call insert(retvals, var)
@@ -37,6 +39,49 @@ function! ExtractMethod() range
call s:em_insert_new_method(name, selection, parameters, retvals, block_start)
endfunction
+function! s:sort_parameters_by_declaration(parameters)
+ if (len(a:parameters) <= 1)
+ return a:parameters
+ endif
+ let pairs = s:build_parameter_declaration_position_pairs(a:parameters)
+ call sort(pairs, "s:sort_parameter_declaration_position_pairs")
+ return s:parameter_names_of(pairs)
+endfunction
+
+function! s:build_parameter_declaration_position_pairs(parameters)
+ let cursor_position = getpos(".")
+ let pairs = []
+
+ for parm in a:parameters
+ if (searchdecl(parm) == 0) " could find and position cursor at parameter declaration
+ call insert(pairs, [parm, getpos(".")])
+ else
+ call insert(pairs, [parm, getpos("$")]) " use end of file to sink to bottom
+ endif
+ call setpos(".",cursor_position)
+ endfor
+
+ return pairs
+endfunction
+
+function! s:sort_parameter_declaration_position_pairs(pair1, pair2)
+ let lineIndex = 1
+ let colIndex = 2
+ if (a:pair1[1][lineIndex] == a:pair2[1][lineIndex])
+ return a:pair1[1][colIndex] - a:pair2[1][colIndex]
+ else
+ return a:pair1[1][lineIndex] - a:pair2[1][lineIndex]
+ endif
+endfunction
+
+function! s:parameter_names_of(pairs)
+ let sorted_parameters = []
+ for pair in a:pairs
+ call extend(sorted_parameters, [pair[0]])
+ endfor
+ return sorted_parameters
+endfunction
+
function! s:ruby_determine_variables(block)
let tokens = s:ruby_tokenize(a:block)
let statements = s:ruby_identify_tokens(tokens)
@@ -196,7 +241,7 @@ function! s:em_insert_new_method(name, selection, parameters, retvals, block_sta
" Build new method text, split into a list for easy insertion
let method_params = ""
if len(a:parameters) > 0
- let method_params = "(" . join(a:parameters, ",") . ")"
+ let method_params = "(" . join(a:parameters, ", ") . ")"
endif
let method_retvals = ""
@@ -232,7 +277,7 @@ function! s:em_insert_new_method(name, selection, parameters, retvals, block_sta
normal V=
" Indent new codeblock
- exec "normal " . start_line_number . "GV" . len(method_lines) . "j="
+ exec "normal " . (start_line_number == 0 ? 1 : start_line_number) . "GV" . len(method_lines) . "j="
" Jump back again,
call setpos(".", cursor_position)
Please sign in to comment.
Something went wrong with that request. Please try again.