millejoh / emacs-ipython-notebook

Jupyter notebook client in Emacs
http://millejoh.github.io/emacs-ipython-notebook/
GNU General Public License v3.0
1.47k stars 122 forks source link

using company-mode over auto-complete #157

Closed gozes closed 5 years ago

gozes commented 7 years ago

I prefer and use company-mode instead of auto-complete for all my programming. So, does this mode support company-mode and if note is there any plan to add support for it?

rocha commented 7 years ago

@gozes it currently does not support it, and I don't know if there are plans to do so. However, I got it working using company-anaconda. The dependency can be removed by writing the appropriate prefix function.

  (add-hook 'ein:notebook-mode-hook #'anaconda-mode)

  (defun user-ein-reply-callback (args content -metadata-not-used-)
    (let ((callback (plist-get args :callback))
          (candidates (plist-get content :matches)))
      (funcall callback candidates)))

  (defun user-company-ein-callback (callback)
    (ein:kernel-complete
     (ein:get-kernel)
     (thing-at-point 'line)
     (current-column)
     (list :complete_reply
           (cons #'user-ein-reply-callback (list :callback callback))))
    )

  (defun user-company-ein-backend (command &optional arg &rest ignored)
    (interactive (list 'interactive))
    (case command
      (interactive (company-begin-backend 'user-company-ein-backend))
      (prefix (company-anaconda-prefix))
      (candidates (cons :async #'user-company-ein-callback))
      (location nil)
      (sorted t)
      )
    )
martibosch commented 7 years ago

would it be possible to make a similar hack work with elpy rather than anaconda-mode?

tutysara commented 7 years ago

I executed the above code in spacemacs from scratch buffer and enabled company-mode. I still didn't got autocompletion. Does this work in spacemacs?

rocha commented 7 years ago

@tutysara I don't know if it works on spacemacs since I don't use it personally. Note that the code above creates a company-mode backend, but does not activate it. To do so, add something like this:

  (add-to-list 'company-backends #'user-company-ein-backend)
tutysara commented 7 years ago

@rocha thank you, let me check by adding this

tutysara commented 7 years ago

sorry it did't worked in spacemacs. Not sure what is wrong, I didn't see any errors or messages.

millejoh commented 7 years ago

EIN's completion backend(s?) are well overdue for an overhaul and unfortuantely ein has no support for company-mode at the moment. It's on my todo list, though!

millejoh commented 7 years ago

Okay - I added a simple backend for company-mode (commit 06269b72bb0884893760ce54a84ba94e69285419). See the function ein:company-backend in the new file ein-company.el. You will need to manually add it to to company-backends in your init file for it to work.

tutysara commented 7 years ago

@millejoh thanks, I got it working in spacemacs. Here is the config --

(spacemacs|add-company-backends
      :backends ein:company-backend
      :modes ein:notebook-mode)

I see the dropdown with the list of completions, Tab and Shift+Tab cycles through the completion and expands selection. Return or click doesn't insert the selected value.

Still trying to figure out the key that inserts the selected value.

Meanwhile I tried the function provided by @rocha, and Return works as expected using

(spacemacs|add-company-backends
      :backends user-company-ein-backend 
      :modes ein:notebook-mode)
millejoh commented 7 years ago

On inserting selected values: Excellent question. I have no idea. I was hoping the issue was just with my weird setup and no one else would have the same problem. Hoped wrong I suppose. I'll go back and try to figure this out - there has to be a keybinding somewhere that is getting overwritten.

millejoh commented 7 years ago

Okay, should be working now. My code was causing company to try to delete a region that included some read-only text (essentially YAOBOE - yet another off by one error). I do really like @rocha 's code, but I didn't want to introduce a dependency on anaconda-mode so am sticking with my slightly clunkier version for the moment.

tutysara commented 7 years ago

Checked it, type from sk it gives a completion sklearn, hit return, it inserts sklearn by replacing from. We get sklearn instead of from sklearn

millejoh commented 7 years ago

I saw this too late last night and pushed a commit that should fix. Can you try updating EIN again? MELPA should be up to date by now.

tutysara commented 7 years ago

works well with the latest version, ty @millejoh. Any plans to add support for documentation and jump to source functionality in company mode?

tutysara commented 7 years ago

I updated to latest version of ein, suddenly I see jump to documentation and source working 👍 Thanks for the adding them

While working on a worksheet, I found another issue with completion. If we have

# some comment
orders.tail()

and if I wanted to change orders to orders_new, I go the line and append _, I see orders_new in the list of completions, if I complete it using return I get

# some commorders_new.tail()

it is deleting some chars from previous line

chrischen3121 commented 6 years ago

This config should be working in spacemacs

(ipython-notebook :variables
                     ein:use-auto-complete t
                     ein:complete-on-dot t
                     ein:completion-backend 'ein:use-company-backend)
c02y commented 4 years ago

@millejoh thanks, I got it working in spacemacs. Here is the config --

(spacemacs|add-company-backends
      :backends ein:company-backend
      :modes ein:notebook-mode)

I see the dropdown with the list of completions, Tab and Shift+Tab cycles through the completion and expands selection. Return or click doesn't insert the selected value.

Still trying to figure out the key that inserts the selected value.

Meanwhile I tried the function provided by @rocha, and Return works as expected using

(spacemacs|add-company-backends
      :backends user-company-ein-backend 
      :modes ein:notebook-mode)

@tutysara ein code changed a lot, the code snippets do not work anymore. This is what I use for spacmacs, I spent a lot time trying to config the auto-comapletion for ein, I cannot believe this is so simple, this should be put into README of ein.

(spacemacs|add-company-backends
      :backends company-anaconda
      :modes ein:notebook-mode)
JonatanSahar commented 4 years ago

I've spent hours on this, but I still can't get this to work even with @c02y 's snippet :/ I get local file word completion but nothing pythonic... I'd appreciate any help you can offer :) thanks!

dickmao commented 4 years ago

I've only ever tested EIN with elpy. I might have tried lsp-mode once, and it failed terribly.

This recent comment https://github.com/syl20bnr/spacemacs/issues/13464#issuecomment-614839563 suggests elpy is persona non grata with spacecadet.

So add elpy to dotspacemacs-additional-packages in .spacemacs. Configure elpy according to the documentation in dotspacemacs/user-config. This is what I use.

(use-package elpy
  ;; emacs.stackexchange.com/questions/10065
  :commands elpy-enable
  :init (with-eval-after-load 'python (elpy-enable))
  :config
  (setq elpy-rpc-backend "jedi"
        elpy-rpc-project-specific 't)
  (delete 'elpy-module-highlight-indentation elpy-modules)
  (delete 'elpy-module-flymake elpy-modules))

Then restart spacemacs. Peek 2020-04-27 12-04

c02y commented 4 years ago

@jonathan-sahar-technion

I tried again, it works well for example

import numpy as np
np.

it will display the drop list of auto-completion.

if it doesn't work for you, inside python cell, check if company-mode is enabled, and check if company-backends contains company-anaconda

JonatanSahar commented 4 years ago

Hey, thanks so much for your reply. I couldn't get elpy to work - it's own virtualenv didn't contain an installation of pip, and even after manually installing it there, elpy still couldn't recognize that it's there... It's been really frustrating trying to get spacemacs/emacs to work. I've grown to really love it - coming from vim - for its capabilities in manipulating and navigating text, but it's just been so hard to get it do behave like an IDE for python that I'm actually about to give up. I'm trying to install it on the new-ish linux-subsystem-for-windows instead of natively on windows - maybe this way will work better.

thanks again!

On Mon, 27 Apr 2020 at 19:06, dickmao notifications@github.com wrote:

I've only ever tested EIN with elpy. I might have tried lsp-mode once, and it failed terribly.

This recent comment syl20bnr/spacemacs#13464 (comment) https://github.com/syl20bnr/spacemacs/issues/13464#issuecomment-614839563 suggests elpy is persona non grata with spacecadet.

So add elpy to dotspacemacs-additional-packages in .spacemacs. Configure elpy according to the documentation in dotspacemacs/user-config. This is what I use.

(use-package elpy ;; emacs.stackexchange.com/questions/10065 :commands elpy-enable :init (with-eval-after-load 'python (elpy-enable)) :config (setq elpy-rpc-backend "jedi" elpy-rpc-project-specific 't) (delete 'elpy-module-highlight-indentation elpy-modules) (delete 'elpy-module-flymake elpy-modules))

Then restart spacemacs. [image: Peek 2020-04-27 12-04] https://user-images.githubusercontent.com/7578770/80393995-3d5dc100-887f-11ea-9d3e-4d047fc2bb27.gif

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/millejoh/emacs-ipython-notebook/issues/157#issuecomment-620080921, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEL3S3FKCH2H2JEL2XKMC3TROWUQTANCNFSM4C2POF6Q .