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

Add encapsulate with parentheses (quotes, brackets, braces) function #3414

Closed
ZelphirKaltstahl opened this issue Sep 12, 2016 · 7 comments
Closed

Comments

@ZelphirKaltstahl
Copy link

Encapsulating parentheses and others

I want to be able to highlight code and press the button for some beginning character like the opening parenthesis, so that the whole highlighted text is surrounded by an opening parenthesis on the left and a closing parenthesis on the right, as an example. Many editors support this kind of behavior and I have gotten used to being able to do this.

What steps will reproduce the problem?

  1. highlight some code
  2. press opening parenthesis key

What is the expected output? What do you see instead?

  • The text should be surrounded by parenthesis.
  • The text was replaced.

Versions and main components

  • Spyder Version: 2.3.9 (in an Anaconda env for Python 3.5)
  • Python Version: Python 3.5
  • Operating system: Fedora 22

Dependencies

Please go to the menu entry Help > Optional Dependencies (or
Help > Dependencies), press the button Copy to clipboard
and paste the contents below:

IPython >=1.0 : 5.1.0 (OK)
jedi >=0.8.1;<0.9.0: 0.9.0 (NOK)
matplotlib >=1.0 : 1.5.1 (OK)
pandas >=0.13.1 : 0.18.1 (OK)
pep8 >=0.6 : 1.7.0 (OK)
psutil >=0.3 : 4.3.1 (OK)
pyflakes >=0.6.0 : 1.3.0 (OK)
pygments >=1.6 : 2.1.3 (OK)
pylint >=0.25 : None (NOK)
qtconsole >=4.0 : 4.2.1 (OK)
rope >=0.9.2 : 0.9.4-1 (OK)
sphinx >=0.6.6 : 1.4.1 (OK)
sympy >=0.7.3 : None (NOK)
zmq >=2.1.11 : 15.4.0 (OK)

@ccordoba12
Copy link
Member

Good idea. It shouldn't be that hard to implement :-)

@ccordoba12 ccordoba12 added this to the v3.1 milestone Sep 12, 2016
@ccordoba12 ccordoba12 modified the milestones: v3.1, v3.2 Oct 15, 2016
@ccordoba12 ccordoba12 modified the milestones: v3.2, v3.3 Feb 14, 2017
@Diego-MX
Copy link

Diego-MX commented Aug 7, 2017

If I may add something to this request, also enclose selection with brackets, curlies, and quotes, when the corresponding key is pressed.

@ccordoba12 ccordoba12 modified the milestones: v4.0beta2, v4.0beta3 Aug 13, 2017
@h4gen
Copy link

h4gen commented Jul 30, 2018

Would like to implement this if someone points me to the right direction in the code.

@jitseniesen
Copy link
Member

I would start by studying

elif text == '(' and not has_selection:

That block automatically inserts the closing parenthesis when you type the opening parenthesis in foo(.

Good luck!

@bcolsen
Copy link
Member

bcolsen commented Jul 30, 2018

@h4gen Spyder already has quote insertion for selected text in master. If you select text and press the ' or " key the selection will get quoted.

This functionality is now implemented as the first example of an editor extension:

https://github.com/spyder-ide/spyder/blob/master/spyder/widgets/sourcecode/extensions/closequotes.py

This section does the part you want to do with brackets:

if self.editor.has_selected_text():
text = self.editor.get_selected_text()
self.editor.insert_text("{0}{1}{0}".format(char, text))
# keep text selected, for inserting multiple quotes
cursor.movePosition(QTextCursor.Left, QTextCursor.MoveAnchor, 1)
cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor,
len(text))
self.editor.setTextCursor(cursor)

I think you will want to write an editor extension similar to closequotes extension. See this PR for more info.

#5002

Sounds like a fun project!

@ccordoba12 ccordoba12 modified the milestones: v4.0betaX, Not sorted Oct 11, 2018
@ccordoba12 ccordoba12 modified the milestones: not sorted, v4.0beta2 Jan 21, 2019
@bcolsen bcolsen self-assigned this Jan 24, 2019
@bcolsen
Copy link
Member

bcolsen commented Jan 24, 2019

@ccordoba12 Hope you didn't have anyone particular in mind to fix this one because I had some time today so I wrote a close bracket extension that fixes this.

@ccordoba12
Copy link
Member

No, not at all. Thanks for your help in solving this one!

@CAM-Gerlach CAM-Gerlach changed the title add encapsulate with parentheses (quotes, brackets, braces) function Add encapsulate with parentheses (quotes, brackets, braces) function Jan 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants