repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/opencode.lua

Raw
local map  = require 'std.map'
local auto = require 'std.auto'
local puk  = require 'std.puk'

puk 'opencode'

vim.g.opencode_opts = {
	lsp = {
		enabled = true,
	},
	events = {
		enabled = true,
		reload = true,
		permissions = {
			enabled = true,
			edits = {
				enabled = true,
			},
		},
	},
}

vim.o.autoread = true -- Required for `opts.events.reload`

local opencode = require 'opencode'

require('bugabinga.statusline').register(function()
  local ok, status = pcall(opencode.statusline)
  if not ok then return '' end
  return status
end)

map.normal.terminal {
	description = 'toggle opencode terminal',
	category = 'opencode',
	keys = '<C-.>',
	command = function ()
		opencode.toggle()
	end,
}

map.normal.visual {
	description = 'ask opencode (with @this)',
	category = 'opencode',
	keys = '<leader>oa',
	command = function ()
		opencode.ask( '@this: ', { submit = true } )
	end,
}

map.normal {
	description = 'ask opencode (free form)',
	category = 'opencode',
	keys = '<leader>oA',
	command = function ()
		opencode.ask()
	end,
}

map.normal.visual {
	description = 'opencode picker',
	category = 'opencode',
	keys = '<leader>ox',
	command = function ()
		opencode.select()
	end,
}

map.normal {
	description = 'scroll opencode up',
	category = 'opencode',
	keys = '<S-C-u>',
	command = function ()
		opencode.command 'session.half.page.up'
	end,
}

map.normal {
	description = 'scroll opencode down',
	category = 'opencode',
	keys = '<S-C-d>',
	command = function ()
		opencode.command 'session.half.page.down'
	end,
}

map.normal.visual {
	description = 'send motion to opencode',
	category = 'opencode',
	keys = 'go',
	command = function ()
		return opencode.operator '@this '
	end,
	expr = true,
}

map.normal {
	description = 'send current line to opencode',
	category = 'opencode',
	keys = 'goo',
	command = function ()
		return opencode.operator '@this ' .. '_'
	end,
	expr = true,
}

auto 'opencode_events' {
	events = 'User',
	pattern = 'OpencodeEvent:*',
	command = function ( args )
		local event = args.data.event
		if event.type == 'session.idle' then
			vim.notify( '`opencode` finished responding', vim.log.levels.INFO )
		elseif event.type == 'session.error' then
			vim.notify( '`opencode` encountered an error', vim.log.levels.ERROR )
		end
	end,
}