textDocument/prepareTypeHierarchy + typeHierarchy/supertypes/subtypes for SystemVerilog classes. Supertypes is one computed offset into the existing resolution machinery — the overlay promotion made cross-file bare class names answerable, and the offset is the LAST byte of the extends name span so pkg::base lands in the tail's reference, never the package's. Subtypes is a workspace census on the callers_of_each pattern (one sources() pass, prefilter, deterministic sort), matching the base name as written — 0 of the corpus's 270 extends clauses are qualified, and the asymmetry with supertypes is documented and pinned from both directions, including the same-name/different-file consequence. Ambiguous bases answer every declaration, like definition. Modules stay on callHierarchy by scope ruling. lsp-types 0.97 has no server-side typeHierarchyProvider field, so initialize_value() inserts the one key into the serialized result — QA's strip-and-compare test proves every other key survives with identical values. QA also traced the parser to show the degenerate extends spans the code guards against cannot be constructed, corrected two README figures the lead applied (the 309 extends count did not reproduce; 270 does), and pinned the generate-block walk gap as a scoped choice. Interface classes stay blocked on the parser, pinned. 45 tests added (dev 33 + QA 12), including the real ibex UVM chain and a 10-deep expansion. The inheritance-edge index want is filed beside the instantiation-edge Handshake as one future round. docs/features/type-hierarchy/README.md Co-Authored-By: Claude Fable 5 <[email protected]>
773 lines
35 KiB
Rust
773 lines
35 KiB
Rust
//! `lucisv-server` — the lucisv language server, speaking LSP over stdio.
|
|
//!
|
|
//! One binary for every editor: VS Code through a thin extension, Vim/Neovim
|
|
//! through built-in LSP config. No editor-specific logic lives below the
|
|
//! protocol layer, and no language logic lives in it — every answer comes from
|
|
//! `vlog_parser`, `vlog_semantic` or `vlog_formatter`. See
|
|
//! `docs/features/lucisv-server/README.md`.
|
|
|
|
mod candidates;
|
|
mod code_action;
|
|
mod completion;
|
|
mod convert;
|
|
mod diagnostics;
|
|
mod expand;
|
|
mod folding;
|
|
mod format;
|
|
mod hierarchy;
|
|
mod hover;
|
|
mod includes;
|
|
mod inlay;
|
|
mod inline;
|
|
mod instantiations;
|
|
mod lens;
|
|
mod linked;
|
|
mod lint;
|
|
mod locate;
|
|
mod nav;
|
|
mod on_type;
|
|
mod pairs;
|
|
mod ports;
|
|
mod rename;
|
|
mod selection;
|
|
mod server;
|
|
mod settings;
|
|
mod signature;
|
|
mod state;
|
|
mod symbols;
|
|
mod sync;
|
|
mod templates;
|
|
mod tokens;
|
|
mod type_def;
|
|
mod type_hierarchy;
|
|
mod uri;
|
|
mod watch;
|
|
|
|
use std::error::Error;
|
|
use std::path::PathBuf;
|
|
|
|
use lsp_server::Connection;
|
|
use lsp_types::{
|
|
CallHierarchyServerCapability, CodeActionKind, CodeActionOptions, CodeActionProviderCapability,
|
|
CodeLensOptions, CompletionOptions, DiagnosticOptions, DiagnosticServerCapabilities,
|
|
DocumentLinkOptions, DocumentOnTypeFormattingOptions, ExecuteCommandOptions,
|
|
FoldingRangeProviderCapability, InitializeParams, InlayHintOptions,
|
|
InlayHintServerCapabilities, LinkedEditingRangeServerCapabilities, OneOf, PositionEncodingKind,
|
|
RenameOptions, SaveOptions, SelectionRangeProviderCapability, SemanticTokensFullOptions,
|
|
SemanticTokensLegend, SemanticTokensOptions, SemanticTokensServerCapabilities,
|
|
ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind,
|
|
TextDocumentSyncOptions, TextDocumentSyncSaveOptions, TypeDefinitionProviderCapability,
|
|
WorkDoneProgressOptions,
|
|
};
|
|
|
|
use crate::server::{ClientAbilities, Server};
|
|
use crate::uri::uri_to_path;
|
|
|
|
fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
|
|
let (connection, io_threads) = Connection::stdio();
|
|
|
|
let (id, params) = connection.initialize_start()?;
|
|
// A client that sends params we cannot read still gets a server; every
|
|
// field we want is optional anyway.
|
|
let params: InitializeParams = serde_json::from_value(params).unwrap_or_default();
|
|
let roots = workspace_roots(¶ms);
|
|
let abilities = ClientAbilities {
|
|
snippets: snippet_support(¶ms),
|
|
file_watching: watch::dynamic_registration(¶ms),
|
|
apply_edit: apply_edit_support(¶ms),
|
|
pull_diagnostics: pull_diagnostics_support(¶ms),
|
|
work_done_progress: work_done_progress_support(¶ms),
|
|
code_lens_refresh: code_lens_refresh_support(¶ms),
|
|
show_document: show_document_support(¶ms),
|
|
};
|
|
connection.initialize_finish(id, initialize_value()?)?;
|
|
|
|
Server::new(connection, roots, abilities).run()?;
|
|
io_threads.join()?;
|
|
Ok(())
|
|
}
|
|
|
|
/// What the server tells the client it can do. Nothing is advertised that is
|
|
/// not implemented: a capability an editor discovers to be a lie is worse than
|
|
/// one it never had.
|
|
fn capabilities() -> ServerCapabilities {
|
|
ServerCapabilities {
|
|
// The protocol default, and what the conversion in `convert.rs`
|
|
// implements. Stated rather than left implicit.
|
|
position_encoding: Some(PositionEncodingKind::UTF16),
|
|
// Incremental sync: a keystroke costs one range, not the whole file.
|
|
// Spelled out as options rather than a bare kind because a bare kind
|
|
// says nothing about open/close or save, and clients fill that in
|
|
// themselves — this states the same defaults a bare kind resolved to,
|
|
// so nothing but `change` moves. `includeText: false` keeps a save
|
|
// from re-sending a buffer the server already tracks edit by edit;
|
|
// the `didSave` handler still honours text if a client sends it.
|
|
text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions {
|
|
open_close: Some(true),
|
|
change: Some(TextDocumentSyncKind::INCREMENTAL),
|
|
// The plain `willSave` notification carries nothing this server
|
|
// can act on, so it is left unadvertised; `willSaveWaitUntil` is
|
|
// the format-on-save hook, for the editors that have no
|
|
// format-on-save of their own. It answers `null` unless
|
|
// `lucisv.formatOnSave` is on — see
|
|
// `docs/features/will-save-wait-until/README.md`.
|
|
will_save: None,
|
|
will_save_wait_until: Some(true),
|
|
save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
|
|
include_text: Some(false),
|
|
})),
|
|
})),
|
|
document_formatting_provider: Some(OneOf::Left(true)),
|
|
document_range_formatting_provider: Some(OneOf::Left(true)),
|
|
// Two trigger characters, and only two. A newline re-indents the line
|
|
// it opened; a `;` lays out the statement it ended, through the range
|
|
// engine. Not `}` — SV closes its blocks with the *word* `end`, and
|
|
// re-triggering on letters would run this on half of every identifier.
|
|
// Advertised unconditionally: on-type formatting only fires where the
|
|
// client's own `editor.formatOnType` is on, which is off by default,
|
|
// so the user has already opted in by the time a request arrives. See
|
|
// `docs/features/on-type-formatting/README.md`.
|
|
document_on_type_formatting_provider: Some(DocumentOnTypeFormattingOptions {
|
|
first_trigger_character: on_type::NEWLINE.to_string(),
|
|
more_trigger_character: Some(vec![on_type::SEMICOLON.to_string()]),
|
|
}),
|
|
definition_provider: Some(OneOf::Left(true)),
|
|
// The same jump one token to the left: from a name to where its type
|
|
// is declared. `Simple(true)` rather than a registration — there is
|
|
// nothing to scope it to that `definitionProvider` is not already
|
|
// scoped to. See `docs/features/type-definition/README.md`.
|
|
type_definition_provider: Some(TypeDefinitionProviderCapability::Simple(true)),
|
|
// SV's closing labels, edited as one name: `module m` and
|
|
// `endmodule : m` change together. `Simple(true)` rather than a
|
|
// registration — there is nothing to scope it to, and the handler
|
|
// declines every position that is not one of the two names. See
|
|
// `docs/features/linked-editing/README.md`.
|
|
linked_editing_range_provider: Some(LinkedEditingRangeServerCapabilities::Simple(true)),
|
|
references_provider: Some(OneOf::Left(true)),
|
|
// `` `include`` lines, underlined and ctrl-clickable. Every link is
|
|
// returned with its target already resolved, so there is nothing for
|
|
// `documentLink/resolve` to do — and go-to-definition answers the same
|
|
// lines, for the editors that have no ctrl-click.
|
|
document_link_provider: Some(DocumentLinkOptions {
|
|
resolve_provider: Some(false),
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
}),
|
|
// The cursor on a `begin` highlights its own `end`, matched over the
|
|
// token stream; anywhere else, the symbol under it highlights its own
|
|
// other occurrences in this file. The handler declines a position
|
|
// that is neither, which leaves the editor's word highlighting in
|
|
// charge there.
|
|
document_highlight_provider: Some(OneOf::Left(true)),
|
|
// One range per matched scope pair and per multi-line block comment,
|
|
// off the same matcher the rainbow reads.
|
|
folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)),
|
|
hover_provider: Some(lsp_types::HoverProviderCapability::Simple(true)),
|
|
document_symbol_provider: Some(OneOf::Left(true)),
|
|
workspace_symbol_provider: Some(OneOf::Left(true)),
|
|
// `prepareProvider` is what stops an editor from offering a rename the
|
|
// engine would refuse: the client asks first, and a refusal there costs
|
|
// the user nothing.
|
|
rename_provider: Some(OneOf::Right(RenameOptions {
|
|
prepare_provider: Some(true),
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
})),
|
|
// The kinds are listed rather than `Simple(true)` so a client can ask
|
|
// for one of them alone — which is how fix-on-save is configured.
|
|
code_action_provider: Some(CodeActionProviderCapability::Options(CodeActionOptions {
|
|
code_action_kinds: Some(vec![
|
|
CodeActionKind::QUICKFIX,
|
|
CodeActionKind::new(code_action::FIX_ALL),
|
|
CodeActionKind::new(ports::REFACTOR_PORTS),
|
|
]),
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
// Every action is returned complete; there is nothing to resolve.
|
|
resolve_provider: None,
|
|
})),
|
|
// The port refactorings. `lucisv.addPort` needs a declaration typed by
|
|
// the user, which no code action can carry, and a Vim user has no
|
|
// lightbulb at all — so all three are commands as well. The code
|
|
// lens's own jump joins them: a lens carries a `Command`, and this is
|
|
// where a command has to be declared to be runnable.
|
|
execute_command_provider: Some(ExecuteCommandOptions {
|
|
commands: commands(),
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
}),
|
|
// "N instantiations" over every module and interface declaration in
|
|
// the buffer, counted across the workspace. Every lens is returned
|
|
// complete — title, command and arguments — so there is nothing for
|
|
// `codeLens/resolve` to do, which is said out loud rather than left
|
|
// unset. See `docs/features/code-lens/README.md`.
|
|
code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(false) }),
|
|
// Module hinting. No trigger characters: an identifier being typed is
|
|
// what asks for these, and the handler declines every position that is
|
|
// not a module item, so an unprompted request costs a token scan.
|
|
completion_provider: Some(CompletionOptions {
|
|
// Every item is returned complete — skeleton, documentation and
|
|
// all — so there is nothing for `completionItem/resolve` to do.
|
|
resolve_provider: None,
|
|
trigger_characters: None,
|
|
all_commit_characters: None,
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
completion_item: None,
|
|
}),
|
|
// `(` opens a connection or parameter list and `,` moves to the next
|
|
// entry; between them they cover every place the ports are worth
|
|
// showing.
|
|
signature_help_provider: Some(SignatureHelpOptions {
|
|
trigger_characters: Some(
|
|
signature::TRIGGERS.iter().map(|t| (*t).to_string()).collect(),
|
|
),
|
|
retrigger_characters: None,
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
}),
|
|
// Port and parameter names on ordered connection lists. Every hint is
|
|
// returned complete — a label, a position and nothing to fetch later —
|
|
// so there is no `inlayHint/resolve`.
|
|
inlay_hint_provider: Some(OneOf::Right(InlayHintServerCapabilities::Options(
|
|
InlayHintOptions {
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
resolve_provider: None,
|
|
},
|
|
))),
|
|
// The module hierarchy, both directions: modules are the nodes and an
|
|
// instantiation is the call. Every item is complete when it is
|
|
// returned, and the client drives the expansion one hop at a time.
|
|
call_hierarchy_provider: Some(CallHierarchyServerCapability::Simple(true)),
|
|
// Ghost text for `if (` and `begin`, LSP 3.18. `true` rather than
|
|
// options: there is nothing to configure on the server side, and the
|
|
// one thing a user can say — `lucisv.inlineTemplates` — is a setting the
|
|
// handler reads, not a registration option. The VS Code client only
|
|
// sends the request after opting into `ProposedFeatures`, so an
|
|
// extension older than this server simply never asks.
|
|
inline_completion_provider: Some(OneOf::Left(true)),
|
|
// Expand-selection. Nothing to configure: the chain is computed from
|
|
// the buffer alone, and every position is answered.
|
|
selection_range_provider: Some(SelectionRangeProviderCapability::Simple(true)),
|
|
// Pull diagnostics (3.17), served from the same parse+lint pass the
|
|
// push model publishes. `inter_file_dependencies` because a save
|
|
// rebuilds the workspace index and *that* changes what other files
|
|
// report; `workspace_diagnostics` because `workspace/diagnostic` is
|
|
// answered — over the open buffers, which is the only scope this
|
|
// server has ever diagnosed.
|
|
diagnostic_provider: Some(DiagnosticServerCapabilities::Options(DiagnosticOptions {
|
|
identifier: Some(diagnostics::SOURCE.to_string()),
|
|
inter_file_dependencies: true,
|
|
workspace_diagnostics: true,
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
})),
|
|
semantic_tokens_provider: Some(SemanticTokensServerCapabilities::SemanticTokensOptions(
|
|
SemanticTokensOptions {
|
|
work_done_progress_options: WorkDoneProgressOptions::default(),
|
|
legend: SemanticTokensLegend {
|
|
token_types: tokens::token_types(),
|
|
token_modifiers: tokens::token_modifiers(),
|
|
},
|
|
// The visible lines, for the first paint of a large file:
|
|
// the same tokenizer, filtered, and re-encoded from the
|
|
// start of the document so the answer stands on its own.
|
|
range: Some(true),
|
|
// `delta: true` rather than a bare `full: true`, so a client
|
|
// that has an answer already asks what changed instead of
|
|
// re-fetching the file's whole token set on every keystroke.
|
|
// `full` itself stays served — the enum's `Delta` variant is
|
|
// the full capability plus deltas, not a replacement.
|
|
full: Some(SemanticTokensFullOptions::Delta { delta: Some(true) }),
|
|
},
|
|
)),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// Every command this server executes: the three port refactorings, and the
|
|
/// jump the code lens hangs off.
|
|
fn commands() -> Vec<String> {
|
|
let mut commands = ports::commands();
|
|
commands.extend(lens::commands());
|
|
commands
|
|
}
|
|
|
|
/// The `initialize` result exactly as it goes on the wire.
|
|
///
|
|
/// `typeHierarchyProvider` is written in here rather than in [`capabilities`]
|
|
/// because `lsp-types` 0.97 models type hierarchy on the *client* side only:
|
|
/// `ServerCapabilities` has no field for it, and `experimental` is a different
|
|
/// key that no client reads for this. So the one capability this server
|
|
/// advertises that the crate cannot spell is inserted into the serialized
|
|
/// object, which is the same JSON a field would have produced.
|
|
fn initialize_value() -> Result<serde_json::Value, serde_json::Error> {
|
|
let mut value = serde_json::to_value(initialize_result())?;
|
|
if let Some(caps) = value.get_mut("capabilities").and_then(serde_json::Value::as_object_mut) {
|
|
caps.insert("typeHierarchyProvider".to_string(), serde_json::Value::Bool(true));
|
|
}
|
|
Ok(value)
|
|
}
|
|
|
|
fn initialize_result() -> lsp_types::InitializeResult {
|
|
lsp_types::InitializeResult {
|
|
capabilities: capabilities(),
|
|
server_info: Some(lsp_types::ServerInfo {
|
|
name: "lucisv-server".to_string(),
|
|
version: Some(env!("CARGO_PKG_VERSION").to_string()),
|
|
}),
|
|
// `lsp-types`' `proposed` feature adds an `offsetEncoding` field here
|
|
// (clangd's unofficial extension). It is not ours to send.
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// Whether the client can expand `${1:x}` tabstops in a completion item.
|
|
///
|
|
/// A client that did not say so gets the skeleton as plain text: a literal
|
|
/// `${1:clk}` written into a buffer is worse than no tabstops at all, and the
|
|
/// protocol gives no way to find out after the fact.
|
|
fn snippet_support(params: &InitializeParams) -> bool {
|
|
params
|
|
.capabilities
|
|
.text_document
|
|
.as_ref()
|
|
.and_then(|text_document| text_document.completion.as_ref())
|
|
.and_then(|completion| completion.completion_item.as_ref())
|
|
.and_then(|item| item.snippet_support)
|
|
.unwrap_or(false)
|
|
}
|
|
|
|
/// Whether the client applies a `workspace/applyEdit` request.
|
|
///
|
|
/// A client that did not say so gets a command's edit back as the request
|
|
/// result — which is what every client got before this was read, and is the
|
|
/// only thing left to try for one that cannot be pushed an edit.
|
|
fn apply_edit_support(params: &InitializeParams) -> bool {
|
|
params
|
|
.capabilities
|
|
.workspace
|
|
.as_ref()
|
|
.and_then(|workspace| workspace.apply_edit)
|
|
.unwrap_or(false)
|
|
}
|
|
|
|
/// Whether the client pulls diagnostics (LSP 3.17).
|
|
///
|
|
/// Sending the capability at all is what says so; its two fields are about
|
|
/// dynamic registration and related documents, neither of which this server
|
|
/// uses. A client that says nothing keeps the push model unchanged.
|
|
fn pull_diagnostics_support(params: &InitializeParams) -> bool {
|
|
params
|
|
.capabilities
|
|
.text_document
|
|
.as_ref()
|
|
.is_some_and(|text_document| text_document.diagnostic.is_some())
|
|
}
|
|
|
|
/// Whether the client renders server-initiated work-done progress.
|
|
///
|
|
/// The spec forbids a server from creating progress at all without this, so a
|
|
/// client that said nothing is sent no `window/workDoneProgress/create` and no
|
|
/// `$/progress` — the workspace rescan is silent for it, exactly as it was
|
|
/// before this was read.
|
|
fn work_done_progress_support(params: &InitializeParams) -> bool {
|
|
params
|
|
.capabilities
|
|
.window
|
|
.as_ref()
|
|
.and_then(|window| window.work_done_progress)
|
|
.unwrap_or(false)
|
|
}
|
|
|
|
/// Whether the client can be asked to re-request its code lenses.
|
|
///
|
|
/// `workspace/codeLens/refresh` is server-initiated, so the spec makes this
|
|
/// capability the gate. A client that said nothing keeps whatever counts it
|
|
/// last asked for until something else makes it ask again — which is what
|
|
/// every client got before code lenses existed at all.
|
|
fn code_lens_refresh_support(params: &InitializeParams) -> bool {
|
|
params
|
|
.capabilities
|
|
.workspace
|
|
.as_ref()
|
|
.and_then(|workspace| workspace.code_lens.as_ref())
|
|
.and_then(|code_lens| code_lens.refresh_support)
|
|
.unwrap_or(false)
|
|
}
|
|
|
|
/// Whether the client opens a document the server asks it to.
|
|
///
|
|
/// This is what makes the instantiation lens actually navigate: an
|
|
/// `executeCommand` result cannot move a cursor, so the jump is a pushed
|
|
/// `window/showDocument`. A client that did not advertise it still gets the
|
|
/// sites back as the command's result.
|
|
fn show_document_support(params: &InitializeParams) -> bool {
|
|
params
|
|
.capabilities
|
|
.window
|
|
.as_ref()
|
|
.and_then(|window| window.show_document.as_ref())
|
|
.is_some_and(|show_document| show_document.support)
|
|
}
|
|
|
|
/// The directories to index: the client's workspace folders, or the
|
|
/// deprecated single root when that is all it sent.
|
|
fn workspace_roots(params: &InitializeParams) -> Vec<PathBuf> {
|
|
if let Some(folders) = ¶ms.workspace_folders {
|
|
let roots: Vec<PathBuf> = folders.iter().filter_map(|f| uri_to_path(&f.uri)).collect();
|
|
if !roots.is_empty() {
|
|
return roots;
|
|
}
|
|
}
|
|
#[allow(deprecated)] // `root_uri` is how older clients name the workspace.
|
|
params.root_uri.as_ref().and_then(uri_to_path).into_iter().collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn every_advertised_capability_is_implemented() {
|
|
let caps = capabilities();
|
|
assert!(caps.document_formatting_provider.is_some());
|
|
assert!(caps.document_range_formatting_provider.is_some());
|
|
assert!(caps.definition_provider.is_some());
|
|
assert!(caps.type_definition_provider.is_some());
|
|
assert!(caps.linked_editing_range_provider.is_some());
|
|
assert!(caps.references_provider.is_some());
|
|
assert!(caps.document_link_provider.is_some());
|
|
assert!(caps.document_highlight_provider.is_some());
|
|
assert!(caps.folding_range_provider.is_some());
|
|
assert!(caps.hover_provider.is_some());
|
|
assert!(caps.document_symbol_provider.is_some());
|
|
assert!(caps.workspace_symbol_provider.is_some());
|
|
assert!(caps.semantic_tokens_provider.is_some());
|
|
assert!(caps.code_action_provider.is_some());
|
|
assert!(caps.rename_provider.is_some());
|
|
assert!(caps.execute_command_provider.is_some());
|
|
assert!(caps.completion_provider.is_some());
|
|
assert!(caps.signature_help_provider.is_some());
|
|
assert!(caps.inlay_hint_provider.is_some());
|
|
assert!(caps.call_hierarchy_provider.is_some());
|
|
assert!(caps.inline_completion_provider.is_some());
|
|
assert!(caps.selection_range_provider.is_some());
|
|
assert!(caps.diagnostic_provider.is_some());
|
|
assert!(caps.code_lens_provider.is_some());
|
|
assert!(caps.document_on_type_formatting_provider.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn the_type_hierarchy_is_advertised_even_though_lsp_types_cannot_spell_it() {
|
|
// `ServerCapabilities` has no `type_hierarchy_provider` field in
|
|
// lsp-types 0.97 — the crate models type hierarchy on the client side
|
|
// only — so the capability is written into the serialized object. A
|
|
// client keys entirely off this key: without it the three requests are
|
|
// never sent, however well they are implemented.
|
|
let value = initialize_value().expect("the initialize result serializes");
|
|
assert_eq!(
|
|
value["capabilities"]["typeHierarchyProvider"],
|
|
serde_json::json!(true),
|
|
"{value:#?}"
|
|
);
|
|
// And the rest of the object is untouched by the insertion.
|
|
assert_eq!(value["capabilities"]["callHierarchyProvider"], serde_json::json!(true));
|
|
assert_eq!(value["serverInfo"]["name"], serde_json::json!("lucisv-server"));
|
|
}
|
|
|
|
// QA (2026-08-26): the spot checks above name two fields; this pins the
|
|
// whole object instead. `initialize_result()` and `capabilities()` are
|
|
// untouched by this round (confirmed against `24e8eab`), so the only way
|
|
// the insertion could disturb anything is the one `caps.insert` call
|
|
// itself — this proves that byte-for-byte rather than by inspection: pull
|
|
// the one inserted key back out and what remains must equal the plain,
|
|
// uninserted serialization exactly. A future round that lets the
|
|
// insertion clobber an existing key, or that adds a field to
|
|
// `ServerCapabilities` under the same JSON name, fails this immediately.
|
|
#[test]
|
|
fn the_json_insertion_disturbs_nothing_else_in_the_initialize_result() {
|
|
let mut with_insertion = initialize_value().expect("the initialize result serializes");
|
|
let plain = serde_json::to_value(initialize_result()).expect("serializes on its own too");
|
|
let caps =
|
|
with_insertion["capabilities"].as_object_mut().expect("capabilities is a JSON object");
|
|
assert_eq!(
|
|
caps.remove("typeHierarchyProvider"),
|
|
Some(serde_json::json!(true)),
|
|
"the one key the insertion is supposed to add"
|
|
);
|
|
assert_eq!(
|
|
with_insertion, plain,
|
|
"removing the inserted key must restore exactly what capabilities() \
|
|
and initialize_result() produce on their own, key for key, value for value"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn on_type_formatting_asks_for_the_newline_and_the_semicolon_and_nothing_else() {
|
|
// The trigger set is the feature: a client sends this request on every
|
|
// one of these characters and on no other, so what is listed here is
|
|
// exactly what `on_type::on_type_formatting` knows how to answer. `}`
|
|
// is deliberately absent — SV closes its blocks with the word `end`.
|
|
let options = capabilities().document_on_type_formatting_provider.expect("advertised");
|
|
assert_eq!(options.first_trigger_character, "\n");
|
|
assert_eq!(options.more_trigger_character, Some(vec![";".to_string()]));
|
|
}
|
|
|
|
#[test]
|
|
fn the_semantic_token_capability_asks_for_ranges_and_for_deltas() {
|
|
// A client keys entirely off this: it sends `full/delta` only where
|
|
// `full.delta` is `true`, and `range` only where `range` is.
|
|
let Some(SemanticTokensServerCapabilities::SemanticTokensOptions(options)) =
|
|
capabilities().semantic_tokens_provider
|
|
else {
|
|
panic!("the options form, not a registration");
|
|
};
|
|
assert_eq!(options.range, Some(true));
|
|
assert_eq!(options.full, Some(SemanticTokensFullOptions::Delta { delta: Some(true) }));
|
|
assert_eq!(options.legend.token_types.len(), 14, "the legend is unchanged");
|
|
}
|
|
|
|
#[test]
|
|
fn the_sync_options_ask_for_the_save_hook_and_not_the_notification() {
|
|
let Some(TextDocumentSyncCapability::Options(options)) = capabilities().text_document_sync
|
|
else {
|
|
panic!("the options form, not a bare kind");
|
|
};
|
|
assert_eq!(options.change, Some(TextDocumentSyncKind::INCREMENTAL));
|
|
assert_eq!(options.will_save_wait_until, Some(true), "the format-on-save hook is answered");
|
|
assert_eq!(options.will_save, None, "the plain notification has nothing for us");
|
|
}
|
|
|
|
#[test]
|
|
fn the_diagnostic_options_say_what_this_server_actually_does() {
|
|
let Some(DiagnosticServerCapabilities::Options(options)) =
|
|
capabilities().diagnostic_provider
|
|
else {
|
|
panic!("plain options, not a dynamic registration");
|
|
};
|
|
assert_eq!(options.identifier.as_deref(), Some(diagnostics::SOURCE), "same tag as a push");
|
|
assert!(options.inter_file_dependencies, "a save changes other files' diagnostics");
|
|
assert!(options.workspace_diagnostics, "`workspace/diagnostic` is answered");
|
|
}
|
|
|
|
#[test]
|
|
fn apply_edit_support_is_read_from_the_clients_own_capabilities() {
|
|
use lsp_types::{ClientCapabilities, WorkspaceClientCapabilities};
|
|
assert!(!apply_edit_support(&InitializeParams::default()), "silence means no");
|
|
|
|
let saying = |apply_edit: Option<bool>| InitializeParams {
|
|
capabilities: ClientCapabilities {
|
|
workspace: Some(WorkspaceClientCapabilities { apply_edit, ..Default::default() }),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
assert!(apply_edit_support(&saying(Some(true))));
|
|
assert!(!apply_edit_support(&saying(Some(false))));
|
|
assert!(!apply_edit_support(&saying(None)));
|
|
}
|
|
|
|
#[test]
|
|
fn work_done_progress_support_is_read_from_the_clients_own_capabilities() {
|
|
use lsp_types::{ClientCapabilities, WindowClientCapabilities};
|
|
assert!(!work_done_progress_support(&InitializeParams::default()), "silence means no");
|
|
|
|
let saying = |work_done_progress: Option<bool>| InitializeParams {
|
|
capabilities: ClientCapabilities {
|
|
window: Some(WindowClientCapabilities { work_done_progress, ..Default::default() }),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
assert!(work_done_progress_support(&saying(Some(true))));
|
|
assert!(!work_done_progress_support(&saying(Some(false))));
|
|
assert!(!work_done_progress_support(&saying(None)));
|
|
}
|
|
|
|
#[test]
|
|
fn a_client_that_sent_the_diagnostic_capability_at_all_is_a_pulling_client() {
|
|
use lsp_types::{
|
|
ClientCapabilities, DiagnosticClientCapabilities, TextDocumentClientCapabilities,
|
|
};
|
|
assert!(!pull_diagnostics_support(&InitializeParams::default()), "silence means push");
|
|
|
|
let pulling = InitializeParams {
|
|
capabilities: ClientCapabilities {
|
|
text_document: Some(TextDocumentClientCapabilities {
|
|
// Both fields left unset: sending the capability is the
|
|
// whole of the statement, and neither field is about
|
|
// whether the client pulls.
|
|
diagnostic: Some(DiagnosticClientCapabilities::default()),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
assert!(pull_diagnostics_support(&pulling));
|
|
}
|
|
|
|
#[test]
|
|
fn the_call_hierarchy_is_advertised_as_a_plain_capability() {
|
|
// Nothing about it is optional or resolvable: the three requests are
|
|
// either all served or none are.
|
|
assert_eq!(
|
|
capabilities().call_hierarchy_provider,
|
|
Some(CallHierarchyServerCapability::Simple(true))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn document_links_resolve_nothing_because_every_target_is_already_known() {
|
|
let options = capabilities().document_link_provider.expect("document links");
|
|
assert_eq!(options.resolve_provider, Some(false));
|
|
}
|
|
|
|
#[test]
|
|
fn inlay_hints_resolve_nothing_because_every_hint_is_complete() {
|
|
let Some(OneOf::Right(InlayHintServerCapabilities::Options(options))) =
|
|
capabilities().inlay_hint_provider
|
|
else {
|
|
panic!("inlay hints must carry options, not a bare `true`");
|
|
};
|
|
assert_eq!(options.resolve_provider, None);
|
|
}
|
|
|
|
#[test]
|
|
fn signature_help_advertises_the_characters_the_handler_answers_on() {
|
|
let options = capabilities().signature_help_provider.expect("signature help");
|
|
assert_eq!(options.trigger_characters, Some(vec!["(".to_string(), ",".to_string()]));
|
|
}
|
|
|
|
#[test]
|
|
fn completion_resolves_nothing_because_every_item_is_complete() {
|
|
let options = capabilities().completion_provider.expect("completion");
|
|
assert_eq!(options.resolve_provider, None);
|
|
assert_eq!(options.trigger_characters, None);
|
|
}
|
|
|
|
#[test]
|
|
fn snippet_support_is_read_from_the_clients_own_capabilities() {
|
|
use lsp_types::{
|
|
ClientCapabilities, CompletionClientCapabilities, CompletionItemCapability,
|
|
TextDocumentClientCapabilities,
|
|
};
|
|
assert!(!snippet_support(&InitializeParams::default()), "silence means no");
|
|
|
|
let saying = |snippet_support: Option<bool>| InitializeParams {
|
|
capabilities: ClientCapabilities {
|
|
text_document: Some(TextDocumentClientCapabilities {
|
|
completion: Some(CompletionClientCapabilities {
|
|
completion_item: Some(CompletionItemCapability {
|
|
snippet_support,
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
assert!(super::snippet_support(&saying(Some(true))));
|
|
assert!(!super::snippet_support(&saying(Some(false))));
|
|
assert!(!super::snippet_support(&saying(None)));
|
|
}
|
|
|
|
#[test]
|
|
fn rename_advertises_prepare_so_the_editor_asks_before_offering() {
|
|
let Some(OneOf::Right(options)) = capabilities().rename_provider else {
|
|
panic!("rename must carry options, not a bare `true`");
|
|
};
|
|
assert_eq!(options.prepare_provider, Some(true));
|
|
}
|
|
|
|
#[test]
|
|
fn the_legend_and_the_encoder_agree_on_length() {
|
|
// The wire format carries indices into these arrays, so a mismatch
|
|
// between the legend and the encoder is a silent mis-colouring.
|
|
assert_eq!(tokens::token_types().len(), 14);
|
|
assert_eq!(tokens::token_modifiers().len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn the_code_action_kinds_are_named_in_the_capability() {
|
|
// A client filtering by kind (fix-on-save does) sees nothing unless the
|
|
// kinds it may ask for are advertised.
|
|
let Some(CodeActionProviderCapability::Options(options)) =
|
|
capabilities().code_action_provider
|
|
else {
|
|
panic!("the kinds must be listed, not just `true`");
|
|
};
|
|
let kinds = options.code_action_kinds.expect("kinds");
|
|
assert!(kinds.contains(&CodeActionKind::QUICKFIX));
|
|
assert!(kinds.contains(&CodeActionKind::new(code_action::FIX_ALL)));
|
|
assert!(kinds.contains(&CodeActionKind::new(ports::REFACTOR_PORTS)));
|
|
}
|
|
|
|
#[test]
|
|
fn every_advertised_command_is_one_the_server_runs() {
|
|
let options = capabilities().execute_command_provider.expect("commands");
|
|
assert_eq!(options.commands, commands());
|
|
// Both halves of the list, so neither can go missing quietly: a lens
|
|
// whose command is not advertised is a lens no client will run.
|
|
for command in ports::commands().iter().chain(lens::commands().iter()) {
|
|
assert!(options.commands.contains(command), "{command} is not advertised");
|
|
}
|
|
assert!(
|
|
options.commands.iter().all(|c| c.starts_with("lucisv.")),
|
|
"{:?}",
|
|
options.commands
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn code_lenses_resolve_nothing_because_every_lens_is_complete() {
|
|
let options = capabilities().code_lens_provider.expect("code lenses");
|
|
assert_eq!(options.resolve_provider, Some(false));
|
|
}
|
|
|
|
#[test]
|
|
fn code_lens_refresh_support_is_read_from_the_clients_own_capabilities() {
|
|
use lsp_types::{
|
|
ClientCapabilities, CodeLensWorkspaceClientCapabilities, WorkspaceClientCapabilities,
|
|
};
|
|
assert!(!code_lens_refresh_support(&InitializeParams::default()), "silence means no");
|
|
|
|
let saying = |refresh_support: Option<bool>| InitializeParams {
|
|
capabilities: ClientCapabilities {
|
|
workspace: Some(WorkspaceClientCapabilities {
|
|
code_lens: Some(CodeLensWorkspaceClientCapabilities { refresh_support }),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
assert!(code_lens_refresh_support(&saying(Some(true))));
|
|
assert!(!code_lens_refresh_support(&saying(Some(false))));
|
|
assert!(!code_lens_refresh_support(&saying(None)), "an empty `codeLens` is not a yes");
|
|
}
|
|
|
|
#[test]
|
|
fn show_document_support_is_read_from_the_clients_own_capabilities() {
|
|
use lsp_types::{
|
|
ClientCapabilities, ShowDocumentClientCapabilities, WindowClientCapabilities,
|
|
};
|
|
assert!(!show_document_support(&InitializeParams::default()), "silence means no");
|
|
|
|
let saying = |support: bool| InitializeParams {
|
|
capabilities: ClientCapabilities {
|
|
window: Some(WindowClientCapabilities {
|
|
show_document: Some(ShowDocumentClientCapabilities { support }),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
assert!(show_document_support(&saying(true)));
|
|
assert!(!show_document_support(&saying(false)));
|
|
}
|
|
|
|
#[test]
|
|
fn utf16_is_advertised_because_that_is_what_convert_implements() {
|
|
assert_eq!(capabilities().position_encoding, Some(PositionEncodingKind::UTF16));
|
|
}
|
|
}
|