ad-astra 1.0.0

Embeddable scripting language platform Ad Astra. Main Crate.
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
////////////////////////////////////////////////////////////////////////////////
// This file is part of "Ad Astra", an embeddable scripting programming       //
// language platform.                                                         //
//                                                                            //
// This work is proprietary software with source-available code.              //
//                                                                            //
// To copy, use, distribute, or contribute to this work, you must agree to    //
// the terms of the General License Agreement:                                //
//                                                                            //
// https://github.com/Eliah-Lakhin/ad-astra/blob/master/EULA.md               //
//                                                                            //
// The agreement grants a Basic Commercial License, allowing you to use       //
// this work in non-commercial and limited commercial products with a total   //
// gross revenue cap. To remove this commercial limit for one of your         //
// products, you must acquire a Full Commercial License.                      //
//                                                                            //
// If you contribute to the source code, documentation, or related materials, //
// you must grant me an exclusive license to these contributions.             //
// Contributions are governed by the "Contributions" section of the General   //
// License Agreement.                                                         //
//                                                                            //
// Copying the work in parts is strictly forbidden, except as permitted       //
// under the General License Agreement.                                       //
//                                                                            //
// If you do not or cannot agree to the terms of this Agreement,              //
// do not use this work.                                                      //
//                                                                            //
// This work is provided "as is", without any warranties, express or implied, //
// except where such disclaimers are legally invalid.                         //
//                                                                            //
// Copyright (c) 2024 Ilya Lakhin (Илья Александрович Лахин).                 //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

use std::{net::SocketAddr, time::Duration};

use log::{Level, LevelFilter};
use lsp_types::{ClientCapabilities, MarkupKind};

/// A general configuration object for the Language Server.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub struct LspServerConfig {
    /// If set to true, the server's job manager will use dedicated threads.
    /// Otherwise, all LSP messages will be handled by a single thread.
    ///
    /// In general, multi-threading mode improves the server's performance.
    ///
    /// The default value is true unless the build target is a wasm target.
    /// For wasm targets, the default value is false.
    pub multi_thread: bool,

    /// If set to true, the server provides script running functionality in
    /// the code editor.
    ///
    /// The script runner also requires the `capabilities.code_lens`,
    /// `capabilities.execute_command`, and `multi_thread` flags to be enabled.
    ///
    /// The default value of this option is true if the build target is not a
    /// wasm target; otherwise, the default value is false.
    pub scripts_runner: bool,

    /// If specified, the server spawns a dedicated thread that periodically
    /// checks the health of other tasks. If a worker thread is not responding,
    /// the server attempts to cancel its task and respawn the thread.
    ///
    /// This option requires the `multi_thread` flag to be enabled.
    ///
    /// By default, the `health_check` feature is enabled and set to 5 seconds
    /// for non-wasm build targets. Otherwise, the feature is disabled.
    pub health_check: Option<Duration>,

    /// The name of the script language.
    ///
    /// The default value is "adastra", but you can rename the language using
    /// this option.
    pub language_id: &'static str,

    /// The extension of script files (e.g., "foo_file.<extension>").
    ///
    /// The language server filters files opened by the client based on their
    /// extension.
    ///
    /// The default value is "adastra", but you can change the file extension
    /// using this option.
    pub file_ext: &'static str,

    /// Configures the client-side and server-side logger.
    pub logger: LspLoggerConfig,

    /// Configures the LSP capabilities of the server.
    pub capabilities: LspCapabilities,
}

impl Default for LspServerConfig {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl LspServerConfig {
    /// The default constructor for this configuration object.
    #[inline(always)]
    pub const fn new() -> Self {
        let multi_thread;
        let scripts_runtime;
        let health_check;

        #[cfg(not(target_family = "wasm"))]
        {
            multi_thread = true;
            scripts_runtime = true;
            health_check = Some(Duration::from_secs(5));
        }

        #[cfg(target_family = "wasm")]
        {
            multi_thread = false;
            scripts_runtime = false;
            health_check = None;
        }

        Self {
            multi_thread,
            scripts_runner: scripts_runtime,
            health_check,
            language_id: "adastra",
            file_ext: "adastra",
            logger: LspLoggerConfig::new(),
            capabilities: LspCapabilities::new(),
        }
    }
}

/// An LSP capabilities configuration object for the Language Server.
///
/// By default, all flags are set to true.
///
/// Note that these features will only be available in the editor if both the
/// server and the client (the editor) support the corresponding capabilities.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub struct LspCapabilities {
    /// The server performs ongoing analysis for script errors and warnings,
    /// and periodically publishes diagnostic results to the client.
    pub publish_diagnostics: bool,

    /// When enabled, the editor shows inlay hints in the source code text that
    /// indicate value types and function signatures.
    pub inlay_hints: bool,

    /// When enabled, the editor supports script code formatting.
    pub formatting: bool,

    /// When enabled, the editor supports code completion features.
    pub completion: bool,

    /// When enabled, the completion menu uses a Markdown renderer for the
    /// description text related to completion suggestions.
    ///
    /// This capability will be ignored if the `completion` flag is disabled.
    pub completion_markdown: bool,

    /// When enabled, the completion menu includes snippets for common
    /// script language constructs.
    ///
    /// This capability will be ignored if the `completion` flag is disabled.
    pub completion_snippets: bool,

    /// When enabled, the editor shows hint text when the user moves the
    /// cursor over code symbols. For example, this might display RustDoc
    /// documentation related to a function or a field.
    pub hover: bool,

    /// When enabled, the editor uses a Markdown renderer for hover hints.
    ///
    /// This capability will be ignored if the `hover` flag is disabled.
    pub hover_markdown: bool,

    /// When enabled, the editor provides a jump-to-definition feature for
    /// code symbols. For example, this allows jumping to a variable's
    /// definition from a variable reference.
    pub goto_definition: bool,

    /// When enabled, the editor highlights related code symbols when the cursor
    /// touches them. For example, the editor will highlight all occurrences
    /// of a variable declaration and all references to that variable.
    pub document_highlight: bool,

    /// When enabled, the editor provides a jump-to-implementation feature for
    /// code symbols. For example, the user can jump from a function call to its
    /// implementation.
    pub goto_implementation: bool,

    /// When enabled, the editor provides quickfix refactoring suggestions to
    /// address some of the diagnostic messages.
    ///
    /// This feature also requires the `publish_diagnostics` capability to be
    /// enabled.
    pub code_action: bool,

    /// When enabled, the editor shows a function's signature description when
    /// the user attempts to call the function inside the script.
    pub signature_help: bool,

    /// When enabled, the signature helper uses a Markdown renderer to display
    /// the signature text.
    ///
    /// This capability will be ignored if the `signature_help` flag is
    /// disabled.
    pub signature_help_markdown: bool,

    /// When enabled, the editor allows renaming identifiers.
    pub rename: bool,

    /// When enabled, the renaming feature in the editor will be more
    /// interactive, highlighting related symbols that are subject to renaming.
    ///
    /// This capability will be ignored if the `rename` flag is disabled.
    pub rename_prepare: bool,

    /// When enabled, the editor automatically renames related identifiers as
    /// the user edits one of them, without requiring the user to enter renaming
    /// mode.
    pub linked_editing_range: bool,

    /// When enabled, the editor supports common code actions, such as running
    /// code.
    ///
    /// This capability and the `execute_command` capability are required for
    /// the script execution feature.
    pub code_lens: bool,

    /// When enabled, the editor supports common code actions, such as running
    /// code.
    ///
    /// This capability and the `code_lens` capability are required for
    /// the script execution feature.
    pub execute_command: bool,
}

impl Default for LspCapabilities {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl LspCapabilities {
    /// The default constructor that enables all server capabilities.
    #[inline(always)]
    pub const fn new() -> Self {
        Self {
            publish_diagnostics: true,
            inlay_hints: true,
            formatting: true,
            completion: true,
            completion_markdown: true,
            completion_snippets: true,
            hover: true,
            hover_markdown: true,
            goto_definition: true,
            document_highlight: true,
            goto_implementation: true,
            code_action: true,
            signature_help: true,
            signature_help_markdown: true,
            rename: true,
            rename_prepare: true,
            linked_editing_range: true,
            code_lens: true,
            execute_command: true,
        }
    }

