|
| 1 | +# Components — askCode.nvim |
| 2 | + |
| 3 | +## init.lua — Orchestrator |
| 4 | + |
| 5 | +Public API and conversation state. Start here for any feature work. |
| 6 | + |
| 7 | +| Function | Signature | Description | |
| 8 | +|---|---|---| |
| 9 | +| `setup` | `(cfg?)` | Merges user config with defaults | |
| 10 | +| `get_config` | `(key?)` | Returns config value or full config | |
| 11 | +| `set_config` | `(key, value)` | Sets config value at runtime | |
| 12 | +| `ask` | `(question, mode)` | Starts a new conversation; closes any existing one | |
| 13 | +| `follow_up` | `(question)` | Appends to active conversation | |
| 14 | +| `ask_replace` | `(question, mode)` | Visual-mode only; shows editable replacement window | |
| 15 | +| `ask_or_follow_up` | `(question, mode)` | Dispatches to `ask` or `follow_up` based on state | |
| 16 | + |
| 17 | +**State** (module-local): |
| 18 | +- `history_file` — path to temp file holding conversation history |
| 19 | +- `win_id`, `buf_id` — current response window |
| 20 | +- `display_content` — accumulated display text |
| 21 | + |
| 22 | +## config.lua — Configuration |
| 23 | + |
| 24 | +Dot-notation config system. All keys defined in `M.default`. |
| 25 | + |
| 26 | +| Function | Description | |
| 27 | +|---|---| |
| 28 | +| `merge_with_default(changes)` | Deep-merges changes using `"keep"` strategy | |
| 29 | +| `get(key?)` | Returns value at dot-path, or full config if nil | |
| 30 | +| `set(key, value)` | Sets value; auto-converts strings to bool/number | |
| 31 | +| `reset_config()` | Restores `M.current_config` to `M.default` | |
| 32 | +| `get_all_keys()` | Returns all dot-separated keys (used for tab completion) | |
| 33 | + |
| 34 | +## runner.lua — Job Execution |
| 35 | + |
| 36 | +Thin wrapper around `vim.fn.jobstart`. |
| 37 | + |
| 38 | +| Function | Signature | Description | |
| 39 | +|---|---| |
| 40 | +| `run_command` | `(cmd, on_stdout, opts?)` | Starts async job; `stdin` defaults to `"null"` | |
| 41 | + |
| 42 | +`opts` fields: `on_stderr`, `on_exit`, `stdout_buffered`, `stdin`. |
| 43 | + |
| 44 | +## ui.lua — Window Management |
| 45 | + |
| 46 | +| Function | Signature | Description | |
| 47 | +|---|---| |
| 48 | +| `show_window` | `(content, on_close, editable?, on_apply?)` | Creates float/split; wires `q`/`Q` keymaps | |
| 49 | +| `update_window` | `(win_id, buf_id, content, cursor_line?, replacement?)` | Replaces buffer content via `vim.schedule` | |
| 50 | + |
| 51 | +Window type is read from `config.current_config.window.type` (`"float"`, `"vertical"`, `"horizontal"`). |
| 52 | + |
| 53 | +## utils.lua — Utilities |
| 54 | + |
| 55 | +| Function | Description | |
| 56 | +|---|---| |
| 57 | +| `log(msg)` | Emits `vim.notify` at DEBUG level when `config.debug` is true | |
| 58 | +| `get_buffer_content(mode)` | Returns visual selection or full buffer as string | |
| 59 | +| `read_file(path)` | Reads file to string | |
| 60 | +| `write_file(path, content)` | Overwrites file | |
| 61 | +| `append_file(path, content)` | Appends to file | |
| 62 | +| `delete_file(path)` | Removes file | |
| 63 | +| `parse_replacement_response(response)` | Extracts `<REPLACE>…</REPLACE>` block | |
| 64 | +| `apply_replacement(content, selection_info)` | Writes lines back to buffer | |
| 65 | + |
| 66 | +## agents/init.lua — Registry |
| 67 | + |
| 68 | +Maps agent name strings to modules. Add new agents here. |
| 69 | + |
| 70 | +```lua |
| 71 | +M.agents = { |
| 72 | + gemini = require("askCode.agents.gemini"), |
| 73 | + kiro = require("askCode.agents.kiro"), |
| 74 | + opencode = require("askCode.agents.opencode"), |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## agents/gemini.lua |
| 79 | + |
| 80 | +| Function | Description | |
| 81 | +|---|---| |
| 82 | +| `prepare_command(prompt)` | `echo <shellescape(prompt)> \| gemini --output-format json` | |
| 83 | +| `parse_response(json)` | Decodes JSON, returns `decoded.response` | |
| 84 | + |
| 85 | +## agents/kiro.lua |
| 86 | + |
| 87 | +| Function | Description | |
| 88 | +|---|---| |
| 89 | +| `prepare_command(prompt)` | `echo <shellescape(prompt)> \| kiro-cli chat --no-interactive 2>&1` | |
| 90 | +| `parse_response(str)` | Strips ANSI codes, extracts content after `>` prompt indicator | |
| 91 | + |
| 92 | +Note: kiro writes its response to stderr; `init.lua` wires `on_stderr` only for this agent. |
| 93 | + |
| 94 | +## agents/opencode.lua |
| 95 | + |
| 96 | +| Function | Description | |
| 97 | +|---|---| |
| 98 | +| `prepare_command(prompt)` | Writes prompt to temp file; runs `opencode run --format json "$(cat <tmpfile>)"` | |
| 99 | +| `parse_response(str)` | Parses newline-delimited JSON events; concatenates all `"text"` type parts | |
| 100 | + |
| 101 | +Uses temp file to avoid shell quoting issues with multiline prompts. `--format json` ensures the process exits cleanly. |
| 102 | + |
| 103 | +## plugin/askCode.lua — Entry Point |
| 104 | + |
| 105 | +Defines three user commands and two `<Plug>` keymaps: |
| 106 | + |
| 107 | +| Command | Args | Description | |
| 108 | +|---|---|---| |
| 109 | +| `:AskCode` | `[question]` | Ask or follow up; prompts if no args | |
| 110 | +| `:AskCodeReplace` | `[request]` | Replacement mode (visual only) | |
| 111 | +| `:AskCodeConfig` | `<key> [value]` | Get or set config at runtime | |
| 112 | + |
| 113 | +`<Plug>(AskCodeExplain)` and `<Plug>(AskCodeAddDocstring)` are pre-built prompt shortcuts. |
0 commit comments