Skip to content

Commit bfae743

Browse files
committed
feat: support function for filetypes
1 parent cfedeb7 commit bfae743

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ require("copilot").setup {
153153
filetypes = {
154154
markdown = true, -- overrides default
155155
terraform = false, -- disallow specific filetype
156+
sh = function ()
157+
if string.match(vim.fs.basename(vim.api.nvim_buf_get_name(0)), '^%.env.*') then
158+
-- disable for .env files
159+
return false
160+
end
161+
return true
162+
end,
156163
},
157164
}
158165
```

lua/copilot/util.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,34 @@ local internal_filetypes = {
7272
["."] = false,
7373
}
7474

75+
---@param filetype_enabled boolean|fun():boolean
76+
local function resolve_filetype_enabled(filetype_enabled)
77+
if type(filetype_enabled) == "function" then
78+
return filetype_enabled()
79+
end
80+
return filetype_enabled
81+
end
82+
7583
---@param ft string
7684
---@param filetypes table<string, boolean>
7785
---@return boolean ft_disabled
7886
---@return string? ft_disabled_reason
7987
local function is_ft_disabled(ft, filetypes)
8088
if filetypes[ft] ~= nil then
81-
return not filetypes[ft], string.format("'filetype' %s rejected by config filetypes[%s]", ft, ft)
89+
return not resolve_filetype_enabled(filetypes[ft]),
90+
string.format("'filetype' %s rejected by config filetypes[%s]", ft, ft)
8291
end
8392

8493
local short_ft = string.gsub(ft, "%..*", "")
8594

8695
if filetypes[short_ft] ~= nil then
87-
return not filetypes[short_ft], string.format("'filetype' %s rejected by config filetypes[%s]", ft, short_ft)
96+
return not resolve_filetype_enabled(filetypes[short_ft]),
97+
string.format("'filetype' %s rejected by config filetypes[%s]", ft, short_ft)
8898
end
8999

90100
if filetypes["*"] ~= nil then
91-
return not filetypes["*"], string.format("'filetype' %s rejected by config filetypes[%s]", ft, "*")
101+
return not resolve_filetype_enabled(filetypes["*"]),
102+
string.format("'filetype' %s rejected by config filetypes[%s]", ft, "*")
92103
end
93104

94105
if internal_filetypes[short_ft] ~= nil then

0 commit comments

Comments
 (0)