Skip to main content

shuck_server/
lib.rs

1#![allow(missing_docs)]
2#![cfg_attr(not(test), warn(clippy::unwrap_used))]
3
4use std::num::NonZeroUsize;
5
6pub use edit::{DocumentKey, PositionEncoding, TextDocument};
7pub use lint::generate_diagnostics;
8use lsp_types::CodeActionKind;
9pub use server::Server;
10pub use session::{Client, ClientOptions, DocumentQuery, DocumentSnapshot, GlobalOptions, Session};
11pub use workspace::{Workspace, Workspaces};
12
13mod edit;
14mod fix;
15mod format;
16mod lint;
17mod logging;
18mod resolve;
19mod server;
20mod session;
21mod workspace;
22
23pub(crate) const SERVER_NAME: &str = "shuck";
24pub(crate) const DIAGNOSTIC_NAME: &str = "shuck";
25
26pub(crate) const SOURCE_FIX_ALL_SHUCK: CodeActionKind = CodeActionKind::new("source.fixAll.shuck");
27
28pub(crate) type Result<T> = anyhow::Result<T>;
29
30pub(crate) fn version() -> &'static str {
31    env!("CARGO_PKG_VERSION")
32}
33
34pub fn run() -> Result<()> {
35    let four = NonZeroUsize::try_from(4usize)
36        .map_err(|_| anyhow::anyhow!("failed to create non-zero worker count"))?;
37    let worker_threads = std::thread::available_parallelism()
38        .unwrap_or(four)
39        .min(four);
40
41    let (connection, io_threads) = server::ConnectionInitializer::stdio();
42    let server_result = Server::new(worker_threads, connection)
43        .map_err(|err| err.context("Failed to start server"))?
44        .run();
45
46    let io_result = io_threads.join();
47    match (server_result, io_result) {
48        (Ok(()), Ok(())) => Ok(()),
49        (Err(server), Ok(())) => Err(server),
50        (Ok(()), Err(io)) => Err(anyhow::Error::new(io).context("IO thread error")),
51        (Err(server), Err(io)) => Err(server.context(format!("IO thread error: {io}"))),
52    }
53}
54
55#[doc(hidden)]
56pub fn run_connection(connection: lsp_server::Connection) -> Result<()> {
57    let four = NonZeroUsize::try_from(4usize)
58        .map_err(|_| anyhow::anyhow!("failed to create non-zero worker count"))?;
59    let worker_threads = std::thread::available_parallelism()
60        .unwrap_or(four)
61        .min(four);
62    Server::new(
63        worker_threads,
64        server::ConnectionInitializer::from_connection(connection),
65    )
66    .map_err(|err| err.context("Failed to start server"))?
67    .run()
68}