Skip to content

Commit

Permalink
feat: add refactoring actions (#73)
Browse files Browse the repository at this point in the history
* feat: add refactoring actions

- expand_plain_crate_to_inline_table
- extract_crate_into_table

* docs: update keymappings
  • Loading branch information
saecki authored Jul 1, 2023
1 parent 6451bdb commit 6b61b51
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 96 deletions.
58 changes: 36 additions & 22 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 Expand Up @@ -430,28 +435,6 @@ require('crates').hide_popup()

### Key mappings
Some examples of key mappings.
```vim
nnoremap <silent> <leader>ct :lua require('crates').toggle()<cr>
nnoremap <silent> <leader>cr :lua require('crates').reload()<cr>
nnoremap <silent> <leader>cv :lua require('crates').show_versions_popup()<cr>
nnoremap <silent> <leader>cf :lua require('crates').show_features_popup()<cr>
nnoremap <silent> <leader>cd :lua require('crates').show_dependencies_popup()<cr>
nnoremap <silent> <leader>cu :lua require('crates').update_crate()<cr>
vnoremap <silent> <leader>cu :lua require('crates').update_crates()<cr>
nnoremap <silent> <leader>ca :lua require('crates').update_all_crates()<cr>
nnoremap <silent> <leader>cU :lua require('crates').upgrade_crate()<cr>
vnoremap <silent> <leader>cU :lua require('crates').upgrade_crates()<cr>
nnoremap <silent> <leader>cA :lua require('crates').upgrade_all_crates()<cr>
nnoremap <silent> <leader>cH :lua require('crates').open_homepage()<cr>
nnoremap <silent> <leader>cR :lua require('crates').open_repository()<cr>
nnoremap <silent> <leader>cD :lua require('crates').open_documentation()<cr>
nnoremap <silent> <leader>cC :lua require('crates').open_crates_io()<cr>
```

You can use `vim.keymap.set` to map lua function to keys directly.
```lua
local crates = require('crates')
local opts = { silent = true }
Expand All @@ -470,12 +453,43 @@ vim.keymap.set('n', '<leader>cU', crates.upgrade_crate, opts)
vim.keymap.set('v', '<leader>cU', crates.upgrade_crates, opts)
vim.keymap.set('n', '<leader>cA', crates.upgrade_all_crates, opts)

vim.keymap.set('n', '<leader>ce', crates.expand_plain_crate_to_inline_table, opts)
vim.keymap.set('n', '<leader>cE', crates.extract_crate_into_table, opts)

vim.keymap.set('n', '<leader>cH', crates.open_homepage, opts)
vim.keymap.set('n', '<leader>cR', crates.open_repository, opts)
vim.keymap.set('n', '<leader>cD', crates.open_documentation, opts)
vim.keymap.set('n', '<leader>cC', crates.open_crates_io, opts)
```

<details>
<summary>In vimscript</summary>

```vim
nnoremap <silent> <leader>ct :lua require('crates').toggle()<cr>
nnoremap <silent> <leader>cr :lua require('crates').reload()<cr>
nnoremap <silent> <leader>cv :lua require('crates').show_versions_popup()<cr>
nnoremap <silent> <leader>cf :lua require('crates').show_features_popup()<cr>
nnoremap <silent> <leader>cd :lua require('crates').show_dependencies_popup()<cr>
nnoremap <silent> <leader>cu :lua require('crates').update_crate()<cr>
vnoremap <silent> <leader>cu :lua require('crates').update_crates()<cr>
nnoremap <silent> <leader>ca :lua require('crates').update_all_crates()<cr>
nnoremap <silent> <leader>cU :lua require('crates').upgrade_crate()<cr>
vnoremap <silent> <leader>cU :lua require('crates').upgrade_crates()<cr>
nnoremap <silent> <leader>cA :lua require('crates').upgrade_all_crates()<cr>
nnoremap <silent> <leader>ce :lua require('crates').expand_plain_crate_to_inline_table()<cr>
nnoremap <silent> <leader>cE :lua require('crates').extract_crate_into_table()<cr>
nnoremap <silent> <leader>cH :lua require('crates').open_homepage()<cr>
nnoremap <silent> <leader>cR :lua require('crates').open_repository()<cr>
nnoremap <silent> <leader>cD :lua require('crates').open_documentation()<cr>
nnoremap <silent> <leader>cC :lua require('crates').open_crates_io()<cr>
```
</details>

### Show appropriate documentation in `Cargo.toml`
How you might integrate `show_popup` into your `init.vim`.
```vim
Expand Down
59 changes: 37 additions & 22 deletions doc/crates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,6 @@ Note crates doesn't use any global key mappings by default.

But here are some recommended ones.
For more information about the lua function see |crates-functions|.
>
nnoremap <silent> <leader>ct :lua require('crates').toggle()<cr>
nnoremap <silent> <leader>cr :lua require('crates').reload()<cr>
nnoremap <silent> <leader>cv :lua require('crates').show_versions_popup()<cr>
nnoremap <silent> <leader>cf :lua require('crates').show_features_popup()<cr>
nnoremap <silent> <leader>cd :lua require('crates').show_dependencies_popup()<cr>
nnoremap <silent> <leader>cu :lua require('crates').update_crate()<cr>
vnoremap <silent> <leader>cu :lua require('crates').update_crates()<cr>
nnoremap <silent> <leader>ca :lua require('crates').update_all_crates()<cr>
nnoremap <silent> <leader>cU :lua require('crates').upgrade_crate()<cr>
vnoremap <silent> <leader>cU :lua require('crates').upgrade_crates()<cr>
nnoremap <silent> <leader>cA :lua require('crates').upgrade_all_crates()<cr>
nnoremap <silent> <leader>cH :lua require('crates').open_homepage()<cr>
nnoremap <silent> <leader>cR :lua require('crates').open_repository()<cr>
nnoremap <silent> <leader>cD :lua require('crates').open_documentation()<cr>
nnoremap <silent> <leader>cC :lua require('crates').open_crates_io()<cr>
<

You can use |vim.keymap.set()| to map lua function to keys directly.
>
local crates = require('crates')
local opts = { silent = true }
Expand All @@ -260,6 +238,9 @@ You can use |vim.keymap.set()| to map lua function to keys directly.
vim.keymap.set('n', '<leader>cf', crates.show_features_popup, opts)
vim.keymap.set('n', '<leader>cd', crates.show_dependencies_popup, opts)
vim.keymap.set('n', '<leader>ce', crates.expand_plain_crate_to_inline_table, opts)
vim.keymap.set('n', '<leader>cE', crates.extract_crate_into_table, opts)
vim.keymap.set('n', '<leader>cu', crates.update_crate, opts)
vim.keymap.set('v', '<leader>cu', crates.update_crates, opts)
vim.keymap.set('n', '<leader>ca', crates.update_all_crates, opts)
Expand All @@ -273,6 +254,31 @@ You can use |vim.keymap.set()| to map lua function to keys directly.
vim.keymap.set('n', '<leader>cC', crates.open_crates_io, opts)
<

And here are the same ones in vimscript.
>
nnoremap <silent> <leader>ct :lua require('crates').toggle()<cr>
nnoremap <silent> <leader>cr :lua require('crates').reload()<cr>
nnoremap <silent> <leader>cv :lua require('crates').show_versions_popup()<cr>
nnoremap <silent> <leader>cf :lua require('crates').show_features_popup()<cr>
nnoremap <silent> <leader>cd :lua require('crates').show_dependencies_popup()<cr>
nnoremap <silent> <leader>ce :lua require('crates').expand_plain_crate_to_inline_table()<cr>
nnoremap <silent> <leader>cE :lua require('crates').extract_crate_into_table()<cr>
nnoremap <silent> <leader>cu :lua require('crates').update_crate()<cr>
vnoremap <silent> <leader>cu :lua require('crates').update_crates()<cr>
nnoremap <silent> <leader>ca :lua require('crates').update_all_crates()<cr>
nnoremap <silent> <leader>cU :lua require('crates').upgrade_crate()<cr>
vnoremap <silent> <leader>cU :lua require('crates').upgrade_crates()<cr>
nnoremap <silent> <leader>cA :lua require('crates').upgrade_all_crates()<cr>
nnoremap <silent> <leader>cH :lua require('crates').open_homepage()<cr>
nnoremap <silent> <leader>cR :lua require('crates').open_repository()<cr>
nnoremap <silent> <leader>cD :lua require('crates').open_documentation()<cr>
nnoremap <silent> <leader>cC :lua require('crates').open_crates_io()<cr>
<

To integrate the crates popup in an idiomatic way into your configuration, you
might want to use one of the following snippets for contextual documentation.

Expand Down Expand Up @@ -374,6 +380,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
Loading

0 comments on commit 6b61b51

Please sign in to comment.