//! LSP command registrations.

use crate::app_state::Screen;
use crate::commands::{CommandRegistry, command};
use crate::operation::Operation;

pub fn register_lsp(registry: &mut CommandRegistry) {
    // Ctrl+Space — trigger completion
    registry.register(
        command("lsp.trigger_completion")
            .title("Trigger Completion")
            .description("Request completions from the LSP server at the current cursor position")
            .context("editor")
            .hidden()
            .handler(|app, _| {
                if let Screen::Editor(ed) = &app.screen {
                    vec![Operation::LspTriggerCompletion {
                        path: ed.buffer.path.clone(),
                        trigger_char: None,
                    }]
                } else {
                    vec![]
                }
            }),
    );

    // F4 — show hover documentation
    registry.register(
        command("lsp.trigger_hover")
            .title("Show Hover Info")
            .description("Show type signature and documentation for the symbol under the cursor")
            .context("editor")
            .hidden()
            .handler(|app, _| {
                if let Screen::Editor(ed) = &app.screen {
                    vec![Operation::LspTriggerHover {
                        path: ed.buffer.path.clone(),
                        idle_generation: None,
                    }]
                } else {
                    vec![]
                }
            }),
    );

    // F12 — go to definition
    registry.register(
        command("lsp.go_to_definition")
            .title("Go to Definition")
            .description("Jump to the definition of the symbol under the cursor")
            .context("editor")
            .hidden()
            .handler(|app, _| {
                if let Screen::Editor(ed) = &app.screen {
                    vec![Operation::LspGoToDefinition {
                        path: ed.buffer.path.clone(),
                    }]
                } else {
                    vec![]
                }
            }),
    );

    // F2 — rename symbol
    registry.register(
        command("lsp.rename")
            .title("Rename Symbol")
            .description("Rename the symbol under the cursor across all files in the project")
            .context("editor")
            .hidden()
            .handler(|app, _| {
                if let Screen::Editor(ed) = &app.screen {
                    vec![Operation::LspTriggerRename {
                        path: ed.buffer.path.clone(),
                    }]
                } else {
                    vec![]
                }
            }),
    );
}
