Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
80 lines (63 loc) · 2.3 KB
/
Copy pathlib.rs
File metadata and controls
80 lines (63 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! ## The Ruff Language Server
use std::num::NonZeroUsize;
use anyhow::Context as _;
use edit::DocumentKey;
pub use logging::{LogLevel, init_logging};
use lsp_types::CodeActionKind;
pub use server::{ConnectionInitializer, Server};
use session::{ClientOptions, Session};
pub use edit::{PositionEncoding, TextDocument};
mod edit;
mod fix;
mod format;
mod lint;
mod logging;
mod resolve;
mod server;
mod session;
mod workspace;
const SERVER_NAME: &str = "ruff";
pub(crate) const DIAGNOSTIC_NAME: &str = "Ruff";
const SOURCE_FIX_ALL_RUFF: CodeActionKind = CodeActionKind::new("source.fixAll.ruff");
const SOURCE_ORGANIZE_IMPORTS_RUFF: CodeActionKind =
CodeActionKind::new("source.organizeImports.ruff");
const NOTEBOOK_SOURCE_FIX_ALL_RUFF: CodeActionKind =
CodeActionKind::new("notebook.source.fixAll.ruff");
const NOTEBOOK_SOURCE_ORGANIZE_IMPORTS_RUFF: CodeActionKind =
CodeActionKind::new("notebook.source.organizeImports.ruff");
/// A common result type used in most cases where a
/// result type is needed.
pub(crate) type Result<T> = anyhow::Result<T>;
/// Whether the server may execute code from the workspace.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum WorkspaceTrust {
Trusted,
Untrusted,
}
fn version() -> &'static str {
ruff_linter::VERSION
}
pub fn run(preview: Option<bool>, workspace_trust: WorkspaceTrust) -> Result<()> {
let four = NonZeroUsize::new(4).unwrap();
// by default, we set the number of worker threads to `num_cpus`, with a maximum of 4.
let worker_threads = std::thread::available_parallelism()
.unwrap_or(four)
.min(four);
let (connection, io_threads) = ConnectionInitializer::stdio();
let server_result = Server::new(worker_threads, connection, preview, workspace_trust, false)
.context("Failed to start server")?
.run();
let io_result = io_threads.join();
let result = match (server_result, io_result) {
(Ok(()), Ok(())) => Ok(()),
(Err(server), Err(io)) => Err(server).context(format!("IO thread error: {io}")),
(Err(server), _) => Err(server),
(_, Err(io)) => Err(io).context("IO thread error"),
};
if let Err(err) = result.as_ref() {
tracing::warn!("Server shut down with an error: {err}");
} else {
tracing::info!("Server shut down");
}
result
}