copilot.el icon indicating copy to clipboard operation
copilot.el copied to clipboard

Better instructions for spacemacs

Open riatzukiza opened this issue 3 years ago • 3 comments
trafficstars

I had difficulty getting this to work in spacemacs with the instructions that were provided. I did ultimately get it working, so I wanted to share my config. I ultimately had to add a bunch of stuff to dotspacemacs/user-config:

(defun dotspacemacs/user-config ()
  "Configuration for user code:
This function is called at the very end of Spacemacs startup, after layer
configuration.
Put your configuration code here, except for variables that should be set
before packages are loaded."
  (defvar bootstrap-version)
  (let ((bootstrap-file
         (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
        (bootstrap-version 5))
    (unless (file-exists-p bootstrap-file)
      (with-current-buffer
          (url-retrieve-synchronously
           "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
           'silent 'inhibit-cookies)
        (goto-char (point-max))
        (eval-print-last-sexp)))
    (load bootstrap-file nil 'nomessage))
  (use-package copilot
    :straight (:host github :repo "zerolfx/copilot.el" :files ("dist" "*.el"))
    :ensure t)
  (setq copilot-node-executable "/home/<username>/.nvm/versions/node/v16.16.0/bin/node")
  (with-eval-after-load 'company
    ;; disable inline previews
    (delq 'company-preview-if-just-one-frontend company-frontends))
  (define-key copilot-completion-map (kbd "<tab>") 'copilot-accept-completion)
  ;; (define-key copilot-completion-map (kbd "TAB") 'copilot-accept-completion)


  (add-hook 'prog-mode-hook 'copilot-mode)

  (define-key evil-insert-state-map (kbd "C-<tab>") 'copilot-accept-completion-by-word)
  (define-key evil-insert-state-map (kbd "C-TAB") 'copilot-accept-completion-by-word)  )

This looks a little hack to me, but it works. If there's a better way to handle that, I'd like to know and help update the instructions for others.

riatzukiza avatar Aug 22 '22 14:08 riatzukiza

Thanks for sharing your configuration. Good to know you can use straight.el in spacemacs. Spacemacs allows adding packages directly from GitHub by changing the dotspacemacs-additional-packages variable. Doesn't it work?

zerolfx avatar Aug 23 '22 05:08 zerolfx

I just tried setting it up and it (almost) works. I had to wrap the define-key calls in (with-eval-after-load 'copilot ...) as it would not find the completion map otherwise. I do have a few problems: the autocompletion doesn't trigger automatically for copilot - I have to invoke copilot-complete, and copilot-mode isn't automatic in programming modes (I use LSP, maybe that complicates things). Not sure how to fix these, so any help is appreciated. But otherwise things do work, I get completions and can accept them.

milanmitrovic avatar Oct 23 '22 15:10 milanmitrovic

#60 may help you

zerolfx avatar Oct 23 '22 17:10 zerolfx

I'm getting the following w/ @milanmitrovic 's suggestion:

Cloning https://github.com/melpa/melpa.git to /Users/jleonard/.emacs.d/.cache/quelpa/melpa
Fetcher: github
Source: zerolfx/copilot.el

Cloning https://github.com/zerolfx/copilot.el.git to /Users/jleonard/.emacs.d/.cache/quelpa/build/copilot/
/Users/jleonard/.emacs.d/.cache/quelpa/build/copilot/copilot.el -> /var/folders/qk/8w2j623n6hj3yhm2xb959gd8y93h_4/T/copilotig9WPE/copilot-20221114.1757/copilot.el
/Users/jleonard/.emacs.d/.cache/quelpa/build/copilot/dist => /var/folders/qk/8w2j623n6hj3yhm2xb959gd8y93h_4/T/copilotig9WPE/copilot-20221114.1757/dist
Parsing tar file...done
Error getting PACKAGE-DESC: (wrong-type-argument arrayp nil)

Is there any better (up to date) guidance on getting this working w/ spacemacs?

jleonard-r7 avatar Nov 25 '22 00:11 jleonard-r7

First, many thanks to @zerolfx for making this! I'm officially a copilot addict and will build side projects around new AI tools as a result. life-changing.

Second, here's my Spacemacs config, in the hope it helps someone.

   dotspacemacs-additional-packages '(org-super-agenda git-auto-commit-mode prettier-js
                                                       (copilot :location (recipe
                                                                           :fetcher github
                                                                           :repo "zerolfx/copilot.el"
                                                                           :files ("*.el" "dist")))
                                                       )

And top of user-config:

  (with-eval-after-load 'company
    ;; disable inline previews
    (delq 'company-preview-if-just-one-frontend company-frontends))

  ;; The with-eval-after-load prevents initial loading errors. Without this I had to comment out the two define-keys, load, uncomment, reload.

  (with-eval-after-load 'copilot
    (define-key copilot-completion-map (kbd "<tab>") 'copilot-accept-completion)
    (define-key copilot-completion-map (kbd "TAB") 'copilot-accept-completion)
    )
     (define-key evil-insert-state-map (kbd "<backtab>") 'copilot-next-completion)

  (add-hook 'prog-mode-hook 'copilot-mode)
  (define-key evil-insert-state-map (kbd "C-<tab>") 'copilot-accept-completion-by-word)
  (define-key evil-insert-state-map (kbd "C-TAB") 'copilot-accept-completion-by-word)

My Spacemacs is up to date, GUI OSX, and this all works well for me. The only issue I have is that completion sometimes fails, unless I go to a new line and then back to where I was working.

coyotespike avatar Nov 26 '22 15:11 coyotespike

I finally got it working pretty well (on latest develop spacemacs and emacs 28.2). There seems to be a lot of clashing between copilot-mode and company-mode over ownership of the <tab> key(s). So, I went with the following meta-centric approach:

  (with-eval-after-load 'company
    ;; disable inline previews
    (delq 'company-preview-if-just-one-frontend company-frontends))

  (add-hook 'prog-mode-hook 'copilot-mode)
  (define-key evil-insert-state-map (kbd "M-<right>") 'copilot-accept-completion-by-line)
  (define-key evil-insert-state-map (kbd "M-<return>") 'copilot-accept-completion)
  (define-key evil-insert-state-map (kbd "M-<tab>") 'copilot-next-completion)

I'm a bit surprised at the README for this project suggesting to use tab since it's clearly a pretty important key to company-mode. M-<return> to accept the full completion works quite nicely since it's easy to follow it up with an additional <return> to add a blank line (which is most often what one wants [at the end of a function definition for example]). Perhaps these settings would resolve the "issue" you mentioned, @coyotespike (although note that for me, it was effectively unusable whereas for you it seems a minor annoyance).

jleonard-r7 avatar Nov 30 '22 02:11 jleonard-r7

@jleonard-r7 Using TAB to accept completion is the default behavior of VSCode, so I tried to make them consistent.

zerolfx avatar Nov 30 '22 03:11 zerolfx

Ah yea. Makes sense. My keybinding space in general is starting to get pretty crowded so it could be particularly bad in my case vs a barebones install.

jleonard-r7 avatar Nov 30 '22 03:11 jleonard-r7

Thanks for that suggestion @jleonard-r7! I'd prefer just TAB as @zerolfx had it but this fixes the annoying need to start a new line then return to the previous line:

    (define-key copilot-completion-map (kbd "M-<tab>") 'copilot-accept-completion)
    (define-key copilot-completion-map (kbd "M-TAB") 'copilot-accept-completion)

EDIT, spoke too soon, it doesn't fix it so I have returned to plain TAB.

coyotespike avatar Dec 02 '22 19:12 coyotespike

For mac users, change this path to

"/home/<username>/.nvm/versions/node/v16.16.0/bin/node" 

this. (found by which)

"/usr/local/bin/node"

mindgitrwx avatar Dec 08 '22 10:12 mindgitrwx

The path for node is very much user and system specific. I don’t think we can make any prescriptions here.

jleonard-r7 avatar Dec 08 '22 19:12 jleonard-r7

@coyotespike's suggestion worked for me! Shift-TAB for next completion is a nice addition to the instructions in the README as well.

sarangak avatar Dec 16 '22 06:12 sarangak

First of all, thanks for all the work here.

Does this only work in evil mode? I use spacemacs with emacs keybindings (so no insert mode) and I'm not seeing the autocompletion appear until I explicitly run copilot-complete in the buffer

image

james-ennis avatar Dec 22 '22 15:12 james-ennis

@james-ennis see my comments / solution posted above

jleonard-r7 avatar Dec 22 '22 17:12 jleonard-r7

@jleonard-r7 still not working for me.

To be clear I have this in dotspacemacs-configuration-layers

     (auto-completion :variables
                      auto-completion-enable-snippets-in-popup t)

This for dotspacemacs-additional-packages

   dotspacemacs-additional-packages
   '((copilot :location (recipe
                         :fetcher github
                         :repo "zerolfx/copilot.el"
                         :files ("*.el" "dist"))))

And this in user-config

  (with-eval-after-load 'company
    ;; disable inline previews
    (delq 'company-preview-if-just-one-frontend company-frontends))

  (add-hook 'prog-mode-hook 'copilot-mode)
  (define-key evil-insert-state-map (kbd "M-<right>") 'copilot-accept-completion-by-line)
  (define-key evil-insert-state-map (kbd "M-<return>") 'copilot-accept-completion)
  (define-key evil-insert-state-map (kbd "M-<tab>") 'copilot-next-completion)

Now when I open a buffer and enable company-mode no auto completion prompts are popping up.

james-ennis avatar Dec 22 '22 18:12 james-ennis

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Apr 05 '23 01:04 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Apr 10 '23 02:04 github-actions[bot]