Skip to content

Commit

Permalink
feat: add refactoring actions
Browse files Browse the repository at this point in the history
- expand_plain_crate_to_inline_table
- extract_crate_into_table
  • Loading branch information
saecki committed Jul 1, 2023
1 parent 6451bdb commit 87cd6a7
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ require('crates').update_crates(alt: boolean|nil)
-- See `crates.upgrade_crate()`.
require('crates').update_all_crates(alt: boolean|nil)

-- Expand a plain crate declaration into an inline table.
require('crates').expand_plain_crate_to_inline_table()
-- Extract an crate declaration from a dependency section into a table.
require('crates').extract_crate_into_table()

-- Open the homepage of the crate on the current line.
require('crates').open_homepage()
-- Open the repository page of the crate on the current line.
Expand Down
9 changes: 9 additions & 0 deletions doc/crates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ update_all_crates({alt}: `boolean|nil`) *crates.update_all_crates()*
See `crates.upgrade_crate()`.


*crates.expand_plain_crate_to_inline_table()*
expand_plain_crate_to_inline_table()
Expand a plain crate declaration into an inline table.


extract_crate_into_table() *crates.extract_crate_into_table()*
Extract an crate declaration from a dependency section into a table.


open_homepage() *crates.open_homepage()*
Open the homepage of the crate on the current line.

Expand Down
26 changes: 26 additions & 0 deletions lua/crates/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ function M.update_all_crates(alt)
end
end

function M.expand_plain_crate_to_inline_table()
local buf = util.current_buf()
local line = util.cursor_pos()
local _, crate = next(util.get_line_crates(buf, Range.pos(line)))
if crate then
edit.expand_plain_crate_to_inline_table(buf, crate)
end
end

function M.extract_crate_into_table()
local buf = util.current_buf()
local line = util.cursor_pos()
local _, crate = next(util.get_line_crates(buf, Range.pos(line)))
if crate then
edit.extract_crate_into_table(buf, crate)
end
end

function M.open_homepage()
local buf = util.current_buf()
local line = util.cursor_pos()
Expand Down Expand Up @@ -164,6 +182,14 @@ function M.get_actions()
actions["upgrade_crate"] = M.upgrade_crate
end
end


if crate.syntax == "plain" then
actions["expand_crate_to_inline_table"] = M.expand_plain_crate_to_inline_table
end
if crate.syntax ~= "table" then
actions["extract_crate_into_table"] = M.extract_crate_into_table
end
end

local diagnostics = util.get_buf_diagnostics(buf) or {}
Expand Down
51 changes: 51 additions & 0 deletions lua/crates/edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,55 @@ function M.disable_def_features(buf, crate, feature)
end
end

function M.expand_plain_crate_to_inline_table(buf, crate)
if crate.syntax ~= "plain" then
return
end

vim.api.nvim_buf_set_text(
buf, crate.lines.s, crate.vers.decl_col.s, crate.lines.s, crate.vers.decl_col.e,
{ crate.explicit_name .. ' = { version = "' .. crate.vers.text .. '" }' })

end

function M.extract_crate_into_table(buf, crate)
if crate.syntax == "table" then
return
end


vim.api.nvim_buf_set_lines(buf, crate.lines.s, crate.lines.e, false, {})


local lines = {
crate.section:display(crate.explicit_name),
}
if crate.vers then
table.insert(lines, "version = " .. '"' .. crate.vers.text .. '"')
end
if crate.path then
table.insert(lines, "path = " .. '"' .. crate.path.text .. '"')
end
if crate.git then
table.insert(lines, "git = " .. '"' .. crate.git.text .. '"')
end
if crate.pkg then
table.insert(lines, "package = " .. '"' .. crate.pkg.text .. '"')
end
if crate.workspace then
table.insert(lines, "workspace = " .. '"' .. crate.workspace.text .. '"')
end
if crate.def then
table.insert(lines, "default_features = " .. '"' .. crate.def.text .. '"')
end
if crate.feat then
table.insert(lines, "features = " .. crate.feat.text)
end

table.insert(lines, "")

local line = crate.section.lines.e - 1
vim.api.nvim_buf_set_lines(buf, line, line, false, lines)
end

return M
9 changes: 9 additions & 0 deletions lua/crates/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ local M = {}











Expand Down Expand Up @@ -169,6 +174,10 @@ M.upgrade_all_crates = actions.upgrade_all_crates
M.update_crate = actions.update_crate
M.update_crates = actions.update_crates
M.update_all_crates = actions.update_all_crates

M.expand_plain_crate_to_inline_table = actions.expand_plain_crate_to_inline_table
M.extract_crate_into_table = actions.extract_crate_into_table

M.open_homepage = actions.open_homepage
M.open_repository = actions.open_repository
M.open_documentation = actions.open_documentation
Expand Down
34 changes: 33 additions & 1 deletion lua/crates/toml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,38 @@ function Crate:cache_key()

end

function Section.new(obj)
return setmetatable(obj, { __index = Section })
end

function Section:display(override_name)
local text = "["

if self.target then
text = text .. self.target .. "."
end

if self.workspace then
text = text .. "workspace."
end

if self.kind == "default" then
text = text .. "dependencies"
elseif self.kind == "dev" then
text = text .. "dev-dependencies"
elseif self.kind == "build" then
text = text .. "build-dependencies"
end

local name = override_name or self.name
if name then
text = text .. "." .. name
end

text = text .. "]"

return text
end

function M.parse_section(text, start)
local prefix, suffix_s, suffix = text:match("^(.*)dependencies()(.*)$")
Expand Down Expand Up @@ -262,7 +294,7 @@ function M.parse_section(text, start)
(section.workspace and section.kind ~= "default") or
(section.workspace and section.target ~= nil)

