forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
169 lines (148 loc) · 4.15 KB
/
Copy pathutil.lua
File metadata and controls
169 lines (148 loc) · 4.15 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
local M = {}
local id = 0
function M.get_next_id()
id = id + 1
return id
end
-- keep for debugging reasons
M.get_editor_info = function ()
local info = vim.empty_dict()
info.editorInfo = vim.empty_dict()
info.editorInfo.name = 'Neovim'
info.editorInfo.version = '0.8.0-dev-809-g7dde6d4fd'
info.editorPluginInfo = vim.empty_dict()
info.editorPluginInfo.name = 'copilot.vim'
info.editorPluginInfo.version = '1.5.3'
return info
end
-- keep for debugging reasons
local get_capabilities = function ()
return {
capabilities = {
textDocumentSync = {
change = 2,
openClose = true
},
workspace = {
workspaceFolders = {
changeNotifications = true,
supported = true
}
}
}
}
end
local format_pos = function()
local pos = vim.api.nvim_win_get_cursor(0)
return { character = pos[2], line = pos[1] - 1 }
end
local get_relfile = function()
local file, _ = string.gsub(vim.api.nvim_buf_get_name(0), vim.loop.cwd() .. "/", "")
return file
end
M.get_copilot_client = function()
-- vim.lsp.get_active_clients({name="copilot"}) -- not in 0.7
for _, client in pairs(vim.lsp.get_active_clients()) do
if client.name == "copilot" then return client end
end
end
local eol_by_fileformat = {
unix = "\n",
dos = "\r\n",
mac = "\r",
}
local language_normalization_map = {
text = "plaintext",
javascriptreact = "javascript",
jsx = "javascript",
typescriptreact = "typescript",
}
local function language_for_file_type(filetype)
local ft = string.gsub(filetype, "%..*", "")
if not ft or ft == "" then
ft = "text"
end
return language_normalization_map[ft] or ft
end
local function relative_path(absolute)
local relative = vim.fn.fnamemodify(absolute, ":.")
if string.sub(relative, 0, 1) == "/" then
return vim.fn.fnamemodify(absolute, ":t")
end
return relative
end
function M.get_doc()
local absolute = vim.api.nvim_buf_get_name(0)
local params = vim.lsp.util.make_position_params(0, "utf-16")
local doc = {
languageId = language_for_file_type(vim.bo.filetype),
path = absolute,
uri = params.textDocument.uri,
relativePath = relative_path(absolute),
insertSpaces = vim.o.expandtab,
tabSize = vim.fn.shiftwidth(),
indentSize = vim.fn.shiftwidth(),
position = params.position,
}
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
if vim.bo.endofline and vim.bo.fixendofline then
table.insert(lines, "")
end
doc.source = table.concat(lines, eol_by_fileformat[vim.bo.fileformat] or "\n")
return doc
end
function M.get_doc_params(params)
params = params or {}
local doc_params = vim.tbl_extend("keep", {
doc = vim.tbl_extend("force", M.get_doc(), params.doc or {}),
}, params)
doc_params.textDocument = {
uri = doc_params.doc.uri,
languageId = doc_params.doc.languageId,
relativePath = doc_params.doc.relativePath,
position = doc_params.doc.position,
}
return doc_params
end
M.get_completion_params = function(opts)
local rel_path = get_relfile()
local uri = vim.uri_from_bufnr(0)
local params = {
doc = {
source = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n"),
relativePath = rel_path,
languageId = normalize_ft(vim.api.nvim_buf_get_option(0, 'filetype')),
insertSpaces = vim.o.expandtab,
tabsize = vim.bo.shiftwidth,
indentsize = vim.bo.shiftwidth,
position = format_pos(),
path = vim.api.nvim_buf_get_name(0),
uri = uri,
},
textDocument = {
languageId = vim.bo.filetype,
relativePath = rel_path,
uri = uri,
}
}
params.position = params.doc.position
if opts then params.doc = vim.tbl_deep_extend('keep', params.doc, opts) end
return params
end
M.get_copilot_path = function(plugin_path)
for _, loc in ipairs({ "/opt", "/start", "" }) do
local copilot_path = plugin_path .. loc .. "/copilot.lua/copilot/index.js"
if vim.fn.filereadable(copilot_path) ~= 0 then
return copilot_path
end
end
end
M.auth = function ()
local c = M.get_copilot_client()
if not c then
print("[Copilot] not running yet!")
return
end
require("copilot.auth").setup(c)
end
return M