Fuco1 / smartparens

Minor mode for Emacs that deals with parens pairs and tries to be smart about it.
GNU General Public License v3.0
1.83k stars 194 forks source link

Autopairing broken in master branch in java-mode/c++mode #985

Open yyoncho opened 5 years ago

yyoncho commented 5 years ago

I think that recent change in lisp/progmodes/cc-mode.el in emacs master branch broke smartparens because smartparens works fine in js-mode/rust-mode but breaks in java-mode/c++-mode. At the same time, it works fine in emacs26. I could debug it on my side if you give me a point what is the place in the code which is responsible to write the closing pair.

Expected behavior

( to be closed.

Actual behavior

When I type ( it is not closed.

Steps to reproduce the problem

Open java/cpp file, do M-x smartparens-mode and write (

Environment & version information

(cc @joaotavora you might be interested since you are participating in a discussion about eletric-pair-mode in emacs-devel which might be related to the issue).

joaotavora commented 5 years ago

you are participating in a discussion about eletric-pair-mode in emacs-devel which might be related to the issue

Thanks. Indeed. It might be related, but let's wait until this is analysed. In the meantime, can you try

(advice-add 'c-unescaped-nls-in-string-p
        :around
        (lambda (_ _)
          t))

And see if the problem persists?

My intuition is that it will persist. It might related to another recent cc-mode change whereby it (cc-mode) decides to bypass post-self-insert-hook (something also don't agree with, and have stated disagreement in the past). But again, let's wait this is analyzed in smartparens.

yyoncho commented 5 years ago

And see if the problem persists?

It still persists. FTR this fixes smartparens (it may be expected).

(define-key java-mode-map "(" nil)

It might related to another recent cc-mode change whereby it (cc-mode) decides to bypass post-self-insert-hook (something also don't agree with, and have stated disagreement in the past). But again, let's wait this is analyzed in smartparens.

I noticed that too - it breaks lsp-mode's onTypeFormatting.

yyoncho commented 5 years ago

Actually, seems like this is causing the issue: When I remove the let block smartparens work fine.

    (let (post-self-insert-hook) ; The only way to get defined functionality
                                        ; from `self-insert-command'.
      (self-insert-command (prefix-numeric-value arg)))

Edit: to clarify this is part of c-electric-paren

joaotavora commented 5 years ago

Edit: to clarify this is part of c-electric-paren

Sooner or later we need a mode that doesn't bind custom commands for ( and ) and so on, or at least doesn't do what c-electric-paren does. Until then, there's not much smartparens or other extensions can do, short of radically changing how they work just for cc-mode ,which is slightly non-optimal to say the least.

Fuco1 commented 5 years ago

Yep, basically smartparens works by having a hook on post-self-insert-hook. I also have a bunch of special hacks on post-command-hook where there is a list of "special insert commands" and I basically fix them on a case-by-case basis. But the recent changes in cc-mode made even this approach broken (though I haven't analyzed exactly why it stopped working).

What we can do is to patch the functions involved with advices or straight-up redefine them but that is pretty invasive solution.

As @yyoncho pointed out "unbinding" the commands works and that's what I personally do in my configuration.

yyoncho commented 5 years ago

As @yyoncho pointed out "unbinding" the commands works and that's what I personally do in my configuration.

I will do that but IMO this must be fixed in the upstream - it doesn't make sense to provide post-self-insert-hook in Emacs core and then break it in another Emacs core component(cc-mode). At least the behaviour of cc-mode should not be the default one...

joaotavora commented 5 years ago

That's right, you should submit an emacs bug report stating just that.

rileyrg commented 4 years ago

Just a "beep": It is broken in PHP-MODE in emacs 27 too.

yyoncho commented 4 years ago

I reported the issue in emacs and it was fixed https://debbugs.gnu.org/cgi/bugreport.cgi?bug=38406 - I am yet to test with ToT whether it works.