conform.nvim icon indicating copy to clipboard operation
conform.nvim copied to clipboard

bug: indentation not preserved

Open aitvann opened this issue 1 year ago • 1 comments

Neovim version (nvim -v)

NVIM v0.10.2

Operating system/version

NixOS unstable-d70bd19e0a38ad4790d3913bf08fcbfc9eeca507

Read debugging tips

Add the debug logs

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

Log file

2024-12-25 01:08:19[DEBUG] Running formatters on /home/aitvann/rust-file.rs: { "injected" }
2024-12-25 01:08:19[INFO] Run injected on /home/aitvann/rust-file.rs
2024-12-25 01:08:19[DEBUG] Injected format sql:3:3: { "sqlfluff" }
2024-12-25 01:08:19[INFO] Run sqlfluff on /home/aitvann/rust-file.rs.1.sql
2024-12-25 01:08:19[DEBUG] Run command: { "/etc/profiles/per-user/aitvann/bin/sqlfluff", "fix", "-" }
2024-12-25 01:08:19[DEBUG] Run CWD: /home/aitvann/tradetech/solana/balancer
2024-12-25 01:08:20[DEBUG] sqlfluff exited with code 0
2024-12-25 01:08:20[DEBUG] Running LSP formatter on /home/aitvann/rust-file.rs

Describe the bug

Indentation is not preserved when formatting SQL in Rust files

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

example code:

fn main() {
    let a = 5;
    sqlx::query!("select name, age from users;");
}

formatting the code above results in this

fn main() {
    let a = 5;
    sqlx::query!(
        "select
    name,
    age
from users;"
    );
}

Expected Behavior

I would expect indentation of the first line to be added to each of the following lines. like this

fn main() {
    let a = 5;
    sqlx::query!(
        "select
            name,
            age
        from users;"
    );
}

Minimal example file

fn main() {
    let a = 5;
    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 avatar Dec 24 '24 22:12 aitvann

I think I ran into the same issue with serde_json::json!:

Something like this:

#[cfg(test)]
mod tests {
    use serde_json::json;

    #[test]
    fn test_deserialize_response() {
        let response = json!({
          "foo": "bar"
        })
        .to_string();
    }
}

Gets turned into:

#[cfg(test)]
mod tests {
    use serde_json::json;

    #[test]
    fn test_deserialize_response() {
        let response = json!({
  "foo": "bar"
})
        .to_string();
    }
}

When I set lsp_format to last, then it does get fixed by the LSP, but not without briefly flashing/showing the incorrect indentation changes applied by the JSON formatter.

I guess a (probably difficult to implement) fix for my specific case would be for the formatters to be applied in an invisible buffer before applying to the main buffer, so that a chain of formatters are applied first, before the final result gets applied to the current buffer.

JeanMertz avatar May 25 '25 05:05 JeanMertz