Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 migration issue #414

Open
dkraklan opened this issue Aug 24, 2024 · 15 comments
Open

v3 migration issue #414

dkraklan opened this issue Aug 24, 2024 · 15 comments

Comments

@dkraklan
Copy link

dkraklan commented Aug 24, 2024

My configuration for rest-nvim has been unchanged for months but after updating to the latest version it breaks my configuration and I get the following error.

Failed to run `config` for rest.nvim

.../.local/share/nvim/lazy/rest.nvim/lua/rest-nvim/init.lua:19: Couldn't convert lua value

# stacktrace:
  - /rest.nvim/lua/rest-nvim/init.lua:19 _in_ **setup**
  - ~/.config/nvim/lua/plugins/rest.lua:70 _in_ **config**
  - /usr/share/nvim/runtime/filetype.lua:36
  - vim/shared.lua:0
  - /usr/share/nvim/runtime/filetype.lua:35
  - vim/_editor.lua:0 _in_ **cmd**
  - /persisted.nvim/lua/persisted/init.lua:69 _in_ **load**
  - /persisted.nvim/lua/persisted/init.lua:43

Configuration is below, looking at the readme it seems the way to configure rest-nvim may have changed, although I'm not sure how to convert what I have to the new method as it doesn't seem to have the same options. I'm also not familiar with using vim.g to configure a module. Is the old method of configuration no longer supported?

 {
        "rest-nvim/rest.nvim",
        ft = "http",
        dependencies = { "luarocks.nvim" },
        config = function()
            require("rest-nvim").setup({
                client = "curl",
                env_file = ".env",
                env_pattern = "\\.env$",
                env_edit_command = "tabedit",
                encode_url = true,
                skip_ssl_verification = false,
                custom_dynamic_variables = {},
                logs = {
                    level = "info",
                    save = true,
                },
                result = {
                    split = {
                        horizontal = false,
                        in_place = true,
                        stay_in_current_window_after_split = true,
                    },
                    behavior = {
                        decode_url = true,
                        show_info = {
                            url = true,
                            headers = true,
                            http_info = true,
                            curl_command = true,
                        },
                        statistics = {
                            enable = true,
                            ---@see https://curl.se/libcurl/c/curl_easy_getinfo.html
                            stats = {
                                { "total_time",      title = "Time taken:" },
                                { "size_download_t", title = "Download size:" },
                            },
                        },
                        formatters = {
                            json = "jq",
                            html = function(body)
                                if vim.fn.executable("tidy") == 0 then
                                    return body, { found = false, name = "tidy" }
                                end
                                local fmt_body = vim.fn
                                    .system({
                                        "tidy",
                                        "-i",
                                        "-q",
                                        "--tidy-mark",
                                        "no",
                                        "--show-body-only",
                                        "auto",
                                        "--show-errors",
                                        "0",
                                        "--show-warnings",
                                        "0",
                                        "-",
                                    }, body)
                                    :gsub("\n$", "")

                                return fmt_body, { found = true, name = "tidy" }
                            end,
                        },
                    },
                    keybinds = {
                        buffer_local = true,
                        prev = "P",
                        next = "N",
                    },
                },
                highlight = {
                    enable = true,
                    timeout = 750,
                },
            })
            vim.keymap.set("n", "<leader>rr", "<cmd>Rest run<CR>", { desc = "Run rest command" })
            vim.keymap.set("n", "<leader>rl", "<cmd>Rest run last<CR>", { desc = "Run last rest command" })
        end,
    },

@boltlessengineer
Copy link
Contributor

Old method is still supported (which just assigns given argument to vim.g.rest_nvim) but the config structure has been changed.

require(“rest-nvim”).setup({
-- your config
})

vim.g.rest_nvim = {
-- your config
}

These two are basically identical.

I’ve considered making some deprecated options but decided to just drop the breaking change as this plugin has been archived for a while and people from v2 version will get breaking changes anyways.

If I manually convert your config to v3 spec, it would be like this:

{
    "rest-nvim/rest.nvim",
    dependencies = { "luarocks.nvim" },
    config = function()
        require("rest-nvim").setup({
            env = {
                pattern = "%.env$"
            },
            ui = {
                keybinds = {
                    prev = "P",
                    next = "N",
                },
            },
        })
        vim.keymap.set("n", "<leader>rr", "<cmd>Rest run<CR>", { desc = "Run rest command" })
        vim.keymap.set("n", "<leader>rl", "<cmd>Rest run last<CR>", { desc = "Run last rest command" })
    end,
}

