Skip to content

Commit f913c52

Browse files
yaadatayaadata
authored andcommitted
feat(codex): rename CodexAdd to CodexMentionFile, add CodexMentionDirectory (#9)
Rename `M.add_file` → `M.mention_file` and `:CodexAdd` → `:CodexMentionFile` to better reflect the `/mention` payload. Add `M.mention_directory` and `:CodexMentionDirectory` for mentioning directories. Reassign `<leader>om` to mention_file, add `<leader>oM` for mention_dir, and remove the default model keymap. Reviewed-on: https://codeberg.org/yaadata/codex.nvim/pulls/9 Co-authored-by: Yadi Abdalhalim <abdalhalim.yaadata@gmail.com> Co-committed-by: Yadi Abdalhalim <abdalhalim.yaadata@gmail.com>
1 parent 6258880 commit f913c52

11 files changed

Lines changed: 341 additions & 115 deletions

File tree

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ require("codex").setup({
8080
focus = "<leader>of",
8181
close = "<leader>ox",
8282
send = "<leader>os",
83-
add = "<leader>oa",
83+
mention_file = "<leader>om",
84+
mention_dir = "<leader>oM",
8485
resume = "<leader>or",
85-
model = "<leader>om",
8686
status = "<leader>oi",
8787
permissions = "<leader>op",
8888
compact = "<leader>oc",
@@ -101,7 +101,8 @@ require("codex").setup({
101101
- `:CodexClose` closes the active Codex terminal session
102102
- `:CodexClearInput` clears the active Codex terminal input line
103103
- `:CodexSend` sends selected lines with path and range context.
104-
- `:CodexAdd [path]` sends `/mention` for a path (or current buffer path).
104+
- `:CodexMentionFile [path]` sends `/mention` for a file (or current buffer path).
105+
- `:CodexMentionDirectory [path]` sends `/mention` for a directory (or current buffer's directory).
105106
- `:CodexResume[!]` resumes in-process or launches `codex resume` (`!` uses
106107
`--last` when launching).
107108
- `:CodexModel` sends `/model`
@@ -126,9 +127,9 @@ require("codex").setup({
126127
focus = "<leader>of",
127128
close = "<leader>ox",
128129
send = "<leader>os",
129-
add = "<leader>oa",
130+
mention_file = "<leader>om",
131+
mention_dir = "<leader>oM",
130132
resume = "<leader>or",
131-
model = "<leader>om",
132133
status = "<leader>oi",
133134
permissions = "<leader>op",
134135
compact = "<leader>oc",
@@ -210,7 +211,8 @@ navigation keymaps, or set individual directions to `false`.
210211
- `require("codex").show_diff()` send `/diff`.
211212
- `require("codex").resume(opts)` resume (`opts.last` supports `--last`).
212213
- `require("codex").send_selection(opts)` send visual/range selection.
213-
- `require("codex").add_file(path)` send `/mention` for a file path.
214+
- `require("codex").mention_file(path)` send `/mention` for a file path.
215+
- `require("codex").mention_directory(path)` send `/mention` for a directory path.
214216
- `require("codex").is_running()` check whether active session is alive.
215217
- `require("codex").get_config()` return resolved config snapshot.
216218

docs/architecture.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ codex.nvim/
4141
│ # prevents double-loading via vim.g.loaded_codex.
4242
├── lua/codex/
4343
│ ├── init.lua # Public API surface (setup, open, close, toggle,
44-
│ │ # send, send_selection, add_file, resume, etc.).
44+
│ │ # send, send_selection, mention_file, mention_directory, resume, etc.).
4545
│ │ # Owns the DI container and session lifecycle.
4646
│ ├── config.lua # Default config table, vim.validate-based
4747
│ │ # validation, and deep-merge with user options.
@@ -202,7 +202,7 @@ failures downstream.
202202
### Auto-Open
203203

204204
APIs that need an active session (`send`, `send_command`, `focus`,
205-
`send_selection`, `add_file`) automatically open one when needed. The
205+
`send_selection`, `mention_file`, `mention_directory`) automatically open one when needed. The
206206
lower-level `send` API opens without focus, while command-facing flows
207207
(`:CodexSend`, `:CodexAdd`) ensure the terminal is opened with focus before
208208
payload dispatch. If the provider handle is not yet ready, payloads are queued

docs/command-interactions.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ then into provider/session/runtime collaborators.
88

99
## Command Mapping
1010

11-
| User Command | Entry Function | Primary Path |
12-
| ----------------------------- | -------------------------------- | ----------------------------------------------------- |
13-
| `:Codex` | `codex.toggle()` | Toggle active terminal or open a focused session |
14-
| `:Codex!` | `codex.open(true)` | Force-open and focus terminal |
15-
| `:CodexFocus` | `codex.focus()` | Focus active session or open one |
16-
| `:CodexClose` | `codex.close()` | Close active session and reset queue |
17-
| `:CodexClearInput` | `codex.clear_input()` | Send `<C-c>` to active session |
18-
| `:CodexSend` | `codex.send_selection(opts)` | Collect selection, format, send via queue |
19-
| `:CodexAdd [path]` | `codex.add_file(path)` | Build `/mention` payload and submit |
20-
| `:CodexResume` | `codex.resume({ last = false })` | In-process `/resume` or launch `codex resume` |
21-
| `:CodexResume!` | `codex.resume({ last = true })` | Launch `codex resume --last` when opening new process |
22-
| `:CodexModel` | `codex.set_model()` | Slash command wrapper (`/model`) |
23-
| `:CodexStatus` | `codex.show_status()` | Slash command wrapper (`/status`) |
24-
| `:CodexPermissions` | `codex.show_permissions()` | Slash command wrapper (`/permissions`) |
25-
| `:CodexCompact` | `codex.compact()` | Slash command wrapper (`/compact`) |
26-
| `:CodexReview [instructions]` | `codex.review(instructions)` | Slash command wrapper (`/review ...`) |
27-
| `:CodexDiff` | `codex.show_diff()` | Slash command wrapper (`/diff`) |
11+
| User Command | Entry Function | Primary Path |
12+
| ------------------------------- | -------------------------------- | ----------------------------------------------------- |
13+
| `:Codex` | `codex.toggle()` | Toggle active terminal or open a focused session |
14+
| `:Codex!` | `codex.open(true)` | Force-open and focus terminal |
15+
| `:CodexFocus` | `codex.focus()` | Focus active session or open one |
16+
| `:CodexClose` | `codex.close()` | Close active session and reset queue |
17+
| `:CodexClearInput` | `codex.clear_input()` | Send `<C-c>` to active session |
18+
| `:CodexSend` | `codex.send_selection(opts)` | Collect selection, format, send via queue |
19+
| `:CodexMentionFile [path]` | `codex.mention_file(path)` | Build `/mention` payload for file and submit |
20+
| `:CodexMentionDirectory [path]` | `codex.mention_directory(path)` | Build `/mention` payload for directory and submit |
21+
| `:CodexResume` | `codex.resume({ last = false })` | In-process `/resume` or launch `codex resume` |
22+
| `:CodexResume!` | `codex.resume({ last = true })` | Launch `codex resume --last` when opening new process |
23+
| `:CodexModel` | `codex.set_model()` | Slash command wrapper (`/model`) |
24+
| `:CodexStatus` | `codex.show_status()` | Slash command wrapper (`/status`) |
25+
| `:CodexPermissions` | `codex.show_permissions()` | Slash command wrapper (`/permissions`) |
26+
| `:CodexCompact` | `codex.compact()` | Slash command wrapper (`/compact`) |
27+
| `:CodexReview [instructions]` | `codex.review(instructions)` | Slash command wrapper (`/review ...`) |
28+
| `:CodexDiff` | `codex.show_diff()` | Slash command wrapper (`/diff`) |
2829

2930
## Setup Registration Flow
3031

@@ -145,27 +146,28 @@ init.lua send_selection()
145146
\- [not ready yet] -> queue + retry loop until ready/timeout
146147
```
147148

148-
### `:CodexAdd [path]`
149+
### `:CodexMentionFile [path]` and `:CodexMentionDirectory [path]`
149150

150151
```text
151-
User runs :CodexAdd [path]
152+
User runs :CodexMentionFile [path] (or :CodexMentionDirectory [path])
152153
|
153154
v
154-
commands.lua -> codex.add_file(path_or_nil)
155+
commands.lua -> codex.mention_file(path_or_nil) (or codex.mention_directory(path_or_nil))
155156
|
156157
v
157-
init.lua add_file(path)
158+
init.lua mention_file(path) / mention_directory(path)
158159
|- ensure_setup()
159-
|- resolve path (arg or current buffer absolute path)
160-
|- [missing path] -> log + return false, "current buffer has no file path"
160+
|- resolve path (arg or current buffer path via %:p / %:p:h)
161+
|- [missing path] -> log + return false, "current buffer has no file/directory path"
161162
|- path.to_relative(...)
162-
|- formatter.format_mention(relative_path)
163-
|- [active + alive] provider.focus(handle) before prompt capture
164-
|- capture_terminal_prompt_input() (best effort)
165-
|- mention_payload = clear_line_sequence + mention_text
166-
\- dispatch_send(mention_payload, { open_focus=true, pre_focus=true, command_path="/mention", on_sent=... })
167-
|- on_sent: submit_with_enter_key("/mention")
168-
\- on_sent: restore captured prompt input via delayed dispatch_send(...)
163+
\- dispatch_mention(relative_path)
164+
|- formatter.format_mention(relative_path)
165+
|- [active + alive] provider.focus(handle) before prompt capture
166+
|- capture_terminal_prompt_input() (best effort)
167+
|- mention_payload = clear_line_sequence + mention_text
168+
\- dispatch_send(mention_payload, { open_focus=true, pre_focus=true, command_path="/mention", on_sent=... })
169+
|- on_sent: submit_with_enter_key("/mention")
170+
\- on_sent: restore captured prompt input via delayed dispatch_send(...)
169171
```
170172

171173
### `:CodexResume` and `:CodexResume!`

lua/codex/config.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ M.defaults = {
5353
focus = "<leader>of",
5454
close = "<leader>ox",
5555
send = "<leader>os",
56-
add = "<leader>oa",
56+
mention_file = "<leader>om",
57+
mention_dir = "<leader>oM",
5758
resume = "<leader>or",
58-
model = "<leader>om",
5959
status = "<leader>oi",
6060
permissions = "<leader>op",
6161
compact = "<leader>oc",
@@ -82,17 +82,17 @@ local valid_keymap_actions = {
8282
focus = true,
8383
close = true,
8484
send = true,
85-
add = true,
85+
mention_file = true,
86+
mention_dir = true,
8687
resume = true,
87-
model = true,
8888
status = true,
8989
permissions = true,
9090
compact = true,
9191
review = true,
9292
diff = true,
9393
}
9494
local valid_keymap_action_list =
95-
"toggle, open, focus, close, send, add, resume, model, status, permissions, compact, review, diff"
95+
"toggle, open, focus, close, send, mention_file, mention_dir, resume, status, permissions, compact, review, diff"
9696

9797
---@param source table
9898
---@param known table

lua/codex/init.lua

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ local state = {
3737

3838
local process_pending_send_item
3939
local session_is_alive
40+
local dispatch_mention
4041
local BRACKETED_PASTE_START = "\27[200~"
4142
local BRACKETED_PASTE_END = "\27[201~"
4243
local SLASH_COMMAND_SUBMIT_SEQUENCE = "\n"
@@ -769,25 +770,12 @@ function M.send_selection(opts)
769770
})
770771
end
771772

772-
---Sends `/mention`, auto-submits it, then restores previously captured prompt input.
773-
---@param path? string Explicit path to mention. When nil, uses current buffer path.
773+
---Sends `/mention` for an already-resolved relative path, auto-submits, and restores prompt input.
774+
---@param resolved_path string Relative path to mention.
774775
---@return codex.SendResult ok True when mention payload is sent.
775776
---@return string|nil err
776-
function M.add_file(path)
777-
ensure_setup()
777+
dispatch_mention = function(resolved_path)
778778
local deps = get_deps()
779-
local resolved_path = path
780-
if resolved_path == nil then
781-
resolved_path = deps.vim.fn.expand("%:p")
782-
end
783-
784-
if not resolved_path or resolved_path == "" then
785-
local err = "current buffer has no file path"
786-
deps.logger.error("failed to add file context: %s", err)
787-
return false, err
788-
end
789-
790-
resolved_path = deps.path.to_relative(deps.vim, resolved_path)
791779
local mention = deps.formatter.format_mention(resolved_path)
792780

793781
local session, provider = get_active_session_and_provider()
@@ -828,6 +816,56 @@ function M.add_file(path)
828816
})
829817
end
830818

819+
---Sends `/mention` for a file, auto-submits it, then restores previously captured prompt input.
820+
---@param path? string Explicit file path to mention. When nil, uses current buffer path.
821+
---@return codex.SendResult ok True when mention payload is sent.
822+
---@return string|nil err
823+
function M.mention_file(path)
824+
ensure_setup()
825+
local deps = get_deps()
826+
local resolved_path = path
827+
if resolved_path == nil then
828+
resolved_path = deps.vim.fn.expand("%:p")
829+
end
830+
831+
if not resolved_path or resolved_path == "" then
832+
local err = "current buffer has no file path"
833+
deps.logger.error("failed to mention file: %s", err)
834+
return false, err
835+
end
836+
837+
resolved_path = deps.path.to_relative(deps.vim, resolved_path)
838+
return dispatch_mention(resolved_path)
839+
end
840+
841+
---Sends `/mention` for a directory, auto-submits it, then restores previously captured prompt input.
842+
---@param path? string Explicit directory path to mention. When nil, uses current buffer's directory.
843+
---@return codex.SendResult ok True when mention payload is sent.
844+
---@return string|nil err
845+
function M.mention_directory(path)
846+
ensure_setup()
847+
local deps = get_deps()
848+
local resolved_path = path
849+
if resolved_path == nil then
850+
local buf_path = deps.vim.fn.expand("%:p")
851+
if not buf_path or buf_path == "" then
852+
local err = "current buffer has no directory path"
853+
deps.logger.error("failed to mention directory: %s", err)
854+
return false, err
855+
end
856+
resolved_path = deps.vim.fn.expand("%:p:h")
857+
end
858+
859+
if not resolved_path or resolved_path == "" then
860+
local err = "current buffer has no directory path"
861+
deps.logger.error("failed to mention directory: %s", err)
862+
return false, err
863+
end
864+
865+
resolved_path = deps.path.to_relative(deps.vim, resolved_path)
866+
return dispatch_mention(resolved_path)
867+
end
868+
831869
---Returns whether an active Codex session is currently alive.
832870
---@return boolean
833871
function M.is_running()

lua/codex/nvim/commands.lua

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,33 @@ function M.register()
104104
})
105105

106106
---@param opts codex.UserCommandOpts
107-
vim.api.nvim_create_user_command("CodexAdd", function(opts)
107+
vim.api.nvim_create_user_command("CodexMentionFile", function(opts)
108108
local codex = require("codex")
109109
local path = opts.args
110110
if path == "" then
111111
path = nil
112112
end
113-
codex.add_file(path)
113+
codex.mention_file(path)
114114
end, {
115-
desc = "Add file context to Codex via /mention",
115+
desc = "Mention a file in Codex via /mention",
116116
nargs = "?",
117117
complete = "file",
118118
})
119119

120+
---@param opts codex.UserCommandOpts
121+
vim.api.nvim_create_user_command("CodexMentionDirectory", function(opts)
122+
local codex = require("codex")
123+
local path = opts.args
124+
if path == "" then
125+
path = nil
126+
end
127+
codex.mention_directory(path)
128+
end, {
129+
desc = "Mention a directory in Codex via /mention",
130+
nargs = "?",
131+
complete = "dir",
132+
})
133+
120134
---@param opts codex.UserCommandOpts
121135
vim.api.nvim_create_user_command("CodexResume", function(opts)
122136
local codex = require("codex")

lua/codex/nvim/keymaps.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,23 @@ function M.register(config, vim_api)
8181
set_mapping(vim_api, "n", maps.open, "<Cmd>Codex!<CR>", "Codex: Open and focus", force)
8282
set_mapping(vim_api, "n", maps.focus, "<Cmd>CodexFocus<CR>", "Codex: Focus terminal", force)
8383
set_mapping(vim_api, "n", maps.close, "<Cmd>CodexClose<CR>", "Codex: Close session", force)
84-
set_mapping(vim_api, "n", maps.add, "<Cmd>CodexAdd<CR>", "Codex: Add current file", force)
84+
set_mapping(
85+
vim_api,
86+
"n",
87+
maps.mention_file,
88+
"<Cmd>CodexMentionFile<CR>",
89+
"Codex: Mention current file",
90+
force
91+
)
92+
set_mapping(
93+
vim_api,
94+
"n",
95+
maps.mention_dir,
96+
"<Cmd>CodexMentionDirectory<CR>",
97+
"Codex: Mention current directory",
98+
force
99+
)
85100
set_mapping(vim_api, "n", maps.resume, "<Cmd>CodexResume<CR>", "Codex: Resume session", force)
86-
set_mapping(vim_api, "n", maps.model, "<Cmd>CodexModel<CR>", "Codex: Set model", force)
87101
set_mapping(vim_api, "n", maps.status, "<Cmd>CodexStatus<CR>", "Codex: Show status", force)
88102
set_mapping(
89103
vim_api,

lua/codex/types.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
---@field focus string|false
5555
---@field close string|false
5656
---@field send string|false
57-
---@field add string|false
57+
---@field mention_file string|false
58+
---@field mention_dir string|false
5859
---@field resume string|false
59-
---@field model string|false
6060
---@field status string|false
6161
---@field permissions string|false
6262
---@field compact string|false

0 commit comments

Comments
 (0)