elsmith 0.1.1

An Emacs Lisp language server with Lean-verified protocol and editing rules
Documentation
use serde::de::DeserializeOwned;
use serde_json::{Value, json};

use super::Server;
use crate::{call_hierarchy, formatting, rename};

const INVALID: (i32, &str) = (-32602, "Invalid parameters");

fn field<T: DeserializeOwned>(params: &Value, key: &str) -> Result<T, (i32, &'static str)> {
    serde_json::from_value(params.get(key).ok_or(INVALID)?.clone()).map_err(|_| INVALID)
}

impl Server<'_> {
    pub(super) fn refactor(
        &mut self,
        method: &str,
        params: &Value,
    ) -> Result<Value, (i32, &'static str)> {
        self.ensure_loaded();
        let world = self.world().ok_or(INVALID)?;
        if matches!(
            method,
            "callHierarchy/incomingCalls" | "callHierarchy/outgoingCalls"
        ) {
            let item = field(params, "item")?;
            return Ok(call_hierarchy::calls(
                &world,
                item,
                method == "callHierarchy/incomingCalls",
            )
            .unwrap_or(Value::Null));
        }
        let uri = params
            .pointer("/textDocument/uri")
            .and_then(Value::as_str)
            .ok_or(INVALID)?;
        match method {
            "textDocument/prepareRename" => {
                Ok(
                    rename::request(&world, uri, field(params, "position")?, None)
                        .unwrap_or(Value::Null),
                )
            }
            "textDocument/rename" => {
                let name = params
                    .get("newName")
                    .and_then(Value::as_str)
                    .ok_or(INVALID)?;
                rename::request(&world, uri, field(params, "position")?, Some(name))
                    .ok_or((-32803, "Cannot rename this symbol safely"))
            }
            "textDocument/prepareCallHierarchy" => {
                Ok(
                    call_hierarchy::prepare(&world, uri, field(params, "position")?)
                        .unwrap_or(Value::Null),
                )
            }
            _ => {
                let options = field(params, "options")?;
                let selection = match method {
                    "textDocument/formatting" => formatting::Selection::Document,
                    "textDocument/rangeFormatting" => {
                        formatting::Selection::Range(field(params, "range")?)
                    }
                    _ => {
                        if !matches!(
                            params.get("ch").and_then(Value::as_str),
                            Some("\n" | ")" | "]")
                        ) {
                            return Err(INVALID);
                        }
                        formatting::Selection::Line(field(params, "position")?)
                    }
                };
                let Some(view) = world.view(uri) else {
                    return Ok(json!([]));
                };
                formatting::edits(view, &world.views(), world.encoding, options, selection)
                    .ok_or(INVALID)
            }
        }
    }
}