laburnum 1.17.0

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
Documentation
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#![doc = include_str!("../readme.md")]
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0
#![allow(async_fn_in_trait)]

pub mod source;

#[cfg(feature = "chumsky")]
pub use laburnum_syntax_macro::laburnum_syntax;

#[cfg(feature = "test")]
pub use source::TestSourceCacheReader;
pub use source::{
  MaterializedSource, Source, SourceCache, SourceKey, SourceStale, SourceView,
  Span, SpanCache, SpanCacheCheckpoint, SpanData, SpanResolver,
};
use std::sync::{Arc, atomic::AtomicBool};

/// A value paired with its source span.
///
/// This is the standard way to associate parsed values with their source
/// locations. Use this throughout your AST to enable LSP features like hover,
/// goto definition, etc.
///
/// # Example
/// ```ignore
/// use laburnum::{Ident, Span, Spanned};
///
/// struct FunctionDef {
///   name: Spanned<Ident>,
///   params: Vec<Spanned<Ident>>,
/// }
/// ```
pub type Spanned<T> = (T, Span);

pub mod hooks;

#[cfg(feature = "chumsky")]
pub mod chumsky;

pub mod database;
pub use database::{
  DynPartition, HasPartition, HasPartitionAt, Partition, PartitionReader,
  PartitionStore, PartitionWriteContextRef, PartitionWriter, RecordKey,
  RecordRef,
  gc::{GarbageCollector, GcPhase, GcPolicy},
  hlist::{HCons, HNil, Here, There},
  query::{
    PartitionQueryBuilder, PartitionWaitingQueryBuilder, QueryClient,
    TypedPartitionQueryBuilder, TypedPartitionWaitingQueryBuilder,
  },
  storage::{
    ContentAddressedStorage, Partitions, PartitionsBuilder, RecordStorage,
  },
};

mod known_idents;

pub mod record;
pub use record::{LaburnumRecord, LaburnumRecordRef, Record};

pub mod diagnostics;
pub mod partitions;
pub mod prelude;
pub mod progress;

pub mod builtin_watchers {
  pub use crate::diagnostics::diagnostic_watcher;
}

pub mod errors;
pub use errors::LaburnumError;

pub mod hash;
pub use hash::{ContentHash, ContentHasher, Ident, IdentHashMap, IdentHashSet};

/// Re-export paste for use by define_partitions! macro.
#[doc(hidden)]
pub use paste;

pub mod fs;
pub mod protocol;
pub mod scheduler;
pub mod server;

pub mod uri;
pub use uri::Uri;

pub mod test;

pub mod connect;
pub mod daemon;

pub use protocol::jsonrpc::Message;

otel::tracer!();

/// Handle to a running laburnum server.
///
/// The server runs on a dedicated thread, processing LSP messages and executing
/// compilation tasks via the scheduler.
pub struct Server {
  pub shutdown_flag: Arc<AtomicBool>,

  thread_handle: Option<std::thread::JoinHandle<()>>,
}

impl Server {
  #[cfg(feature = "test")]
  /// set shutdown flag true, but dont wait for threads to end
  pub fn close_test(self) -> Option<std::thread::JoinHandle<()>> {
    self
      .shutdown_flag
      .store(true, std::sync::atomic::Ordering::SeqCst);

    self.thread_handle
  }

  /// Waits for the server thread to complete and joins it.
  ///
  /// This blocks until the server shuts down (typically when the LSP client
  /// disconnects).
  pub fn close(self) -> std::thread::Result<()> {
    otel::span!("laburnum.server.close");

    eprintln!("DEBUG: Server::close() - Setting shutdown flag");
    self
      .shutdown_flag
      .store(true, std::sync::atomic::Ordering::SeqCst);

    match self.thread_handle {
      | Some(h) => {
        eprintln!("DEBUG: Server::close() - Waiting for thread to join");
        let result = h.join();
        eprintln!(
          "DEBUG: Server::close() - Thread joined: {:?}",
          result.is_ok()
        );

        result.map(|_| ())
      },
      | None => {
        eprintln!("DEBUG: Server::close() - No thread to join");
        Ok(())
      },
    }
  }
}

