Skip to content
Merged
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
2 changes: 2 additions & 0 deletions lua/CopilotChat/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
---@field resources string|table<string>|nil
---@field sticky string|table<string>|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)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions lua/CopilotChat/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

local constants = require('CopilotChat.constants')
local utils = require('CopilotChat.utils')
local config = require('CopilotChat.config')

local M = {}

Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 })
Expand Down
Loading