3939
4040--- @param force ? boolean
4141function M .buf_attach (force )
42+ local bufnr = vim .api .nvim_get_current_buf ()
43+
4244 if lsp .initialization_failed () then
4345 logger .error (" copilot-language-server failed to initialize" )
4446 M .startup_error = " initialization of copilot-language-server failed"
@@ -60,21 +62,14 @@ function M.buf_attach(force)
6062 return
6163 end
6264
63- -- In case it has changed, we update it
64- M .config .root_dir = utils .get_root_dir (config .root_dir )
65-
6665 logger .trace (" attaching to buffer" )
67- local ok , client_id_or_err = pcall (vim .lsp .start , M .config )
68- if not ok then
69- logger .error (string.format (" failed to start LSP client: %s" , client_id_or_err ))
70- return
71- end
7266
73- if client_id_or_err then
74- store_client_id ( client_id_or_err )
75- else
76- logger .error ( " LSP client failed to start (no client ID returned) " )
67+ -- This could cause slowdowns when going into Insert mode
68+ if not vim . lsp . buf_is_attached ( bufnr , M . id ) then
69+ vim . lsp . buf_attach_client ( bufnr , M . id )
70+ logger .trace ( " explicitly attached client to buffer " )
7771 end
72+
7873 logger .trace (" buffer attached" )
7974end
8075
@@ -89,43 +84,45 @@ function M.get()
8984 return vim .lsp .get_client_by_id (M .id )
9085end
9186
87+ --- @return boolean
9288function M .is_disabled ()
9389 return is_disabled
9490end
9591
96- --- @param callback fun ( client : table ): nil
97- function M .use_client (callback )
92+ function M .ensure_client_started ()
93+ if M .id then
94+ return
95+ end
96+
9897 if is_disabled then
9998 logger .notify (" copilot is offline" )
10099 return
101100 end
102101
103- local client = M .get ()
104-
105- if not client then
106- if not M .config then
107- logger .error (" copilot.setup is not called yet" )
108- return
109- end
102+ if not M .config then
103+ M .config = client_config .create (config )
104+ end
110105
111- client_config .add_callback (callback )
106+ if not M .config then
107+ logger .error (" copilot.setup is not called yet" )
108+ return
109+ end
112110
113- if not util .should_attach () then
114- logger .debug (" not attaching to buffer based on should_attach criteria" )
115- return
116- end
111+ M .config .root_dir = utils .get_root_dir (config .root_dir )
112+ local client_id , err = vim .lsp .start (M .config )
117113
118- local client_id , err = vim .lsp .start (M .config )
114+ if not client_id then
115+ logger .error (string.format (" error starting LSP client: %s" , err ))
116+ return
117+ end
119118
120- if not client_id then
121- logger .error (string.format (" error starting LSP client: %s" , err ))
122- return
123- end
119+ store_client_id (client_id )
120+ end
124121
125- store_client_id ( client_id )
126- elseif not client . initialized then
127- client_config . add_callback ( callback )
128- else
122+ --- @param callback fun ( client : table ): nil
123+ function M . use_client ( callback )
124+ local client = M . get ( )
125+ if client then
129126 callback (client )
130127 end
131128end
@@ -153,9 +150,7 @@ function M.setup()
153150 end ),
154151 })
155152
156- vim .schedule (function ()
157- M .buf_attach ()
158- end )
153+ M .ensure_client_started ()
159154end
160155
161156function M .teardown ()
0 commit comments