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