    /// A constructor that disables all server capabilities.
    #[inline(always)]
    pub const fn none() -> Self {
        Self {
            publish_diagnostics: false,
            inlay_hints: false,
            formatting: false,
            completion: false,
            completion_markdown: false,
            completion_snippets: false,
            hover: false,
            hover_markdown: false,
            goto_definition: false,
            document_highlight: false,
            goto_implementation: false,
            code_action: false,
            signature_help: false,
            signature_help_markdown: false,
            rename: false,
            rename_prepare: false,
            linked_editing_range: false,
            code_lens: false,
            execute_command: false,
        }
    }

    pub(super) fn from_client(capabilities: &ClientCapabilities) -> Self {
        let mut result = Self::none();

        if let Some(text_document) = &capabilities.text_document {
            result.publish_diagnostics = text_document.publish_diagnostics.is_some();
            result.inlay_hints = text_document.inlay_hint.is_some();
            result.formatting = text_document.formatting.is_some();

            if let Some(completion) = &text_document.completion {
                result.completion = true;

                if let Some(completion_item) = &completion.completion_item {
                    if let Some(documentation_format) = &completion_item.documentation_format {
                        result.completion_markdown = documentation_format
                            .iter()
                            .any(|kind| kind == &MarkupKind::Markdown);
                    }

                    result.completion_snippets = completion_item
                        .snippet_support
                        .filter(|flag| *flag == true)
                        .is_some()
                }
            }

            if let Some(hover) = &text_document.hover {
                result.hover = true;

                if let Some(content_format) = &hover.content_format {
                    result.hover_markdown = content_format
                        .iter()
                        .any(|kind| kind == &MarkupKind::Markdown);
                }
            }

            result.goto_definition = text_document.definition.is_some();
            result.document_highlight = text_document.document_highlight.is_some();
            result.goto_implementation = text_document.implementation.is_some();
            result.code_action = text_document.code_action.is_some();

            if let Some(signature_help) = &text_document.signature_help {
                result.signature_help = true;

                if let Some(signature_information) = &signature_help.signature_information {
                    if let Some(documentation_format) = &signature_information.documentation_format
                    {
                        result.signature_help_markdown = documentation_format
                            .iter()
                            .any(|kind| kind == &MarkupKind::Markdown);
                    }
                }
            }

            if let Some(rename) = &text_document.rename {
                result.rename = true;
                result.rename_prepare = rename.prepare_support.filter(|flag| *flag).is_some()
            }

            result.linked_editing_range = text_document.linked_editing_range.is_some();
            result.code_lens = text_document.code_lens.is_some();
        }

        if let Some(workspace) = &capabilities.workspace {
            result.execute_command = workspace.execute_command.is_some();
        }

        result
    }

    #[inline(always)]
    pub(super) fn intersect(&mut self, other: Self) {
        self.publish_diagnostics = self.publish_diagnostics && other.publish_diagnostics;
        self.inlay_hints = self.inlay_hints && other.inlay_hints;
        self.formatting = self.formatting && other.formatting;
        self.completion = self.completion && other.completion;
        self.completion_markdown = self.completion_markdown && other.completion_markdown;
        self.completion_snippets = self.completion_snippets && other.completion_snippets;
        self.hover = self.hover && other.hover;
        self.hover_markdown = self.hover_markdown && other.hover_markdown;
        self.goto_definition = self.goto_definition && other.goto_definition;
        self.document_highlight = self.document_highlight && other.document_highlight;
        self.goto_implementation = self.goto_implementation && other.goto_implementation;
        self.code_action = self.code_action && other.code_action;
        self.signature_help = self.signature_help && other.signature_help;
        self.signature_help_markdown =
            self.signature_help_markdown && other.signature_help_markdown;
        self.rename = self.rename && other.rename;
        self.rename_prepare = self.rename_prepare && other.rename_prepare;
        self.linked_editing_range = self.linked_editing_range && other.linked_editing_range;
        self.code_lens = self.code_lens && other.code_lens;
        self.execute_command = self.execute_command && other.execute_command;
    }
}

/// A configuration of the communication channel between the server and
/// the client (editor).
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum LspTransportConfig {
    /// Communication will occur through the STD-IO stream of the server's
    /// process.
    ///
    /// This is the default option, widely supported by the majority
    /// of code editors (clients).
    Stdio,

    /// The server will connect to the specified TCP socket, opened by the
    /// client.
    TcpClient(SocketAddr),

    /// The server will open the specified TCP socket, and the client will
    /// connect to this socket.
    TcpServer(SocketAddr),
}

impl Default for LspTransportConfig {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl LspTransportConfig {
    /// The default constructor for this enum.
    ///
    /// The default variant is `Stdio`, representing the communication stream
    /// of the server's process.
    #[inline(always)]
    pub const fn new() -> Self {
        Self::Stdio
    }
}

/// A configuration for the server and client loggers.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub struct LspLoggerConfig {
    /// If set to false, both client and server logging will be disabled,
    /// and the rest of the configuration options will be ignored.
    pub enabled: bool,

