-- Simple init.lua for educational LSP testing

-- Start the LSP client
local client = vim.lsp.start_client({
    name = "educationallsp",
    cmd = { "/Users/russellboley/git/educationallsp/target/debug/educationallsp" },
    root_dir = vim.fn.getcwd(),
})

if not client then
    vim.notify("Failed to start educationallsp")
    return
end

-- Set up LSP key mappings
local function setup_lsp_keymaps(bufnr)
    local opts = { noremap = true, silent = true, buffer = bufnr }
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
    vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
    vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, opts)
    vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, opts)
end

-- Attach to markdown files
vim.api.nvim_create_autocmd("FileType", {
    pattern = "markdown",
    callback = function()
        local bufnr = vim.api.nvim_get_current_buf()
        vim.lsp.buf_attach_client(bufnr, client)
        setup_lsp_keymaps(bufnr)
    end,
})

-- Also attach immediately if opening a markdown file
if vim.bo.filetype == "markdown" then
    local bufnr = vim.api.nvim_get_current_buf()
    vim.lsp.buf_attach_client(bufnr, client)
    setup_lsp_keymaps(bufnr)
end
