diff --git a/lua/gopher/config.lua b/lua/gopher/config.lua index 6081ecc..ab99a28 100644 --- a/lua/gopher/config.lua +++ b/lua/gopher/config.lua @@ -41,6 +41,7 @@ local default_config = { gotests = "gotests", impl = "impl", iferr = "iferr", + json2go = "json2go", }, ---@class gopher.ConfigGotests gotests = { diff --git a/lua/gopher/installer.lua b/lua/gopher/installer.lua index 680096a..61e1788 100644 --- a/lua/gopher/installer.lua +++ b/lua/gopher/installer.lua @@ -9,6 +9,7 @@ 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 diff --git a/lua/gopher/json2go.lua b/lua/gopher/json2go.lua new file mode 100644 index 0000000..0fe24f5 --- /dev/null +++ b/lua/gopher/json2go.lua @@ -0,0 +1,108 @@ +local c = require "gopher.config" +local log = require "gopher._utils.log" +local u = require "gopher._utils" +local r = require "gopher._utils.runner" +local json2go = {} + +---@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 + +---@param json_str string +---@return string? +function json2go.transform(json_str) + local rs = r.sync({ c.commands.json2go }, { 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 + +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.api.nvim_command "vsplit" -- TODO: make it configurable + + 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 + +---@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 diff --git a/plugin/gopher.lua b/plugin/gopher.lua index b2ccc1f..47658fb 100644 --- a/plugin/gopher.lua +++ b/plugin/gopher.lua @@ -70,6 +70,11 @@ cmd("GoTagClear", function() require("gopher").tags.clear() end) +-- :GoJson +cmd("GoJson", function(opts) + require("gopher.json2go").json2go(opts.fargs[1]) +end, "?") + -- :GoTest cmd("GoTestAdd", function() require("gopher").test.add() @@ -89,7 +94,6 @@ cmd("GoMod", function(opts) end, "*") cmd("GoGet", function(opts) - vim.print(opts) require("gopher").get(opts.fargs) end, "*") diff --git a/spec/fixtures/json2go/interativly_input.go b/spec/fixtures/json2go/interativly_input.go new file mode 100644 index 0000000..c9ecbf5 --- /dev/null +++ b/spec/fixtures/json2go/interativly_input.go @@ -0,0 +1,2 @@ +package main + diff --git a/spec/fixtures/json2go/interativly_output.go b/spec/fixtures/json2go/interativly_output.go new file mode 100644 index 0000000..3314555 --- /dev/null +++ b/spec/fixtures/json2go/interativly_output.go @@ -0,0 +1,5 @@ +package main + +type AutoGenerated struct { + Json bool `json:"json"` +} diff --git a/spec/integration/json2go_test.lua b/spec/integration/json2go_test.lua new file mode 100644 index 0000000..473d66e --- /dev/null +++ b/spec/integration/json2go_test.lua @@ -0,0 +1,19 @@ +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 "" + 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() +end + +return T