laburnum 1.17.1

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
Documentation
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

//! `prepare_rename` and `rename`.
//!
//! `rename` returns the `WorkspaceEdit` JSON; it does not apply the edit.
//! The agent applies the resulting text edits with its own Edit/Write
//! tools. This sidesteps the question of whether the MCP server should
//! touch the filesystem.

use {
  crate::{
    connect::lsp::{
      LspClient,
      request::{
        PrepareRenameRequest,
        Rename as RenameRequest,
      },
    },
    connect::mcp::{
      schema,
      tool::{
        Tool,
        ToolError,
        ToolRegistry,
      },
    },
    protocol::lsp::{
      PrepareRenameResponse,
      RenameParams,
      TextDocumentPositionParams,
      WorkspaceEdit,
    },
  },
  serde_json::json,
};

pub fn register(registry: &mut ToolRegistry) {
  registry.register::<PrepareRename>();
  registry.register::<Rename>();
}

pub enum PrepareRename {}

impl Tool for PrepareRename {
  type Input = TextDocumentPositionParams;
  type Output = Option<PrepareRenameResponse>;

  const NAME: &'static str = "prepare_rename";
  const DESCRIPTION: &'static str =
    "Validate that the symbol at the given position can be renamed, and \
     return the range that would be replaced. Wraps LSP \
     `textDocument/prepareRename`. Does not perform any rename.";

  fn input_schema() -> serde_json::Value {
    schema::text_document_position()
  }

  async fn call(
    client: &LspClient,
    input: Self::Input,
  ) -> Result<Self::Output, ToolError> {
    Ok(client.send_request::<PrepareRenameRequest>(input).await?)
  }
}

pub enum Rename {}

impl Tool for Rename {
  type Input = RenameParams;
  type Output = Option<WorkspaceEdit>;

  const NAME: &'static str = "rename";
  const DESCRIPTION: &'static str =
    "Compute a workspace-wide rename of the symbol at the given position. \
     Wraps LSP `textDocument/rename`. Returns the resulting `WorkspaceEdit` \
     JSON; does NOT write to the filesystem. The agent is expected to apply \
     the edits itself using its own Edit/Write tools.";

  fn input_schema() -> serde_json::Value {
    json!({
      "type": "object",
      "properties": {
        "textDocument": schema::text_document_identifier(),
        "position":     schema::position(),
        "newName":      { "type": "string" }
      },
      "required": ["textDocument", "position", "newName"]
    })
  }

  async fn call(
    client: &LspClient,
    input: Self::Input,
  ) -> Result<Self::Output, ToolError> {
    Ok(client.send_request::<RenameRequest>(input).await?)
  }
}