Skip to content

Commit c73dfdc

Browse files
authored
feat: support 'filetypes' option (#52)
1 parent 55fbea9 commit c73dfdc

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,17 @@ suggestion = {
8484
dismiss = "<C-]>",
8585
},
8686
},
87-
ft_disable = {},
87+
filetypes = {
88+
yaml = false,
89+
markdown = false,
90+
help = false,
91+
gitcommit = false,
92+
gitrebase = false,
93+
hgcommit = false,
94+
svn = false,
95+
cvs = false,
96+
["."] = false,
97+
},
8898
copilot_node_command = 'node', -- Node version must be < 18
8999
plugin_manager_path = vim.fn.stdpath("data") .. "/site/pack/packer",
90100
server_opts_overrides = {},
@@ -97,7 +107,7 @@ in a split window.
97107

98108
If `auto_refresh` is `true`, the suggestions are refreshed as you type in the buffer.
99109

100-
#### suggestion
110+
##### suggestion
101111

102112
When `auto_trigger` is `true`, copilot starts suggesting as soon as you enter insert mode.
103113

@@ -120,15 +130,30 @@ end)
120130
```
121131

122132

123-
##### ft_disable
133+
##### filetypes
124134

125-
Prevents copilot from attaching to buffers with specific filetypes.
135+
Specify filetypes for attaching copilot.
126136

127137
Example:
128138

129139
```lua
130140
require("copilot").setup {
131-
ft_disable = { "markdown", "terraform" },
141+
filetypes = {
142+
markdown = true, -- overrides default
143+
terraform = false, -- disallow specific filetype
144+
},
145+
}
146+
```
147+
148+
If you add `"*"` as a filetype, the default configuration for `filetypes` won't be used anymore. e.g.
149+
150+
```lua
151+
require("copilot").setup {
152+
filetypes = {
153+
javascript = true, -- allow specific filetype
154+
typescript = true, -- allow specific filetype
155+
["*"] = false, -- disable for all other filetypes and ignore default `filetypes`
156+
},
132157
}
133158
```
134159

lua/copilot/client.lua

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,50 @@ 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+
1251
M.buf_attach_copilot = function()
13-
if vim.tbl_contains(M.params.ft_disable, vim.bo.filetype) then return end
52+
if is_ft_disabled(vim.bo.filetype) then
53+
return
54+
end
55+
1456
if not vim.bo.buflisted or not vim.bo.buftype == "" then return end
1557
-- The filter param to get_active_clients() can be used on Neovim 0.8 and later.
1658
for _, client in pairs(vim.lsp.get_active_clients()) do

lua/copilot/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ local defaults = {
2626
dismiss = "<C-]>",
2727
}
2828
},
29-
ft_disable = {},
29+
ft_disable = nil,
30+
filetypes = {},
3031
copilot_node_command = "node",
3132
plugin_manager_path = vim.fn.stdpath("data") .. "/site/pack/packer",
3233
server_opts_overrides = {},

0 commit comments

Comments
 (0)