Skip to content

Commit b0d3440

Browse files
author
Bassam Data
committed
feat: Prevent Copilot LSP from initializing in temporary or blacklisted buffers
1 parent 89cdac8 commit b0d3440

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

lsp/copilot_ls.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ return {
3131
root_dir = vim.uv.cwd(),
3232
on_init = function(client)
3333
local au = vim.api.nvim_create_augroup("copilotlsp.init", { clear = true })
34+
local bufnr = vim.api.nvim_get_current_buf()
35+
if not require("copilot-lsp.util").should_attach_to_buffer(bufnr) then
36+
return
37+
end
3438
--NOTE: Inline Completions
3539
--TODO: We dont currently use this code path, so comment for now until a UI is built
3640
-- vim.api.nvim_create_autocmd("TextChangedI", {

lua/copilot-lsp/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ M.defaults = {
1616
clear_on_large_distance = true,
1717
count_horizontal_moves = true,
1818
reset_on_approaching = true,
19+
buffer_blacklist = {},
1920
},
2021
}
2122

lua/copilot-lsp/nes/init.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ end
2828
--- Requests the NextEditSuggestion from the current cursor position
2929
---@param copilot_lss? vim.lsp.Client|string
3030
function M.request_nes(copilot_lss)
31+
local bufnr = vim.api.nvim_get_current_buf()
32+
if not utils.should_attach_to_buffer(bufnr) then
33+
return
34+
end
3135
local pos_params = vim.lsp.util.make_position_params(0, "utf-16")
32-
local version = vim.lsp.util.buf_versions[vim.api.nvim_get_current_buf()]
36+
local version = vim.lsp.util.buf_versions[bufnr]
3337
if type(copilot_lss) == "string" then
3438
copilot_lss = vim.lsp.get_clients({ name = copilot_lss })[1]
3539
end

lua/copilot-lsp/util.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
local M = {}
2+
local config = require("copilot-lsp.config").config
3+
local api = vim.api
4+
25
---@param edit copilotlsp.InlineEdit
36
function M.apply_inline_edit(edit)
47
local bufnr = vim.uri_to_bufnr(edit.textDocument.uri)
@@ -7,6 +10,27 @@ function M.apply_inline_edit(edit)
710
vim.lsp.util.apply_text_edits({ edit }, bufnr, "utf-16")
811
end
912

13+
--- Checks if Copilot should attach to a buffer
14+
---@param bufnr integer
15+
---@return boolean
16+
function M.should_attach_to_buffer(bufnr)
17+
if
18+
not vim.fn.getbufvar(bufnr, "&buflisted") == 1
19+
or vim.bo[bufnr].buftype ~= ""
20+
or not api.nvim_buf_is_valid(bufnr)
21+
or not api.nvim_buf_is_loaded(bufnr)
22+
then
23+
return false
24+
end
25+
local name = api.nvim_buf_get_name(bufnr)
26+
for _, b_name in ipairs(config.nes.buffer_blacklist or {}) do
27+
if name:match(b_name) then
28+
return false
29+
end
30+
end
31+
return true
32+
end
33+
1034
---Debounces calls to a function, and ensures it only runs once per delay
1135
---even if called repeatedly.
1236
---@param fn fun(...: any)

0 commit comments

Comments
 (0)