ruff/crates
Dhruv Manilawala 66179af4f1
Add cell field to JSON output format (#7664)
## Summary

This PR adds a new `cell` field to the JSON output format which
indicates the Notebook cell this diagnostic (and fix) belongs to. It
also updates the location for the diagnostic and fixes as per the
`NotebookIndex`. It will be used in the VSCode extension to display the
diagnostic in the correct cell.

The diagnostic and edit start and end source locations are translated
for the notebook as per the `NotebookIndex`. The end source location for
an edit needs some special handling.

### Edit end location

To understand this, the following context is required:

1. Visible lines in Jupyter Notebook vs JSON array strings: The newline
is part of the string in the JSON format. This means that if there are 3
visible lines in a cell where the last line is empty then the JSON would
contain 2 strings in the source array, both ending with a newline:

**JSON format:**
```json
[
	"# first line\n",
	"# second line\n",
]
```

**Notebook view:**
```python
1 # first line
2 # second line
3
```

2. If an edit needs to remove an entire line including the newline, then
the end location would be the start of the next row.

To remove a statement in the following code:
```python
import os
```

The edit would be:
```
start: row 1, col 1
end: row 2, col 1
```

Now, here's where the problem lies. The notebook index doesn't have any
information for row 2 because it doesn't exists in the actual notebook.
The newline was added by Ruff to concatenate the source code and it's
removed before writing back. But, the edit is computed looking at that
newline.

This means that while translating the end location for an edit belong to
a Notebook, we need to check if both the start and end location belongs
to the same cell. If not, then the end location should be the first
character of the next row and if so, translate that back to the last
character of the previous row. Taking the above example, the translated
location for Notebook would be:
```
start: row 1, col 1
end: row 1, col 10
```

## Test Plan

Add test cases for notebook output in the JSON format and update
existing snapshots.
2023-10-13 01:06:02 +00:00
..
flake8_to_ruff Drop formatting specific rules from the default set (#7900) 2023-10-11 11:29:34 -05:00
ruff_benchmark Bump ureq from 2.7.1 to 2.8.0 (#7756) 2023-10-02 09:49:59 -04:00
ruff_cache Introduce FormatterSettings (#7545) 2023-09-21 08:01:24 +02:00
ruff_cli Add cell field to JSON output format (#7664) 2023-10-13 01:06:02 +00:00
ruff_dev Rename FixKind to FixAvailability (#7658) 2023-10-02 14:38:25 +00:00
ruff_diagnostics Add settings for promoting and demoting fixes (#7841) 2023-10-10 20:04:21 +00:00
ruff_formatter Use workspace tracing in ruff_formatter crate (#7849) 2023-10-08 09:50:10 -04:00
ruff_index Add unreachable code rule (#5384) 2023-07-04 14:27:23 +00:00
ruff_linter Add cell field to JSON output format (#7664) 2023-10-13 01:06:02 +00:00
ruff_macros Rename FixKind to FixAvailability (#7658) 2023-10-02 14:38:25 +00:00
ruff_notebook Add cell field to JSON output format (#7664) 2023-10-13 01:06:02 +00:00
ruff_python_ast [refurb] Implement single-item-membership-test (FURB171) (#7815) 2023-10-08 14:08:47 +00:00
ruff_python_codegen Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_python_formatter Format comment before parameter default correctly (#7870) 2023-10-12 17:50:12 +02:00
ruff_python_index Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_python_literal Implement our own small-integer optimization (#7584) 2023-09-25 15:13:21 +00:00
ruff_python_parser Disallow f-strings in match pattern literal (#7857) 2023-10-09 10:11:08 +00:00
ruff_python_resolver Replace .map_or(false, $closure) with .is_some_and(closure) (#6244) 2023-08-01 19:29:42 +02:00
ruff_python_semantic Check sequence type before triggering unnecessary-enumerate (FURB148) len suggestion (#7781) 2023-10-03 14:39:14 +00:00
ruff_python_stdlib Improvements to RUF015 (#7848) 2023-10-08 14:49:45 +00:00
ruff_python_trivia Extend pragma comment cases (#7687) 2023-09-28 18:55:19 +00:00
ruff_shrinking Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_source_file Add cell field to JSON output format (#7664) 2023-10-13 01:06:02 +00:00
ruff_text_size Unify line size settings between ruff and the formatter (#6873) 2023-08-28 06:44:56 +00:00
ruff_wasm Drop formatting specific rules from the default set (#7900) 2023-10-11 11:29:34 -05:00
ruff_workspace Drop formatting specific rules from the default set (#7900) 2023-10-11 11:29:34 -05:00