Skip to content

Commit 6b2a44e

Browse files
committed
perf: don't enforce path mapping with dummy function by default
1 parent d7a3bdc commit 6b2a44e

3 files changed

Lines changed: 49 additions & 44 deletions

File tree

lua/opencode/api_client.lua

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,7 @@ local state = require('opencode.state')
33
local url_encode = require('opencode.util').url_encode
44
local apply_path_map = require('opencode.util').apply_path_map
55
local reverse_transform_paths_recursive = require('opencode.util').reverse_transform_paths_recursive
6-
7-
--- Transform file paths in API payloads using configured path_map
8-
--- @param data any The data to transform (table, string, or other)
9-
--- @return any transformed_data The data with paths transformed
10-
local function transform_paths_recursive(data)
11-
if type(data) ~= 'table' then
12-
return data
13-
end
14-
15-
local result = {}
16-
for key, value in pairs(data) do
17-
if type(value) == 'string' and (key == 'filePath' or key == 'path' or key == 'directory') then
18-
result[key] = apply_path_map(value)
19-
elseif type(value) == 'table' then
20-
result[key] = transform_paths_recursive(value)
21-
else
22-
result[key] = value
23-
end
24-
end
25-
return result
26-
end
6+
local transform_paths_recursive = require('opencode.util').transform_paths_recursive
277

288
--- @class OpencodeApiClient
299
--- @field base_url string The base URL of the opencode server
@@ -52,8 +32,6 @@ function OpencodeApiClient:_ensure_base_url()
5232
return true
5333
end
5434

55-
local state = require('opencode.state')
56-
5735
if not state.opencode_server then
5836
-- this is last resort - try to start the server and could be blocking
5937
state.opencode_server = server_job.ensure_server():wait() --[[@as OpencodeServer]]

lua/opencode/config.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ M.defaults = {
2121
spawn_command = nil,
2222
kill_command = nil,
2323
auto_kill = true,
24-
path_map = function(path)
25-
return path
26-
end,
27-
reverse_path_map = function(path)
28-
return path
29-
end,
24+
path_map = nil,
25+
reverse_path_map = nil,
3026
},
3127
keymap = {
3228
editor = {

lua/opencode/util.lua

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ end
9898
--- @param str string The string to encode
9999
--- @return string encoded_string The URL-encoded string
100100
function M.url_encode(str)
101-
if not str then return '' end
101+
if not str then
102+
return ''
103+
end
102104
str = tostring(str)
103105
str = string.gsub(str, '\n', '\r\n')
104106
str = string.gsub(str, '([^%w%-%.%_%~])', function(c)
@@ -114,10 +116,10 @@ function M.apply_path_map(path)
114116
if not path then
115117
return path
116118
end
117-
119+
118120
local config = require('opencode.config')
119121
local path_map = config.server.path_map
120-
122+
121123
if type(path_map) == 'function' then
122124
local ok, result = pcall(path_map, path)
123125
if ok and result then
@@ -134,44 +136,46 @@ function M.apply_path_map(path)
134136
return path_map .. relative_path
135137
end
136138
end
137-
139+
138140
return path
139141
end
140142

141143
function M.apply_reverse_path_map(path)
142144
if not path then
143145
return path
144146
end
145-
147+
146148
local config = require('opencode.config')
147149
local reverse_path_map = config.server.reverse_path_map
148-
150+
149151
if type(reverse_path_map) == 'function' then
150152
local ok, result = pcall(reverse_path_map, path)
151153
if ok and result then
152154
return result
153155
end
154156
return path
155157
end
156-
158+
157159
return path
158160
end
159161

160162
function M.reverse_transform_paths_recursive(data)
163+
local config = require('opencode.config')
164+
local reverse_path_map = config.server.reverse_path_map
165+
if not reverse_path_map or type(reverse_path_map) ~= 'function' then
166+
return data
167+
end
168+
161169
if type(data) ~= 'table' then
162170
return data
163171
end
164172

165173
local result = {}
166174
for key, value in pairs(data) do
167-
if type(value) == 'string' and (
168-
key == 'filePath' or
169-
key == 'path' or
170-
key == 'file' or
171-
key == 'directory' or
172-
key == 'cwd' or
173-
key == 'root'
174-
) then
175+
if
176+
type(value) == 'string'
177+
and (key == 'filePath' or key == 'path' or key == 'file' or key == 'directory' or key == 'cwd' or key == 'root')
178+
then
175179
result[key] = M.apply_reverse_path_map(value)
176180
elseif type(value) == 'table' and (key == 'files' or key == 'deleted_files') then
177181
result[key] = vim.tbl_map(M.apply_reverse_path_map, value)
@@ -184,6 +188,33 @@ function M.reverse_transform_paths_recursive(data)
184188
return result
185189
end
186190

191+
--- Transform file paths in API payloads using configured path_map
192+
--- @param data any The data to transform (table, string, or other)
193+
--- @return any transformed_data The data with paths transformed
194+
function M.transform_paths_recursive(data)
195+
local config = require('opencode.config')
196+
local path_map = config.server.path_map
197+
if not path_map or type(path_map) ~= 'function' then
198+
return data
199+
end
200+
201+
if type(data) ~= 'table' then
202+
return data
203+
end
204+
205+
local result = {}
206+
for key, value in pairs(data) do
207+
if type(value) == 'string' and (key == 'filePath' or key == 'path' or key == 'directory') then
208+
result[key] = M.apply_path_map(value)
209+
elseif type(value) == 'table' then
210+
result[key] = M.transform_paths_recursive(value)
211+
else
212+
result[key] = value
213+
end
214+
end
215+
return result
216+
end
217+
187218
--- Format a timestamp as time (e.g., "10:23 AM", "13 Oct 03:32 PM" "13 Oct 2025 03:32 PM")
188219
--- @param timestamp number
189220
--- @return string: Formatted time string

0 commit comments

Comments
 (0)