Skip to content

Commit 695285e

Browse files
authored
Merge pull request #2 from ThePrimeagen/master
Merge latest origin changes
2 parents c011a98 + f716790 commit 695285e

File tree

109 files changed

+443
-171
lines changed

Some content is hidden

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

109 files changed

+443
-171
lines changed

README.md

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ use {
5555
### Supported Languages<a name="supported-languages"></a>
5656

5757
Given that this is a work in progress, the languages supported for the
58-
operations listed below is **constantly changing**. As of now, these languages are
59-
supported (with individual support for each function may vary):
58+
operations listed below is **constantly changing**. As of now, these languages
59+
are supported (with individual support for each function may vary):
6060

6161
- TypeScript
6262
- JavaScript
@@ -72,22 +72,41 @@ supported (with individual support for each function may vary):
7272
- Support for various common refactoring operations
7373
- **106: Extract Function**
7474
- In visual mode, extracts the selected code to a separate function
75-
- Optionally prompts for function param types and return types (see [configuration for type prompt operations](#config-prompt))
75+
- Optionally prompts for function param types and return types (see
76+
[configuration for type prompt operations](#config-prompt))
7677
- Also possible to Extract Block.
77-
- Both Extract Function and Extract Block have the capability to extract to a separate file.
78+
- Both Extract Function and Extract Block have the capability to extract to
79+
a separate file.
7880
- **119: Extract Variable**
79-
- In visual mode, extracts occurences of a selected expression to its own variable, replacing occurences of that expression with the variable
81+
- In visual mode, extracts occurences of a selected expression to its own
82+
variable, replacing occurences of that expression with the variable
8083
- **123: Inline Variable**
8184
- Inverse of extract variable
8285
- Replaces all occurences of a variable with its value
83-
- Cursor must be on an identifier for this function to work
86+
- Can be used in normal mode or visual mode
87+
- Using this function in normal mode will automatically find the variable
88+
under the cursor and inline it
89+
- Using this function in visual mode will find the variable(s) in the
90+
visual selection.
91+
- If there is more than one variable in the selection, the plugin will
92+
prompt for which variable to inline,
93+
- If there is only one variable in the visual selection, it will
94+
automatically inline that variable
8495

8596
### Debug Features<a name="debug-features"></a>
8697

8798
- Also comes with various useful features for debugging
88-
- **Printf:** Automated insertion of print statement to mark the calling of a function
89-
- **Print var:** Automated insertion of print statement to print a variable at a given point in the code
90-
- **Cleanup:** Automated cleanup of all print statements generated by the plugin
99+
- **Printf:** Automated insertion of print statement to mark the calling of a
100+
function
101+
- **Print var:** Automated insertion of print statement to print a variable
102+
at a given point in the code. This map can be made with either visual or
103+
normal mode:
104+
- Using this function in visual mode will print out whatever is in the
105+
visual selection.
106+
- Passing `{ normal = true }` to the function will automatically find the variable
107+
under the cursor and print it from normal mode without needing visual mode at all
108+
- **Cleanup:** Automated cleanup of all print statements generated by the
109+
plugin
91110

92111
## Configuration<a name="configuration"></a>
93112

@@ -102,6 +121,37 @@ setup function.
102121
require('refactoring').setup({})
103122
```
104123

124+
Here are all the available options for the setup function and their defaults:
125+
126+
```lua
127+
require('refactoring').setup({
128+
prompt_func_return_type = {
129+
go = false,
130+
java = false,
131+
132+
cpp = false,
133+
c = false,
134+
h = false,
135+
hpp = false,
136+
cxx = false,
137+
},
138+
prompt_func_param_type = {
139+
go = false,
140+
java = false,
141+
142+
cpp = false,
143+
c = false,
144+
h = false,
145+
hpp = false,
146+
cxx = false,
147+
},
148+
printf_statements = {},
149+
print_var_statements = {},
150+
})
151+
```
152+
153+
See each of the sections below for details on each configuration option.
154+
105155
### Configuration for Refactoring Operations<a name="config-refactoring"></a>
106156

107157
#### Using Direct Remaps<a name="config-refactoring-direct"></a>
@@ -144,13 +194,14 @@ vim.api.nvim_set_keymap(
144194
)
145195
```
146196

147-
This remap should also be made in **visual mode**, or functionality for some refactors will not work properly.
197+
This remap should also be made in **visual mode**, or functionality for some
198+
refactors will not work properly.
148199

149200
#### Using Telescope<a name="config-refactoring-telescope"></a>
150201

151-
If you would prefer to use Telescope to choose a refactor when you're in visual mode,
152-
you can do so use using the **Telescope extension.** Here is an example config
153-
for this setup:
202+
If you would prefer to use Telescope to choose a refactor when you're in visual
203+
mode, you can do so use using the **Telescope extension.** Here is an example
204+
config for this setup:
154205

155206
```lua
156207
-- load refactoring Telescope extension
@@ -167,7 +218,8 @@ vim.api.nvim_set_keymap(
167218

168219
### Configuration for Debug Operations<a name="config-debug"></a>
169220

170-
Finally, you can configure remaps for the debug operations of this plugin like this:
221+
Finally, you can configure remaps for the debug operations of this plugin like
222+
this:
171223

172224
```lua
173225
-- You can also use below = true here to to change the position of the printf
@@ -179,7 +231,11 @@ vim.api.nvim_set_keymap(
179231
{ noremap = true }
180232
)
181233

182-
-- Print var: this remap should be made in visual mode
234+
-- Print var
235+
236+
-- Remap in normal mode and passing { normal = true } will automatically find the variable under the cursor and print it
237+
vim.api.nvim_set_keymap("n", "<leader>rv", ":lua require('refactoring').debug.print_var({ normal = true })<CR>", { noremap = true })
238+
-- Remap in visual mode will print whatever is in the visual selection
183239
vim.api.nvim_set_keymap("v", "<leader>rv", ":lua require('refactoring').debug.print_var({})<CR>", { noremap = true })
184240

185241
-- Cleanup function: this remap should be made in normal mode
@@ -193,7 +249,8 @@ functionalities.
193249

194250
##### Customizing Printf Statements<a name="config-debug-stringification-printf"></a>
195251

196-
You can add to the printf statements for any language by adding something like the below to your configuration:
252+
You can add to the printf statements for any language by adding something like
253+
the below to your configuration:
197254

198255
```lua
199256
require('refactoring').setup({
@@ -236,8 +293,10 @@ sequence like this: `%%s`. For an example custom print var statement, go to
236293
[this folder](lua/refactoring/tests/debug/print_var), select your language, and
237294
view `multiple-statements/print_var.config`.
238295

239-
**Note:** for either of these functions, if you have multiple statements
240-
(including the default), the plugin will prompt for which one should be inserted.
296+
**Note:** for either of these functions, if you have multiple custom
297+
statements, the plugin will prompt for which one should be inserted. If you
298+
just have one custom statement in your config, it will override the default
299+
automatically.
241300

242301
### Configuration for Type Prompt Operations<a name="config-prompt"></a>
243302

lua/refactoring/config/init.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ local default_formatting = {
3030

3131
local default_prompt_func_param_type = {
3232
go = false,
33+
java = false,
3334

34-
-- All of the cs
3535
cpp = false,
3636
c = false,
3737
h = false,
@@ -41,6 +41,13 @@ local default_prompt_func_param_type = {
4141

4242
local default_prompt_func_return_type = {
4343
go = false,
44+
java = false,
45+
46+
cpp = false,
47+
c = false,
48+
h = false,
49+
hpp = false,
50+
cxx = false,
4451
}
4552

4653
local default_printf_statements = {}

lua/refactoring/debug/cleanup.lua

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,27 @@ local function cleanup(bufnr, config)
2222
end
2323

2424
for row_num, line in ipairs(lines) do
25-
local region = Region:from_values(
26-
bufnr,
27-
row_num,
28-
1,
29-
row_num,
30-
100000
31-
)
25+
local region
26+
if row_num ~= 1 then
27+
region = Region:from_values(
28+
bufnr,
29+
row_num - 1,
30+
100000,
31+
row_num,
32+
100000
33+
)
34+
else
35+
print(
36+
"NOTE! Can't delete first line of file, leaving blank"
37+
)
38+
region = Region:from_values(
39+
bufnr,
40+
row_num,
41+
1,
42+
row_num,
43+
100000
44+
)
45+
end
3246

3347
if opts.printf then
3448
if

lua/refactoring/debug/print_var.lua

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,46 @@ local Region = require("refactoring.region")
44
local refactor_setup = require("refactoring.tasks.refactor_setup")
55
local post_refactor = require("refactoring.tasks.post_refactor")
66
local lsp_utils = require("refactoring.lsp_utils")
7+
local ts_utils = require("nvim-treesitter.ts_utils")
8+
local parsers = require("nvim-treesitter.parsers")
79
local debug_utils = require("refactoring.debug.debug_utils")
810
local ensure_code_gen = require("refactoring.tasks.ensure_code_gen")
911
local get_select_input = require("refactoring.get_select_input")
1012

11-
local function get_variable()
13+
local function get_variable(opts, point)
14+
if opts.normal then
15+
local bufnr = 0
16+
local root_lang_tree = parsers.get_parser(bufnr)
17+
local row = point.row
18+
local col = point.col
19+
local lang_tree = root_lang_tree:language_for_range({
20+
point.row,
21+
point.col,
22+
point.row,
23+
point.col,
24+
})
25+
for _, tree in ipairs(lang_tree:trees()) do
26+
local root = tree:root()
27+
if root and ts_utils.is_in_node_range(root, row, col) then
28+
root:named_descendant_for_range(row, col, row, col)
29+
end
30+
end
31+
local node = ts_utils.get_node_at_cursor()
32+
local filetype = vim.bo[bufnr].filetype
33+
-- TODO: Can we do something with treesitter files here?
34+
if filetype == "php" then
35+
return "$" .. ts_utils.get_node_text(node)[1]
36+
end
37+
return ts_utils.get_node_text(node)[1]
38+
end
1239
local variable_region = Region:from_current_selection()
1340
return variable_region:get_text()[1]
1441
end
1542

43+
local function get_indent_amount(refactor)
44+
return refactor.whitespace.cursor / refactor.whitespace.tabstop
45+
end
46+
1647
local function printDebug(bufnr, config)
1748
return Pipeline
1849
:from_task(refactor_setup(bufnr, config))
@@ -27,8 +58,20 @@ local function printDebug(bufnr, config)
2758
opts.below = true
2859
point.col = opts.below and 100000 or 1
2960

61+
if opts.normal == nil then
62+
opts.normal = false
63+
end
64+
3065
-- Get variable text
31-
local variable = get_variable()
66+
local variable = get_variable(opts, point)
67+
local indent
68+
if refactor.ts.allows_indenting_task then
69+
local indent_amount = get_indent_amount(refactor)
70+
indent = refactor.code.indent({
71+
indent_width = refactor.whitespace.tabstop,
72+
indent_amount = indent_amount,
73+
})
74+
end
3275

3376
local debug_path = debug_utils.get_debug_path(refactor, point)
3477
local prefix = string.format("%s %s:", debug_path, variable)
@@ -42,17 +85,17 @@ local function printDebug(bufnr, config)
4285
local print_var_statement
4386

4487
if custom_print_var_statements then
45-
local all_statements = vim.list_extend(
46-
default_print_var_statement,
47-
custom_print_var_statements
48-
)
49-
print_var_statement = get_select_input(
50-
all_statements,
51-
"print_var: Select a statement to insert:",
52-
function(item)
53-
return item
54-
end
55-
)
88+
if #custom_print_var_statements > 1 then
89+
print_var_statement = get_select_input(
90+
custom_print_var_statements,
91+
"print_var: Select a statement to insert:",
92+
function(item)
93+
return item
94+
end
95+
)
96+
else
97+
print_var_statement = custom_print_var_statements[1]
98+
end
5699
else
57100
print_var_statement = default_print_var_statement[1]
58101
end
@@ -64,12 +107,23 @@ local function printDebug(bufnr, config)
64107
}
65108

66109
local print_statement = refactor.code.print_var(print_var_opts)
67-
.. refactor.code.comment("__AUTO_GENERATED_PRINT_VAR__")
110+
111+
local statement
112+
if indent ~= nil then
113+
local temp = {}
114+
temp[1] = indent
115+
temp[2] = print_statement
116+
statement = table.concat(temp, "")
117+
else
118+
statement = print_statement
119+
end
68120

69121
refactor.text_edits = {
70122
lsp_utils.insert_new_line_text(
71123
Region:from_point(point),
72-
print_statement,
124+
statement
125+
.. " "
126+
.. refactor.code.comment("__AUTO_GENERATED_PRINT_VAR__"),
73127
opts
74128
),
75129
}

0 commit comments

Comments
 (0)