Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated support for Ruby #326

Merged
merged 32 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c8d57c8
start adding support for Ruby
olimorris May 11, 2022
a9d1c12
start adding Ruby treesitter queries
olimorris May 15, 2022
d1f38ea
Merge branch 'ThePrimeagen:master' into master
olimorris May 15, 2022
0a669c8
fix: ruby extract.start
olimorris May 16, 2022
c080d91
complete simple-function refactor
olimorris May 18, 2022
5066051
refactor with-comments
olimorris May 18, 2022
ce71335
chore: remove with-comments test for now
olimorris May 20, 2022
8a1560e
wip: ruby extract class method
olimorris May 20, 2022
e102a55
feat: ruby extract function in a class
olimorris May 20, 2022
89bf376
chore: cleanup ruby treesitter
olimorris May 20, 2022
399f562
wip extract block
olimorris May 21, 2022
4ee4e79
wip #1 extract-block
olimorris May 24, 2022
8e24ae9
add comment block to code generator
olimorris May 25, 2022
bed915d
add back with-comment tests
olimorris May 25, 2022
c83567e
wip: extract block almost working
olimorris Jun 27, 2022
5871cb6
wip: extract function with comments
olimorris Jun 27, 2022
c011a98
feat: 119 extract variable
olimorris Jun 27, 2022
1faa4c7
update README.md for Ruby support
olimorris Jun 27, 2022
695285e
Merge pull request #2 from ThePrimeagen/master
olimorris Jun 27, 2022
c62efaf
Merge branch 'master' of https://github.com/olimorris/refactoring.nvim
olimorris Jun 27, 2022
d4e77fd
wip: add another extract_block example
olimorris Jun 27, 2022
6bede65
remove comment from function_body
olimorris Jun 27, 2022
615eccd
feat: add simple debugging
olimorris Jun 27, 2022
90078f9
rename debug to simple-function
olimorris Jun 27, 2022
21ae5da
feat: add debug with custom config
olimorris Jun 27, 2022
9df6a03
feat: add modules to debug
olimorris Jun 27, 2022
4ebbbe5
feat: add debug cleanup tests
olimorris Jun 27, 2022
8d7536c
feat: add debug print_var
olimorris Jun 27, 2022
344bbf9
fix: puts formatting
olimorris Jun 27, 2022
ed70898
feat: add support for 123
olimorris Jun 29, 2022
5da9076
fix: formatting for 123 tests
olimorris Jun 29, 2022
0a8faff
fix: debug print_var
olimorris Jun 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ are supported (with individual support for each function may vary):
- Python
- Java
- PHP
- Ruby

### Refactoring Features<a name="refactoring-features"></a>

Expand Down
2 changes: 2 additions & 0 deletions lua/refactoring/code_generation/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local c = require("refactoring.code_generation.langs.c")
local python = require("refactoring.code_generation.langs.python")
local php = require("refactoring.code_generation.langs.php")
local java = require("refactoring.code_generation.langs.java")
local ruby = require("refactoring.code_generation.langs.ruby")

