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: incorrect range formatting with stylua #89

Closed
petobens opened this issue Sep 29, 2023 · 6 comments
Closed

bug: incorrect range formatting with stylua #89

petobens opened this issue Sep 29, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@petobens
Copy link
Contributor

Neovim version (nvim -v)

NVIM v0.10.0-dev-1240+g9afbfb4d64

Operating system/version

Arch Linux

Output of :ConformInfo

~ │Formatters for this buffer:                                                                                                                                                                                    │
~ │stylua ready (lua)                                                                                                                                                                                             │

Describe the bug

Range formatting doesn't seem to work properly (at least with stylua)

Steps To Reproduce

  1. Open nvim with the minimal init.lua file
  2. Open a foo.lua file with
local x=2
local y=3
local z=4
  1. Select 2 lines as in the GIF and press ,fc
  2. Those 2 lines don't get properly formatted

Expected Behavior

After 4. get proper formatting (spaces around equal sign) for visually selected lines.

Minimal example file

No response

Minimal init.lua

local root = '/tmp/nvim-minimal'

-- Set stdpaths to use root dir
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 and configure plugins
local plugins = {
    {
        'stevearc/conform.nvim',
        config = function()
            vim.o.formatexpr = [[v:lua.require('conform').formatexpr()]]
            require('conform').setup({
                format_on_save = {
                    timeout_ms = 200,
                    lsp_fallback = false,
                },
                notify_on_error = false,
                formatters_by_ft = {
                    lua = { 'stylua' },
                },
            })
            vim.keymap.set({ 'n', 'v' }, ',fc', function()
                require('conform').format({ async = true, lsp_fallback = false })
            end)
        end,
    },
}
require('lazy').setup(plugins, {
    root = root .. '/plugins',
})

Additional context

conformvisual

@petobens petobens added the bug Something isn't working label Sep 29, 2023
@petobens
Copy link
Contributor Author

Side note comment: I modify/extender my linter arguments with:

-- Formatter args
local utils = require('conform.util')
---- Json
utils.add_formatter_args(require('conform.formatters.jq'), { '--indent', '4' })
---- Lua
utils.add_formatter_args(
    require('conform.formatters.stylua'),
    { '--config-path=' .. vim.env.HOME .. '/.config/stylua.toml' }
)
---- Python
utils.add_formatter_args(
    require('conform.formatters.isort'),
    { '--settings-file=' .. vim.env.HOME .. '/.isort.cfg' }
)

I was wondering whether it was possible to define something like

local formatters = require('conform.formatters')
utils.add_formatter_args(formatters.jq, { '--indent', '4' })

but I get

Failed to run `config` for conform.nvim

.../pedro/.config/nvim/lua/plugin-config/conform_config.lua:39: module 'conform.formatters' not found:

Any pointers? (sorry for not opening a new issue about this)

@stevearc
Copy link
Owner

This is just how stylua works. From the readme:

Only whole statements lying within the range will be formatted. If part of a statement falls outside the range, the statement will be ignored.

You can verify this yourself by running the stylua command directly in the terminal and viewing the output.

As for your second question, I added a helper so that you can do require("conform.formatters").jq. It uses a bit of metatable magic, but in this case it seems pretty benign and not terribly confusing.

@petobens
Copy link
Contributor Author

Thank you!

@petobens
Copy link
Contributor Author

As for your second question, I added a helper so that you can do require("conform.formatters").jq. It uses a bit of metatable magic, but in this case it seems pretty benign and not terribly confusing.

Hi! I saw that you just added the prepend_args config. Now my settings look like:

local formatters = require('conform.formatters')
formatters.jq.args = { '--indent', '4' }
require('conform').formatters.stylua =
    { prepend_args = { '--config-path=' .. vim.env.HOME .. '/.config/stylua.toml' } }

Is it possible to add the same helper/metatable-magic so as to rewrite the second line as:

formatters.stylua.prepend_args =
    { '--config-path=' .. vim.env.HOME .. '/.config/stylua.toml' }

Thanks in advance

@stevearc
Copy link
Owner

As an alternate proposal, what about

local conform = require("conform")
conform.formatters.jq = {
  args = { '--indent', '4' }
}
conform.formatters.stylua = {
  prepend_args = { '--config-path=' .. vim.env.HOME .. '/.config/stylua.toml' }
}

@petobens
Copy link
Contributor Author

Make sense! Thanks

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