Several notes:

  • env_edit_command is deprecated. You can use default Telescope keybinds to choose how to open .env file.
  • Seems like you are using lazy.nvim as your plugin manager, as lazy.nvim now supports luarocks, you can remove the luarocks.nvim dependency. (it depends on your lazy.nvim version. check if you are on latest version)
  • I’ve removed some default options that you don’t have to set. See :h rest-nvim.config or README of this project for other available options

@dkraklan
Copy link
Author

Thank you @boltlessengineer , that helps a lot, i've gotten the plugin working again. However it seems some features I really enjoyed were removed.

  • The results panel now opens to a buffer that doesn't show up as a tab, I used this often to place it in a specific location in my layout as I would leave it open always.
  • Lost the ability to control the split and choose horizontal and open in place.
  • Json isn't formatted, even when setting response.hooks.format = true ( not sure if this is intended to format json or not)

Those features made this plugin work great with my workflow and really enjoyed it :) , if more appropriate I can open feature request (or multiple not sure what is preffered).

Thanks!

@boltlessengineer
Copy link
Contributor

Those features are not removed but implemented as a different form now.
See :h rest-nvim.commands

Controlling result pane opening mode

Now all window-opening rest.nvim user commands support command-modifiers (:h command-modifiers.)
If you want to open the result pane in tab/horizontal split, you can put :tab or :horizontal (`:hor in short) in front of whatever commands.

For example, you can open result pane with :tab Rest open, go to previous tab, and then run the :Rest run.
Opening in horizontal split works same with :hori Rest open
(I think :Rest run should also support command-modifiers, I’ll fix that)

If formatting doesn’t work

rest.nvim now uses Neovim’s builtin formatting feature to format the buffer. (:h gq)
Check if you set formatexpr or formatprg for json filetype (try run :set formatexpr? and :set formatprg? in json file.)
If those values are empty, formatting might not work.

LSP and conform.nvim provides formatexpr option, so you can use those

If you use conform.nvim for formatting (recommended)

set formatexpr to v:lua.require'conform'.formatexpr() in json filetype

vim.bo.formatexpr =v:lua.requireconform’.formatexpr()”

If you use LSP or none-ls for formatting

set formatexpr to v:vim.lsp.formatexpr() in json filetype

vim.bo.formatexpr =v:vim.lsp.formatexpr()”

@boltlessengineer boltlessengineer changed the title Failed to run 'config' - Couldn't convert lua value v3 migration issue Aug 25, 2024
@boltlessengineer
Copy link
Contributor

pinning this issue as more people might have similar problems.

@dkraklan
Copy link
Author

Thank you, the info on the modifiers helped, got me back to my workflow that I had before, and would be great to have command-modifiers on Rest run, look forward to it.

Now I'm still struggling with formatting. I am indeed using an LSP, I checked if formatexpr or formatprg was set in a json file, and it was indeed not. I manually forced this .

vim.api.nvim_create_autocmd("FileType", {
    pattern = "json",
    callback = function()
        vim.bo.formatexpr = "v:lua.vim.lsp.formatexpr()"
        print("It's a json file")
    end,
})

I have the print statement to confirm that when I get the results in the debug window that it's detecting JSON. I get the message, however I don't get formatting. I did confirm that i now see formatexpr set in a JSON file.

I saw in #417 you mentioned opening a JSON file and trying gggqG , tested this in a JSON file and it does work.

@boltlessengineer
Copy link
Contributor

What formatter are you using? Do you have a formatter plugin or language server that should attached to any json buffer?
What json string do you expect it to be formatted and how it is rendered in rest.nvim result window?
What kind of request have you tested? Do you still get unformatted json with spec/examples/basic_get.http file?

If you set formatprg to jq with this snippet, do you still have same result? (you need jq installed in your machine to test this)

vim.api.nvim_create_autocmd("FileType", {
    pattern = "json",
    callback = function(ev)
        vim.bo[ev.buf].formatprg =jqprint("It's a json file")
    end,
})

@dkraklan
Copy link
Author

  • I'm using nvm-lspconfig, should be attached to json files. When I disable my manual forcing of formatexpr and check it on a json file it's set to formatexpr=v:lua.vim.lsp.formatexpr()

  • The json I expect to be formatted / how its rendered.

### POST_deploy#1
POST http://localhost:8080/v1/services/deploy
HTTP/1.1 200 OK

# @_RES
{"service":{"id":"f3affe36-32fe-46c9-a404-88054c8725ba","name":"node-369","image":"nginx:latest","status":"DEPLOYING","ports":[{"container_port":80,"protocol":"TCP"}],"replicas":3,"zone_id":"32940b1f-99b4-41e1-9074-e3a2b93829c0","env":{"ENV1":"ENV1VALUE"},"cpu_allocated":2,"memory_allocated":1024}}

# @_END
  • output of spec/examples/basic_get.http
### test#1
GET https://api.github.com/users/boltlessengineer
HTTP/2 200 

# @_RES
{"login":"boltlessengineer","id":60088301,"node_id":"MDQ6VXNlcjYwMDg4MzAx","avatar_url":"https://avatars.githubusercontent.com/u/60088301?v=4","gravatar_id":"","url":"https://api.github.com/users/boltlessengineer","html_url":"https://github.com/boltlessengineer","followers_url":"https://api.github.com/users/boltlessengineer/followers","following_url":"https://api.github.com/users/boltlessengineer/following{/other_user}","gists_url":"https://api.github.com/users/boltlessengineer/gists{/gist_id}","starred_url":"https://api.github.com/users/boltlessengineer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/boltlessengineer/subscriptions","organizations_url":"https://api.github.com/users/boltlessengineer/orgs","repos_url":"https://api.github.com/users/boltlessengineer/repos","events_url":"https://api.github.com/users/boltlessengineer/events{/privacy}","received_events_url":"https://api.github.com/users/boltlessengineer/received_events","type":"User","site_admin":false,"name":"Seongmin Lee","company":null,"blog":"https://boltlessengineer.github.io","location":null,"email":null,"hireable":true,"bio":"Student developer","twitter_username":null,"public_repos":48,"public_gists":0,"followers":27,"following":7,"created_at":"2020-01-20T06:50:13Z","updated_at":"2024-08-18T02:09:38Z"}
# @_END
  • if I set formatprg to jq formatting works properly.

@boltlessengineer
Copy link
Contributor

What language server do you expect to format the json file?
Can you run :LspInfo on a json file where you can format it with gq command?

@dkraklan
Copy link
Author

Expected language server is jsonls

 
 Language client log: /home/redacted/.local/state/nvim/lsp.log
 Detected filetype:   json
 
 1 client(s) attached to this buffer: 
 
 Client: jsonls (id: 1, bufnr: [4])
 	filetypes:       json, jsonc
 	autostart:       true
 	root directory:  /home/redacted/projects/go/contman
 	cmd:             /home/redacted/.local/share/nvim/mason/bin/vscode-json-language-server --stdio
 
 Configured servers list: ansiblels, gdscript, gopls, golangci_lint_ls, lua_ls, jsonls

@boltlessengineer
Copy link
Contributor

boltlessengineer commented Aug 25, 2024

Can confirm not working. Seems like it takes time for language server to be attached to a scratch buffer.
I can adjust the code to wait until LspAttach event, but that will make formatting way slower.

I would recommend you to use setlocal formatprg=jq for json filetype than language servers to get faster formatting.

setlocal formatexpr=
setlocal formatprg=jq

or in lua:

vim.bo.formatexpr = ""
vim.bo.formatprg = "jq"

You can put this setting in ftplugin/json.{vim,lua} in your config or create a FileType autocommand like the snippet above.

@VesperQuartz
Copy link

Environment variable is broken for me, it makes request to gibberish URL
POST %2522http%253a%252f%252f127.0.0.1%253a4000%2522/ping

@boltlessengineer
Copy link
Contributor

@mrlectus please create a new issue with reproduce steps

@jrebs
Copy link

jrebs commented Sep 27, 2024

Can confirm not working. Seems like it takes time for language server to be attached to a scratch buffer. I can adjust the code to wait until LspAttach event, but that will make formatting way slower.

I would recommend you to use setlocal formatprg=jq for json filetype than language servers to get faster formatting.

setlocal formatexpr=
setlocal formatprg=jq

or in lua:

vim.bo.formatexpr = “”
vim.bo.formatprg =jq

You can put this setting in ftplugin/json.{vim,lua} in your config or create a FileType autocommand like the snippet above.

Thank you! I spent a while toiling with no json formatting in my responses and this (ftplugin/json.lua file created) did the trick.

@pdarulewski
Copy link

Can confirm not working. Seems like it takes time for language server to be attached to a scratch buffer. I can adjust the code to wait until LspAttach event, but that will make formatting way slower.

I would recommend you to use setlocal formatprg=jq for json filetype than language servers to get faster formatting.

setlocal formatexpr=
setlocal formatprg=jq

or in lua:

vim.bo.formatexpr = “”
vim.bo.formatprg =jq

You can put this setting in ftplugin/json.{vim,lua} in your config or create a FileType autocommand like the snippet above.

I can confirm it works but not with these quotation marks, it worked for me when I used " instead:

vim.bo.formatexpr = ""
vim.bo.formatprg = "jq"

@boltlessengineer
Copy link
Contributor

@pdarulewski didn't noticed I used wrong quotes. I just edited my comment. Thank you for noticing me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants