Skip to content

Commit

Permalink
refactor!:
Browse files Browse the repository at this point in the history
- vim.tbl_filter/map -> vim.iter
- better type annotations
- Specify Neovim 0.10 as requirement instead of nightly
  • Loading branch information
TheLeoP committed Jul 21, 2024
1 parent 8ef9cfb commit 6c23706
Show file tree
Hide file tree
Showing 28 changed files with 293 additions and 404 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h6>'If I use an environment that has good automated refactorings, I can trust those refactorings' - Martin Fowler</h6>

[![Lua](https://img.shields.io/badge/Lua-blue.svg?style=for-the-badge&logo=lua)](http://www.lua.org)
[![Neovim Nightly](https://img.shields.io/badge/Neovim%20Nightly-green.svg?style=for-the-badge&logo=neovim)](https://neovim.io)
[![Neovim 0.10](https://img.shields.io/badge/Neovim%200.10-green.svg?style=for-the-badge&logo=neovim)](https://neovim.io)
![Work In Progress](https://img.shields.io/badge/Work%20In%20Progress-orange?style=for-the-badge)

</div>
Expand Down Expand Up @@ -38,7 +38,7 @@

### Requirements<a name="requirements"></a>

- **Neovim Nightly**
- **Neovim 0.10**
- Treesitter
- Plenary

Expand Down
8 changes: 5 additions & 3 deletions lua/refactoring/code_generation/langs/typescriptreact.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ end
---@param opts call_function_opts
local function tsx_call_function(opts)
if opts.region_type == "jsx_element" or opts.contains_jsx then
local args = vim.tbl_map(function(arg)
return string.format("%s={%s}", arg, arg)
end, opts.args)
local args = vim.iter(opts.args)
:map(function(arg)
return string.format("%s={%s}", arg, arg)
end)
:totable()
return string.format("< %s %s/>", opts.name, table.concat(args, " "))
else
return ts.call_function(opts)
Expand Down
35 changes: 17 additions & 18 deletions lua/refactoring/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,28 @@ end
local function command_complete(arg_lead, cmd_line, _cursor_pos)
local refactors = require("refactoring.refactor")

local number_of_arguments = #vim.split(cmd_line, " ")
local number_of_arguments = #vim.split(cmd_line, " ", { trimempty = true })

if number_of_arguments > 2 then
return {}
end

local options = vim.tbl_filter(
--- @param option any
function(option)
return type(option) == "string"
end,
vim.tbl_keys(refactors)
)

local filtered_options = vim.tbl_filter(
--- @param name string
function(name)
return vim.startswith(name, arg_lead)
end,
options
)

return filtered_options
local options = vim.iter(vim.tbl_keys(refactors))
:filter(
--- @param option any
function(option)
return type(option) == "string"
end
)
:filter(
--- @param name string
function(name)
return vim.startswith(name, arg_lead)
end
)
:totable()

return options
end

function M.setup()
Expand Down
6 changes: 3 additions & 3 deletions lua/refactoring/get_select_input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local Config = require("refactoring.config")

local async = require("plenary.async")

---@type fun(items: string[], prompt?: string, format?: function, kind?: string) : string?, integer?
---@type fun(items: unknown[], prompt?: string, format?: function, kind?: string) : unknown?, integer?
local select_input = async.wrap(function(items, prompt, format, kind, callback)
vim.ui.select(items, {
prompt = prompt,
Expand All @@ -14,8 +14,8 @@ end, 5)
---@param items unknown[]
---@param question string
---@param format? fun(item: unknown) : string
---@return string?
---@return integer?
---@return unknown?
---@return integer
local function get_select_input(items, question, format)
-- TODO: Extract to class
local automation_input = Config.get():get_automated_input()
Expand Down
67 changes: 35 additions & 32 deletions lua/refactoring/indent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,42 @@ M.buf_indent_amount = function(point, refactor, below, bufnr)
end

---@type table<integer, boolean>
local hash = {}
line_numbers = vim.tbl_filter(
---@param line_number integer
---@return boolean
function(line_number)
if hash[line_number] then
return false
local already_seend = {}
line_numbers = vim.iter(line_numbers)
:filter(
---@param line_number integer
---@return boolean
function(line_number)
if already_seend[line_number] then
return false
end
already_seend[line_number] = true
local distance = point.row - line_number
return distance ~= 0
end
hash[line_number] = true
local distance = point.row - line_number
return distance ~= 0
end,
line_numbers
)

local line_numbers_up = vim.tbl_filter(
---@param line_number integer
---@return boolean
function(line_number)
local distance = point.row - line_number
return distance > 0
end,
line_numbers
)
local line_numbers_down = vim.tbl_filter(
---@param line_number integer
---@return boolean
function(line_number)
local distance = point.row - line_number
return distance < 0
end,
line_numbers
)
)
:totable()

local line_numbers_up = vim.iter(line_numbers)
:filter(
---@param line_number integer
---@return boolean
function(line_number)
local distance = point.row - line_number
return distance > 0
end
)
:totable()
local line_numbers_down = vim.iter(line_numbers)
:filter(
---@param line_number integer
---@return boolean
function(line_number)
local distance = point.row - line_number
return distance < 0
end
)
:totable()

---@param a integer
---@param b integer
Expand Down
2 changes: 1 addition & 1 deletion lua/refactoring/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function M.select_refactor(opts)
vim.cmd("norm! ")
end

require("plenary.async").run(function()
require("plenary.async").void(function()
local selected_refactor = require("refactoring.get_select_input")(
M.get_refactors(),
"Refactoring: select a refactor to apply:"
Expand Down
65 changes: 0 additions & 65 deletions lua/refactoring/query2.lua

This file was deleted.

93 changes: 47 additions & 46 deletions lua/refactoring/refactor/106.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,50 @@ end
---@param refactor Refactor
---@return string[]
local function get_return_vals(refactor)
local region_vars = utils.region_intersect(
refactor.ts:get_local_declarations(refactor.scope),
refactor.region
)

region_vars = vim.tbl_map(
---@param node TSNode
---@return TSNode[]
function(node)
return refactor.ts:get_local_var_names(node)[1]
end,
region_vars
)

region_vars = vim.tbl_filter(
---@param node TSNode
---@return TSNode[]
function(node)
return node
end,
region_vars
)

local refs = refactor.ts:get_references(refactor.scope)
refs = utils.after_region(refs, refactor.region)

refs = vim.tbl_map(
---@param node TSNode
---@return TSNode[]
function(node)
return utils.node_to_parent_if_needed(refactor, node)
end,
refs
)
region_vars = vim.tbl_map(
---@param node TSNode
---@return TSNode[]
function(node)
return utils.node_to_parent_if_needed(refactor, node)
end,
region_vars
)
local local_declarations =
refactor.ts:get_local_declarations(refactor.scope)

local region_declarations = vim.iter(local_declarations)
:filter(function(node)
return utils.region_intersect(node, refactor.region)
end)
:map(
---@param node TSNode
---@return TSNode
function(node)
return refactor.ts:get_local_var_names(node)[1]
end
)
:filter(
---@param node TSNode
function(node)
return not not node
end
)
:map(
---@param node TSNode
---@return TSNode
function(node)
return utils.node_to_parent_if_needed(refactor, node)
end
)
:totable()

local refs = vim.iter(refactor.ts:get_references(refactor.scope))
:filter(function(node)
return utils.after_region(node, refactor.region)
end)
:map(
---@param node TSNode
---@return TSNode
function(node)
return utils.node_to_parent_if_needed(refactor, node)
end
)
:totable()

local bufnr = refactor.buffers[1]
local region_var_map = utils.nodes_to_text_set(bufnr, region_vars)
local region_var_map = utils.nodes_to_text_set(bufnr, region_declarations)

local ref_map = utils.nodes_to_text_set(bufnr, refs)
local return_vals =
Expand Down Expand Up @@ -326,11 +325,13 @@ end
local function extract_block_setup(refactor)
local region = Region:from_point(Point:from_cursor(), refactor.bufnr)
local region_node = region:to_ts_node(refactor.ts:get_root())
---@type boolean, TSNode|nil|string
local ok, scope = pcall(refactor.ts.get_scope, refactor.ts, region_node)
if not ok then
---@cast scope string
return ok, scope
end
---@cast scope TSNode

if scope == nil then
return false, "Scope is nil. Couldn't find scope for current block"
Expand Down Expand Up @@ -430,7 +431,7 @@ local function extract_setup(refactor)
end
local region_above_scope = utils.get_non_comment_region_above_node(refactor)

--- @type LspTextEdit | {bufnr: integer}
---@type RefactorTextEdit
local extract_function
if is_class then
extract_function = text_edits_utils.insert_new_line_text(
Expand Down Expand Up @@ -488,8 +489,8 @@ local function extract_setup(refactor)

for _, match in query:iter_matches(refactor.root, refactor.bufnr, 0, -1) do
if match then
local first = match[1]
local last = match[#match]
local first = match[1] --[[@as TSNode]]
local last = match[#match] --[[@as TSNode]]
local start_row, _, _, _ = first:range()
local _, _, end_row, end_col = last:range()

Expand Down
Loading

0 comments on commit 6c23706

Please sign in to comment.