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

[python console][feature] smart brackets #51391

Merged
merged 18 commits into from
Jan 10, 2023

Conversation

YoannQDQ
Copy link
Contributor

@YoannQDQ YoannQDQ commented Jan 7, 2023

Motivation

The QGIS Python Console has an autoCloseBracket setting, which automatically insert a closing parentheses / brace / bracket when an opening char is entered.

  • This option does not have a consistent behavior if the closing character is entered.
  • If the user enters an opening character, then backspace, the auto-inserted closing character still remains just after the cursor.
  • It is only available in the Python Console Script Editor, and the Python Console prompt
  • Finally, this option does not handle single and double quotes, and does not allow to enclose the selected text with parentheses / quotes / braces...

Proposal

Tweak autoCloseBracket behavior to mimic the behavior of modern IDEs
Add an autoSurround setting to surround selected text when typing quotes/parentheses/brackets.
This is implemented in the QgsCodeEditorPython, so it also works in Expression Editor, Project Macro Editor, Script Editor, etc.

console_settings

Peek 2023-01-07 10-44

project_macro

When some text was selected (and autoSurround is on)

  • When an opening character is entered, the selected text is enclosed in the opening/closing pair. Selection remains the same, so it is possible to quote a selected word and enclose it in parentheses just by typing " then (
    • Special case for multiline selection with quotes and double quotes: selection is enclosed in triple single/double quotes.

When no text is selected (and autoCloseBracket is on)

  • When no text is selected, if an opening character is entered, insert the matching closing character just after the cursor. Note that this behavior is disabled if the current cursor is inside a string or comment
  • When the backspace key is pressed, if cursor is directly enclosed in an opening/closing pair, remove both characters
  • When a closing character is pressed, if cursor is just before the same closing character, just move the cursor and do not insert a duplicated character

Pairs of characters:

  • [ and ]
  • { and }
  • [ and ]
  • " and "
  • ' and '

Additionaly, for the characters ` (backtick) and *, only the enclose selected text feature is enabled

@github-actions github-actions bot added the Python Console Python Console label Jan 7, 2023
@github-actions github-actions bot added this to the 3.30.0 milestone Jan 7, 2023
@nyalldawson
Copy link
Collaborator

Nice work @YoannQDQ -- this autocompletion has been needing some love!

Can you please restore the option to enable/disable this behaviour though? It's not universally liked and it's important to allow some way to disable it (eg I personally always disable bracket autocompletion in editors!)

@YoannQDQ
Copy link
Contributor Author

YoannQDQ commented Jan 7, 2023

Sure, I'll add it back. Enclosing selected text with quotes/parenthesis/brackets should still be always on, right?

Any thought on what should be the default value for the autoCloseBracket and the autoInsertionImport settings? Currently it's False and True. I was enclined to set them to True and False, since I'm quite annoyed with the latter and I (obviously) like the former, but you may disagree. It may be best to set both to False by default.

@m-kuhn
Copy link
Member

m-kuhn commented Jan 8, 2023

Thanks for that.

+1 for disabling autoInsertImport, it's really annoying.
+0 for enabling autoCloseBracket by default

@nyalldawson
Copy link
Collaborator

Currently it's False and True. I was enclined to set them to True and False

This sounds good to me. I think defaulting to bracket autocompletion is fine, so long as there's a way to disable this. That matches common IDE practice then (at least this is how Pycharm/other Jetbrains products/QtCreator default)

@YoannQDQ
Copy link
Contributor Author

YoannQDQ commented Jan 9, 2023

Ok, I believe it is now ready to be merged!

src/gui/codeeditors/qgscodeeditorpython.cpp Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditorpython.cpp Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditorpython.cpp Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditorpython.cpp Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditorpython.cpp Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditorpython.cpp Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditorpython.h Outdated Show resolved Hide resolved
@nyalldawson nyalldawson added Needs Documentation When merging a labeled PR, an issue will be created in the Doc repo. Changelog Items that are queued to appear in the visual changelog - remove after harvesting labels Jan 10, 2023
@nyalldawson nyalldawson merged commit 74c6048 into qgis:master Jan 10, 2023
@github-actions
Copy link

@YoannQDQ
This pull request has been tagged as requiring documentation.

A documentation ticket will be opened at https://github.com/qgis/QGIS-Documentation when this PR is merged.

Please update the description (not the comments) with helpful description and screenshot to help the work from documentors.
Also, any commit having [needs-doc] or [Needs Documentation] in will see its message pushed to the issue, so please be as verbose as you can.

Thank you!

@github-actions
Copy link

@YoannQDQ
A documentation ticket has been opened at qgis/QGIS-Documentation#7949
It is your responsibility to visit this ticket and add as much detail as possible for the documentation team to correctly document this change.
Thank you!

@YoannQDQ YoannQDQ deleted the python-console-smart-brackets branch January 11, 2023 13:04
@zacharlie zacharlie added ChangelogHarvested This PR description has been harvested in the Changelog already. and removed Changelog Items that are queued to appear in the visual changelog - remove after harvesting labels Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ChangelogHarvested This PR description has been harvested in the Changelog already. Needs Documentation When merging a labeled PR, an issue will be created in the Doc repo. Python Console Python Console
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants