Skip to content

Commit 0152229

Browse files
authored
Support cancellation requests (#18627)
1 parent 1f27d53 commit 0152229

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1324
-857
lines changed

crates/ruff/src/commands/server.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
use std::num::NonZeroUsize;
2-
31
use crate::ExitStatus;
42
use anyhow::Result;
5-
use ruff_server::Server;
6-
7-
pub(crate) fn run_server(
8-
worker_threads: NonZeroUsize,
9-
preview: Option<bool>,
10-
) -> Result<ExitStatus> {
11-
let server = Server::new(worker_threads, preview)?;
123

13-
server.run().map(|()| ExitStatus::Success)
4+
pub(crate) fn run_server(preview: Option<bool>) -> Result<ExitStatus> {
5+
ruff_server::run(preview)?;
6+
Ok(ExitStatus::Success)
147
}

crates/ruff/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::fs::File;
44
use std::io::{self, BufWriter, Write, stdout};
5-
use std::num::NonZeroUsize;
65
use std::path::{Path, PathBuf};
76
use std::process::ExitCode;
87
use std::sync::mpsc::channel;
@@ -223,13 +222,7 @@ fn analyze_graph(
223222
}
224223

225224
fn server(args: ServerCommand) -> Result<ExitStatus> {
226-
let four = NonZeroUsize::new(4).unwrap();
227-
228-
// by default, we set the number of worker threads to `num_cpus`, with a maximum of 4.
229-
let worker_threads = std::thread::available_parallelism()
230-
.unwrap_or(four)
231-
.min(four);
232-
commands::server::run_server(worker_threads, args.resolve_preview())
225+
commands::server::run_server(args.resolve_preview())
233226
}
234227

235228
pub fn check(args: CheckCommand, global_options: GlobalConfigArgs) -> Result<ExitStatus> {

crates/ruff_server/src/lib.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! ## The Ruff Language Server
22
3+
use std::num::NonZeroUsize;
4+
5+
use anyhow::Context as _;
36
pub use edit::{DocumentKey, NotebookDocument, PositionEncoding, TextDocument};
47
use lsp_types::CodeActionKind;
5-
pub use server::Server;
6-
pub use session::{ClientOptions, DocumentQuery, DocumentSnapshot, GlobalOptions, Session};
8+
pub use server::{ConnectionSender, MainLoopSender, Server};
9+
pub use session::{Client, ClientOptions, DocumentQuery, DocumentSnapshot, GlobalOptions, Session};
710
pub use workspace::{Workspace, Workspaces};
811

9-
#[macro_use]
10-
mod message;
12+
use crate::server::ConnectionInitializer;
1113

1214
mod edit;
1315
mod fix;
@@ -37,3 +39,35 @@ pub(crate) type Result<T> = anyhow::Result<T>;
3739
pub(crate) fn version() -> &'static str {
3840
ruff_linter::VERSION
3941
}
42+
43+
pub fn run(preview: Option<bool>) -> Result<()> {
44+
let four = NonZeroUsize::new(4).unwrap();
45+
46+
// by default, we set the number of worker threads to `num_cpus`, with a maximum of 4.
47+
let worker_threads = std::thread::available_parallelism()
48+
.unwrap_or(four)
49+
.min(four);
50+
51+
let (connection, io_threads) = ConnectionInitializer::stdio();
52+
53+
let server_result = Server::new(worker_threads, connection, preview)
54+
.context("Failed to start server")?
55+
.run();
56+
57+
let io_result = io_threads.join();
58+
59+
let result = match (server_result, io_result) {
60+
(Ok(()), Ok(())) => Ok(()),
61+
(Err(server), Err(io)) => Err(server).context(format!("IO thread error: {io}")),
62+
(Err(server), _) => Err(server),
63+
(_, Err(io)) => Err(io).context("IO thread error"),
64+
};
65+
66+
if let Err(err) = result.as_ref() {
67+
tracing::warn!("Server shut down with an error: {err}");
68+
} else {
69+
tracing::info!("Server shut down");
70+
}
71+
72+
result
73+
}

crates/ruff_server/src/message.rs

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)