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

bug: text passed to formatters for injected regions in TS/JS files includes code from outside region #229

Closed
1 task done
b0o opened this issue Dec 2, 2023 · 1 comment · Fixed by #251
Closed
1 task done
Labels
bug Something isn't working P0 Highest priority, will receive attention

Comments

@b0o
Copy link

b0o commented Dec 2, 2023

Neovim version (nvim -v)

v0.10.0-dev-1711+g65de1a22c4

Operating system/version

Arch Linux

Add the debug logs

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

Log file

01:21:50[DEBUG] Running formatters on /tmp/repro/repro.js: { "injected" }
01:21:50[INFO] Run injected on /tmp/repro/repro.js
01:21:50[DEBUG] Injected format glsl:5:10: { "tee" }
01:21:50[INFO] Run tee on /tmp/repro/repro.js.glsl
01:21:50[DEBUG] Run command: { "tee", "/tmp/repro/.repro//intercepted.glsl" }
01:21:50[DEBUG] tee exited with code 0

Describe the bug

When running formatters on injected code blocks within JavaScript/TypeScript, code from outside of the block is sent to the formatter.

To demonstrate this, in the minimal init.lua below, I've set up conform to use the injected formatter for JS files. I plan to demonstrate this with glsl injected code blocks, so for the glsl filetype, I've set up a simple formatter that uses the tee command to log stdin to a file and print stdin to stdout. By looking at the file written by tee, we can see exactly what text was sent to the formatter.

Steps To Reproduce

  1. nvim -u repro.lua
  2. make sure to restart neovim after the initial lazy install so that treesitter loads correctly
  3. nvim -u repro.lua repro.js
  4. :write to trigger format on save
  5. take a look at .repro/intercepted.glsl to see what was sent to the formatter, it will look like this:
const vert = glsl`
  void main() {
    vec4 position = vec4(0.0, 0.0, 0.0, 1.0);
    position = position * projectionMatrix;
    gl_Position = position;
  }

As you can see, the entire line where the start of the injected region occurs is sent to the formatter, including code that's not intended to be part of the injected region.

Expected Behavior

I would expect intercepted.glsl to look like this:

  void main() {
    vec4 position = vec4(0.0, 0.0, 0.0, 1.0);
    position = position * projectionMatrix;
    gl_Position = position;
  }

Minimal example file

// This is a test

const foo = "bar";

const vert = glsl`
  void main() {
    vec4 position = vec4(0.0, 0.0, 0.0, 1.0);
    position = position * projectionMatrix;
    gl_Position = position;
  }
`;

const hello = "world";

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,
        format_on_save = {
          lsp_fallback = true,
          timeout_ms = 500,
        },
        formatters = {
          tee = {
            command = "tee",
            args = { root .. "/intercepted.glsl" },
          },
        },
        formatters_by_ft = {
          glsl = { "tee" },
          javascript = { "injected" },
        },
      })
    end,
  },
  {
    "nvim-treesitter/nvim-treesitter",
    config = function()
      require("nvim-treesitter.configs").setup({
        ensure_installed = {
          "javascript",
          "glsl",
        },
        highlight = {
          enable = true,
        },
      })
    end,
  },
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")

Additional context

No response

@stevearc
Copy link
Owner

stevearc commented Dec 5, 2023

Looks related to #224. I think I know how to fix it, just need to find the time

@stevearc stevearc added the P0 Highest priority, will receive attention label Dec 5, 2023
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