Skip to content

Commit 4e3b1c3

Browse files
authored
Merge pull request #1 from ThePrimeagen/stylua
feat: Add stylua
2 parents d35f43c + ff54b59 commit 4e3b1c3

File tree

9 files changed

+146
-67
lines changed

9 files changed

+146
-67
lines changed

lua/refactoring/106.lua

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ local REFACTORING_OPTIONS = {
1212
extract_function = function(opts)
1313
return {
1414
create = table.concat(vim.tbl_flatten({
15-
string.format("local function %s(%s)", opts.name, table.concat(opts.args, ", ")),
15+
string.format(
16+
"local function %s(%s)",
17+
opts.name,
18+
table.concat(opts.args, ", ")
19+
),
1620
opts.body,
1721
"end",
18-
""
22+
"",
1923
}), "\n"),
2024

21-
call = string.format("%s(%s)", opts.name, table.concat(opts.args, ", ")),
25+
call = string.format(
26+
"%s(%s)",
27+
opts.name,
28+
table.concat(opts.args, ", ")
29+
),
2230
}
2331
end,
24-
}
25-
}
32+
},
33+
},
2634
}
2735

2836
local function get_selection_range()
@@ -42,24 +50,34 @@ local function vim_range_to_ts_range(start_row, start_col, end_row, end_col)
4250
end
4351

4452
-- 106
45-
local function get_text_edits(selected_local_references, end_row, lang, start_col, start_row, end_col, scope_range, function_name)
53+
local function get_text_edits(
54+
selected_local_references,
55+
end_row,
56+
lang,
57+
start_col,
58+
start_row,
59+
end_col,
60+
scope_range,
61+
function_name
62+
)
4663
-- local declaration within the selection range.
4764
local lsp_text_edits = {}
48-
local extract_function = REFACTORING_OPTIONS.code_generation[lang].extract_function({
49-
args = vim.tbl_keys(selected_local_references),
50-
body = vim.api.nvim_buf_get_lines(0, start_row, end_row, false),
51-
name = function_name,
52-
})
65+
local extract_function =
66+
REFACTORING_OPTIONS.code_generation[lang].extract_function({
67+
args = vim.tbl_keys(selected_local_references),
68+
body = vim.api.nvim_buf_get_lines(0, start_row, end_row, false),
69+
name = function_name,
70+
})
5371
table.insert(lsp_text_edits, {
5472
range = scope_range,
55-
newText = string.format("\n%s", extract_function.create)
73+
newText = string.format("\n%s", extract_function.create),
5674
})
5775
table.insert(lsp_text_edits, {
5876
range = {
59-
start = {line = start_row, character = start_col},
60-
["end"] = {line = end_row, character = end_col}
77+
start = { line = start_row, character = start_col },
78+
["end"] = { line = end_row, character = end_col },
6179
},
62-
newText = string.format("\n%s", extract_function.call)
80+
newText = string.format("\n%s", extract_function.call),
6381
})
6482
return lsp_text_edits
6583
end
@@ -96,17 +114,38 @@ REFACTORING.extract = function(bufnr)
96114
local lang = vim.bo.filetype
97115
local start_row, start_col, end_row, end_col = get_selection_range()
98116
local ts_start_row, ts_start_col, ts_end_row, ts_end_col =
99-
vim_range_to_ts_range(start_row, start_col, end_row, end_col)
117+
vim_range_to_ts_range(
118+
start_row,
119+
start_col,
120+
end_row,
121+
end_col
122+
)
100123
local root = utils.get_root(lang)
101-
local scope = utils.get_scope_over_selection(root, start_row, start_col, end_row + 1, end_col, lang)
124+
local scope = utils.get_scope_over_selection(
125+
root,
126+
start_row,
127+
start_col,
128+
end_row + 1,
129+
end_col,
130+
lang
131+
)
102132

103133
if scope == nil then
104134
error("Scope is nil")
105135
end
106136

107137
local local_defs = vim.tbl_filter(function(node)
108-
return not utils.range_contains_node(node, ts_start_row, ts_start_col, ts_end_row, ts_end_col)
109-
end, utils.get_locals_defs(scope, lang))
138+
return not utils.range_contains_node(
139+
node,
140+
ts_start_row,
141+
ts_start_col,
142+
ts_end_row,
143+
ts_end_col
144+
)
145+
end, utils.get_locals_defs(
146+
scope,
147+
lang
148+
))
110149

111150
local function_args = utils.get_function_args(scope, lang)
112151
local local_def_map = get_local_definitions(local_defs, function_args)
@@ -115,8 +154,15 @@ REFACTORING.extract = function(bufnr)
115154

116155
for _, local_ref in pairs(local_references) do
117156
local local_name = ts_utils.get_node_text(local_ref)[1]
118-
if utils.range_contains_node(local_ref, ts_start_row, ts_start_col, ts_end_row, ts_end_col) and
119-
local_def_map[local_name] then
157+
if
158+
utils.range_contains_node(
159+
local_ref,
160+
ts_start_row,
161+
ts_start_col,
162+
ts_end_row,
163+
ts_end_col
164+
) and local_def_map[local_name]
165+
then
120166
selected_local_references[local_name] = true
121167
end
122168
end
@@ -129,7 +175,16 @@ REFACTORING.extract = function(bufnr)
129175
local function_name = vim.fn.input("106: Extract Function Name > ")
130176

131177
-- TODO: Polor, could you also make the variable that is returned the first
132-
local text_edits = get_text_edits(selected_local_references, end_row, lang, start_col, start_row, end_col, scope_range, function_name)
178+
local text_edits = get_text_edits(
179+
selected_local_references,
180+
end_row,
181+
lang,
182+
start_col,
183+
start_row,
184+
end_col,
185+
scope_range,
186+
function_name
187+
)
133188
vim.lsp.util.apply_text_edits(text_edits, 0)
134189
end
135190

lua/refactoring/106_spec.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ describe("Refactoring", function()
1717
vim.cmd(":norm! ggVjj")
1818

1919
eq(0, vim.fn.col("'<"), "Selections start at 0")
20-
eq(#"foo = foo + 5 + foo" + 1, vim.fn.col("'>"), "Selection stops + 1 after last line.")
20+
eq(
21+
#"foo = foo + 5 + foo" + 1,
22+
vim.fn.col("'>"),
23+
"Selection stops + 1 after last line."
24+
)
2125

2226
-- print("COL", vim.fn.col("'<"), vim.fn.col("'>"))
2327
end)
2428
end)
25-
26-
27-

lua/refactoring/dev.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ M.reload = function()
44
end
55

66
return M
7-

lua/refactoring/intersect.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
-- Array<node_wrapper>
32
local intersect_nodes = function(nodes, row, col)
43
local found = {}
@@ -14,4 +13,3 @@ local intersect_nodes = function(nodes, row, col)
1413
end
1514
return found
1615
end
17-

lua/refactoring/query.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
function find_identifiers()
22
end
3-
4-

lua/refactoring/refactoring.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
2-

lua/refactoring/utils.lua

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ M.get_bounded_query = function(query, lang, startR, stopR)
2727
table.insert(out, node)
2828
end)
2929
end
30-
return out;
30+
return out
3131
end
3232

3333
local refactor_constants = {
3434
lua = {
3535
scope = {
3636
["function"] = true,
37-
["function_definition"] = true
38-
}
39-
}
37+
["function_definition"] = true,
38+
},
39+
},
4040
}
4141

4242
-- determines if a contains node b.
@@ -48,8 +48,8 @@ function M.node_contains(a, b)
4848
end
4949

5050
local start_row, start_col, end_row, end_col = b:range()
51-
return ts_utils.is_in_node_range(a, start_row, start_col) and
52-
ts_utils.is_in_node_range(a, end_row, end_col)
51+
return ts_utils.is_in_node_range(a, start_row, start_col)
52+
and ts_utils.is_in_node_range(a, end_row, end_col)
5353
end
5454

5555
-- determines if a node exists within a range. Imagine a range selection
@@ -59,7 +59,8 @@ end
5959
-- @param a the containing node
6060
-- @param b the node to be contained
6161
M.range_contains_node = function(node, start_row, start_col, end_row, end_col)
62-
local node_start_row, node_start_col, node_end_row, node_end_col = node:range()
62+
local node_start_row, node_start_col, node_end_row, node_end_col =
63+
node:range()
6364

6465
-- There are five possible conditions
6566
-- 1. node start/end row are contained exclusively within the range.
@@ -77,11 +78,10 @@ M.range_contains_node = function(node, start_row, start_col, end_row, end_col)
7778
if start_row < node_start_row and end_row > node_end_row then
7879
return true
7980
elseif start_row == end_row then
80-
return start_row == node_start_row and
81-
end_row == node_end_row and
82-
start_col <= node_start_col and
83-
end_col >= node_end_col
84-
81+
return start_row == node_start_row
82+
and end_row == node_end_row
83+
and start_col <= node_start_col
84+
and end_col >= node_end_col
8585
elseif start_row == node_start_row and start_row == node_end_row then
8686
return start_col <= node_start_col
8787
elseif end_row == node_start_row and end_row == node_end_row then
@@ -93,23 +93,27 @@ M.range_contains_node = function(node, start_row, start_col, end_row, end_col)
9393
return false
9494
end
9595

96-
M.get_scope_over_selection = function(root, start_line, start_col, end_line, end_col, lang)
97-
local start_scope = M.get_scope(root, start_line, start_col, lang)
98-
local end_scope = M.get_scope(root, end_line, end_col, lang)
96+
M.get_scope_over_selection =
97+
function(root, start_line, start_col, end_line, end_col, lang)
98+
local start_scope = M.get_scope(root, start_line, start_col, lang)
99+
local end_scope = M.get_scope(root, end_line, end_col, lang)
99100

100-
if start_scope ~= end_scope then
101-
error("Selection spans over two scopes, cannot determine scope")
102-
end
101+
if start_scope ~= end_scope then
102+
error("Selection spans over two scopes, cannot determine scope")
103+
end
103104

