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

feat!: large rework of configuration logic #491

Merged
merged 13 commits into from
Jul 19, 2024
Merged

Conversation

stevearc
Copy link
Owner

@stevearc stevearc commented Jul 15, 2024

This PR bundles several changes related to how conform can be configured. There are two breaking changes in the form of deprecating a method and deprecating the formatter alternation syntax, however both will continue to work for now after one warning notification. The final breaking change is dropping support for Neovim 0.8, which was done because the tests now fail on 0.8 due to nvim-treesitter. Since 0.9 has been around for a while, it's easier to just bump the requirement and bundle that breaking change with these others.

Default format options

feature: New default_format_opts option that will allow you to configure the default behavior of conform.format()
motivation: The primary reason for this is because the per-filetype configuration (below) needs this to make sense. If we can configure the lsp_format on a per-filetype basis, we also probably want to be able to customize the default value to avoid repetition.
example:

require("conform").setup({
  default_format_opts = {
    lsp_format = "fallback",
    timeout_ms = 500,
  },
})

Per-filetype configuration of format options

feature: formatters_by_ft can now customize some format options
motivation: make it easier to customize behavior for specific filetypes. Previous advice for changing the LSP fallback behavior for one filetype was fairly involved. People frequently perform formatting in two places: on save, and on a particular keymap (e.g. <leader>f). It can be annoying to keep the behavior in those two places in sync. This, combined with default_format_opts, should make it much easier to keep the logic simple in both places.
example:

require("conform").setup({
  formatters_by_ft = {
    go = { "goimports", "gofmt", lsp_format = "never" },
    rust = { "rustfmt", lsp_format = "fallback" },
    javascript = { "prettierd", "prettier", timeout_ms = 2000, stop_after_first = true },
  },
  format_on_save = {},
})
vim.keymap.set("", "<leader>f", function()
  require("conform").format({ async = true })
end, { desc = "Format code" })

Notification for no available formatters

