Add PostgreSQL LSP server with directive-scoped REST query API
CI / test (push) Waiting to run

This commit is contained in:
2026-11-20 18:22:13 +03:00
commit 4ecf11af48
32 changed files with 8734 additions and 0 deletions
+126
View File
@@ -0,0 +1,126 @@
use std::io::Write;
use std::sync::Arc;
use anyhow::Result;
use clap::Parser;
use omg_pgs::cli::Cli;
use omg_pgs::db::DbManager;
use omg_pgs::{lsp, rest};
use tokio::net::TcpListener;
use tokio::task::JoinError;
use tracing_subscriber::EnvFilter;
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
init_tracing(cli.verbose);
let manager = Arc::new(DbManager::new());
// The REST port is optional in practice: when an editor starts several
// instances of the binary, only the first one can own the port. The LSP
// must keep working in the others.
let rest_listener = match TcpListener::bind(("127.0.0.1", cli.rest_port)).await {
Ok(listener) => {
tracing::info!("REST listening on http://127.0.0.1:{}", cli.rest_port);
Some(listener)
}
Err(error) => {
tracing::warn!(
port = cli.rest_port,
error = %error,
"could not bind REST port; continuing in LSP-only mode"
);
None
}
};
let rest_available = rest_listener.is_some();
let mut rest_task = tokio::spawn(async move {
match rest_listener {
Some(listener) => rest::serve(listener).await,
None => std::future::pending::<Result<()>>().await,
}
});
// Stdout belongs to the LSP protocol; all diagnostics from this process go
// to stderr through the tracing subscriber.
let lsp_result = tokio::select! {
result = lsp::serve_stdio(manager) => Some(result),
result = &mut rest_task => handle_rest_result(result),
_ = tokio::signal::ctrl_c() => {
tracing::info!("shutting down");
terminate(0);
}
};
let Some(lsp_result) = lsp_result else {
terminate(0);
};
match lsp_result {
Ok(true) => {
// The editor completed the handshake and then closed the session.
rest_task.abort();
terminate(0);
}
Ok(false) if rest_available => {
// Typical headless REST use: stdin is closed without an LSP
// handshake. Keep serving the REST API until interrupted.
tracing::warn!(
"LSP stdin closed before initialization; keeping the REST API running (Ctrl+C to stop)"
);
tokio::select! {
result = &mut rest_task => handle_rest_result(result),
_ = tokio::signal::ctrl_c() => {
tracing::info!("shutting down");
}
}
terminate(0);
}
Ok(false) => terminate(0),
Err(error) => {
tracing::error!(error = %error, "language server failed");
terminate(1);
}
}
}
fn handle_rest_result(result: std::result::Result<Result<()>, JoinError>) -> ! {
match result {
Ok(Ok(())) => {
tracing::warn!("REST server stopped");
terminate(0);
}
Ok(Err(error)) => {
tracing::error!(error = %error, "REST server failed");
terminate(1);
}
Err(error) => {
tracing::error!(error = %error, "REST task failed");
terminate(1);
}
}
}
/// Exits the process without waiting for the blocking stdin reader used by
/// `tokio::io::stdin`. That reader cannot be cancelled while the editor keeps
/// the pipe open, so a normal runtime shutdown would hang after `exit`.
fn terminate(code: i32) -> ! {
let _ = std::io::stderr().flush();
std::process::exit(code);
}
fn init_tracing(verbose: u8) {
let default = match verbose {
0 => "omg_pgs=info,warn",
1 => "omg_pgs=debug,warn",
_ => "omg_pgs=trace,warn",
};
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default));
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_writer(std::io::stderr)
.with_target(false)
.init();
}