Skip to content

Commit de2c8be

Browse files
committed
refactor: copilot attach conditions
1 parent db575ff commit de2c8be

File tree

2 files changed

+68
-47
lines changed

2 files changed

+68
-47
lines changed

lua/copilot/client.lua

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,10 @@ local register_autocmd = function ()
99
})
1010
end
1111

12-
local default_filetypes = {
13-
yaml = false,
14-
markdown = false,
15-
help = false,
16-
gitcommit = false,
17-
gitrebase = false,
18-
hgcommit = false,
19-
svn = false,
20-
cvs = false,
21-
["."] = false,
22-
}
23-
24-
local function is_ft_disabled(ft)
25-
--- for backward compatibility
26-
if M.params.ft_disable and vim.tbl_contains(M.params.ft_disable, ft) then
27-
return true
28-
end
29-
30-
if M.params.filetypes[ft] ~= nil then
31-
return not M.params.filetypes[ft]
32-
end
33-
34-
local short_ft = string.gsub(ft, "%..*", "")
35-
36-
if M.params.filetypes[short_ft] ~= nil then
37-
return not M.params.filetypes[short_ft]
38-
end
39-
40-
if M.params.filetypes['*'] ~= nil then
41-
return not M.params.filetypes['*']
42-
end
43-
44-
if default_filetypes[short_ft] ~= nil then
45-
return not default_filetypes[short_ft]
46-
end
47-
48-
return false
49-
end
50-
5112
---@param force? boolean
5213
function M.buf_attach(client, force)
53-
if not force then
54-
if is_ft_disabled(vim.bo.filetype) then
55-
return
56-
end
57-
58-
if not vim.bo.buflisted or not vim.bo.buftype == "" then
59-
return
60-
end
14+
if not force and not util.should_attach(M.params.filetypes) then
15+
return
6116
end
6217

6318
client = client or util.get_copilot_client()
@@ -106,6 +61,13 @@ end
10661

10762
M.start = function(params)
10863
M.params = params
64+
--- for backward compatibility
65+
if M.params.ft_disable then
66+
for _, disabled_ft in ipairs(M.params.ft_disable) do
67+
M.params.filetypes[disabled_ft] = false
68+
end
69+
end
70+
10971
vim.lsp.start_client(M.merge_server_opts(params))
11072
end
11173

lua/copilot/util.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,65 @@ M.get_copilot_client = function()
5757
end
5858
end
5959

60+
local internal_filetypes = {
61+
yaml = false,
62+
markdown = false,
63+
help = false,
64+
gitcommit = false,
65+
gitrebase = false,
66+
hgcommit = false,
67+
svn = false,
68+
cvs = false,
69+
["."] = false,
70+
}
71+
72+
---@param ft string
73+
---@param filetypes table<string, boolean>
74+
---@return boolean ft_disabled
75+
---@return string? ft_disabled_reason
76+
local function is_ft_disabled(ft, filetypes)
77+
if filetypes[ft] ~= nil then
78+
return not filetypes[ft], string.format("'filetype' %s rejected by config filetypes[%s]", ft, ft)
79+
end
80+
81+
local short_ft = string.gsub(ft, "%..*", "")
82+
83+
if filetypes[short_ft] ~= nil then
84+
return not filetypes[short_ft], string.format("'filetype' %s rejected by config filetypes[%s]", ft, short_ft)
85+
end
86+
87+
if filetypes["*"] ~= nil then
88+
return not filetypes["*"], string.format("'filetype' %s rejected by config filetypes[%s]", ft, "*")
89+
end
90+
91+
if internal_filetypes[short_ft] ~= nil then
92+
return not internal_filetypes[short_ft], string.format("'filetype' %s rejected by internal_filetypes[%s]", ft, short_ft)
93+
end
94+
95+
return false
96+
end
97+
98+
---@param filetypes table<string, boolean>
99+
---@return boolean should_attach
100+
---@return string? no_attach_reason
101+
function M.should_attach(filetypes)
102+
local ft_disabled, ft_disabled_reason = is_ft_disabled(vim.bo.filetype, filetypes)
103+
104+
if ft_disabled then
105+
return not ft_disabled, ft_disabled_reason
106+
end
107+
108+
if not vim.bo.buflisted then
109+
return false, "buffer not 'buflisted'"
110+
end
111+
112+
if not vim.bo.buftype == "" then
113+
return false, "buffer 'buftype' is " .. vim.bo.buftype
114+
end
115+
116+
return true
117+
end
118+
60119
function M.is_attached(client)
61120
client = client or M.get_copilot_client()
62121
return client and vim.lsp.buf_is_attached(0, client.id) or false

0 commit comments

Comments
 (0)