Skip to main content

lean_ctx/core/editor_registry/writers/
mod.rs

1// Auto-split from the former monolithic writers.rs. Grouped by operation
2// (install/uninstall) + shared helpers; behavior is unchanged.
3
4use super::types::{ConfigType, EditorTarget};
5
6mod install;
7mod shared;
8mod uninstall;
9
10pub use shared::auto_approve_tools;
11pub use uninstall::remove_lean_ctx_mcp_server;
12// Routers below dispatch to every install/uninstall writer; a glob keeps the
13// dispatch table readable and lets the test module reach them via `super::*`.
14#[allow(clippy::wildcard_imports)]
15use install::*;
16#[allow(clippy::wildcard_imports)]
17use uninstall::*;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum WriteAction {
21    Created,
22    Updated,
23    Already,
24}
25
26#[derive(Debug, Clone, Copy, Default)]
27pub struct WriteOptions {
28    pub overwrite_invalid: bool,
29}
30
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct WriteResult {
33    pub action: WriteAction,
34    pub note: Option<String>,
35}
36
37pub fn write_config(target: &EditorTarget, binary: &str) -> Result<WriteResult, String> {
38    write_config_with_options(target, binary, WriteOptions::default())
39}
40
41pub fn write_config_with_options(
42    target: &EditorTarget,
43    binary: &str,
44    opts: WriteOptions,
45) -> Result<WriteResult, String> {
46    let allow_rule_steering = crate::core::config::Config::load().rules_injection_effective()
47        != crate::core::config::RulesInjection::Off;
48
49    write_config_with_options_and_rule_steering(target, binary, opts, allow_rule_steering)
50}
51
52pub fn write_config_with_options_and_rule_steering(
53    target: &EditorTarget,
54    binary: &str,
55    opts: WriteOptions,
56    allow_rule_steering: bool,
57) -> Result<WriteResult, String> {
58    if let Some(parent) = target.config_path.parent() {
59        std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
60    }
61
62    match target.config_type {
63        ConfigType::McpJson => write_mcp_json(target, binary, opts),
64        ConfigType::Zed => write_zed_config(target, binary, opts),
65        ConfigType::Codex => write_codex_config(target, binary),
66        ConfigType::VsCodeMcp => write_vscode_mcp(target, binary, opts),
67        ConfigType::CopilotCli => write_copilot_cli(target, binary, opts),
68        ConfigType::OpenCode => write_opencode_config(target, binary, opts),
69        ConfigType::Crush => write_crush_config(target, binary, opts),
70        ConfigType::JetBrains => write_jetbrains_config(target, binary, opts),
71        ConfigType::Amp => write_amp_config(target, binary, opts),
72        ConfigType::HermesYaml => write_hermes_yaml(target, binary, opts),
73        ConfigType::GeminiSettings => write_gemini_settings(target, binary, opts),
74        ConfigType::QoderSettings => write_qoder_settings(target, binary, opts),
75        ConfigType::AugmentVsCode => write_augment_vscode(target, binary, opts),
76        ConfigType::OpenClaw => write_openclaw_config(target, binary, opts),
77        ConfigType::VibeToml => write_vibe_toml(target, binary, opts),
78        ConfigType::CommandCode => {
79            write_commandcode_config_with_rule_steering(target, binary, opts, allow_rule_steering)
80        }
81        ConfigType::ClineCli => write_cline_cli_config(target, binary, opts),
82        ConfigType::OmpMcp => write_omp_mcp(target, binary, opts),
83        ConfigType::CodeWhale => write_codewhale_config(target, binary, opts),
84    }
85}
86
87pub fn remove_lean_ctx_server(
88    target: &EditorTarget,
89    opts: WriteOptions,
90) -> Result<WriteResult, String> {
91    match target.config_type {
92        ConfigType::McpJson
93        | ConfigType::JetBrains
94        | ConfigType::GeminiSettings
95        | ConfigType::QoderSettings
96        | ConfigType::CommandCode
97        | ConfigType::ClineCli
98        | ConfigType::OmpMcp => remove_lean_ctx_mcp_server(&target.config_path, opts),
99        ConfigType::VsCodeMcp | ConfigType::CopilotCli => {
100            remove_lean_ctx_vscode_server(&target.config_path, opts)
101        }
102        ConfigType::Codex => remove_lean_ctx_codex_server(&target.config_path),
103        ConfigType::OpenCode | ConfigType::Crush => {
104            remove_lean_ctx_named_json_server(&target.config_path, "mcp", opts)
105        }
106        ConfigType::Zed => {
107            remove_lean_ctx_named_json_server(&target.config_path, "context_servers", opts)
108        }
109        ConfigType::Amp => remove_lean_ctx_amp_server(&target.config_path, opts),
110        ConfigType::HermesYaml => remove_lean_ctx_hermes_yaml_server(&target.config_path),
111        ConfigType::AugmentVsCode => {
112            remove_lean_ctx_augment_vscode_server(&target.config_path, opts)
113        }
114        ConfigType::OpenClaw => remove_lean_ctx_openclaw_server(&target.config_path, opts),
115        ConfigType::VibeToml => remove_lean_ctx_vibe_toml_server(&target.config_path, opts),
116        // #1402: CodeWhale accepts either root key, and we merge into whichever
117        // one the user already had — so removal has to visit both, or an entry
118        // written under `servers` would survive `uninstall`.
119        ConfigType::CodeWhale => {
120            let servers = remove_lean_ctx_named_json_server(&target.config_path, "servers", opts)?;
121            let mcp_servers =
122                remove_lean_ctx_named_json_server(&target.config_path, "mcpServers", opts)?;
123            Ok(if servers.action == WriteAction::Updated {
124                servers
125            } else {
126                mcp_servers
127            })
128        }
129    }
130}
131
132#[cfg(test)]
133mod tests;