1#![warn(missing_docs)]
2#![cfg_attr(not(test), warn(clippy::unwrap_used))]
3
4use std::num::NonZeroUsize;
11
12pub use edit::{DocumentKey, PositionEncoding, TextDocument};
13pub use lint::generate_diagnostics;
14use lsp_types::CodeActionKind;
15pub use server::Server;
16pub use session::{Client, ClientOptions, DocumentQuery, DocumentSnapshot, GlobalOptions, Session};
17pub use workspace::{Workspace, Workspaces};
18
19mod analysis;
20mod edit;
21mod editor;
22mod editor_features;
23mod fix;
24mod format;
25#[cfg(feature = "fuzzing")]
26#[doc(hidden)]
27pub mod fuzzing;
28mod lint;
29mod logging;
30mod resolve;
31mod server;
32mod session;
33mod symbols;
34mod workspace;
35
36pub(crate) const SERVER_NAME: &str = "shuck";
37pub(crate) const DIAGNOSTIC_NAME: &str = "shuck";
38
39pub(crate) const SOURCE_FIX_ALL_SHUCK: CodeActionKind = CodeActionKind::new("source.fixAll.shuck");
40
41pub(crate) type Result<T> = anyhow::Result<T>;
42
43pub(crate) fn version() -> &'static str {
44 env!("CARGO_PKG_VERSION")
45}
46
47pub fn run() -> Result<()> {
49 let four = NonZeroUsize::try_from(4usize)
50 .map_err(|_| anyhow::anyhow!("failed to create non-zero worker count"))?;
51 let worker_threads = std::thread::available_parallelism()
52 .unwrap_or(four)
53 .min(four);
54
55 let (connection, io_threads) = server::ConnectionInitializer::stdio();
56 let server_result = match start_server(worker_threads, connection)? {
57 Some(server) => server.run(),
58 None => Ok(()),
59 };
60
61 let io_result = io_threads.join();
62 match (server_result, io_result) {
63 (Ok(()), Ok(())) => Ok(()),
64 (Err(server), Ok(())) => Err(server),
65 (Ok(()), Err(io)) => Err(anyhow::Error::new(io).context("IO thread error")),
66 (Err(server), Err(io)) => Err(server.context(format!("IO thread error: {io}"))),
67 }
68}
69
70#[doc(hidden)]
71pub fn run_connection(connection: lsp_server::Connection) -> Result<()> {
72 let four = NonZeroUsize::try_from(4usize)
73 .map_err(|_| anyhow::anyhow!("failed to create non-zero worker count"))?;
74 let worker_threads = std::thread::available_parallelism()
75 .unwrap_or(four)
76 .min(four);
77 match start_server(
78 worker_threads,
79 server::ConnectionInitializer::from_connection(connection),
80 )? {
81 Some(server) => server.run(),
82 None => Ok(()),
83 }
84}
85
86fn start_server(
87 worker_threads: NonZeroUsize,
88 connection: server::ConnectionInitializer,
89) -> Result<Option<Server>> {
90 match Server::new(worker_threads, connection) {
91 Ok(server) => Ok(Some(server)),
92 Err(error) if is_disconnected(&error) => Ok(None),
93 Err(error) => Err(error.context("Failed to start server")),
94 }
95}
96
97fn is_disconnected(error: &anyhow::Error) -> bool {
98 error
99 .downcast_ref::<lsp_server::ProtocolError>()
100 .is_some_and(lsp_server::ProtocolError::channel_is_disconnected)
101}