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: Nested injection does not get formatted #607

Closed
2 tasks done
aitvann opened this issue Dec 24, 2024 · 5 comments
Closed
2 tasks done

bug: Nested injection does not get formatted #607

aitvann opened this issue Dec 24, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@aitvann
Copy link

aitvann commented Dec 24, 2024

Neovim version (nvim -v)

NVIM v0.10.2

Operating system/version

NixOS unstable-d70bd19e0a38ad4790d3913bf08fcbfc9eeca507

Read debugging tips

Add the debug logs

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

Log file

2024-12-25 02:19:21[DEBUG] Running formatters on /home/aitvann/playground/conform/example-nested.md: { "injected" }
2024-12-25 02:19:21[INFO] Run injected on /home/aitvann/playground/conform/example-nested.md
2024-12-25 02:19:21[DEBUG] Injected format rust:5:10: { "rustfmt" }
2024-12-25 02:19:21[INFO] Run rustfmt on /home/aitvann/playground/conform/example-nested.md.1.rs
2024-12-25 02:19:21[DEBUG] Run command: { "/nix/store/qpma970g7a0a36qdqrp6aqagynkriygd-rust-default-1.83.0/bin/rustfmt", "--emit=stdout", "--edition=2021" }
2024-12-25 02:19:21[DEBUG] Run default CWD: /home/aitvann/playground/conform
2024-12-25 02:19:21[DEBUG] rustfmt exited with code 0
2024-12-25 02:19:21[DEBUG] Running LSP formatter on /home/aitvann/playground/conform/example-nested.md

Describe the bug

Nested SQL injection in Rust code snippet inside Markdown file does not get formatted

# Some Header


``` rust
fn foo(a: u8) {
    let a = a;
    let b = a ;
    sqlx::query!("SELECT name     , age from users   ;");
}
\```

instead I get formatting for everything but SQL (notice blank line gone, ; at the right place but SQL untouched)

# Some Header

``` rust
fn foo(a: u8) {
    let a = a;
    let b = a;
    sqlx::query!("SELECT name     , age from users   ;");
}
\```

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

example Markdown file:

# Some Header


``` rust
fn foo(a: u8) {
    let a = a;
    let b = a ;
    sqlx::query!("SELECT name     , age from users   ;");
}
\```

Expected Behavior

I would expect to get something like this:

# Some Header

``` rust
fn foo(a: u8) {
    let a = a;
    let b = a;
    sqlx::query!(
        "SELECT
            name,
            age
        FROM users;"
    );
}
\```

Minimal example file

# Some Header


``` rust
fn foo(a: u8) {
    let a = a;
    let b = a ;
    sqlx::query!("SELECT name     , age from users   ;");
}
\```

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,
        -- add your config here
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

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

require('conform').setup({
    formatters_by_ft = {
        sql = { "injected", lsp_format = "last" },
        rust = { "injected", lsp_format = "last" },
        markdown = { "injected", lsp_format = "last" },
    },
    formatters = {
        injected = {
            options = {
                ignore_errors = false,
                lang_to_formatters = {
                    sql = { "sqlfluff" },
                    rust = { "rustfmt" },
                },
            },

        }
    }
})

Additional context

No response

@aitvann aitvann added the bug Something isn't working label Dec 24, 2024
@stevearc
Copy link
Owner

stevearc commented Jan 3, 2025

You're customizing lang_to_formatters to format injected rust blocks with rustfmt. It's not even going to use the injected formatter. I haven't tested it on nested language blocks, so it still may not work at all, but to try it you should set rust = { "rustfmt", "injected" }

@stevearc stevearc added the question Further information is requested label Jan 3, 2025
@aitvann
Copy link
Author

aitvann commented Jan 3, 2025

if a read the docs right rust = { "rustfmt", "injected" } will use rustfmt to format normal files (not injected). the reason I configured the plugin this way is because I want lsp to be the only thing that formats my files and run cli tools only on injections (ideally would be to use lsp for injections as well so I can keep configuration for all formatters in just one place, efm-langserver, but that's too much to ask). in other words I would like cli tools to be the backup option and not the other way around. my configuration was working as expected in most cases

@github-actions github-actions bot removed the question Further information is requested label Jan 3, 2025
@stevearc
Copy link
Owner

stevearc commented Jan 3, 2025

I'm saying to try it here

    formatters = {
        injected = {
            options = {
                ignore_errors = false,
                lang_to_formatters = {
                    sql = { "sqlfluff" },
                    rust = { "rustfmt", "injected" }, -- this line
                },
            },

        }
    }

That should only affect how you format rust that is embedded in other languages, not a standalone rust file that would have a language server attached

@stevearc stevearc added the question Further information is requested label Jan 3, 2025
@aitvann
Copy link
Author

aitvann commented Jan 4, 2025

oh now I see. this way it works. It is a little inconvenient to think about what possible combinations of injections my files could have, I expected it to look at formatters_by_ft and figure out what formatters to use this way. if it is an intended behavior feel free to close the issue

@github-actions github-actions bot removed the question Further information is requested label Jan 4, 2025
@stevearc
Copy link
Owner

stevearc commented Jan 5, 2025

The injected formatter does look at formatters_by_ft by default, however you defined an override in lang_to_formatters so it uses that instead.

@stevearc stevearc closed this as completed Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants