smartparens icon indicating copy to clipboard operation
smartparens copied to clipboard

C++ template angle brackets not supported <>

Open jgarvin opened this issue 9 years ago • 2 comments

So obviously C++ has syntactic ambiguities that means support will be imperfect, but it would be nice to be able to at least highlight a region and press < to enclose it in angle brackets.

For determining if <> should be considered a comparsion, shift, or an opener/closer I think this is a decent heuristic based on common style:

  • If there isn't a space on either side, assume it's a template opener, so "a<b" "a <b" and "a< b" have an opener but "a < b" does not. Same for "a<<b" "a<< b" and "a <<b", etc. In modern C++ code usually spaces on both sides indicates a comparison but on one side probably means templates.
  • Any instance of multiple space separated "<" or ">" s is a template opener/closer. When space separated it can't be bit shift, and with no non-space characters between it can't be a comparison.
  • Every "<" or ">"on a line only containing only instances of that character is an opener/closer. Comparators usually appear on the same line as at least one of their arguments.

Additionally can infer closers/openers based on the following being invalid syntax otherwise (optionally may have spaces between parts):

  • ::

  • ,

  • ;

  • <>
  • <typename
  • <class
  • template<

jgarvin avatar May 11 '15 22:05 jgarvin

Well, this should all be possible and some code could probably be recycled from the ruby setup.

(Aditionally I'm currently working on a simple parser library that could be used to determine these situations, I plan to integrate it into SP somehow.)

That being said, I'm not going to implement this myself. I don't use C++ and so I would (rather selfishly) concentrate on things that bring benefit to me :)

If you come up with something I'm happy to merge and give you push rights so you can maintain the C++ support.

Fuco1 avatar May 12 '15 15:05 Fuco1

@jgarvin if you're still interested on working this, I've recently had to solve essentially the same problem in rust-mode. Take a look at smartparens-rust.el and test/smartparens-rust-test.el for examples.

Wilfred avatar Dec 11 '15 11:12 Wilfred