forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.lua
More file actions
142 lines (121 loc) · 3.51 KB
/
select.lua
File metadata and controls
142 lines (121 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---@class CopilotChat.select.Selection
---@field content string
---@field start_line number
---@field end_line number
---@field filename string
---@field filetype string
---@field bufnr number
local log = require('plenary.log')
local utils = require('CopilotChat.utils')
local M = {}
--- Use #selection instead
---@deprecated
function M.visual(_)
log.warn('CopilotChat.select.visual is deprecated, use #selection instead')
return nil
end
--- Use #selection instead
---@deprecated use #selection instead
function M.buffer(_)
log.warn('CopilotChat.select.buffer is deprecated, use #selection instead')
return nil
end
--- Use #selection instead
---@deprecated use #selection instead
function M.line(_)
log.warn('CopilotChat.select.line is deprecated, use #selection instead')
return nil
end
--- Use #selection instead
---@deprecated use #selection instead
function M.unnamed(_)
log.warn('CopilotChat.select.unnamed is deprecated, use #selection instead')
return nil
end
--- Get the marks used for selection
---@return string[]
function M.marks()
local config = require('CopilotChat.config')
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?
function M.highlight(bufnr, clear)
local selection_ns = vim.api.nvim_create_namespace('copilot-chat-selection')
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
vim.api.nvim_buf_clear_namespace(buf, selection_ns, 0, -1)
end
if clear then
return
end
local selection = M.get(bufnr)
if not selection then
return
end
vim.api.nvim_buf_set_extmark(selection.bufnr, selection_ns, selection.start_line - 1, 0, {
hl_group = 'CopilotChatSelection',
end_row = selection.end_line,
strict = false,
})
end
--- Get the selection from the target buffer
---@param bufnr number
---@return CopilotChat.select.Selection?
function M.get(bufnr)
if not utils.buf_valid(bufnr) then
return nil
end
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
if start_line > finish_line then
start_line, finish_line = finish_line, start_line
end
local ok, lines = pcall(vim.api.nvim_buf_get_lines, bufnr, start_line - 1, finish_line, false)
if not ok then
return nil
end
local lines_content = table.concat(lines, '\n')
if vim.trim(lines_content) == '' then
return nil
end
return {
content = lines_content,
filename = vim.api.nvim_buf_get_name(bufnr),
filetype = vim.bo[bufnr].filetype,
start_line = start_line,
end_line = finish_line,
bufnr = bufnr,
}
end
--- Sets the selection to specific lines in buffer or clears it
---@param bufnr number
---@param winnr number?
---@param start_line number?
---@param end_line number?
function M.set(bufnr, winnr, start_line, end_line)
if not utils.buf_valid(bufnr) then
return
end
local marks = M.marks()
if not start_line or not end_line then
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, 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 })
end
end
return M