Compare commits
No commits in common. "main" and "v0.4.0" have entirely different histories.
16 changed files with 1 additions and 312 deletions
28
README.md
28
README.md
|
|
@ -62,7 +62,6 @@ Requirements:
|
|||
- [impl](https://github.com/josharian/impl)
|
||||
- [gotests](https://github.com/cweill/gotests)
|
||||
- [iferr](https://github.com/koron/iferr)
|
||||
- [json2go](https://github.com/olexsmir/json2go)
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
|
@ -186,24 +185,6 @@ Requirements:
|
|||
```
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>
|
||||
<b>Convert json to Go types</b>
|
||||
</summary>
|
||||
|
||||

|
||||
|
||||
`:GoJson` opens a temporary buffer where you can paste or write JSON.
|
||||
Saving the buffer (`:w` or `:wq`) automatically closes it and inserts the generated Go struct into the original buffer at the cursor position.
|
||||
|
||||
Alternatively, you can pass JSON directly as an argument:
|
||||
```vim
|
||||
:GoJson {"name": "Alice", "age": 30}
|
||||
```
|
||||
|
||||
Additionally, `gopher.json2go` provides lua api, see `:h gopher.nvim-json2go` for details.
|
||||
</details>
|
||||
|
||||
|
||||
<details>
|
||||
<summary>
|
||||
|
|
@ -272,15 +253,6 @@ require("gopher").setup {
|
|||
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
|
||||
message = nil,
|
||||
},
|
||||
json2go = {
|
||||
-- command used to open interactive input.
|
||||
-- e.g: `split`, `botright split`, `tabnew`
|
||||
interactive_cmd = "vsplit",
|
||||
|
||||
-- name of autogenerated struct
|
||||
-- e.g: "MySuperCoolName"
|
||||
type_name = nil,
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ Table of Contents
|
|||
Config ................................................ |gopher.nvim-config|
|
||||
Commands ............................................ |gopher.nvim-commands|
|
||||
Modify struct tags ............................... |gopher.nvim-struct-tags|
|
||||
json2go .............................................. |gopher.nvim-json2go|
|
||||
Auto implementation of interface methods ................ |gopher.nvim-impl|
|
||||
Generating unit tests boilerplate .................... |gopher.nvim-gotests|
|
||||
Iferr .................................................. |gopher.nvim-iferr|
|
||||
|
|
@ -70,7 +69,6 @@ or use `require("gopher").install_deps()` if you prefer lua api.
|
|||
gotests = "gotests",
|
||||
impl = "impl",
|
||||
iferr = "iferr",
|
||||
json2go = "json2go",
|
||||
},
|
||||
---@class gopher.ConfigGotests
|
||||
gotests = {
|
||||
|
|
@ -98,24 +96,12 @@ or use `require("gopher").install_deps()` if you prefer lua api.
|
|||
---@type string|nil
|
||||
option = nil,
|
||||
},
|
||||
---@class gopher.ConfigIfErr
|
||||
iferr = {
|
||||
-- choose a custom error message, nil to use default
|
||||
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
|
||||
---@type string|nil
|
||||
message = nil,
|
||||
},
|
||||
---@class gopher.ConfigJson2Go
|
||||
json2go = {
|
||||
-- command used to open interactive input.
|
||||
-- e.g: `split`, `botright split`, `tabnew`
|
||||
interactive_cmd = "vsplit",
|
||||
|
||||
-- name of autogenerated struct, if nil none, will the default one of json2go.
|
||||
-- e.g: "MySuperCoolName"
|
||||
---@type string|nil
|
||||
type_name = nil,
|
||||
},
|
||||
}
|
||||
<
|
||||
Class ~
|
||||
|
|
@ -171,42 +157,6 @@ Example:
|
|||
}
|
||||
<
|
||||
|
||||
==============================================================================
|
||||
------------------------------------------------------------------------------
|
||||
*gopher.nvim-json2go*
|
||||
|
||||
Convert json to go type annotations.
|
||||
|
||||
Usage ~
|
||||
|
||||
`:GoJson` opens a temporary buffer where you can paste or write JSON.
|
||||
Saving the buffer (`:w` or `:wq`) automatically closes it and inserts the
|
||||
generated Go struct into the original buffer at the cursor position.
|
||||
|
||||
Alternatively, you can pass JSON directly as an argument:
|
||||
>vim
|
||||
:GoJson {"name": "Alice", "age": 30}
|
||||
<
|
||||
------------------------------------------------------------------------------
|
||||
*json2go.transform()*
|
||||
`json2go.transform`({json_str})
|
||||
|
||||
Parameters ~
|
||||
{json_str} `(string)` Json string that is going to be converted to go type.
|
||||
Return ~
|
||||
`(string)` `(optional)` Go type, or nil if failed.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*json2go.json2go()*
|
||||
`json2go.json2go`({json_str})
|
||||
Converts json string to go type, and puts result under the cursor. If
|
||||
[json_str] is nil, will open an interactive prompt (with cmd set in
|
||||
config).
|
||||
|
||||
Parameters ~
|
||||
{json_str} `(optional)` `(string)`
|
||||
|
||||
|
||||
==============================================================================
|
||||
------------------------------------------------------------------------------
|
||||
*gopher.nvim-impl*
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ local default_config = {
|
|||
gotests = "gotests",
|
||||
impl = "impl",
|
||||
iferr = "iferr",
|
||||
json2go = "json2go",
|
||||
},
|
||||
---@class gopher.ConfigGotests
|
||||
gotests = {
|
||||
|
|
@ -69,24 +68,12 @@ local default_config = {
|
|||
---@type string|nil
|
||||
option = nil,
|
||||
},
|
||||
---@class gopher.ConfigIfErr
|
||||
iferr = {
|
||||
-- choose a custom error message, nil to use default
|
||||
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
|
||||
---@type string|nil
|
||||
message = nil,
|
||||
},
|
||||
---@class gopher.ConfigJson2Go
|
||||
json2go = {
|
||||
-- command used to open interactive input.
|
||||
-- e.g: `split`, `botright split`, `tabnew`
|
||||
interactive_cmd = "vsplit",
|
||||
|
||||
-- name of autogenerated struct, if nil none, will the default one of json2go.
|
||||
-- e.g: "MySuperCoolName"
|
||||
---@type string|nil
|
||||
type_name = nil,
|
||||
},
|
||||
}
|
||||
--minidoc_afterlines_end
|
||||
|
||||
|
|
@ -117,7 +104,6 @@ function config.setup(user_config)
|
|||
vim.validate("commands.gotests", _config.commands.gotests, "string")
|
||||
vim.validate("commands.impl", _config.commands.impl, "string")
|
||||
vim.validate("commands.iferr", _config.commands.iferr, "string")
|
||||
vim.validate("commands.json2go", _config.commands.json2go, "string")
|
||||
vim.validate("gotests", _config.gotests, "table")
|
||||
vim.validate("gotests.template", _config.gotests.template, "string")
|
||||
vim.validate("gotests.template_dir", _config.gotests.template_dir, { "string", "nil" })
|
||||
|
|
@ -128,8 +114,6 @@ function config.setup(user_config)
|
|||
vim.validate("gotag.option", _config.gotag.option, { "string", "nil" })
|
||||
vim.validate("iferr", _config.iferr, "table")
|
||||
vim.validate("iferr.message", _config.iferr.message, { "string", "nil" })
|
||||
vim.validate("json2go.installer_timeout", _config.json2go.interactive_cmd, "string")
|
||||
vim.validate("json2go.type_name", _config.json2go.type_name, { "string", "nil" })
|
||||
end
|
||||
|
||||
setmetatable(config, {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ local urls = {
|
|||
impl = "github.com/josharian/impl@latest",
|
||||
gotests = "github.com/cweill/gotests/...@develop",
|
||||
iferr = "github.com/koron/iferr@latest",
|
||||
json2go = "olexsmir.xyz/json2go/cmd/json2go@latest",
|
||||
}
|
||||
|
||||
---@param opt vim.SystemCompleted
|
||||
|
|
|
|||
|
|
@ -1,137 +0,0 @@
|
|||
---@toc_entry json2go
|
||||
---@tag gopher.nvim-json2go
|
||||
---@text
|
||||
--- Convert json to go type annotations.
|
||||
---
|
||||
---@usage
|
||||
--- `:GoJson` opens a temporary buffer where you can paste or write JSON.
|
||||
--- Saving the buffer (`:w` or `:wq`) automatically closes it and inserts the
|
||||
--- generated Go struct into the original buffer at the cursor position.
|
||||
---
|
||||
--- Alternatively, you can pass JSON directly as an argument:
|
||||
--- >vim
|
||||
--- :GoJson {"name": "Alice", "age": 30}
|
||||
--- <
|
||||
|
||||
local c = require "gopher.config"
|
||||
local log = require "gopher._utils.log"
|
||||
local u = require "gopher._utils"
|
||||
local r = require "gopher._utils.runner"
|
||||
local json2go = {}
|
||||
|
||||
---@dochide
|
||||
---@param bufnr integer
|
||||
---@param cpos integer
|
||||
---@param type_ string
|
||||
local function apply(bufnr, cpos, type_)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local input_lines = u.remove_empty_lines(vim.split(type_, "\n"))
|
||||
for i, line in pairs(input_lines) do
|
||||
table.insert(lines, cpos + i, line)
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
|
||||
end
|
||||
|
||||
-- Convert json string to go type.
|
||||
---
|
||||
---@param json_str string Json string that is going to be converted to go type.
|
||||
---@return string? Go type, or nil if failed.
|
||||
function json2go.transform(json_str)
|
||||
local cmd = { c.commands.json2go }
|
||||
if c.json2go.type_name then
|
||||
table.insert(cmd, "-type", c.json2go.type_name)
|
||||
end
|
||||
|
||||
local rs = r.sync(cmd, { stdin = json_str })
|
||||
if rs.code ~= 0 then
|
||||
u.notify("json2go: got this error: " .. rs.stdout, vim.log.levels.ERROR)
|
||||
log.error("json2go: got this error: " .. rs.stdout)
|
||||
return
|
||||
end
|
||||
return rs.stdout
|
||||
end
|
||||
|
||||
---@dochide
|
||||
---@param ocpos integer
|
||||
local function interactive(ocpos)
|
||||
local obuf = vim.api.nvim_get_current_buf()
|
||||
local owin = vim.api.nvim_get_current_win()
|
||||
|
||||
-- setup the input window
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.cmd(c.json2go.interactive_cmd)
|
||||
|
||||
local win = vim.api.nvim_get_current_win()
|
||||
vim.api.nvim_win_set_buf(win, buf)
|
||||
vim.api.nvim_buf_set_name(buf, "[GoJson input]")
|
||||
vim.api.nvim_set_option_value("filetype", "jsonc", { buf = buf })
|
||||
vim.api.nvim_set_option_value("buftype", "acwrite", { buf = buf })
|
||||
vim.api.nvim_set_option_value("swapfile", false, { buf = buf })
|
||||
vim.api.nvim_set_option_value("bufhidden", "delete", { buf = buf })
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
|
||||
"// Write your json here.",
|
||||
"// Writing and quitting (:wq), will generate go struct under the cursor.",
|
||||
"",
|
||||
"",
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd("BufLeave", { buffer = buf, command = "stopinsert" })
|
||||
vim.api.nvim_create_autocmd("BufWriteCmd", {
|
||||
buffer = buf,
|
||||
once = true,
|
||||
callback = function()
|
||||
local input = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
|
||||
local inp = table.concat(
|
||||
vim
|
||||
.iter(input)
|
||||
:filter(function(line)
|
||||
local found = string.find(line, "^//.*")
|
||||
return (not found) or (line == "")
|
||||
end)
|
||||
:totable(),
|
||||
"\n"
|
||||
)
|
||||
|
||||
local go_type = json2go.transform(inp)
|
||||
if not go_type then
|
||||
error "cound't convert json to go type"
|
||||
end
|
||||
|
||||
vim.api.nvim_set_option_value("modified", false, { buf = buf })
|
||||
apply(obuf, ocpos, go_type)
|
||||
|
||||
vim.api.nvim_set_current_win(owin)
|
||||
vim.api.nvim_win_set_cursor(owin, { ocpos + 1, 0 })
|
||||
|
||||
vim.schedule(function()
|
||||
pcall(vim.api.nvim_win_close, win, true)
|
||||
pcall(vim.api.nvim_buf_delete, buf, {})
|
||||
end)
|
||||
end,
|
||||
})
|
||||
|
||||
vim.cmd "normal! G"
|
||||
vim.cmd "startinsert"
|
||||
end
|
||||
|
||||
--- Converts json string to go type, and puts result under the cursor. If
|
||||
--- [json_str] is nil, will open an interactive prompt (with cmd set in
|
||||
--- config).
|
||||
---
|
||||
---@param json_str? string
|
||||
function json2go.json2go(json_str)
|
||||
local cur_line = vim.api.nvim_win_get_cursor(0)[1]
|
||||
if not json_str then
|
||||
interactive(cur_line)
|
||||
return
|
||||
end
|
||||
|
||||
local go_type = json2go.transform(json_str)
|
||||
if not go_type then
|
||||
error "cound't convert json to go type"
|
||||
end
|
||||
|
||||
apply(0, cur_line, go_type)
|
||||
end
|
||||
|
||||
return json2go
|
||||
|
|
@ -70,12 +70,6 @@ cmd("GoTagClear", function()
|
|||
require("gopher").tags.clear()
|
||||
end)
|
||||
|
||||
-- :GoJson
|
||||
cmd("GoJson", function(opts)
|
||||
local inp = ((opts.args ~= "" and opts.args) or nil)
|
||||
require("gopher.json2go").json2go(inp)
|
||||
end, "*")
|
||||
|
||||
-- :GoTest
|
||||
cmd("GoTestAdd", function()
|
||||
require("gopher").test.add()
|
||||
|
|
@ -95,6 +89,7 @@ cmd("GoMod", function(opts)
|
|||
end, "*")
|
||||
|
||||
cmd("GoGet", function(opts)
|
||||
vim.print(opts)
|
||||
require("gopher").get(opts.fargs)
|
||||
end, "*")
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ local files = {
|
|||
"lua/gopher/config.lua",
|
||||
"plugin/gopher.lua",
|
||||
"lua/gopher/struct_tags.lua",
|
||||
"lua/gopher/json2go.lua",
|
||||
"lua/gopher/impl.lua",
|
||||
"lua/gopher/gotests.lua",
|
||||
"lua/gopher/iferr.lua",
|
||||
|
|
|
|||
2
spec/fixtures/json2go/interativly_input.go
vendored
2
spec/fixtures/json2go/interativly_input.go
vendored
|
|
@ -1,2 +0,0 @@
|
|||
package main
|
||||
|
||||
5
spec/fixtures/json2go/interativly_output.go
vendored
5
spec/fixtures/json2go/interativly_output.go
vendored
|
|
@ -1,5 +0,0 @@
|
|||
package main
|
||||
|
||||
type AutoGenerated struct {
|
||||
Json bool `json:"json"`
|
||||
}
|
||||
2
spec/fixtures/json2go/manual_input.go
vendored
2
spec/fixtures/json2go/manual_input.go
vendored
|
|
@ -1,2 +0,0 @@
|
|||
package main
|
||||
|
||||
7
spec/fixtures/json2go/manual_output.go
vendored
7
spec/fixtures/json2go/manual_output.go
vendored
|
|
@ -1,7 +0,0 @@
|
|||
package main
|
||||
|
||||
type AutoGenerated struct {
|
||||
User struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"user"`
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
local t = require "spec.testutils"
|
||||
local child, T, json2go = t.setup "json2go"
|
||||
|
||||
json2go["should convert interativly"] = function()
|
||||
local rs = t.setup_test("json2go/interativly", child, { 2, 0 })
|
||||
child.cmd "GoJson"
|
||||
child.type_keys [[{"json": true}]]
|
||||
child.type_keys "<Esc>"
|
||||
child.cmd "wq" -- quit prompt
|
||||
child.cmd "write" -- the fixture file
|
||||
|
||||
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||
t.cleanup(rs)
|
||||
end
|
||||
|
||||
json2go["should convert argument"] = function()
|
||||
local rs = t.setup_test("json2go/manual", child, { 2, 0 })
|
||||
child.cmd [[GoJson {"user": {"name": "user-ovic"}}]]
|
||||
child.cmd "write"
|
||||
|
||||
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||
t.cleanup(rs)
|
||||
end
|
||||
|
||||
return T
|
||||
|
|
@ -18,6 +18,3 @@ tasks:
|
|||
|
||||
impl:
|
||||
cmd: vhs impl.tape
|
||||
|
||||
json2go:
|
||||
cmd: vhs json2go.tape
|
||||
|
|
|
|||
BIN
vhs/json2go.gif
BIN
vhs/json2go.gif
Binary file not shown.
|
Before Width: | Height: | Size: 282 KiB |
|
|
@ -1,2 +0,0 @@
|
|||
package main
|
||||
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
Output json2go.gif
|
||||
Require nvim
|
||||
Require json2go
|
||||
|
||||
Set FontFamily "JetBrains Mono"
|
||||
Set Height 800
|
||||
Set Width 1500
|
||||
Set Padding 20
|
||||
Set Shell "bash"
|
||||
Set Theme "tokyonight"
|
||||
Set TypingSpeed 250ms
|
||||
|
||||
Hide Type@0ms "./nvim.sh json2go.go" Enter Show
|
||||
|
||||
Type@0ms "G"
|
||||
Type@400ms ":GoJson" Sleep 500ms Enter
|
||||
Type@70ms "{" Enter
|
||||
Type@70ms `"json": true,` Enter
|
||||
Type@70ms `"user": {"name": "Name", "of_age": true}` Enter
|
||||
Type@70ms "}"
|
||||
Escape Type@500ms ":wq" Enter
|
||||
Sleep 1s
|
||||
|
||||
Type@25ms "G2o" Escape
|
||||
Type@120ms `:GoJson {"json": true}` Enter
|
||||
|
||||
Sleep 5s
|
||||
Loading…
Add table
Add a link
Reference in a new issue