104-
return start_scope
105-
end
105+
return start_scope
106+
end
106107

107108
M.get_scope = function(root, line, col, lang)
108109
local function_scopes = {}
109110
local query = vim.treesitter.get_query(lang, "locals")
110111

111112
for id, n, _ in query:iter_captures(root, 0, 0, -1) do
112-
if query.captures[id] == "scope" and refactor_constants[lang].scope[n:type()] then
113+
if
114+
query.captures[id] == "scope"
115+
and refactor_constants[lang].scope[n:type()]
116+
then
113117
table.insert(function_scopes, n)
114118
end
115119
end
@@ -120,9 +124,10 @@ M.get_scope = function(root, line, col, lang)
120124
-- should a scope that contains another scope but terminates at the
121125
-- same point be the outer or inner? Should potentially be considered
122126
-- a list of scopes...
123-
if ts_utils.is_in_node_range(scope, line, col) and
124-
(out == nil or M.node_contains(out, scope)) then
125-
127+
if
128+
ts_utils.is_in_node_range(scope, line, col)
129+
and (out == nil or M.node_contains(out, scope))
130+
then
126131
out = scope
127132
end
128133
end
@@ -133,7 +138,9 @@ end
133138
local function get_refactoring_query(lang)
134139
local query = vim.treesitter.get_query(lang, "refactoring")
135140
if not query then
136-
error("refactoring not supported in this language. Please provide a queries/<lang>/refactoring.scm")
141+
error(
142+
"refactoring not supported in this language. Please provide a queries/<lang>/refactoring.scm"
143+
)
137144
end
138145
return query
139146
end
@@ -142,7 +149,10 @@ local function pluck_by_capture(scope, lang, query, capture_name)
142149
local local_defs = {}
143150
local root = M.get_root(lang)
144151
for id, node, _ in query:iter_captures(root, 0, 0, -1) do
145-
if query.captures[id] == capture_name and M.node_contains(scope, node) then
152+
if
153+
query.captures[id] == capture_name
154+
and M.node_contains(scope, node)
155+
then
146156
table.insert(local_defs, node)
147157
end
148158
end
@@ -151,15 +161,30 @@ local function pluck_by_capture(scope, lang, query, capture_name)
151161
end
152162

153163
M.get_function_args = function(scope, lang)
154-
return pluck_by_capture(scope, lang, get_refactoring_query(lang), "definition.function_argument")
164+
return pluck_by_capture(
165+
scope,
166+
lang,
167+
get_refactoring_query(lang),
168+
"definition.function_argument"
169+
)
155170
end
156171

157172
M.get_locals_defs = function(scope, lang)
158-
return pluck_by_capture(scope, lang, get_refactoring_query(lang), "definition.local_var")
173+
return pluck_by_capture(
174+
scope,
175+
lang,
176+
get_refactoring_query(lang),
177+
"definition.local_var"
178+
)
159179
end
160180

161-
M.get_all_identifiers = function(scope, lang)
162-
return pluck_by_capture(scope, lang, vim.treesitter.get_query(lang, "locals"), "reference")
181+
M.get_all_identifiers = function(scope, lang)
182+
return pluck_by_capture(
183+
scope,
184+
lang,
185+
vim.treesitter.get_query(lang, "locals"),
186+
"reference"
187+
)
163188
end
164189

165190
-- is there a better way?
@@ -168,7 +193,7 @@ M.range_to_table = function(node)
168193
return "range nil"
169194
end
170195
local a, b, c, d = node:range()
171-
return {a, b, c, d}
196+
return { a, b, c, d }
172197
end
173198

174199
return M

lua/refactoring/vim-helpers.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
21
local M = {}
32

43
-- TODO: Probably do it this way... seems mucho better
54
-- https://github.com/neovim/neovim/blob/6f48c018b526a776e38e94f58769c30141de9e0c/runtime/lua/vim/lsp/util.lua#L243
65
function M.move_text(buf, from_start_row, from_end_row, to_start_row)
76
if from_start_row <= to_start_row and from_end_row >= to_start_row then
8-
error("vim-helpers#move_text has been provided a destination within the removal location. Impossible!")
7+
error(
8+
"vim-helpers#move_text has been provided a destination within the removal location. Impossible!"
9+
)
910
end
1011

1112
if not vim.api.nvim_buf_is_valid(buf) then
@@ -14,7 +15,8 @@ function M.move_text(buf, from_start_row, from_end_row, to_start_row)
1415

1516
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
1617
for idx = from_start_row, from_end_row do
17-
local to_remove_idx = from_start_row > to_start_row and idx or from_start_row
18+
local to_remove_idx = from_start_row > to_start_row and idx
19+
or from_start_row
1820
local line = lines[to_remove_idx]
1921

2022
table.remove(lines, to_remove_idx)

stylua.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
column_width = 80
2+
indent_type = "Spaces"
3+
indent_width = 4

0 commit comments

Comments
 (0)