feature: You will now get a once-per-filetype notification when you call conform.format on a buffer that does have formatters configured when none of those formatters are available (i.e. they aren't installed). There is an option to disable this notification.
motivation: #480
example:

require("conform").setup({
  -- to disable these new notifications
  notify_no_formatters = false,
})

Deprecate formatter alternation syntax for option to only run the first available formatter

feature: Previously you could use nested list syntax like { { "prettierd", "prettier" } } to run the first formatter available instead of all formatters. Now, conform.format takes a single boolean option stop_after_first that will determine whether to use all formatters, or just the first available.
motivation: This is a useful feature, but the syntax is a weird special case and complicates the data structures we accept and deal with internally. It's also harder for new users to understand what magic is happening.
example:

require("conform").setup({
  formatters_by_ft = {
    -- deprecated syntax
    -- javascript = { { "prettierd", "prettier" } },
    javascript = { "prettierd", "prettier", stop_after_first = true },
  },
})

Note that we are aware that this new format is slightly less flexible, since you can't use constructs like { { "prettierd", "prettier" }, "injected" } anymore. This behavior is still possible, you will just need to write it yourself using get_formatter_info and checking what is available. We think that the simplification of the code and the common case is worth the added complication for these advanced uses.

Deprecate will_fallback_lsp for list_formatters_to_run

feature: new API method that will return the exact list of formatters that will be run, accounting for all stop_after_first, LSP fallback, and other logic.
motivation: the original purpose for will_fallback_lsp was for use in statusline plugins to help them determine if the formatters will be used or if conform will use LSP instead. With the refactor of lsp_format to add more ways LSP formatting can work, it makes more sense to just bundle all this logic up into a function that just gives you what you want. This new function should make it easier to integrate conform into statuslines.
example:

_G.conform_status = function()
  local formatters, use_lsp = require("conform").list_formatters_to_run()
  local names = vim.tbl_map(function(formatter)
    return formatter.name
  end, formatters)
  if use_lsp then
    table.insert(names, "LSP")
  end
  return "[" .. table.concat(names, ", ") .. "]"
end

vim.o.statusline = [[%!v:lua.conform_status()]]

@stevearc stevearc merged commit d60d41c into master Jul 19, 2024
8 checks passed
@stevearc stevearc deleted the stevearc-config-refactor branch July 19, 2024 15:46
@lukoshkin
Copy link

  json = { { "prettierd", "prettier" }, "fixjson" },
  markdown = { { "prettierd", "prettier" }, "markdownlint" },

How to implement these settings with the new logic? That is, I want to use fixjson and markdownlint always and make it only optional and first available in the pair "prettierd", "prettier"

nfielder added a commit to nfielder/dotfiles that referenced this pull request Jul 22, 2024
conform.nvim changed configuration logic to no longer support multiple
formatters in a certain syntax.

See stevearc/conform.nvim#491
yamgent added a commit to yamgent/mynvim that referenced this pull request Jul 26, 2024
* chore!: upgrade conform & fidget

No breaking change on fidget.

Breaking changes on conform.nvim:
* stevearc/conform.nvim@9228b2f
    * Change `lsp_fallback = true` -> `lsp_format = "fallback"`

* chore!: upgrade gitsigns.nvim

Breaking changes for gitsigns.nvim:
* lewis6991/gitsigns.nvim@d9d94e0
    * Not affected. Not using nvim v0.8.
* lewis6991/gitsigns.nvim@720061a
    * Not affected. Not using nvim v0.8.
* lewis6991/gitsigns.nvim@3d7e49c
    * Not affected. Not using the option.
* lewis6991/gitsigns.nvim@61f5b64
    * Not affected. Not using the option 'yadm'.
* lewis6991/gitsigns.nvim@92a8fbb
    * Not affected. Not using the option 'current_line_blame_formatter_opts'.

* chore!: upgrade lazy.nvim

Affected changes for lazy.nvim:
* folke/lazy.nvim@183f59e
    * Partially affected, there's change in startup script

* chore: upgrade neo-tree

Breaking changes for neo-tree:
* nvim-neo-tree/neo-tree.nvim@403a9c5
    * Not affected, not using `enable_normal_mode_for_inputs`

* chore: upgrade nvim-lspconfig

Breaking changes for nvim-lspconfig:
* neovim/nvim-lspconfig@946c58c
    * Not affected, not using solidity-language-server
* neovim/nvim-lspconfig@2054452
    * Not affected, not using omnisharp.
* neovim/nvim-lspconfig@70d1c2c
    * Not affected, not using ruff-lsp.

* chore!: upgrade nvim-treesitter

Too many breaking changes here. We are affected, but nothing is
unsolvable without re-running :TSUpdate.

* chore!: upgrade telescope-zf-native.nvim

No breaking changes in this plugin, BUT lazy.nvim enabled luarocks,
which causes issues with this plugin, so have to disable luarocks.

* chore: upgrade kanagawa and tokyonight

Both have breaking changes but we do not use the options.

* chore!: upgrade nvim-dap-ui

Dependencies for plugin has changed.

* chore!: upgrade the rest of the plugins

They reported no breaking changes, but you never know...

* feat: use softer version of gruvbox

* chore: add maintenance.md

* fix: on Windows, use zig compiler for treesitter instead of clang

On Windows:

Rust: `cargo build` -> builds successfully.
Zig: `zig build-exe` -> builds successfully.
C++: `clang++ main.cpp` -> unable to find a visual studio installation; try running clang from a developer command prompt [-wmsvc-not-found]

Windows C++ toolchain is still a joke today, and the joke's not funny
anymore.

As the zig way of building treesitter is unstable on other platforms,
limit zig usage to just Windows.

* feat: switch colorscheme to gruvbox

* fix: conform replace outdated nested syntax

The syntax was changed in
stevearc/conform.nvim#491

The replacement for nested formatter is:
https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#run-the-first-available-formatter-followed-by-more-formatters

* chore: upgrade plugins again
askoufis added a commit to askoufis/dotfiles that referenced this pull request Jul 31, 2024
- Some config was recently deprecated. See stevearc/conform.nvim#491.
glwbr added a commit to glwbr/kickstart.nixvim that referenced this pull request Sep 2, 2024
- This change allows for the use of the `stop_after_first` option to run formatters sequentially,
stopping after the first successful one. Aligns with the recent changes in conform.nvim to
better support attribute sets and sequential execution of formatters.

- Using a list of lists will throw a deprecation warning

Ref: stevearc/conform.nvim#491
Docs: https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#run-the-first-available-formatter-followed-by-more-formatters
glwbr added a commit to glwbr/kickstart.nixvim that referenced this pull request Sep 2, 2024
- This change allows for the use of the `stop_after_first` option to run formatters sequentially,
stopping after the first successful one. Aligns with the recent changes in conform.nvim to
better support attribute sets and sequential execution of formatters.
- Running nixvim with nixpkgs unstable while using a list of lists will
now throw a deprecation warning

Ref: stevearc/conform.nvim#491
Docs: https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#run-the-first-available-formatter-followed-by-more-formatters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants