diff --git a/lua/CopilotChat/config.lua b/lua/CopilotChat/config.lua index d3f2b8cb..ba24edb1 100644 --- a/lua/CopilotChat/config.lua +++ b/lua/CopilotChat/config.lua @@ -20,6 +20,7 @@ ---@field resources string|table|nil ---@field sticky string|table|nil ---@field language string? +---@field selection 'visual'|'unnamed'|nil ---@field temperature number? ---@field headless boolean? ---@field callback nil|fun(response: CopilotChat.client.Message, source: CopilotChat.source) @@ -62,6 +63,7 @@ return { sticky = nil, -- Default sticky prompt or array of sticky prompts to use at start of every new chat (can be specified manually in prompt via >). language = 'English', -- Default language to use for answers + selection = 'visual', -- Selection source temperature = 0.1, -- Result temperature headless = false, -- Do not write to chat buffer and use history (useful for using custom processing) callback = nil, -- Function called when full response is received diff --git a/lua/CopilotChat/select.lua b/lua/CopilotChat/select.lua index 1d2b8bcb..669876df 100644 --- a/lua/CopilotChat/select.lua +++ b/lua/CopilotChat/select.lua @@ -8,6 +8,7 @@ local constants = require('CopilotChat.constants') local utils = require('CopilotChat.utils') +local config = require('CopilotChat.config') local M = {} @@ -35,6 +36,16 @@ function M.unnamed(_) return nil end +--- Get the marks used for selection +---@return string[] +function M.marks() + local marks = { '<', '>' } + if config.selection == 'unnamed' then + marks = { '[', ']' } + end + return marks +end + --- Highlight selection in target buffer or clear it ---@param bufnr number ---@param clear boolean? @@ -68,8 +79,9 @@ function M.get(bufnr) return nil end - local start_line = unpack(vim.api.nvim_buf_get_mark(bufnr, '<')) - local finish_line = unpack(vim.api.nvim_buf_get_mark(bufnr, '>')) + local marks = M.marks() + local start_line = unpack(vim.api.nvim_buf_get_mark(bufnr, marks[1])) + local finish_line = unpack(vim.api.nvim_buf_get_mark(bufnr, marks[2])) if start_line == 0 or finish_line == 0 then return nil end @@ -106,17 +118,17 @@ function M.set(bufnr, winnr, start_line, end_line) return end + local marks = M.marks() + if not start_line or not end_line then - for _, mark in ipairs({ '<', '>', '[', ']' }) do + for _, mark in ipairs(marks) do pcall(vim.api.nvim_buf_del_mark, bufnr, mark) end return end - pcall(vim.api.nvim_buf_set_mark, bufnr, '<', start_line, 0, {}) - pcall(vim.api.nvim_buf_set_mark, bufnr, '>', end_line, 0, {}) - pcall(vim.api.nvim_buf_set_mark, bufnr, '[', start_line, 0, {}) - pcall(vim.api.nvim_buf_set_mark, bufnr, ']', end_line, 0, {}) + pcall(vim.api.nvim_buf_set_mark, bufnr, marks[1], start_line, 0, {}) + pcall(vim.api.nvim_buf_set_mark, bufnr, marks[2], end_line, 0, {}) if winnr and vim.api.nvim_win_is_valid(winnr) then pcall(vim.api.nvim_win_set_cursor, winnr, { start_line, 0 })