return section
return Section.new(section)
end

return nil
Expand Down
26 changes: 26 additions & 0 deletions teal/crates/actions.tl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ function M.update_all_crates(alt: boolean|nil)
end
end

function M.expand_plain_crate_to_inline_table()
local buf = util.current_buf()
local line = util.cursor_pos()
local _, crate = next(util.get_line_crates(buf, Range.pos(line)))
if crate then
edit.expand_plain_crate_to_inline_table(buf, crate)
end
end

function M.extract_crate_into_table()
local buf = util.current_buf()
local line = util.cursor_pos()
local _, crate = next(util.get_line_crates(buf, Range.pos(line)))
if crate then
edit.extract_crate_into_table(buf, crate)
end
end

function M.open_homepage()
local buf = util.current_buf()
local line = util.cursor_pos()
Expand Down Expand Up @@ -164,6 +182,14 @@ function M.get_actions(): {string:function}
actions["upgrade_crate"] = M.upgrade_crate
end
end

-- refactorings
if crate.syntax == "plain" then
actions["expand_crate_to_inline_table"] = M.expand_plain_crate_to_inline_table
end
if crate.syntax ~= "table" then
actions["extract_crate_into_table"] = M.extract_crate_into_table
end
end

local diagnostics = util.get_buf_diagnostics(buf) or {}
Expand Down
10 changes: 5 additions & 5 deletions teal/crates/api.tl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ local record M
record QueuedJob
kind: JobKind
name: string

crate_callbacks: {function(Crate|nil, boolean)}

version: string
deps_callbacks: {function({Dependency}|nil, boolean)}
end
Expand Down Expand Up @@ -83,7 +83,7 @@ local function enqueue_crate_job(name: string, callbacks: {function(Crate|nil, b
return
end
end

table.insert(M.queued_jobs, {
kind = "crate",
name = name,
Expand All @@ -97,7 +97,7 @@ local function enqueue_deps_job(name: string, version: string, callbacks: {funct
vim.list_extend(j.deps_callbacks, callbacks)
end
end

table.insert(M.queued_jobs, {
kind = "deps",
name = name,
Expand Down Expand Up @@ -203,7 +203,7 @@ local function fetch_crate(name: string, callbacks: {function(Crate|nil, boolean
vim.list_extend(exisiting.callbacks, callbacks)
return
end

if M.num_requests >= state.cfg.max_parallel_requests then
enqueue_crate_job(name, callbacks)
return
Expand Down
2 changes: 1 addition & 1 deletion teal/crates/diagnostic.tl
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function M.process_api_crate(crate: toml.Crate, api_crate: Crate): CrateInfo, {D
else
info.dep_kind = "registry"
end

if api_crate then
if api_crate.name ~= crate:package() then
table.insert(diagnostics, crate_diagnostic(
Expand Down
51 changes: 51 additions & 0 deletions teal/crates/edit.tl
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,55 @@ function M.disable_def_features(buf: integer, crate: toml.Crate, feature: toml.F
end
end

function M.expand_plain_crate_to_inline_table(buf: integer, crate: toml.Crate)
if crate.syntax ~= "plain" then
return
end

vim.api.nvim_buf_set_text(
buf, crate.lines.s, crate.vers.decl_col.s, crate.lines.s, crate.vers.decl_col.e,
{ crate.explicit_name .. ' = { version = "' .. crate.vers.text .. '" }' }
)
end

function M.extract_crate_into_table(buf: integer, crate: toml.Crate)
if crate.syntax == "table" then
return
end

-- remove original line
vim.api.nvim_buf_set_lines( buf, crate.lines.s, crate.lines.e, false, {})

-- insert table after dependency section
local lines = {
crate.section:display(crate.explicit_name),
}
if crate.vers then
table.insert(lines, "version = " .. '"' .. crate.vers.text ..'"')
end
if crate.path then
table.insert(lines, "path = " .. '"' .. crate.path.text ..'"')
end
if crate.git then
table.insert(lines, "git = " .. '"' .. crate.git.text ..'"')
end
if crate.pkg then
table.insert(lines, "package = " .. '"' .. crate.pkg.text ..'"')
end
if crate.workspace then
table.insert(lines, "workspace = " .. '"' .. crate.workspace.text ..'"')
end
if crate.def then
table.insert(lines, "default_features = " .. '"' .. crate.def.text ..'"')
end
if crate.feat then
table.insert(lines, "features = " .. crate.feat.text)
end

table.insert(lines, "")

local line = crate.section.lines.e - 1
vim.api.nvim_buf_set_lines(buf, line, line, false, lines)
end

return M
9 changes: 9 additions & 0 deletions teal/crates/init.tl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ local record M
-- See `f#crates.upgrade_crate()`.
update_all_crates: function(alt: boolean|nil)

-- Expand a plain crate declaration into an inline table.
expand_plain_crate_to_inline_table: function()
-- Extract an crate declaration from a dependency section into a table.
extract_crate_into_table: function()

-- Open the homepage of the crate on the current line.
open_homepage: function()
-- Open the repository page of the crate on the current line.
Expand Down Expand Up @@ -169,6 +174,10 @@ M.upgrade_all_crates = actions.upgrade_all_crates
M.update_crate = actions.update_crate
M.update_crates = actions.update_crates
M.update_all_crates = actions.update_all_crates

M.expand_plain_crate_to_inline_table = actions.expand_plain_crate_to_inline_table
M.extract_crate_into_table = actions.extract_crate_into_table

M.open_homepage = actions.open_homepage
M.open_repository = actions.open_repository
M.open_documentation = actions.open_documentation
Expand Down
Loading

0 comments on commit 87cd6a7

Please sign in to comment.