aboutsummaryrefslogtreecommitdiffstats
path: root/lsp/copilot.lua
diff options
context:
space:
mode:
authorYi Ming <ofseed@foxmail.com>2025-08-25 11:04:48 +0800
committerGitHub <noreply@github.com>2025-08-24 20:04:48 -0700
commit6f184fbf6898e7034f784d959854fc9e76e637fe (patch)
tree66cc7f1bd7a8ad42cc15c8ee607de6dcb1f59518 /lsp/copilot.lua
parentfeat(vue_ls): preliminary typescript-tools support #4030 (diff)
downloadnvim-lspconfig-6f184fbf6898e7034f784d959854fc9e76e637fe.tar
nvim-lspconfig-6f184fbf6898e7034f784d959854fc9e76e637fe.tar.gz
nvim-lspconfig-6f184fbf6898e7034f784d959854fc9e76e637fe.tar.bz2
nvim-lspconfig-6f184fbf6898e7034f784d959854fc9e76e637fe.tar.lz
nvim-lspconfig-6f184fbf6898e7034f784d959854fc9e76e637fe.tar.xz
nvim-lspconfig-6f184fbf6898e7034f784d959854fc9e76e637fe.tar.zst
nvim-lspconfig-6f184fbf6898e7034f784d959854fc9e76e637fe.zip
feat(copilot): initial support for copilot-language-server #4029
Diffstat (limited to 'lsp/copilot.lua')
-rw-r--r--lsp/copilot.lua107
1 files changed, 107 insertions, 0 deletions
diff --git a/lsp/copilot.lua b/lsp/copilot.lua
new file mode 100644
index 00000000..690cd153
--- /dev/null
+++ b/lsp/copilot.lua
@@ -0,0 +1,107 @@
+---@brief
+---
+--- https://www.npmjs.com/package/@github/copilot-language-server
+---
+--- The Copilot Language Server enables any editor or IDE
+--- to integrate with GitHub Copilot via
+--- [the language server protocol](https://microsoft.github.io/language-server-protocol/).
+---
+--- **[GitHub Copilot](https://github.com/features/copilot)**
+--- is an AI pair programmer tool that helps you write code faster and smarter.
+---
+--- **Sign up for [GitHub Copilot Free](https://github.com/settings/copilot)!**
+---
+--- Please see [terms of use for GitHub Copilot](https://docs.github.com/en/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot)
+---
+
+---@param bufnr integer,
+---@param client vim.lsp.Client
+local function sign_in(bufnr, client)
+ client:request(
+ ---@diagnostic disable-next-line: param-type-mismatch
+ 'signIn',
+ vim.empty_dict(),
+ function(err, result)
+ if err then
+ vim.notify(err.message, vim.log.levels.ERROR)
+ return
+ end
+ if result.command then
+ local code = result.userCode
+ local command = result.command
+ vim.fn.setreg('+', code)
+ vim.fn.setreg('*', code)
+ local continue = vim.fn.confirm(
+ 'Copyied your one-time code to clipboard.\n' .. 'Open the browser to complete the sign-in process?',
+ '&Yes\n&No'
+ )
+ if continue == 1 then
+ client:exec_cmd(command, { bufnr = bufnr }, function(cmd_err, cmd_result)
+ if cmd_err then
+ vim.notify(err.message, vim.log.levels.ERROR)
+ return
+ end
+ if cmd_result.status == 'OK' then
+ vim.notify('Signed in as ' .. cmd_result.user .. '.')
+ end
+ end)
+ end
+ end
+
+ if result.status == 'PromptUserDeviceFlow' then
+ vim.notify('Enter your one-time code ' .. result.code .. ' in ' .. result.verificationUri)
+ elseif result.status == 'AlreadySignedIn' then
+ vim.notify('Already signed in as ' .. result.user .. '.')
+ end
+ end
+ )
+end
+
+---@param client vim.lsp.Client
+local function sign_out(_, client)
+ client:request(
+ ---@diagnostic disable-next-line: param-type-mismatch
+ 'signOut',
+ vim.empty_dict(),
+ function(err, result)
+ if err then
+ vim.notify(err.message, vim.log.levels.ERROR)
+ return
+ end
+ if result.status == 'NotSignedIn' then
+ vim.notify('Not signed in.')
+ end
+ end
+ )
+end
+
+return {
+ cmd = {
+ 'copilot-language-server',
+ '--stdio',
+ },
+ root_markers = { '.git' },
+ init_options = {
+ editorInfo = {
+ name = 'Neovim',
+ version = tostring(vim.version()),
+ },
+ editorPluginInfo = {
+ name = 'Neovim',
+ version = tostring(vim.version()),
+ },
+ },
+ settings = {
+ telemetry = {
+ telemetryLevel = 'all',
+ },
+ },
+ on_attach = function(client, bufnr)
+ vim.api.nvim_buf_create_user_command(bufnr, 'LspCopilotSignIn', function()
+ sign_in(bufnr, client)
+ end, { desc = 'Sign in Copilot with GitHub' })
+ vim.api.nvim_buf_create_user_command(bufnr, 'LspCopilotSignOut', function()
+ sign_out(bufnr, client)
+ end, { desc = 'Sign out Copilot with GitHub' })
+ end,
+}