-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathutil.lua
116 lines (98 loc) · 3.14 KB
/
util.lua
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
local config = require("copilot.config")
local logger = require("copilot.logger")
local M = {}
---@return { editorInfo: copilot_editor_info, editorPluginInfo: copilot_editor_plugin_info }
function M.get_editor_info()
local info = {
editorInfo = {
name = "Neovim",
version = string.match(vim.fn.execute("version"), "NVIM v(%S+)"),
},
editorPluginInfo = {
name = "copilot.lua",
-- reflects version of github/copilot-language-server-release
version = "1.304.0",
},
}
return info
end
local copilot_lua_version = nil
function M.get_copilot_lua_version()
if not copilot_lua_version then
local plugin_version_ok, plugin_version = pcall(function()
local plugin_dir = M.get_plugin_path()
return vim.fn.systemlist(string.format("cd %s && git rev-parse HEAD", plugin_dir))[1]
end)
copilot_lua_version = plugin_version_ok and plugin_version or "dev"
end
return copilot_lua_version
end
---@return boolean should_attach
---@return string? no_attach_reason
function M.should_attach()
local ft = config.filetypes
local ft_disabled, ft_disabled_reason = require("copilot.client.filetypes").is_ft_disabled(vim.bo.filetype, ft)
if ft_disabled then
return not ft_disabled, ft_disabled_reason
end
local bufnr = vim.api.nvim_get_current_buf()
local bufname = vim.api.nvim_buf_get_name(bufnr)
local conf_attach = config.should_attach(bufnr, bufname)
if not conf_attach then
return false, "copilot is disabled"
end
return true
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") -- copilot server uses utf-16
local doc = {
uri = params.textDocument.uri,
version = vim.api.nvim_buf_get_var(0, "changedtick"),
relativePath = relative_path(absolute),
insertSpaces = vim.o.expandtab,
tabSize = vim.fn.shiftwidth(),
indentSize = vim.fn.shiftwidth(),
position = params.position,
}
return doc
end
-- Used by copilot.cmp so watch out if moving it
function M.get_doc_params(overrides)
overrides = overrides or {}
local params = vim.tbl_extend("keep", {
doc = vim.tbl_extend("force", M.get_doc(), overrides.doc or {}),
}, overrides)
params.textDocument = {
uri = params.doc.uri,
version = params.doc.version,
relativePath = params.doc.relativePath,
}
params.position = params.doc.position
return params
end
M.get_plugin_path = function()
local copilot_path = vim.api.nvim_get_runtime_file("lua/copilot/init.lua", false)[1]
if vim.fn.filereadable(copilot_path) ~= 0 then
return vim.fn.fnamemodify(copilot_path, ":h:h:h")
else
logger.error("could not read" .. copilot_path)
end
end
---@param str string
---@return integer
function M.strutf16len(str)
if vim.fn.exists("*strutf16len") == 1 then
return vim.fn.strutf16len(str)
else
return vim.fn.strchars(vim.fn.substitute(str, [==[\\%#=2[^\u0001-\uffff]]==], " ", "g"))
end
end
return M