ruff/crates/ruff_server/tests/document.rs
Jane Lewis b0731ef9cb
ruff server: Support Jupyter Notebook (*.ipynb) files (#11206)
## Summary

Closes https://github.com/astral-sh/ruff/issues/10858.

`ruff server` now supports `*.ipynb` (aka Jupyter Notebook) files.
Extensive internal changes have been made to facilitate this, which I've
done some work to contextualize with documentation and an pre-review
that highlights notable sections of the code.

`*.ipynb` cells should behave similarly to `*.py` documents, with one
major exception. The format command `ruff.applyFormat` will only apply
to the currently selected notebook cell - if you want to format an
entire notebook document, use `Format Notebook` from the VS Code context
menu.

## Test Plan

The VS Code extension does not yet have Jupyter Notebook support
enabled, so you'll first need to enable it manually. To do this,
checkout the `pre-release` branch and modify `src/common/server.ts` as
follows:

Before:
![Screenshot 2024-05-13 at 10 59
06 PM](c6a3c604-c405-4968-b8a2-5d670de89172)

After:
![Screenshot 2024-05-13 at 10 58
24 PM](94ab2e3d-0609-448d-9c8c-cd07c69a513b)

I recommend testing this PR with large, complicated notebook files. I
used notebook files from [this popular
repository](https://github.com/jakevdp/PythonDataScienceHandbook/tree/master/notebooks)
in my preliminary testing.

The main thing to test is ensuring that notebook cells behave the same
as Python documents, besides the aforementioned issue with
`ruff.applyFormat`. You should also test adding and deleting cells (in
particular, deleting all the code cells and ensure that doesn't break
anything), changing the kind of a cell (i.e. from markup -> code or vice
versa), and creating a new notebook file from scratch. Finally, you
should also test that source actions work as expected (and across the
entire notebook).

Note: `ruff.applyAutofix` and `ruff.applyOrganizeImports` are currently
broken for notebook files, and I suspect it has something to do with
https://github.com/astral-sh/ruff/issues/11248. Once this is fixed, I
will update the test plan accordingly.

---------

Co-authored-by: nolan <nolan.king90@gmail.com>
2024-05-21 22:29:30 +00:00

91 lines
2.5 KiB
Rust

const PANDAS_HTML_SRC: &str = include_str!("../resources/test/fixtures/pandas_html.py");
use lsp_types::{Position, Range, TextDocumentContentChangeEvent};
use ruff_server::{PositionEncoding, TextDocument};
#[test]
fn delete_lines_pandas_html() {
let mut document = TextDocument::new(PANDAS_HTML_SRC.to_string(), 1);
let changes = vec![
TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 79,
character: 0,
},
end: Position {
line: 91,
character: 67,
},
}),
range_length: Some(388),
text: String::new(),
},
TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 81,
character: 4,
},
end: Position {
line: 81,
character: 36,
},
}),
range_length: Some(32),
text: "p".into(),
},
TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 81,
character: 5,
},
end: Position {
line: 81,
character: 5,
},
}),
range_length: Some(0),
text: "a".into(),
},
TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 81,
character: 6,
},
end: Position {
line: 81,
character: 6,
},
}),
range_length: Some(0),
text: "s".into(),
},
TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 81,
character: 7,
},
end: Position {
line: 81,
character: 7,
},
}),
range_length: Some(0),
text: "s".into(),
},
];
let mut version = 2;
for change in changes {
document.apply_changes(vec![change], version, PositionEncoding::UTF16);
version += 1;
}
insta::assert_snapshot!(document.contents());
}