Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart parentheses in ipython-notebook #5491

Closed
aldanor opened this issue Mar 15, 2016 · 7 comments
Closed

Smart parentheses in ipython-notebook #5491

aldanor opened this issue Mar 15, 2016 · 7 comments
Labels
- Bug tracker - Python stale marked as a stale issue/pr (usually by a bot)

Comments

@aldanor
Copy link

aldanor commented Mar 15, 2016

Description

Smart parentheses are not behaving well in ipython-notebook (proper modes not enabled?).

Reproduction guide

  • Start Emacs
  • Open an IPython notebook, connect to it via SPC a i n
  • Type print() in a new cell, observe the error; you may or may not be able to close the parenthesis
  • Alternatively: type
foo()
bar()

Then, delete the closing parenthesis of foo() and type it again. You then end up with something like this:

foo()
bar)()
  • Alternatively: type foo('bar'). You will end up with
foo('bar)')

Observed behaviour:
smartparens-mode and smartparens-global-mode are enabled in Python buffers but not enabled in ipython-notebook layer. This leads to some weird behaviour when trying to close parentheses, sometimes extra parens are inserted or it won't let you close them at all. In any case, the error message is: "Search failed. This means there is an unmatched expression somewhere or we are at the beginning/end of file.", which seems to be an :unmatched-expression error thrown in sp-get-paired-expression.

Enabling smartparens-mode and smartparens-global-mode seems to fix it, but I'm not sure how it should be fixed properly.

System Info

  • OS: darwin
  • Emacs: 24.5.1
  • Spacemacs: 0.105.14
  • Spacemacs branch: origin/develop (rev. 82fdd9a)
  • Graphic display: t
  • Distribution: spacemacs
  • Editing style: vim
  • Completion: helm
  • Layers:
((auto-completion :variables auto-completion-enable-sort-by-usage t)
 (c-c   :variables c-c  -default-mode-for-headers 'c  -mode c-c  -enable-clang-support t)
 csv deft emacs-lisp eyebrowse
 (git :variables git-magit-status-fullscreen t magit-save-repository-buffers 'dontask magit-revision-show-gravatars nil)
 (github :variables gh-profile-default-profile "aldanor")
 html javascript ipython-notebook markdown org
 (python :variables python-fill-column 99 python-test-runner 'pytest)
 rust search-engine
 (shell :variables shell-default-shell 'eshell shell-enable-smart-eshell t)
 shell-scripts spacemacs-helm syntax-checking themes-megapack version-control yaml aldanor dash osx)
@aldanor
Copy link
Author

aldanor commented Mar 15, 2016

Looks like adding

(add-hook 'ein:notebook-multilang-mode-hook 'smartparens-mode)

to post-init-ein should be sufficient to address the first problem.

The second problem (closing parenthesis on one line erroneously duplicates it and closes it on the next line) seems to also be present in python-mode which is quite frustrating.

@TheBB
Copy link
Contributor

TheBB commented Mar 15, 2016

@StreakyCobra Does this have something to do with your trick?

@StreakyCobra
Copy link
Contributor

Unless dotspacemacs-smart-closing-parenthesis is set to t (what shouldn't be the case by default), it shouldn't have any effect on Spacemacs, buuut…

I can indeed reproduce the Python bug when it's activated, it's related to the use of sp-navigate-close-if-unbalanced t in the function, turning it off makes this bug disappear. So I would say it's a bug between smartparens and python as I don't notice this behaviour in other modes, like in sh-mode for instance.

@aldanor I suppose you also have dotspacemacs-smart-closing-parenthesis set to t, it's what is causing your bug, try turning it off.


Keeping sp-navigate-close-if-unbalanced set to t is useful for this kind of case:

Starting with the following text (not in the last line of the file, this is a limitation of smartparens, no idea why…):

([auie

And pressing two times a closing parenthesis ())):

  • When set to t:

    ([auie])
    
  • When set to nil:

    ([auie))
    

@aldanor
Copy link
Author

aldanor commented Mar 15, 2016

@StreakyCobra Indeed turning dotspacemacs-smart-closing-parenthesis off fixes the second problem. I was actually using it for another feature: in the following example

(auie

navigate the cursor between au and ie. and press ):

  • If smart closing parenthesis is enabled: you get (auie) and the cursor jumps to the end.
  • Otherwise, you get (au)ie.
    Kind of sad to have to disable it in Python. Btw I couldn't find any matching issues in smartparens repo upstream.

@StreakyCobra
Copy link
Contributor

Yes I added this feature recently because it was really useful for dealing with closing parenthesis (bracket, quote, etc.) in a lot of ways. The implementation is more like a hack on top of smartparens, but it's an elegant hack (from my point of view at least, but I'm probably biased 😉)

I am able to reproduce this with a minimum configuration:

  • Start Spacemacs

  • Press SPC q D, enter smartparens and validate

  • This restart a stock emacs with just smartparens loaded

  • C-x C-f test.py to open a python file

  • Enable smartparens with M-x smartparens-mode

  • Turn on sp-navigate-close-if-unbalanced with M-: (setq sp-navigate-close-if-unbalanced t)

  • Enter the following content:

    foo(
    bar()
    
  • Go at the end of the first line

  • Call sp-up-sexp with M-x sp-up-sexp

  • You can observe the wrong behaviour giving this:

    foo(
    bar)()
    

So it seems to be a smartparens bug (if it is a bug at all, maybe it's the expected behaviour?).

I don't have a lot of time these days, so I encourage you to report it upstream to smartparens 😃 You can give them the reproduction steps and eventually point them to this link if you do it.

@aldanor
Copy link
Author

aldanor commented Mar 15, 2016

@StreakyCobra Yea, looks like it happens in all prog modes, not just Python (e.g. fire a scratch buffer with prog-mode and the error is the same).

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!

@github-actions github-actions bot added the stale marked as a stale issue/pr (usually by a bot) label Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- Bug tracker - Python stale marked as a stale issue/pr (usually by a bot)
Projects
None yet
Development

No branches or pull requests

3 participants