smartparens icon indicating copy to clipboard operation
smartparens copied to clipboard

spaces in parentheses

Open Pitometsu opened this issue 9 years ago • 11 comments

Feature request


To solve this question in right way, it should be implemented as part of smartparens (most adequate parentheses-drive mode).

Propose next behaviour:

when smartparens-space-in-parens
action result
example (example-string|)
<space> => ( example-string |)
<backspace> => (example-string|)

Pitometsu avatar Jun 28 '16 02:06 Pitometsu

Any updates here? :scream_cat:

Pitometsu avatar Sep 18 '17 15:09 Pitometsu

There's already implemented similar logic in https://github.com/Fuco1/smartparens/blob/6ce2f7af6d28391cc9ce7325bf1797b8b71a7910/smartparens-latex.el#L56

Pitometsu avatar Sep 18 '17 15:09 Pitometsu

I don't know, there seems to be a lot of edge cases. For example in lisp you can type (foo which would result into ( foo |)... but maybe I just want to write (foo bar) (no leading space). Having to hit backspace would be annoying.

I think this would have to be implemented on a case-by-case basis as per major-mode, and even better maybe as a set of functions that users can use in their own config (so they wouldn't need to reimplement all the logic).

However it is quite simple to do so. If you specify this as a post-handler it will do what you want ("| " "SPC").

Here's my config for java-style docstrings

(sp-local-pair 'php-mode "/**" "*/" :post-handlers '(("| " "SPC")
                                                     (my-php-handle-docstring "RET")))

The "string syntax" for specifying these hooks is described here: https://github.com/Fuco1/smartparens/wiki/Permissions#insertion-specification

If this is universal enough for C++ we can set it up for C++, but I wouldn't do it as a global thing.

Fuco1 avatar Sep 18 '17 17:09 Fuco1

You right here, agree: spaces would be convenient only just after the first paren. Updated behaviour proposal here:

action result
( => (|)
<space> => (_|_)
<space> => (__|__)
<backspace> => (_|_)
word => (_word|_)
<space> => (_word_|_)
<backspace> => (_word|_)
<backspace> x4 => (_|_)
<backspace> => (|)

Pitometsu avatar Sep 18 '17 21:09 Pitometsu

For sure, there's should be some flag to enable/disable such behaviour (not sure about per-mode).

Pitometsu avatar Sep 18 '17 21:09 Pitometsu

It should work even for already exist pairs with content:

action result
(|example) => (|example)
<space> => (_|example_)
<space> => (__|example__)
<backspace> x2 => (|example)

Pitometsu avatar Sep 18 '17 21:09 Pitometsu

I implemented a load of my own code to achieve this. Although I'd quite happily delete my hard work to have it supported by smartparens!

The logic I ended up with was:

  • SPACE after open paren also inserts one before the matching end paren.

  • SPACE before closing paren does not insert one after matching open paren.

    • this avoids problems where you've typed (foo |) on your way to typing (foo bar)
  • DELETE either space (after open paren, or before close paren) removes one from the matching paren, so long as there are not fewer spaces there.

It took some experimentation to get this logic right, but I can report that it works well.

edam avatar Oct 23 '18 11:10 edam

@edam Do you think your code is in state that could be contributed to smartparens? I would be happy to add a feature like this.

Obviosuly we would need to make it a bit configurable for people to at least have a way to disable it, so there would be some extra work required.

Fuco1 avatar Oct 23 '18 12:10 Fuco1

@Fuco1 it is currently implemented in terms of a loadable extension, but I would be happy to rework it in to a branch. I could do with cleaning up a few things anyway.

edam avatar Oct 14 '19 12:10 edam

@edam Maybe we can ship it as an additional config that can be required or enabled by some flag. Feel free to put the code somewhere and we can have a look!

Fuco1 avatar Oct 15 '19 17:10 Fuco1

@edam is the code available somewhere, in your config or as a package / repo?

Fuco1 avatar Mar 23 '24 10:03 Fuco1