Skip to content

Commit 397a38d

Browse files
committed
refactor: extract filetype-related utils
1 parent e69323a commit 397a38d

File tree

4 files changed

+144
-145
lines changed

4 files changed

+144
-145
lines changed

lua/copilot/client/filetypes.lua

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
local M = {}
2+
3+
local language_normalization_map = {
4+
bash = "shellscript",
5+
bst = "bibtex",
6+
cs = "csharp",
7+
cuda = "cuda-cpp",
8+
dosbatch = "bat",
9+
dosini = "ini",
10+
gitcommit = "git-commit",
11+
gitrebase = "git-rebase",
12+
make = "makefile",
13+
objc = "objective-c",
14+
objcpp = "objective-cpp",
15+
ps1 = "powershell",
16+
raku = "perl6",
17+
sh = "shellscript",
18+
text = "plaintext",
19+
}
20+
21+
function M.language_for_file_type(filetype)
22+
-- trim filetypes after dot, e.g. `yaml.gotexttmpl` -> `yaml`
23+
local ft = string.gsub(filetype, "%..*", "")
24+
if not ft or ft == "" then
25+
ft = "text"
26+
end
27+
return language_normalization_map[ft] or ft
28+
end
29+
30+
local internal_filetypes = {
31+
yaml = false,
32+
markdown = false,
33+
help = false,
34+
gitcommit = false,
35+
gitrebase = false,
36+
hgcommit = false,
37+
svn = false,
38+
cvs = false,
39+
["."] = false,
40+
}
41+
42+
---@param filetype_enabled boolean|fun():boolean
43+
local function resolve_filetype_enabled(filetype_enabled)
44+
if type(filetype_enabled) == "function" then
45+
return filetype_enabled()
46+
end
47+
return filetype_enabled
48+
end
49+
50+
---@param ft string
51+
---@param filetypes table<string, boolean>
52+
---@return boolean ft_disabled
53+
---@return string? ft_disabled_reason
54+
function M.is_ft_disabled(ft, filetypes)
55+
if filetypes[ft] ~= nil then
56+
return not resolve_filetype_enabled(filetypes[ft]),
57+
string.format("'filetype' %s rejected by config filetypes[%s]", ft, ft)
58+
end
59+
60+
local short_ft = string.gsub(ft, "%..*", "")
61+
62+
if filetypes[short_ft] ~= nil then
63+
return not resolve_filetype_enabled(filetypes[short_ft]),
64+
string.format("'filetype' %s rejected by config filetypes[%s]", ft, short_ft)
65+
end
66+
67+
if filetypes["*"] ~= nil then
68+
return not resolve_filetype_enabled(filetypes["*"]),
69+
string.format("'filetype' %s rejected by config filetypes[%s]", ft, "*")
70+
end
71+
72+
if internal_filetypes[short_ft] ~= nil then
73+
return not internal_filetypes[short_ft],
74+
string.format("'filetype' %s rejected by internal_filetypes[%s]", ft, short_ft)
75+
end
76+
77+
return false
78+
end
79+
80+
return M

lua/copilot/client/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,15 @@ local function prepare_client_config(overrides)
272272
name = "copilot",
273273
capabilities = capabilities,
274274
get_language_id = function(_, filetype)
275-
return util.language_for_file_type(filetype)
275+
return require("copilot.client.filetypes").language_for_file_type(filetype)
276276
end,
277277
on_init = function(client, initialize_result)
278278
if M.id == client.id then
279279
M.capabilities = initialize_result.capabilities
280280
end
281281

282282
vim.schedule(function()
283-
local configurations = util.get_workspace_configurations()
283+
local configurations = utils.get_workspace_configurations()
284284
api.notify_change_configuration(client, configurations)
285285
logger.trace("workspace configuration", configurations)
286286

lua/copilot/client/utils.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local config = require("copilot.config")
2+
local logger = require("copilot.logger")
13
local M = {}
24

35
---@param config_root_dir RootDirFuncOrString
@@ -18,4 +20,63 @@ function M.get_root_dir(config_root_dir)
1820
return root_dir
1921
end
2022

23+
---@return copilot_workspace_configurations
24+
function M.get_workspace_configurations()
25+
local filetypes = vim.deepcopy(config.filetypes) --[[@as table<string, boolean>]]
26+
27+
if filetypes["*"] == nil then
28+
filetypes = vim.tbl_deep_extend("keep", filetypes, require("copilot.client.filetypes").internal_filetypes)
29+
end
30+
31+
local copilot_model = config and config.copilot_model ~= "" and config.copilot_model or ""
32+
33+
---@type string[]
34+
local disabled_filetypes = vim.tbl_filter(function(ft)
35+
return filetypes[ft] == false
36+
end, vim.tbl_keys(filetypes))
37+
table.sort(disabled_filetypes)
38+
39+
return {
40+
settings = {
41+
github = {
42+
copilot = {
43+
selectedCompletionModel = copilot_model,
44+
},
45+
},
46+
enableAutoCompletions = not not (config.panel.enabled or config.suggestion.enabled),
47+
disabledLanguages = vim.tbl_map(function(ft)
48+
return { languageId = ft }
49+
end, disabled_filetypes),
50+
},
51+
}
52+
end
53+
54+
---@return copilot_window_show_document_result
55+
---@param result copilot_window_show_document
56+
function M.show_document(_, result)
57+
logger.trace("window/showDocument:", result)
58+
local success, _ = pcall(vim.ui.open, result.uri)
59+
if not success then
60+
if vim.ui.open ~= nil then
61+
vim.api.nvim_echo({
62+
{ "window/showDocument" },
63+
{ vim.inspect({ _, result }) },
64+
{ "\n", "NONE" },
65+
}, true, {})
66+
error("Unsupported OS: vim.ui.open exists but failed to execute.")
67+
else
68+
vim.api.nvim_echo({
69+
{ "window/showDocument" },
70+
{ vim.inspect({ _, result }) },
71+
{ "\n", "NONE" },
72+
}, true, {})
73+
error("Unsupported Version: vim.ui.open requires Neovim >= 0.10")
74+
end
75+
end
76+
77+
return {
78+
success = success,
79+
}
80+
end
81+
2182
return M

lua/copilot/util.lua

Lines changed: 1 addition & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ local logger = require("copilot.logger")
33

44
local M = {}
55

6-
local id = 0
7-
function M.get_next_id()
8-
id = id + 1
9-
return id
10-
end
11-
126
---@return { editorInfo: copilot_editor_info, editorPluginInfo: copilot_editor_plugin_info }
137
function M.get_editor_info()
148
local info = {
@@ -37,61 +31,11 @@ function M.get_copilot_lua_version()
3731
return copilot_lua_version
3832
end
3933

40-
local internal_filetypes = {
41-
yaml = false,
42-
markdown = false,
43-
help = false,
44-
gitcommit = false,
45-
gitrebase = false,
46-
hgcommit = false,
47-
svn = false,
48-
cvs = false,
49-
["."] = false,
50-
}
51-
52-
---@param filetype_enabled boolean|fun():boolean
53-
local function resolve_filetype_enabled(filetype_enabled)
54-
if type(filetype_enabled) == "function" then
55-
return filetype_enabled()
56-
end
57-
return filetype_enabled
58-
end
59-
60-
---@param ft string
61-
---@param filetypes table<string, boolean>
62-
---@return boolean ft_disabled
63-
---@return string? ft_disabled_reason
64-
local function is_ft_disabled(ft, filetypes)
65-
if filetypes[ft] ~= nil then
66-
return not resolve_filetype_enabled(filetypes[ft]),
67-
string.format("'filetype' %s rejected by config filetypes[%s]", ft, ft)
68-
end
69-
70-
local short_ft = string.gsub(ft, "%..*", "")
71-
72-
if filetypes[short_ft] ~= nil then
73-
return not resolve_filetype_enabled(filetypes[short_ft]),
74-
string.format("'filetype' %s rejected by config filetypes[%s]", ft, short_ft)
75-
end
76-
77-
if filetypes["*"] ~= nil then
78-
return not resolve_filetype_enabled(filetypes["*"]),
79-
string.format("'filetype' %s rejected by config filetypes[%s]", ft, "*")
80-
end
81-
82-
if internal_filetypes[short_ft] ~= nil then
83-
return not internal_filetypes[short_ft],
84-
string.format("'filetype' %s rejected by internal_filetypes[%s]", ft, short_ft)
85-
end
86-
87-
return false
88-
end
89-
9034
---@return boolean should_attach
9135
---@return string? no_attach_reason
9236
function M.should_attach()
9337
local ft = config.filetypes
94-
local ft_disabled, ft_disabled_reason = is_ft_disabled(vim.bo.filetype, ft)
38+
local ft_disabled, ft_disabled_reason = require("copilot.client.filetypes").is_ft_disabled(vim.bo.filetype, ft)
9539

9640
if ft_disabled then
9741
return not ft_disabled, ft_disabled_reason
@@ -100,33 +44,6 @@ function M.should_attach()
10044
return true
10145
end
10246

103-
local language_normalization_map = {
104-
bash = "shellscript",
105-
bst = "bibtex",
106-
cs = "csharp",
107-
cuda = "cuda-cpp",
108-
dosbatch = "bat",
109-
dosini = "ini",
110-
gitcommit = "git-commit",
111-
gitrebase = "git-rebase",
112-
make = "makefile",
113-
objc = "objective-c",
114-
objcpp = "objective-cpp",
115-
ps1 = "powershell",
116-
raku = "perl6",
117-
sh = "shellscript",
118-
text = "plaintext",
119-
}
120-
121-
function M.language_for_file_type(filetype)
122-
-- trim filetypes after dot, e.g. `yaml.gotexttmpl` -> `yaml`
123-
local ft = string.gsub(filetype, "%..*", "")
124-
if not ft or ft == "" then
125-
ft = "text"
126-
end
127-
return language_normalization_map[ft] or ft
128-
end
129-
13047
local function relative_path(absolute)
13148
local relative = vim.fn.fnamemodify(absolute, ":.")
13249
if string.sub(relative, 0, 1) == "/" then
@@ -167,37 +84,6 @@ function M.get_doc_params(overrides)
16784
return params
16885
end
16986

170-
---@return copilot_workspace_configurations
171-
function M.get_workspace_configurations()
172-
local filetypes = vim.deepcopy(config.filetypes) --[[@as table<string, boolean>]]
173-
174-
if filetypes["*"] == nil then
175-
filetypes = vim.tbl_deep_extend("keep", filetypes, internal_filetypes)
176-
end
177-
178-
local copilot_model = config and config.copilot_model ~= "" and config.copilot_model or ""
179-
180-
---@type string[]
181-
local disabled_filetypes = vim.tbl_filter(function(ft)
182-
return filetypes[ft] == false
183-
end, vim.tbl_keys(filetypes))
184-
table.sort(disabled_filetypes)
185-
186-
return {
187-
settings = {
188-
github = {
189-
copilot = {
190-
selectedCompletionModel = copilot_model,
191-
},
192-
},
193-
enableAutoCompletions = not not (config.panel.enabled or config.suggestion.enabled),
194-
disabledLanguages = vim.tbl_map(function(ft)
195-
return { languageId = ft }
196-
end, disabled_filetypes),
197-
},
198-
}
199-
end
200-
20187
M.get_plugin_path = function()
20288
local copilot_path = vim.api.nvim_get_runtime_file("lua/copilot/init.lua", false)[1]
20389
if vim.fn.filereadable(copilot_path) ~= 0 then
@@ -217,32 +103,4 @@ function M.strutf16len(str)
217103
end
218104
end
219105

220-
---@return copilot_window_show_document_result
221-
---@param result copilot_window_show_document
222-
function M.show_document(_, result)
223-
logger.trace("window/showDocument:", result)
224-
local success, _ = pcall(vim.ui.open, result.uri)
225-
if not success then
226-
if vim.ui.open ~= nil then
227-
vim.api.nvim_echo({
228-
{ "window/showDocument" },
229-
{ vim.inspect({ _, result }) },
230-
{ "\n", "NONE" },
231-
}, true, {})
232-
error("Unsupported OS: vim.ui.open exists but failed to execute.")
233-
else
234-
vim.api.nvim_echo({
235-
{ "window/showDocument" },
236-
{ vim.inspect({ _, result }) },
237-
{ "\n", "NONE" },
238-
}, true, {})
239-
error("Unsupported Version: vim.ui.open requires Neovim >= 0.10")
240-
end
241-
end
242-
243-
return {
244-
success = success,
245-
}
246-
end
247-
248106
return M

0 commit comments

Comments
 (0)