Skip to content

Commit af4e1a8

Browse files
authored
feat: add_visual_selection_inline should support non-file buffer (#442)
``` opts.keymap.output_window = { ['r'] = { 'add_visual_selection_inline', mode = { 'v' } }, } ```
1 parent c36fd33 commit af4e1a8

2 files changed

Lines changed: 36 additions & 21 deletions

File tree

lua/opencode/context.lua

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,28 @@ end
181181
function M.build_inline_selection_text(range)
182182
local buf = vim.api.nvim_get_current_buf()
183183

184-
if not util.is_buf_a_file(buf) then
185-
vim.notify('Cannot add selection: not a file buffer', vim.log.levels.WARN)
186-
return nil
187-
end
188-
189184
local current_selection = BaseContext.get_current_selection(nil, range)
190185
if not current_selection then
191186
vim.notify('No visual selection found', vim.log.levels.WARN)
192187
return nil
193188
end
194189

195190
local file = BaseContext.get_current_file_for_selection(buf)
196-
if not file then
197-
vim.notify('Cannot determine file for selection', vim.log.levels.WARN)
198-
return nil
199-
end
200191

192+
local n = 3
193+
current_selection.text:gsub('(`+)', function(run)
194+
n = math.max(n, #run + 1)
195+
end)
196+
197+
local fence = string.rep('`', n)
201198
local filetype = vim.bo[buf].filetype or ''
202-
local text = string.format('**`%s`**\n\n```%s\n%s\n```', file.path, filetype, current_selection.text)
199+
local code = current_selection.text:gsub('^%s+', ''):gsub('%s+$', '')
200+
local text ---@type string
201+
if file then
202+
text = string.format('**`%s`**\n\n%s%s\n%s\n%s', file.path, fence, filetype, code, fence)
203+
else
204+
text = string.format('%s%s\n%s\n%s', fence, filetype, code, fence)
205+
end
203206

204207
return text
205208
end

tests/unit/context_spec.lua

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,33 +1194,45 @@ describe('build_inline_selection_text', function()
11941194
vim.bo = original_bo
11951195
end)
11961196

1197-
it('should return nil and notify when not a file buffer', function()
1197+
it('should return formatted text without file path for non-file buffer', function()
11981198
local original_is_buf_a_file = util.is_buf_a_file
1199+
local original_get_current_selection = BaseContext.get_current_selection
1200+
local original_get_current_file_for_selection = BaseContext.get_current_file_for_selection
11991201
local original_get_current_buf = vim.api.nvim_get_current_buf
12001202

12011203
util.is_buf_a_file = function()
12021204
return false
12031205
end
1206+
BaseContext.get_current_selection = function()
1207+
return { text = 'function foo()\n return 42\nend', lines = '10, 12' }
1208+
end
1209+
BaseContext.get_current_file_for_selection = function()
1210+
return nil
1211+
end
12041212
vim.api.nvim_get_current_buf = function()
12051213
return 10
12061214
end
12071215

1208-
local original_notify = vim.notify
1209-
local notifications = {}
1210-
vim.notify = function(msg, level)
1211-
table.insert(notifications, { msg = msg, level = level })
1212-
end
1216+
local original_bo = vim.bo
1217+
vim.bo = setmetatable({}, {
1218+
__index = function(_, _)
1219+
return { filetype = 'lua' }
1220+
end,
1221+
})
12131222

12141223
local text = context.build_inline_selection_text()
12151224

1216-
assert.is_nil(text)
1217-
assert.equal(1, #notifications)
1218-
assert.equal('Cannot add selection: not a file buffer', notifications[1].msg)
1219-
assert.equal(vim.log.levels.WARN, notifications[1].level)
1225+
assert.is_not_nil(text)
1226+
assert.is_nil(text:match('%*%*`'))
1227+
assert.is_not_nil(text:match('```lua'))
1228+
assert.is_not_nil(text:match('function foo%(%)'))
1229+
assert.is_not_nil(text:match('```$'))
12201230

12211231
util.is_buf_a_file = original_is_buf_a_file
1232+
BaseContext.get_current_selection = original_get_current_selection
1233+
BaseContext.get_current_file_for_selection = original_get_current_file_for_selection
12221234
vim.api.nvim_get_current_buf = original_get_current_buf
1223-
vim.notify = original_notify
1235+
vim.bo = original_bo
12241236
end)
12251237

12261238
it('should return nil and notify when no visual selection found', function()

0 commit comments

Comments
 (0)