Files
lucisv/crates/vfmt_server/src/main.rs
T
lucioandClaude Fable 5 33f58196e3 Refactoring M2: vlog_refactor crate + LSP rename
New crate vlog_refactor: rename any symbol vlog_semantic resolves, in-file
and cross-file, with whole-edit-or-nothing verification (token identity +
before/after resolution print over every file spelling either name).
End labels rename too via a new vlog_semantic Target::Declaration bound at
declaration time. Server ships prepareRename/rename; refusals are LSP
request errors, define-body/macro-argument/hierarchical-tail occurrences
warn instead of guessing. QA round 1 found two silent HIGHs (third-file
collision, macro-argument silence) — both closed with regression tests.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-24 05:12:53 -07:00

188 lines
7.5 KiB
Rust

//! `vfmt-server` — the vfmt 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/vfmt-server/README.md`.
mod code_action;
mod convert;
mod diagnostics;
mod format;
mod hover;
mod lint;
mod locate;
mod nav;
mod pairs;
mod rename;
mod server;
mod settings;
mod state;
mod symbols;
mod tokens;
mod uri;
use std::error::Error;
use std::path::PathBuf;
use lsp_server::Connection;
use lsp_types::{
CodeActionKind, CodeActionOptions, CodeActionProviderCapability, InitializeParams, OneOf,
PositionEncodingKind, RenameOptions, SemanticTokensFullOptions, SemanticTokensLegend,
SemanticTokensOptions, SemanticTokensServerCapabilities, ServerCapabilities,
TextDocumentSyncCapability, TextDocumentSyncKind, WorkDoneProgressOptions,
};
use crate::server::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(&params);
connection.initialize_finish(id, serde_json::to_value(initialize_result())?)?;
Server::new(connection, roots).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),
text_document_sync: Some(TextDocumentSyncCapability::Kind(TextDocumentSyncKind::FULL)),
document_formatting_provider: Some(OneOf::Left(true)),
document_range_formatting_provider: Some(OneOf::Left(true)),
definition_provider: Some(OneOf::Left(true)),
references_provider: Some(OneOf::Left(true)),
// The cursor on a `begin` highlights its own `end`, matched over the
// token stream. The handler declines anything that is not half of a
// pair, which leaves the editor's word highlighting in charge.
document_highlight_provider: Some(OneOf::Left(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),
]),
work_done_progress_options: WorkDoneProgressOptions::default(),
// Every action is returned complete; there is nothing to resolve.
resolve_provider: None,
})),
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(),
},
range: None,
full: Some(SemanticTokensFullOptions::Bool(true)),
},
)),
..Default::default()
}
}
fn initialize_result() -> lsp_types::InitializeResult {
lsp_types::InitializeResult {
capabilities: capabilities(),
server_info: Some(lsp_types::ServerInfo {
name: "vfmt-server".to_string(),
version: Some(env!("CARGO_PKG_VERSION").to_string()),
}),
}
}
/// 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) = &params.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.references_provider.is_some());
assert!(caps.document_highlight_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());
// Deliberately absent until the engine exists — see the README.
assert!(caps.completion_provider.is_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_two_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)));
}
#[test]
fn utf16_is_advertised_because_that_is_what_convert_implements() {
assert_eq!(capabilities().position_encoding, Some(PositionEncodingKind::UTF16));
}
}