local M = {
javascript = javascript,
Expand All @@ -18,6 +19,7 @@ local M = {
python = python,
php = php,
java = java,
ruby = ruby,

-- TODO: Take this and make all code generation subclassed.
-- This should just be a function of code generation.
Expand Down
104 changes: 104 additions & 0 deletions lua/refactoring/code_generation/langs/ruby.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
local code_utils = require("refactoring.code_generation.utils")
local code_gen_indent = require("refactoring.code_generation.indent")

local function ruby_function_with_params(opts)
return string.format(
[[
def %s(%s)
%s
end

]],
opts.name,
table.concat(opts.args, ", "),
code_utils.stringify_code(opts.body)
)
end

local function ruby_function_no_params(opts)
return string.format(
[[
def %s
%s
end

]],
opts.name,
code_utils.stringify_code(opts.body)
)
end

local function ruby_function(opts)
if not next(opts.args) then
return ruby_function_no_params(opts)
else
return ruby_function_with_params(opts)
end
end

local function ruby_class_function(opts)
return ruby_function(opts)
end

local indent_char = " "

local ruby = {
comment = function(statement)
return string.format("# %s", statement)
end,
constant = function(opts)
return string.format("%s = %s\n", opts.name, opts.value)
end,
["function"] = function(opts)
return ruby_function(opts)
end,
function_return = function(opts)
return ruby_function(opts)
end,
["return"] = function(code)
return string.format("%s", code_utils.stringify_code(code))
end,
call_function = function(opts)
return string.format("%s(%s)", opts.name, table.concat(opts.args, ", "))
end,
class_function = function(opts)
return ruby_class_function(opts)
end,
class_function_return = function(opts)
return ruby_class_function(opts)
end,
call_class_function = function(opts)
return string.format("%s(%s)", opts.name, table.concat(opts.args, ", "))
end,
terminate = function(code)
return code
end,
-- This is for returing multiple arguments from a function
-- @param names string|table
pack = function(opts)
return code_utils.returnify(opts, "%s")
end,
indent_char_length = function(first_line)
return code_gen_indent.indent_char_length(first_line, indent_char)
end,
indent_char = function()
return indent_char
end,
indent = function(opts)
return code_gen_indent.indent(opts, indent_char)
end,
print = function(opts)
return string.format(opts.statement, opts.content)
end,
default_printf_statement = function()
return { 'puts "%s"' }
end,
default_print_var_statement = function()
return { 'puts "%s #{%s}"' }
end,
print_var = function(opts)
return string.format(opts.statement, opts.prefix, opts.var)
end,
}

return ruby
2 changes: 2 additions & 0 deletions lua/refactoring/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ local default_formatting = {
-- Python needs tons of work to become correct.
python = {},

ruby = {},

default = {
cmd = nil, -- format.lua checks to see if the command is nil or not
},
Expand Down
1 change: 1 addition & 0 deletions lua/refactoring/tests/debug/cleanup/rb/cleanup.commands
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
1
5 changes: 5 additions & 0 deletions lua/refactoring/tests/debug/cleanup/rb/cleanup.expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def main
puts('ruby is awesome')
i = 3
end
7 changes: 7 additions & 0 deletions lua/refactoring/tests/debug/cleanup/rb/cleanup.start.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
puts('test') # __AUTO_GENERATED_PRINTF__
def main
puts('main') # __AUTO_GENERATED_PRINTF__
puts('ruby is awesome')
i = 3
puts(' i: {str(i)}') # __AUTO_GENERATED_PRINT_VAR__
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:4
:execute "norm! wve\<Esc>"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "custom print_var %s #{%s}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SimpleClass
def simple_function(a)
test = 1
test_other = 11
puts "custom print_var SimpleClass#simple_function test_other: #{test_other}" # __AUTO_GENERATED_PRINT_VAR__
for x in test..test_other do
puts x, a
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SimpleClass
def simple_function(a)
test = 1
test_other = 11
for x in test..test_other do
puts x, a
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:4
:execute "norm! we\<Esc>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SimpleClass
def simple_function(a)
test = 1
test_other = 11
puts "SimpleClass#simple_function test_other: #{test_other}" # __AUTO_GENERATED_PRINT_VAR__
for x in test..test_other do
puts x, a
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SimpleClass
def simple_function(a)
test = 1
test_other = 11
for x in test..test_other do
puts x, a
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:4
:execute "norm! wve\<Esc>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SimpleClass
def simple_function(a)
test = 1
test_other = 11
puts "SimpleClass#simple_function test_other: #{test_other}" # __AUTO_GENERATED_PRINT_VAR__
for x in test..test_other do
puts x, a
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SimpleClass
def simple_function(a)
test = 1
test_other = 11
for x in test..test_other do
puts x, a
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:3
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module SimpleModule
class SimpleClass
def simple_function(a)
puts "SimpleModule#SimpleClass#simple_function" # __AUTO_GENERATED_PRINTF__
test = 1
test_other = 11
for x in test..test_other do
puts x, a
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SimpleModule
class SimpleClass
def simple_function(a)
test = 1
test_other = 11
for x in test..test_other do
puts x, a
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "debug path %s"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def main
puts "debug path main" # __AUTO_GENERATED_PRINTF__
pass
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def main
pass
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:3
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SimpleClass
def simple_function(a)
test = 1
puts "SimpleClass#simple_function" # __AUTO_GENERATED_PRINTF__
test_other = 11
for x in test..test_other do
puts x, a
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SimpleClass
def simple_function(a)
test = 1
test_other = 11
for x in test..test_other do
puts x, a
end
end
end
1 change: 1 addition & 0 deletions lua/refactoring/tests/debug_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ local function set_config_options(filename_prefix, filename_extension)
["ts"] = "typescript",
["js"] = "javascript",
["py"] = "python",
["rb"] = "ruby",
}

-- get the real filetype from the above table if possible
Expand Down
2 changes: 2 additions & 0 deletions lua/refactoring/tests/refactor/106/rb/class/extract.commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:6
:execute "norm! Vjj\<Esc>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
13
5
15 changes: 15 additions & 0 deletions lua/refactoring/tests/refactor/106/rb/class/extract.expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class SimpleClass
def foo_bar(a, test, test_other)
[test, test_other].each do |v|
puts "#{a} #{v}"
end
end


def simple_function(a)
test = 1
test_other = 11

foo_bar(a, test, test_other)
end
end
1 change: 1 addition & 0 deletions lua/refactoring/tests/refactor/106/rb/class/extract.inputs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo_bar
10 changes: 10 additions & 0 deletions lua/refactoring/tests/refactor/106/rb/class/extract.start.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SimpleClass
def simple_function(a)
test = 1
test_other = 11

[test, test_other].each do |v|
puts "#{a} #{v}"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:5
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

def foo_bar(a)
test = 1
test_other = 11

[test, test_other].each do |v|
puts "#{a} #{v}"
end
end

def simple_function(a)
foo_bar(a)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo_bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def simple_function(a)
test = 1
test_other = 11

[test, test_other].each do |v|
puts "#{a} #{v}"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:5
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
3
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

def foo_bar(test)
test_other = 11

[test, test_other].each do |v|
puts "#{v}"
end
end

def simple_function
test = 1
foo_bar(test)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo_bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def simple_function
test = 1
test_other = 11

[test, test_other].each do |v|
puts "#{v}"
end
end
Loading