From 3f2e7f49bd4234ede0a3d15b9c172950d545d9e5 Mon Sep 17 00:00:00 2001 From: ray-x Date: Thu, 4 Aug 2022 23:30:52 +1000 Subject: [PATCH] json to go struct --- README.md | 9 ++++++- doc/go.txt | 5 ++++ lua/go/commands.lua | 16 ++++++++++- lua/go/install.lua | 60 +++++++++++++++++++++--------------------- lua/go/json2struct.lua | 58 ++++++++++++++++++++++++++++++++++++++++ lua/go/runner.lua | 1 + 6 files changed, 117 insertions(+), 32 deletions(-) create mode 100644 lua/go/json2struct.lua diff --git a/README.md b/README.md index 900e855..7c6edae 100644 --- a/README.md +++ b/README.md @@ -521,6 +521,13 @@ Please use jsonls/null-ls check your launch.json is valid json file. Following s Here is a sample [launch.json](https://github.com/ray-x/go.nvim/blob/master/playground/sampleApp/.vscode/launch.json) +### Json to Go struct +* ["x]GoJson2Struct! +Visual select the json and run `GoJson2Struct youStructName` +-bang will put result to register `a` +if ["x] specified, will put get json from clipboard + + ### Commands | Command | Description | @@ -561,7 +568,7 @@ Sample vimrc for DAP ## Commands -Check [go.lua](https://github.com/ray-x/go.nvim/blob/master/lua/go.lua) on all the commands provided +Check [commands.lua](https://github.com/ray-x/go.nvim/blob/master/lua/go/commands.lua) on all the commands provided ## configuration diff --git a/doc/go.txt b/doc/go.txt index 0367b2d..e323cfa 100644 --- a/doc/go.txt +++ b/doc/go.txt @@ -323,6 +323,11 @@ COMMANDS *go-nvim-commands* :GoTermClose Closes the floating term. + +:["x]GoJson2Struct + Convert json (visual select) to go struct. + bang: put result to register + \"x : get json from register x ============================================================================== OPTIONS *go-nvim-options* diff --git a/lua/go/commands.lua b/lua/go/commands.lua index 4dba2fc..d228e52 100644 --- a/lua/go/commands.lua +++ b/lua/go/commands.lua @@ -348,10 +348,24 @@ return { create_cmd('GoMockGen', require('go.mockgen').run, { nargs = '*', -- bang = true, - complete = function(ArgLead, CmdLine, CursorPos) + complete = function(_, _, _) -- return completion candidates as a list-like table return { '-p', '-d', '-i', '-s' } end, }) + + create_cmd('GoEnv', function(opts) + require('go.env').load_env(unpack(opts.fargs)) + end, { nargs = '*' }) + create_cmd('GoJson2Struct', function(opts) require('go.json2struct').run(opts) end, { + nargs = '*', + bang = true, + register = true, + -- complete = function(ArgLead, CmdLine, CursorPos) + complete = function(_, _, _) + return { 'myStruct'} + end, + range = true, + }) end, } diff --git a/lua/go/install.lua b/lua/go/install.lua index 9bd7632..78e8a9d 100644 --- a/lua/go/install.lua +++ b/lua/go/install.lua @@ -1,28 +1,29 @@ local uv = vim.loop local DIR_SEP = package.config:sub(1, 1) -local utils = require("go.utils") +local utils = require('go.utils') local log = utils.log local url = { - gofumpt = "mvdan.cc/gofumpt", - golines = "github.com/segmentio/golines", - ["golangci-lint"] = "github.com/golangci/golangci-lint/cmd/golangci-lint", - goimports = "golang.org/x/tools/cmd/goimports", - gorename = "golang.org/x/tools/cmd/gorename", - gomodifytags = "github.com/fatih/gomodifytags", - gopls = "golang.org/x/tools/gopls", - gotests = "github.com/cweill/gotests/...", - iferr = "github.com/koron/iferr", - callgraph = "golang.org/x/tools/cmd/callgraph", - guru = "golang.org/x/tools/cmd/guru", - impl = "github.com/josharian/impl", - fillstruct = "github.com/davidrjenni/reftools/cmd/fillstruct", - fillswitch = "github.com/davidrjenni/reftools/cmd/fillswitch", - dlv = "github.com/go-delve/delve/cmd/dlv", - ginkgo = "github.com/onsi/ginkgo/v2/ginkgo", - richgo = "github.com/kyoh86/richgo", - gotestsum = "gotest.tools/gotestsum", - mockgen = "github.com/golang/mock" + gofumpt = 'mvdan.cc/gofumpt', + golines = 'github.com/segmentio/golines', + ['golangci-lint'] = 'github.com/golangci/golangci-lint/cmd/golangci-lint', + goimports = 'golang.org/x/tools/cmd/goimports', + gorename = 'golang.org/x/tools/cmd/gorename', + gomodifytags = 'github.com/fatih/gomodifytags', + gopls = 'golang.org/x/tools/gopls', + gotests = 'github.com/cweill/gotests/...', + iferr = 'github.com/koron/iferr', + callgraph = 'golang.org/x/tools/cmd/callgraph', + guru = 'golang.org/x/tools/cmd/guru', + impl = 'github.com/josharian/impl', + fillstruct = 'github.com/davidrjenni/reftools/cmd/fillstruct', + fillswitch = 'github.com/davidrjenni/reftools/cmd/fillswitch', + dlv = 'github.com/go-delve/delve/cmd/dlv', + ginkgo = 'github.com/onsi/ginkgo/v2/ginkgo', + richgo = 'github.com/kyoh86/richgo', + gotestsum = 'gotest.tools/gotestsum', + mockgen = 'github.com/golang/mock', + ['json-to-struct'] = 'github.com/tmc/json-to-struct', } local tools = {} @@ -31,7 +32,7 @@ for tool, _ in pairs(url) do end local function is_installed(bin) - local env_path = os.getenv("PATH") + local env_path = os.getenv('PATH') local sep = utils.sep2() local ext = utils.ext() local base_paths = vim.split(env_path, sep, true) @@ -40,7 +41,6 @@ local function is_installed(bin) if uv.fs_stat(value .. DIR_SEP .. bin .. ext) then return true end - end return false end @@ -49,23 +49,23 @@ local function go_install(pkg) local u = url[pkg] if u == nil then vim.notify( - "command " .. pkg .. " not supported, please update install.lua, or manually install it", + 'command ' .. pkg .. ' not supported, please update install.lua, or manually install it', vim.lsp.log_levels.WARN ) return end - u = u .. "@latest" - local setup = { "go", "install", u } + u = u .. '@latest' + local setup = { 'go', 'install', u } vim.fn.jobstart(setup, { on_stdout = function(_, data, _) log(setup) - if type(data) == "table" and #data > 0 then - data = table.concat(data, " ") + if type(data) == 'table' and #data > 0 then + data = table.concat(data, ' ') end - local msg = "install " .. u .. " finished" + local msg = 'install ' .. u .. ' finished' if #data > 1 then msg = msg .. data end @@ -79,11 +79,11 @@ local function install(bin, verbose) verbose = _GO_NVIM_CFG.verbose end if not is_installed(bin) then - vim.notify("installing " .. bin, vim.lsp.log_levels.INFO) + vim.notify('installing ' .. bin, vim.lsp.log_levels.INFO) go_install(bin) else if verbose then - vim.notify(bin .. " already install, use GoUpdateBinary to update it", vim.lsp.log_levels.DEBUG) + vim.notify(bin .. ' already install, use GoUpdateBinary to update it', vim.lsp.log_levels.DEBUG) end end return is_installed(bin) diff --git a/lua/go/json2struct.lua b/lua/go/json2struct.lua new file mode 100644 index 0000000..7c99ce5 --- /dev/null +++ b/lua/go/json2struct.lua @@ -0,0 +1,58 @@ +local runner = require('go.runner') +local utils = require('go.utils') +local log = utils.log +local uv, api = vim.loop, vim.api +local M = {} +-- visual select json text and run the command +function M.run(opts) + local args, bang = opts.args, opts.bang + local register = opts.register + local json + if register then + json = vim.fn.getreg(register) + else + local range = vim.lsp.util.make_given_range_params().range + log(range) + + json = vim.api.nvim_buf_get_lines(0, range.start.line, range['end'].line + 1, true) + json[1] = json[1]:sub(range.start.character + 1) + json[#json] = json[#json]:sub(1, range['end'].character + 1) + end + if #json == 1 then + json = vim.split(json[1], '\n') + end + log(json) + + local cmd = { 'json-to-struct', '-name', args[1] or 'myStruct' } + + local opts = { + update_buffer = true, + on_exit = function(code, signal, output_buf) + log(code, signal, output_buf) + if code ~= 0 or signal ~= 0 then + vim.notify( + 'error code' .. tostring(code) .. ' ' .. tostring(signal) .. vim.inspect(output_buf or ''), + vim.lsp.log_levels.WARN + ) + end + local output = vim.split(output_buf, '\n') + if output[1] == 'package main' then + table.remove(output, 1) + end + vim.schedule(function() + if not bang then + api.nvim_buf_set_lines(0, range['end'].line + 1, range['end'].line + 1, false, output) + else + vim.fn.setreg('g', table.concat(output, '\n')) + vim.notify('Json To Struct JSON converted and placed in register "g"') + end + end) + end, + } + stdin, _, _ = runner.run(cmd, opts) + uv.write(stdin, json) + + return cmd, opts +end + +return M diff --git a/lua/go/runner.lua b/lua/go/runner.lua index e43e550..f0cc3de 100644 --- a/lua/go/runner.lua +++ b/lua/go/runner.lua @@ -103,6 +103,7 @@ local run = function(cmd, opts) end) stdout:read_start(update_chunk) -- stderr:read_start(update_chunk) + return stdin, stdout, stderr end local function make(...)