Skip to content

bug: injected formatter fails #224

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

Closed
1 task done
magnuslarsen opened this issue Nov 29, 2023 · 1 comment · Fixed by #251
Closed
1 task done

bug: injected formatter fails #224

magnuslarsen opened this issue Nov 29, 2023 · 1 comment · Fixed by #251
Labels
bug Something isn't working P0 Highest priority, will receive attention

Comments

@magnuslarsen
Copy link
Contributor

magnuslarsen commented Nov 29, 2023

Neovim version (nvim -v)

NVIM v0.10.0-dev

Operating system/version

Ubuntu 22.04

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

Log file

14:56:49[DEBUG] Running formatters on /home/mlar/test2.py: { "injected" }
14:56:49[INFO] Run injected on /home/mlar/test2.py
14:56:49[DEBUG] Injected format json:1:2: { "fixjson" }
14:56:49[INFO] Run fixjson on /home/mlar/test2.py.json
14:56:49[DEBUG] Run command: { "fixjson" }
14:56:49[INFO] fixjson exited with code 1
14:56:49[DEBUG] fixjson stdout: { "" }
14:56:49[DEBUG] fixjson stderr: { "Error: <stdin>: Expected 'r' instead of 'e' at line 1 column 3 of the JSON5 data. Still to read: \"est_json = \\\"\\\"\\\"\\n{ \\\"th\"", "" }
14:56:49[ERROR] Formatter 'fixjson' error: Error: <stdin>: Expected 'r' instead of 'e' at line 1 column 3 of the JSON5 data. Still to read: "est_json = \"\"\"\n{ \"th"

14:56:49[ERROR] Formatter 'fixjson' error: Error: <stdin>: Expected 'r' instead of 'e' at line 1 column 3 of the JSON5 data. Still to read: "est_json = \"\"\"\n{ \"th"

Describe the bug

On latest master it fails with the following error

14:44:35[ERROR] Formatter 'fixjson' error: Error: <stdin>: Expected 'r' instead of 'e' at line 1 column 3 of the JSON5 data. Still to read: "est_json = \"\"\"\n{ \"th"

If I checkout commit#eeef888 it works again. By the looks of the log, it seems that conform includes the variable as well as the injection.content?

It does correctly identify json blobs (but still keeps the variable name as well), when having multiple lines of other python code. Also the highlight injection does work, highlighting the contents of test_json as json, and the surrounds as Python code (as expected)

Steps To Reproduce

Create the injections.scm file, as well as the python test file; open the python file and run :lua require("conform").format() and see an error :'(

Expected Behavior

Formatting to succeed, as in commit#eeef888

Minimal example file

This Python injection:

; Variables ending in "_json" and "_JSON"
(expression_statement
  (assignment
    left: (identifier) @_left (#match? @_left "(_json|_JSON)$")
    right: (string
      (string_content) @injection.content
      (#set! injection.language "json")
    )
  )
)

Fails to format this python code:

test_json = """
{ "this": "is very annoying", "that": "is also annoying" }
"""

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        formatters_by_ft = {
          json = { "fixjson" },
          ["*"] = { "injected" }
        }
      })
    end,
  },
  -- add any other plugins here
	{
		"nvim-treesitter/nvim-treesitter",
		config = function(_, _)
			require("nvim-treesitter.configs").setup({
				ensure_installed = { "python", "json" },
				auto_install = true,
				highlight = { enable = true },
			})
		end,
	},

}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

Hmm, I must be honest, I don't know how to make nvim-treesitter use custom injections when using nvim -u repro.lua ... :'(

I will have a deeper look tomorrow, maybe making a PR :-)

Interestingly, the json injection-formatter works in a markdown document like this:

```json
{ "this": "is very annoying", "that": "is also annoying" }
```
@magnuslarsen magnuslarsen added the bug Something isn't working label Nov 29, 2023
@stevearc
Copy link
Owner

Could be related to #207. I could see this happening if the injection was catching the newlines adjacent to the quote and not just the single line with the JSON content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P0 Highest priority, will receive attention
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants