Skip to main content

hedl_lsp/
lib.rs

1// Dweve HEDL - Hierarchical Entity Data Language
2//
3// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
4//
5// SPDX-License-Identifier: Apache-2.0
6//
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License in the LICENSE file at the
10// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! HEDL Language Server Protocol (LSP) Implementation
19//!
20//! This crate provides IDE integration for HEDL through the Language Server Protocol,
21//! enabling rich editing experiences in LSP-compatible editors like VS Code, Neovim,
22//! Emacs, and others.
23//!
24//! # Features
25//!
26//! - **Diagnostics**: Real-time error and warning reporting with syntax and lint checks
27//! - **Autocomplete**: Context-aware completion for IDs, types, references, and directives
28//! - **Hover**: Documentation and type information on hover with entity validation
29//! - **Go to Definition**: Navigate to entity and type definitions across the document
30//! - **Find References**: Find all usages of entities and types
31//! - **Document Symbols**: Hierarchical outline view with entities and schemas
32//! - **Workspace Symbols**: Search symbols across all open documents
33//! - **Semantic Highlighting**: Type-aware syntax highlighting for better readability
34//! - **Document Formatting**: Canonical HEDL formatting
35//!
36//! # Performance
37//!
38//! The LSP implementation includes four key performance optimizations:
39//!
40//! 1. **Debouncing** (200ms): Batches multiple keystrokes together, reducing parse
41//!    operations by ~90% during typing.
42//! 2. **Dirty Tracking**: Content hash-based change detection prevents redundant
43//!    parsing when document content hasn't changed.
44//! 3. **Caching**: Parsed documents are cached and reused for LSP queries without
45//!    blocking the UI.
46//! 4. **Reference Index**: O(1) hash map lookups for definitions and references,
47//!    replacing previous O(n) linear search bottleneck.
48//!
49//! # Memory Management
50//!
51//! The implementation includes multiple safeguards for memory management:
52//!
53//! - **Document Size Limit**: Maximum 500MB per document (configurable) to prevent memory exhaustion
54//! - **Open Document Limit**: Maximum 1000 simultaneously open documents with LRU eviction
55//! - **UTF-8 Safety**: All string slicing operations are UTF-8 boundary aware
56//! - **Input Validation**: Comprehensive bounds checking on all LSP positions
57//!
58//! # Usage
59//!
60//! ## Running the Server
61//!
62//! ```bash
63//! # Run the language server (stdio transport)
64//! hedl-lsp
65//!
66//! # With debug logging
67//! RUST_LOG=debug hedl-lsp
68//!
69//! # With trace-level logging for maximum detail
70//! RUST_LOG=trace hedl-lsp
71//! ```
72//!
73//! ## Programmatic Usage
74//!
75//! ### Default Configuration (500 MB documents, 1000 document cache)
76//!
77//! ```no_run
78//! use hedl_lsp::HedlLanguageServer;
79//! use tower_lsp::{LspService, Server};
80//!
81//! #[tokio::main]
82//! async fn main() {
83//!     let stdin = tokio::io::stdin();
84//!     let stdout = tokio::io::stdout();
85//!
86//!     let (service, socket) = LspService::new(|client| {
87//!         HedlLanguageServer::new(client)
88//!     });
89//!
90//!     Server::new(stdin, stdout, socket).serve(service).await;
91//! }
92//! ```
93//!
94//! ### Custom Configuration (e.g., 1 GB documents, 2000 document cache)
95//!
96//! ```no_run
97//! use hedl_lsp::HedlLanguageServer;
98//! use tower_lsp::{LspService, Server};
99//!
100//! #[tokio::main]
101//! async fn main() {
102//!     let stdin = tokio::io::stdin();
103//!     let stdout = tokio::io::stdout();
104//!
105//!     let (service, socket) = LspService::new(|client| {
106//!         HedlLanguageServer::with_config(
107//!             client,
108//!             2000,                    // max documents
109//!             1024 * 1024 * 1024       // 1 GB max document size
110//!         )
111//!     });
112//!
113//!     Server::new(stdin, stdout, socket).serve(service).await;
114//! }
115//! ```
116//!
117//! # Architecture
118//!
119//! The crate is organized into several modules:
120//!
121//! - `backend`: LSP server implementation with document management
122//! - [`analysis`]: Document parsing and analysis with entity/reference extraction
123//! - [`completion`]: Context-aware autocompletion logic
124//! - [`hover`]: Hover information provider with type and entity details
125//! - [`reference_index`]: O(1) reference index for fast definition and reference lookups
126//! - [`symbols`]: Document and workspace symbol providers
127//! - [`utf_encoding`]: Safe UTF-8/UTF-16 encoding conversion utilities for LSP
128//! - [`diagnostics`]: LSP-specific diagnostics for inline child lists and other features
129//! - [`code_actions`]: Quick fixes and refactoring actions
130
131#![cfg_attr(not(test), warn(missing_docs))]
132/// Document analysis for HEDL files.
133pub mod analysis;
134mod backend;
135/// Code actions for HEDL LSP.
136pub mod code_actions;
137/// Autocompletion for HEDL files.
138pub mod completion;
139/// LSP constants and magic number definitions.
140pub mod constants;
141/// LSP-specific diagnostics for HEDL files.
142pub mod diagnostics;
143/// Document management with caching and LRU eviction.
144pub mod document_manager;
145/// Hover information for HEDL files.
146pub mod hover;
147/// Reference index for fast definition and reference lookups.
148pub mod reference_index;
149/// Rename refactoring for HEDL symbols.
150pub mod rename;
151/// Document symbols for HEDL files.
152pub mod symbols;
153/// Safe string handling utilities for LSP encoding operations.
154pub mod utf_encoding;
155
156#[cfg(test)]
157mod tests;
158
159pub use backend::HedlLanguageServer;
160pub use document_manager::{CacheStatistics, DocumentCache};
161
162/// LSP server version
163pub const VERSION: &str = env!("CARGO_PKG_VERSION");