ruff/crates/ruff_server
Dhruv Manilawala 37ad994318
Use default settings if initialization options is empty or not provided (#11566)
## Summary

This PR fixes the bug to avoid flattening the global-only settings for
the new server.

This was added in https://github.com/astral-sh/ruff/pull/11497, possibly
to correctly de-serialize an empty value (`{}`). But, this lead to a bug
where the configuration under the `settings` key was not being read for
global-only variant.

By using #[serde(default)], we ensure that the settings field in the
`GlobalOnly` variant is optional and that an empty JSON object `{}` is
correctly deserialized into `GlobalOnly` with a default `ClientSettings`
instance.

fixes: #11507 

## Test Plan

Update the snapshot and existing test case. Also, verify the following
settings in Neovim:

1. Nothing

```lua
ruff = {
  cmd = {
    '/Users/dhruv/work/astral/ruff/target/debug/ruff',
    'server',
    '--preview',
  },
}
```

2. Empty dictionary

```lua
ruff = {
  cmd = {
    '/Users/dhruv/work/astral/ruff/target/debug/ruff',
    'server',
    '--preview',
  },
  init_options = vim.empty_dict(),
}
```

3. Empty `settings`

```lua
ruff = {
  cmd = {
    '/Users/dhruv/work/astral/ruff/target/debug/ruff',
    'server',
    '--preview',
  },
  init_options = {
    settings = vim.empty_dict(),
  },
}
```

4. With some configuration:

```lua
ruff = {
  cmd = {
    '/Users/dhruv/work/astral/ruff/target/debug/ruff',
    'server',
    '--preview',
  },
  init_options = {
    settings = {
      configuration = '/tmp/ruff-repro/pyproject.toml',
    },
  },
}
```
2024-05-27 21:06:34 +05:30
..
assets Update documentation for ruff server with new migration guide (#11499) 2024-05-22 14:36:33 -07:00
docs Minor edits to ruff server docs (#11500) 2024-05-22 23:53:53 +00:00
resources/test/fixtures Use default settings if initialization options is empty or not provided (#11566) 2024-05-27 21:06:34 +05:30
src Use default settings if initialization options is empty or not provided (#11566) 2024-05-27 21:06:34 +05:30
tests ruff server: Support Jupyter Notebook (*.ipynb) files (#11206) 2024-05-21 22:29:30 +00:00
Cargo.toml ruff server: Support Jupyter Notebook (*.ipynb) files (#11206) 2024-05-21 22:29:30 +00:00
CONTRIBUTING.md High-level project overview and contributing guide for ruff server (#10565) 2024-03-25 23:08:37 -07:00
README.md Minor edits to ruff server docs (#11500) 2024-05-22 23:53:53 +00:00

The Ruff Language Server

Welcome!

ruff server is a language server that powers Ruff's editor integrations.

The job of the language server is to listen for requests from the client (in this case, the code editor of your choice) and call into Ruff's linter and formatter crates to construct real-time diagnostics or formatted code, which is then sent back to the client. It also tracks configuration files in your editor's workspace, and will refresh its in-memory configuration whenever those files are modified.

Setup

We have specific setup instructions depending on your editor. If you don't see your editor on this list and would like a setup guide, please open an issue.

If you're transferring your configuration from ruff-lsp, regardless of editor, there are several settings which have changed or are no longer available. See the migration guide for more.

VS Code

Install the Ruff extension from the VS Code Marketplace.

As this server is still in Beta, you will need to enable the "Native Server" extension setting, either in the settings UI:

A screenshot showing an enabled "Native Server" extension setting in the VS Code settings view

Or in your settings.json:

{
  "ruff.nativeServer": true
}

From there, you can configure Ruff to format Python code on-save with:

{
  "[python]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "charliermarsh.ruff"
  }
}

For more, see Configuring VS Code in the Ruff extension documentation.

By default, the extension will run against the ruff binary that it discovers in your environment. If you don't have ruff installed, the extension will fall back to a bundled version of the binary.

Neovim

See the Neovim setup guide.

Helix

See the Helix setup guide.

Contributing

If you're interested in contributing to ruff server - well, first of all, thank you! Second of all, you might find the contribution guide to be a useful resource.

Finally, don't hesitate to reach out on Discord if you have questions.