Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ programs.nixvim = {
| `@marks` | Global marks |
| `@grapple` | [grapple.nvim](https://github.com/cbochs/grapple.nvim) tags |

#### Absolute Paths

By default, file paths in contexts are relative to Neovim's working directory. If `opencode` is running in a different directory, these relative paths may not resolve correctly.

Set `absolute_paths = true` to use absolute file paths instead:

```lua
vim.g.opencode_opts = {
absolute_paths = true,
}
```

### Prompts

Select or reference prompts to review, explain, and improve your code:
Expand Down
5 changes: 5 additions & 0 deletions lua/opencode/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ vim.g.opencode_opts = vim.g.opencode_opts
---If set, `opencode.nvim` will append `--port <port>` to `provider.cmd` if not already present.
---@field port? number
---
---Use absolute file paths instead of paths relative to Neovim's CWD.
---Useful when `opencode` is running in a different directory than Neovim.
---@field absolute_paths? boolean
---
---Contexts to inject into prompts, keyed by their placeholder.
---@field contexts? table<string, fun(context: opencode.Context): string|nil>
---
Expand Down Expand Up @@ -45,6 +49,7 @@ vim.g.opencode_opts = vim.g.opencode_opts
---@type opencode.Opts
local defaults = {
port = nil,
absolute_paths = false,
-- stylua: ignore
contexts = {
["@this"] = function(context) return context:this() end,
Expand Down
7 changes: 5 additions & 2 deletions lua/opencode/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,12 @@ end
function Context.format(args)
local result = ""
if (args.buf and is_buf_valid(args.buf)) or args.path then
local rel_path = vim.fn.fnamemodify(args.path or vim.api.nvim_buf_get_name(args.buf), ":.")
local raw_path = args.path or vim.api.nvim_buf_get_name(args.buf)
local use_absolute = require("opencode.config").opts.absolute_paths
-- `:p` expands to full path, `:." makes relative to CWD
local path = vim.fn.fnamemodify(raw_path, use_absolute and ":p" or ":.")
-- Must be preceeded by @ and followed by space for `opencode` to parse as a file reference
result = "@" .. rel_path .. " "
result = "@" .. path .. " "
end
if args.start_line and args.end_line and args.start_line > args.end_line then
args.start_line, args.end_line = args.end_line, args.start_line
Expand Down