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