[red-knot] Add 'Format document' to playground (#17217)
Some checks are pending
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[Knot Playground] Release / publish (push) Waiting to run

## Summary
This is more "because we can" than something we need. 

But since we're already building an "almost IDE" 

## Test Plan



https://github.com/user-attachments/assets/3a4bdad1-ba32-455a-9909-cfeb8caa1b28
This commit is contained in:
Micha Reiser 2025-04-07 09:26:03 +02:00 committed by GitHub
parent 12d7fad4ef
commit 3150812ac4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 162 additions and 27 deletions

View file

@ -18,6 +18,7 @@ use ruff_db::system::{
};
use ruff_db::Upcast;
use ruff_notebook::Notebook;
use ruff_python_formatter::formatted_file;
use ruff_source_file::{LineIndex, OneIndexed, SourceLocation};
use ruff_text_size::Ranged;
use wasm_bindgen::prelude::*;
@ -142,7 +143,11 @@ impl Workspace {
}
#[wasm_bindgen(js_name = "closeFile")]
pub fn close_file(&mut self, file_id: &FileHandle) -> Result<(), Error> {
#[allow(
clippy::needless_pass_by_value,
reason = "It's intentional that the file handle is consumed because it is no longer valid after closing"
)]
pub fn close_file(&mut self, file_id: FileHandle) -> Result<(), Error> {
let file = file_id.file;
self.db.project().close_file(&mut self.db, file);
@ -184,6 +189,10 @@ impl Workspace {
Ok(format!("{:#?}", parsed.syntax()))
}
pub fn format(&self, file_id: &FileHandle) -> Result<Option<String>, Error> {
formatted_file(&self.db, file_id.file).map_err(into_error)
}
/// Returns the token stream for `path` serialized as a string.
pub fn tokens(&self, file_id: &FileHandle) -> Result<String, Error> {
let parsed = ruff_db::parsed::parsed_module(&self.db, file_id.file);