Skip to content

Commit 50cc6e4

Browse files
authored
Merge pull request #326 from olimorris/master
Updated support for Ruby
2 parents f716790 + 0a8faff commit 50cc6e4

File tree

68 files changed

+484
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+484
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions

lua/refactoring/code_generation/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local c = require("refactoring.code_generation.langs.c")
77
local python = require("refactoring.code_generation.langs.python")
88
local php = require("refactoring.code_generation.langs.php")
99
local java = require("refactoring.code_generation.langs.java")
10+
local ruby = require("refactoring.code_generation.langs.ruby")
1011

1112
local M = {
1213
javascript = javascript,
@@ -18,6 +19,7 @@ local M = {
1819
python = python,
1920
php = php,
2021
java = java,
22+
ruby = ruby,
2123

2224
-- TODO: Take this and make all code generation subclassed.
2325
-- This should just be a function of code generation.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
local code_utils = require("refactoring.code_generation.utils")
2+
local code_gen_indent = require("refactoring.code_generation.indent")
3+
4+
local function ruby_function_with_params(opts)
5+
return string.format(
6+
[[
7+
def %s(%s)
8+
%s
9+
end
10+
11+
]],
12+
opts.name,
13+
table.concat(opts.args, ", "),
14+
code_utils.stringify_code(opts.body)
15+
)
16+
end
17+
18+
local function ruby_function_no_params(opts)
19+
return string.format(
20+
[[
21+
def %s
22+
%s
23+
end
24+
25+
]],
26+
opts.name,
27+
code_utils.stringify_code(opts.body)
28+
)
29+
end
30+
31+
local function ruby_function(opts)
32+
if not next(opts.args) then
33+
return ruby_function_no_params(opts)
34+
else
35+
return ruby_function_with_params(opts)
36+
end
37+
end
38+
39+
local function ruby_class_function(opts)
40+
return ruby_function(opts)
41+
end
42+
43+
local indent_char = " "
44+
45+
local ruby = {
46+
comment = function(statement)
47+
return string.format("# %s", statement)
48+
end,
49+
constant = function(opts)
50+
return string.format("%s = %s\n", opts.name, opts.value)
51+
end,
52+
["function"] = function(opts)
53+
return ruby_function(opts)
54+
end,
55+
function_return = function(opts)
56+
return ruby_function(opts)
57+
end,
58+
["return"] = function(code)
59+
return string.format("%s", code_utils.stringify_code(code))
60+
end,
61+
call_function = function(opts)
62+
return string.format("%s(%s)", opts.name, table.concat(opts.args, ", "))
63+
end,
64+
class_function = function(opts)
65+
return ruby_class_function(opts)
66+
end,
67+
class_function_return = function(opts)
68+
return ruby_class_function(opts)
69+
end,
70+
call_class_function = function(opts)
71+
return string.format("%s(%s)", opts.name, table.concat(opts.args, ", "))
72+
end,
73+
terminate = function(code)
74+
return code
75+
end,
76+
-- This is for returing multiple arguments from a function
77+
-- @param names string|table
78+
pack = function(opts)
79+
return code_utils.returnify(opts, "%s")
80+
end,
81+
indent_char_length = function(first_line)
82+
return code_gen_indent.indent_char_length(first_line, indent_char)
83+
end,
84+
indent_char = function()
85+
return indent_char
86+
end,
87+
indent = function(opts)
88+
return code_gen_indent.indent(opts, indent_char)
89+
end,
90+
print = function(opts)
91+
return string.format(opts.statement, opts.content)
92+
end,
93+
default_printf_statement = function()
94+
return { 'puts "%s"' }
95+
end,
96+
default_print_var_statement = function()
97+
return { 'puts "%s #{%s}"' }
98+
end,
99+
print_var = function(opts)
100+
return string.format(opts.statement, opts.prefix, opts.var)
101+
end,
102+
}
103+
104+
return ruby

lua/refactoring/config/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ local default_formatting = {
2121
-- Python needs tons of work to become correct.
2222
python = {},
2323

24+
ruby = {},
25+
2426
default = {
2527
cmd = nil, -- format.lua checks to see if the command is nil or not
2628
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1
2+
1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
def main
3+
puts('ruby is awesome')
4+
i = 3
5+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
puts('test') # __AUTO_GENERATED_PRINTF__
2+
def main
3+
puts('main') # __AUTO_GENERATED_PRINTF__
4+
puts('ruby is awesome')
5+
i = 3
6+
puts(' i: {str(i)}') # __AUTO_GENERATED_PRINT_VAR__
7+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:4
2+
:execute "norm! wve\<Esc>"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts "custom print_var %s #{%s}"

0 commit comments

Comments
 (0)