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

docs(neovim): add configuration guide for Neovim as LSP client #270

Merged
merged 17 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/editor-neovim.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: "Neovim"
editor:
markdown:
wrap: sentence
canonical: true
---

# Installing the air client

First, install the Air client by following the instructions on the [Air CLI documentation](https://posit-dev.github.io/air/cli.html).

# Configuration

Air can be configured as a Language Server Protocol (LSP) client via [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) or as a formatter plugin via [conform.nvim](https://github.com/stevearc/conform.nvim).

## LSP Configuration with nvim-lspconfig

To configure Air as an LSP client, add the following to your `nvim/lua/plugins/lspconfig.lua`:

```lua
require("lspconfig").air.setup({})
```

To enable automatic formatting on save, add the following to your `nvim/after/plugin/lsp.lua` (or other appropriate location):

```lua
vim.api.nvim_create_autocmd("BufWritePre", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it makes sense to provide this autocmd? I'd expect everyone to already have something like this in their config.

However, for the completeness (e.g. for Neovim newcomers) it might make sense to keep it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth to consider: using ftplugin.

Putting the below command in either ftplugin/r.lua or after/ftplugin/r.lua would have a roughly the same effect. I supposed it's a style preference of using pattern vs. filetypes.

vim.api.nvim_create_autocmd("BufWritePre", {
  buffer = 0,
  callback = function()
    vim.lsp.buf.format()
  end,
})

Copy link
Contributor Author

@PMassicotte PMassicotte Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth to consider: using ftplugin.

Putting the below command in either ftplugin/r.lua or after/ftplugin/r.lua would have a roughly the same effect. I supposed it's a style preference of using pattern vs. filetypes.

vim.api.nvim_create_autocmd("BufWritePre", {
  buffer = 0,
  callback = function()
    vim.lsp.buf.format()
  end,
})

I think you are right, either are fine. Maybe we can propose both solution/approach. Using the filter might be better for those that also have rlanguageserver to be specific on which one to use?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When responding to another comment, I just realized that on_attach seems to make the all-in-one setup() call:

require("lspconfig").air.setup({
  on_attach = function(_, bufnr)
    vim.api.nvim_create_autocmd("BufWritePre", {
      buffer = bufnr,
      callback = function()
        vim.lsp.buf.format()
      end,
    })
  end,
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will use your solution, it makes sens and likely easier to configure for neovim newcomer. Thank you.

pattern = { "*.R", "*.r" },
callback = function()
vim.lsp.buf.format({
async = true,
filter = function(client)
return client.name == "air"
end,
})
end,
})
```

## conform.nvim configuration

Air can be configured as a formatter plugin via [conform.nvim](https://github.com/stevearc/conform.nvim). This allows you to format R code with Air inside code chunks in Quarto and RMarkdown documents. It can be configured by adding the following to your `nvim/lua/plugins/conform.lua`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Air can be configured as a formatter plugin via [conform.nvim](https://github.com/stevearc/conform.nvim). This allows you to format R code with Air inside code chunks in Quarto and RMarkdown documents. It can be configured by adding the following to your `nvim/lua/plugins/conform.lua`:
Air can be used as a formatter via [conform.nvim](https://github.com/stevearc/conform.nvim). This allows using Air to format R code inside code chunks in Quarto and RMarkdown documents. It can be configured by adding the following to your configuration:

This allows you to format R code with Air inside code chunks in Quarto and RMarkdown documents.

Is this an out-of-the-box functionality or does one need to configure tree-sitter injections? 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is out of the box, my conform config is pretty simple.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TymekDev and @PMassicotte can you please double check that Air works in various Quarto and RMarkdown documents? I have my doubts because I went looking in conform and could not find any specific Quarto support for LSP/formatting passthrough.

If it does seem to be working, I'd love to know what I missed so I can study how it works on the conform side

Copy link

@m-muecke m-muecke Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TymekDev and @PMassicotte can you please double check that Air works in various Quarto and RMarkdown documents? I have my doubts because I went looking in conform and could not find any specific Quarto support for LSP/formatting passthrough.

If it does seem to be working, I'd love to know what I missed so I can study how it works on the conform side

@DavisVaughan conform.nvim supports injected language formatting this is probably what's meant here, see the following: https://github.com/stevearc/conform.nvim/blob/master/doc/advanced_topics.md#injected-language-formatting-code-blocks. I can confirm that it works with Rmarkdown and Quarto files, I'm guessing since Rmarkdown and Quarto inherit from markdown treesitter it works "out of the box".


```lua
return {
"stevearc/conform.nvim",
enabled = true,
config = function()
local conform = require("conform")

conform.setup({
notify_on_error = true,
format_on_save = {
lsp_fallback = true,
},
formatters_by_ft = {
r = { "air" },
},
})
end,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a lazy.nvim-specific setup. I'd suggest using a generic setup (example below) and point to conform's README for a way to install the plugin.

require("conform").setup({
  formatters_by_ft = {
    r = { "air" },
  },
})

What about filetypes for R markdown and Quatro?

Copy link
Contributor Author

@PMassicotte PMassicotte Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently not supported.

#265

```
8 changes: 5 additions & 3 deletions docs/editors.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ Many do, including [Positron](https://positron.posit.co/), [VS Code](https://cod
Air also provides some support for code editors that allow for external formatters, such as [RStudio](https://posit.co/products/open-source/rstudio/).
Follow one of our editor specific guides to get set up with Air in your preferred editor:

- [VS Code](editor-vscode.qmd)
- [VS Code](editor-vscode.qmd)

- [Positron](editor-vscode.qmd)
- [Positron](editor-vscode.qmd)

- [RStudio](editor-rstudio.qmd)
- [RStudio](editor-rstudio.qmd)

- [neovim](editor-neovim.qmd)