    /// The general logging level.
    ///
    /// In debug builds, the default option is [LevelFilter::Debug], while in
    /// production builds, the default value is [LevelFilter::Info].
    pub level: LevelFilter,

    /// Configuration of the log messages that will be shown in the editor.
    pub client: LspLoggerClientConfig,

    /// Configuration of the log messages that will be shown on the server side.
    ///
    /// Typically, the server logs also include client log messages. However,
    /// the server logs contain more detailed messages useful for debugging,
    /// which are less relevant for the editor's user.
    pub server: LspLoggerServerConfig,
}

impl Default for LspLoggerConfig {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl LspLoggerConfig {
    /// The default constructor for this configuration object.
    #[inline(always)]
    pub const fn new() -> Self {
        #[cfg(debug_assertions)]
        let level = LevelFilter::Debug;

        #[cfg(not(debug_assertions))]
        let level = LevelFilter::Info;

        Self {
            enabled: true,
            level,
            client: LspLoggerClientConfig::new(),
            server: LspLoggerServerConfig::new(),
        }
    }

    /// Sets the `enabled` flag to false, effectively disabling the loggers.
    #[inline(always)]
    pub const fn disabled() -> Self {
        Self {
            enabled: false,
            ..Self::new()
        }
    }
}

/// A configuration for the client-side logger (editor logger).
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum LspLoggerClientConfig {
    /// The server will not send any log messages to the client.
    Off,

    /// The log messages sent from the server to the client will be shown
    /// in the trace channel of the editor.
    Trace,

    /// The log messages sent from the server to the client will be shown
    /// in the debug window of the editor.
    ///
    /// This is the default variant.
    Window,
}

impl Default for LspLoggerClientConfig {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl LspLoggerClientConfig {
    /// The default constructor for this enum.
    ///
    /// The default variant is `Window`, meaning that log messages will be
    /// shown in the editor's debug window.
    #[inline(always)]
    pub const fn new() -> Self {
        Self::Window
    }
}

/// A configuration for the server-side logger.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum LspLoggerServerConfig {
    /// The server will not show any log messages on the server side.
    Off,

    /// The server prints log messages to the STDERR stream of the process.
    ///
    /// This is the default option.
    Stderr,

    /// The server prints log messages to the syslog. This option is only
    /// available on Unix operating systems.
    Syslog,

    /// A custom logger. The server-side log messages will be sent to the
    /// specified function.
    ///
    /// This feature is particularly useful for wasm builds of the server,
    /// intended to run in browser-based WASM modules.
    ///
    /// In WASM environments, general logging options are not available
    /// (WASM does not have an STDERR stream and does not have access to
    /// syslog). Using this function, you can set up a separate logging channel
    /// that sends log messages to the WASM host.
    Custom(fn(Level, String)),
}

impl Default for LspLoggerServerConfig {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl LspLoggerServerConfig {
    /// The default constructor for this enum.
    ///
    /// The default variant is `Stderr`, meaning that log messages will be
    /// sent to the STDERR stream of the server's process.
    #[inline(always)]
    pub const fn new() -> Self {
        return Self::Stderr;
    }
}