/// Main laburnum instance managing the compilation database and LSP server.
///
/// # Type Parameters
///
/// * `S` - Your [`Partitions`] implementation defining what data the
///   database stores
/// * `T` - Your [`LanguageServer`](protocol::lsp::LanguageServer)
///   implementation handling LSP requests
///
/// # Example
///
/// ```no_run
/// use laburnum::Laburnum;
///
/// # struct MyStorage;
/// # impl laburnum::Partitions for MyStorage {
/// #   type Index = usize;
/// #   type RecordRef<'a> = &'a ();
/// #   type Builder = MyStorageBuilder;
/// #   fn get(&self, _: &Self::Index) -> Option<Self::RecordRef<'_>> { None }
/// #   fn hash_contents(&self, _: &mut laburnum::ContentHasher) {}
/// # }
/// # struct MyStorageBuilder;
/// # impl laburnum::database::storage::PartitionsBuilder for MyStorageBuilder {
/// #   type Storage = MyStorage;
/// #   type Record = ();
/// #   fn push(&mut self, _: Self::Record) -> usize { 0 }
/// #   fn build(self) -> Self::Storage { MyStorage }
/// # }
/// # struct MyServer;
/// # impl laburnum::protocol::lsp::LanguageServer<MyStorage> for MyServer {}
///
/// let server = MyServer;
/// let mut laburnum = Laburnum::<MyStorage, _>::new(server)
///     .build_stdio()
///     .expect("Failed to create server");
///
/// laburnum.run_blocking(); // Runs until client disconnects
/// ```
pub struct Laburnum<
  P: database::storage::Partitions,
  T: protocol::lsp::LanguageServer<P>,
> {
  scheduler: std::sync::Arc<scheduler::Scheduler<P, T>>,

  io_threads: Option<connect::ipc::IoThreads>,
}

impl<P: database::storage::Partitions>
  Laburnum<P, server::LaburnumLanguageServer>
{
  /// Creates a builder with the default
  /// [`LaburnumLanguageServer`](server::LaburnumLanguageServer) implementation.
  ///
  /// The default server provides basic LSP functionality but no
  /// language-specific features. This is mainly useful for testing and
  /// examples.
  pub fn example() -> LaburnumBuilder<P, server::LaburnumLanguageServer> {
    LaburnumBuilder::new(Arc::new(server::LaburnumLanguageServer))
  }
}

impl<P: database::storage::Partitions, T: protocol::lsp::LanguageServer<P>>
  Laburnum<P, T>
where
  T: crate::hooks::LaburnumHooks<P, T>,
{
  /// Creates a new [`LaburnumBuilder`] with your custom language server
  /// implementation.
  ///
  /// # Example
  ///
  /// ```no_run
  /// use laburnum::Laburnum;
  /// # struct MyStorage;
  /// # impl laburnum::Partitions for MyStorage {
  /// #   type Index = usize;
  /// #   type RecordRef<'a> = &'a ();
  /// #   type Builder = MyStorageBuilder;
  /// #   fn get(&self, _: &Self::Index) -> Option<Self::RecordRef<'_>> { None }
  /// #   fn hash_contents(&self, _: &mut laburnum::ContentHasher) {}
  /// # }
  /// # struct MyStorageBuilder;
  /// # impl laburnum::database::storage::PartitionsBuilder for MyStorageBuilder {
  /// #   type Storage = MyStorage;
  /// #   type Record = ();
  /// #   fn push(&mut self, _: Self::Record) -> usize { 0 }
  /// #   fn build(self) -> Self::Storage { MyStorage }
  /// # }
  /// # struct MyServer;
  /// # impl laburnum::protocol::lsp::LanguageServer<MyStorage> for MyServer {}
  ///
  /// let server = MyServer;
  /// let builder = Laburnum::<MyStorage, _>::new(server);
  /// ```
  #[allow(clippy::new_ret_no_self)]
  pub fn new(server: T) -> LaburnumBuilder<P, T> {
    LaburnumBuilder::new(Arc::new(server))
  }

  /// Starts the server on a new thread and returns immediately.
  ///
  /// Returns a [`Server`] handle that can be used to wait for completion.
  /// The server runs until the LSP client disconnects.
  pub fn run(mut self) -> Server {
    let shutdown_flag = self.scheduler.shutdown_flag.clone();

    let thread_handle = otel::span!("laburnum.run", in |cx|{
      std::thread::spawn(move || {
        let _guard = cx.attach();

        self.run_blocking()
      })
    });

    Server {
      shutdown_flag,
      thread_handle: Some(thread_handle),
    }
  }

  /// Runs the server on the current thread, blocking until shutdown.
  ///
  /// This processes LSP messages and executes compilation tasks until the
  /// client disconnects. Use [`run`](Self::run) if you want non-blocking
  /// execution.
  pub fn run_blocking(&mut self) {
    self.scheduler.run();
  }

  /// Shuts down the server and wa
  /// its for I/O threads to complete.
  pub fn shutdown(self) -> std::io::Result<()> {
    if let Some(io_threads) = self.io_threads {
      io_threads.join()?;
    }
    Ok(())
  }
}

/// Builder for configuring and creating a [`Laburnum`] instance.
///
/// Allows customization of filesystems and scheduler configuration before
/// building.
pub struct LaburnumBuilder<
  P: database::storage::Partitions,
  T: protocol::lsp::LanguageServer<P>,
> {
  server: Arc<T>,
  filesystems: Vec<crate::fs::FS>,
  scheduler_config: scheduler::SchedulerConfiguration,
  _phantom: std::marker::PhantomData<P>,
}

impl<P: database::storage::Partitions, T: protocol::lsp::LanguageServer<P>>
  LaburnumBuilder<P, T>
{
  /// Creates a new builder with the given language server implementation.
  pub fn new(server: Arc<T>) -> Self {
    Self {
      server,
      filesystems: Vec::new(),
      scheduler_config: scheduler::SchedulerConfiguration::default(),
      _phantom: std::marker::PhantomData,
    }
  }

  /// Adds a filesystem to the server.
  ///
  /// Multiple filesystems can be added. They are queried in order when
  /// resolving file paths.
  pub fn filesystem(mut self, fs: crate::fs::FS) -> Self {
    self.filesystems.push(fs);
    self
  }

  /// Configures the task scheduler.
  ///
  /// Use this to customize worker thread count, GC settings, and other
  /// scheduler behavior.
  ///
  /// # Example
  ///
  /// ```no_run
  /// use laburnum::{Laburnum, scheduler::SchedulerConfiguration};
  /// use std::time::Duration;
  /// # struct MyStorage;
  /// # impl laburnum::Partitions for MyStorage {
  /// #   type Index = usize;
  /// #   type RecordRef<'a> = &'a ();
  /// #   type Builder = MyStorageBuilder;
  /// #   fn get(&self, _: &Self::Index) -> Option<Self::RecordRef<'_>> { None }
  /// #   fn hash_contents(&self, _: &mut laburnum::ContentHasher) {}
  /// # }
  /// # struct MyStorageBuilder;
  /// # impl laburnum::database::storage::PartitionsBuilder for MyStorageBuilder {
  /// #   type Storage = MyStorage;
  /// #   type Record = ();
  /// #   fn push(&mut self, _: Self::Record) -> usize { 0 }
  /// #   fn build(self) -> Self::Storage { MyStorage }
  /// # }
  /// # struct MyServer;
  /// # impl laburnum::protocol::lsp::LanguageServer<MyStorage> for MyServer {}
  ///
  /// let config = SchedulerConfiguration {
  ///     enable_gc: true,
  ///     gc_interval: Duration::from_secs(120),
  ///     ..Default::default()
  /// };
  ///
  /// let server = MyServer;
  /// let laburnum = Laburnum::<MyStorage, _>::new(server)
  ///     .scheduler_config(config)
  ///     .build_stdio()
  ///     .expect("Failed to build");
  /// ```
  pub fn scheduler_config(
    mut self,
    config: scheduler::SchedulerConfiguration,
  ) -> Self {
    self.scheduler_config = config;
    self
  }

  /// Builds a laburnum instance using standard I/O for LSP communication.
  ///
  /// This is the standard way to run an LSP server - reads messages from stdin,
  /// writes responses to stdout. Use this when your language server is launched
  /// by an LSP client (editor).
  ///
  /// # Worker Threads
  ///
  /// Automatically creates `num_cpus - 1` worker threads, leaving one CPU for
  /// the main thread that handles LSP protocol messages.
  ///
  /// # Errors
  ///
  /// Returns an error if stdin/stdout cannot be accessed or if thread creation
  /// fails.
  pub fn build_stdio(self) -> std::io::Result<Laburnum<P, T>> {
    let (connection, io_threads) = connect::ipc::Connection::stdio()?;

    let filesystems =
      std::sync::Arc::new(parking_lot::RwLock::new(self.filesystems));

    let source_cache =
      std::sync::Arc::new(parking_lot::RwLock::new(SourceCache::new()));

    let worker_count = num_cpus::get().saturating_sub(1).max(1);

    Ok(Laburnum {
      scheduler: scheduler::Scheduler::new_with_config(
        connection,
        self.server,
        filesystems,
        source_cache,
        worker_count,
        self.scheduler_config,
      ),
      io_threads: Some(io_threads),
    })
  }

  /// Builds a laburnum instance for testing with in-memory communication.
  ///
  /// Returns both a server handle and an LSP client. Messages are passed via
  /// in-memory channels rather than stdin/stdout, making this suitable for
  /// integration tests.
  ///
  /// # Worker Threads
  ///
  /// Uses a single worker thread for deterministic test behavior.
  ///
  /// # Example
  ///
  /// ```no_run
  /// use laburnum::Laburnum;
  /// # struct MyStorage;
  /// # impl laburnum::Partitions for MyStorage {
  /// #   type Index = usize;
  /// #   type RecordRef<'a> = &'a ();
  /// #   type Builder = MyStorageBuilder;
  /// #   fn get(&self, _: &Self::Index) -> Option<Self::RecordRef<'_>> { None }
  /// #   fn hash_contents(&self, _: &mut laburnum::ContentHasher) {}
  /// # }
  /// # struct MyStorageBuilder;
  /// # impl laburnum::database::storage::PartitionsBuilder for MyStorageBuilder {
  /// #   type Storage = MyStorage;
  /// #   type Record = ();
  /// #   fn push(&mut self, _: Self::Record) -> usize { 0 }
  /// #   fn build(self) -> Self::Storage { MyStorage }
  /// # }
  /// # struct MyServer;
  /// # impl laburnum::protocol::lsp::LanguageServer<MyStorage> for MyServer {}
  ///
  /// let server = MyServer;
  /// let (server_handle, client) = Laburnum::<MyStorage, _>::new(server)
  ///     .build_test();
  ///
  /// // Use client to send LSP requests in tests
  /// ```
  // #[cfg(feature = "test")]
  pub fn build_test(self) -> (Server, connect::lsp::LspClient) {
    let (server_conn, client_conn) = connect::ipc::Connection::memory();
    let client = connect::lsp::LspClient::new_test(client_conn);

    let filesystems =
      std::sync::Arc::new(parking_lot::RwLock::new(self.filesystems));

    let source_cache =
      std::sync::Arc::new(parking_lot::RwLock::new(SourceCache::new()));

    let server = Laburnum {
      scheduler: scheduler::Scheduler::new_with_config(
        server_conn,
        self.server,
        filesystems,
        source_cache,
        1,
        self.scheduler_config,
      ),
      io_threads: None,
    }
    .run();

    (server, client)
  }

  pub fn build_server(self, server_conn: connect::ipc::Connection) -> Server {
    otel::span!("laburnum.build_server");

    let source_cache =
      std::sync::Arc::new(parking_lot::RwLock::new(SourceCache::new()));

    Laburnum {
      scheduler: scheduler::Scheduler::new_with_config(
        server_conn,
        self.server,
        std::sync::Arc::new(parking_lot::RwLock::new(self.filesystems)),
        source_cache,
        1,
        self.scheduler_config,
      ),
      io_threads: None,
    }
    .run()
  }

  pub fn build_daemon(
    self,
    config: daemon::DaemonConfig,
  ) -> daemon::DaemonServer<P, T>
  where
    T: hooks::LaburnumHooks<P, T>,
  {
    otel::span!("laburnum.build_daemon");

    let filesystems =
      std::sync::Arc::new(parking_lot::RwLock::new(self.filesystems));

    let source_cache =
      std::sync::Arc::new(parking_lot::RwLock::new(SourceCache::new()));

    let worker_count = num_cpus::get().saturating_sub(1).max(1);

    daemon::DaemonServer::new(
      self.server.clone(),
      config,
      filesystems,
      source_cache,
      worker_count,
      self.scheduler_config,
    )
  }
}

pub struct TestHarness {
  pub server: Server,
}