Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:2giosangmitom Sqmeow nvim Session

From Leeroopedia
Knowledge Sources
Domains Session_Management, Connection_Management, Query_Execution, Editing, View_Processing
Last Updated 2026-10-16 14:00 GMT

Overview

Concrete tool for managing editor session state — connections, result history, running calls, and view generations — provided by the sqmeow-core engine.

Description

The Session struct owns all state for one editor session. Connections and completed results are shared through Arc so asynchronous work can outlive the RPC handler that started it. Mutex-protected collections own session membership; cloned handles remain valid after eviction or disconnect but are no longer discoverable by id.

Key method groups:

  • Connection management: insert_connection registers an open connection; connection looks it up; remove_connection forgets it.
  • Call lifecycle: next_call_id allocates a unique id; begin_call registers a running call with a cancellation token; cancel trips it.
  • Result history: store_run stores results of one run as a history group (multi-statement = one slot); eviction removes the oldest run when history is full.
  • View generation: begin_view / finish_view order view requests independently of their computation, discarding stale results.
  • Insert stash: stash_inserted / take_inserted keep rows from INSERT RETURNING for the next query run to display.
  • Options: configure applies settings patches (max_rows, history_size, timeout_ms).

The Call struct represents one finished result with its immutable ResultSet, optional View, and view generation counter. display() snapshots the current view under one lock.

Usage

  • Connection lifecycle: insert_connection after successful connect.
  • Query execution: next_call_id, begin_call, store_run for results.
  • In-grid editing: stash_inserted for INSERT RETURNING rows.
  • Local result processing: begin_view, finish_view, with_call, call.
  • Result export: with_call, call to read stored results.

Code Reference

Source Location

  • Repository: sqmeow.nvim
  • File: core/sqmeow-core/src/server/session.rs
  • Lines: L1-408

Signature

pub struct Session { /* connections, calls, running, options, next_call_id, inserted */ }

impl Session {
    pub fn options(&self) -> Options;
    pub fn configure(&self, patch: OptionsPatch) -> Options;
    pub fn insert_connection(&self, connection: Connection);
    pub fn connection(&self, id: ConnId) -> Option<Arc<Connection>>;
    pub fn remove_connection(&self, id: ConnId) -> Option<Arc<Connection>>;
    pub fn next_call_id(&self) -> CallId;
    pub fn begin_call(&self, id: CallId) -> RunningCall<'_>;
    pub fn cancel(&self, id: CallId) -> bool;
    pub fn store_run(&self, calls: Vec<Call>) -> Vec<Arc<Call>>;
    pub fn stash_inserted(&self, conn_id: ConnId, rows: Vec<ResultSet>);
    pub fn take_inserted(&self, conn_id: ConnId) -> Vec<ResultSet>;
    pub fn with_call<T>(&self, id: CallId, read: impl FnOnce(&Call) -> T) -> Option<T>;
    pub fn call(&self, id: CallId) -> Option<Arc<Call>>;
}

impl Call {
    pub fn display(&self) -> (Option<Arc<ResultSet>>, Option<Arc<Vec<usize>>>);
    pub fn begin_view(&self) -> u64;
    pub fn finish_view(&self, generation: u64, finish: impl FnOnce());
}

Import

use crate::server::session::{Session, Call, CallId, ConnId, Connection, View, Options, OptionsPatch};

I/O Contract

Inputs

Name Type Required Description
connection Connection Yes Open connection with id, name, Backend, read_only, optional Tunnel
calls Vec<Call> Yes Results of one run to store (multi-statement = one history slot)
id CallId Yes /// Result/connection id to look up
patch OptionsPatch Yes /// Settings patch (max_rows, history_size, timeout_ms)

Outputs

Name Type Description
store_run Vec<Arc<Call>> /// Shared handles; oldest run evicted when full
with_call Option<T> /// Result of reading the stored call, or None if evicted
begin_view u64 /// Generation number to gate stale completions
finish_view () /// Calls finish() only if generation is current

Usage Examples

Storing a multi-statement run

let calls = vec![call1, call2, call3]; // 3 statements, 1 history slot
let stored = session.store_run(calls);
// All 3 calls share one history group; evicted together

View generation ordering

let gen = call.begin_view(); // Increment generation
// ... async computation ...
call.finish_view(gen, || {
    // Only runs if no newer view request superseded this one
    *call.view.lock().unwrap() = Some(View::Rows(Arc::new(indices)));
});

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment