File tree Expand file tree Collapse file tree 4 files changed +33
-23
lines changed Expand file tree Collapse file tree 4 files changed +33
-23
lines changed Original file line number Diff line number Diff line change @@ -116,7 +116,19 @@ require('copilot').setup({
116
116
root_dir = function ()
117
117
return vim .fs .dirname (vim .fs .find (" .git" , { upward = true })[1 ])
118
118
end ,
119
- should_attach = nil , -- type is fun(bufnr: integer, bufname: string): boolean
119
+ should_attach = function (_ , _ )
120
+ if not vim .bo .buflisted then
121
+ logger .debug (" not attaching, buffer is not 'buflisted'" )
122
+ return false
123
+ end
124
+
125
+ if vim .bo .buftype ~= " " then
126
+ logger .debug (" not attaching, buffer 'buftype' is " .. vim .bo .buftype )
127
+ return false
128
+ end
129
+
130
+ return true
131
+ end ,
120
132
server_opts_overrides = {},
121
133
})
122
134
```
@@ -324,6 +336,7 @@ If none is found, it will use the current working directory.
324
336
325
337
This function is called to determine if copilot should attach to the buffer or not.
326
338
It is useful if you would like to go beyond the filetypes and have more control over when copilot should attach.
339
+ You can also use it to attach to buflisted buffers by simply omiting that portion from the function.
327
340
Since this happens before attaching to the buffer, it is good to prevent Copilot from reading sensitive files.
328
341
329
342
An example of this would be:
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ local M = {
17
17
node_version_error = nil ,
18
18
startup_error = nil ,
19
19
initialized = false ,
20
- --- @type copilot_should_attach | nil
20
+ --- @type copilot_should_attach
21
21
should_attach = nil ,
22
22
}
23
23
95
95
96
96
--- @param force ? boolean
97
97
function M .buf_attach (force )
98
- if M .should_attach then
99
- local bufnr = vim .api .nvim_get_current_buf ()
100
- local bufname = vim .api .nvim_buf_get_name (bufnr )
101
-
102
- if not M .should_attach (bufnr , bufname ) then
103
- logger .debug (" copilot is disabled by should_attach" )
104
- return
105
- end
106
- end
107
-
108
98
if is_disabled then
109
99
logger .warn (" copilot is disabled" )
110
100
return
111
101
end
112
102
113
- if not force and not util .should_attach () then
103
+ local bufnr = vim .api .nvim_get_current_buf ()
104
+ local bufname = vim .api .nvim_buf_get_name (bufnr )
105
+
106
+ if not force and not M .should_attach (bufnr , bufname ) and not util .should_attach () then
114
107
return
115
108
end
116
109
Original file line number Diff line number Diff line change @@ -62,8 +62,20 @@ local default_config = {
62
62
return vim .fs .dirname (vim .fs .find (" .git" , { upward = true })[1 ])
63
63
end ,
64
64
--- @alias copilot_should_attach fun ( bufnr : integer , bufname : string ): boolean
65
- --- @type copilot_should_attach | nil
66
- should_attach = nil ,
65
+ --- @type copilot_should_attach
66
+ should_attach = function (_ , _ )
67
+ if not vim .bo .buflisted then
68
+ logger .debug (" not attaching, bugger is not 'buflisted'" )
69
+ return false
70
+ end
71
+
72
+ if vim .bo .buftype ~= " " then
73
+ logger .debug (" not attaching, buffer 'buftype' is " .. vim .bo .buftype )
74
+ return false
75
+ end
76
+
77
+ return true
78
+ end ,
67
79
}
68
80
69
81
local mod = {
Original file line number Diff line number Diff line change @@ -104,14 +104,6 @@ function M.should_attach()
104
104
return not ft_disabled , ft_disabled_reason
105
105
end
106
106
107
- if not vim .bo .buflisted then
108
- return false , " buffer not 'buflisted'"
109
- end
110
-
111
- if vim .bo .buftype ~= " " then
112
- return false , " buffer 'buftype' is " .. vim .bo .buftype
113
- end
114
-
115
107
return true
116
108
end
117
109
You can’t perform that action at this time.
0 commit comments