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

PR: Add Editor extensions #5002

Merged
merged 24 commits into from
Aug 28, 2017
Merged

Conversation

rlaverde
Copy link
Member

@rlaverde rlaverde commented Aug 18, 2017

Fixes: #877


See #4987 (comment)
I use #877 as example of an editor extension

They idea is to create an extension in spyder/widgets/sourcecode/extensions/ with a class that Inherits o EditorExtension (api.editor.extension), create custom methods and connect them in the on_uninstall method, and the add them to the Codeeditor.editor_extensions

class MultiCursorsEditorFeature(EditorFeature):
     def _on_key_pressed():
          ....
     def _on_mouse_released():

     def `on_uninstall():
           self._editor.key_pressed.connect(self._on_key_pressed)
           self._editor.sig_mouse_pressed.connect(self._on_mouse_pressed)

class CodeEditor():

    def __init__(self):
         editor_extensions = EditorExtensionsManager
         editor_extensions.register(MultiCursorEditorsFeature)

There is already some signals for key press, and paint event, I also added a signal for mouse pressed. Maybe there is missing a signal for timerEvent.

ping @jnsebgosselin

@rlaverde rlaverde self-assigned this Aug 18, 2017
@pep8speaks
Copy link

pep8speaks commented Aug 18, 2017

Hello @rlaverde! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on August 23, 2017 at 16:01 Hours UTC

@@ -247,6 +249,8 @@ class CodeEditor(TextEditBaseWidget):
#: Signal emitted when a key is pressed
key_pressed = Signal(QKeyEvent)

sig_mouse_pressed = Signal(QMouseEvent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem necessary as part of this PR, right?

@ccordoba12
Copy link
Member

I really like this, it's very neat! I just have one suggestion: could we add as part of this PR an EditorExtension that involves some kind of text manipulation in CodeEditor? It can be something very simple, nothing fancy.

That way it'd be easier to understand how to use extensions for the rest of us ;-)

@rlaverde
Copy link
Member Author

@ccordoba12 This is ready, I moved close quote logic to an editor extension, and write some tests, although I didn't found any errors with that code.

Should I enable close quote by default?

return ''


class QuoteEditorExtension(EditorExtension):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this CloseQuoteExtension

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, CloseQuotesExtension

@ccordoba12
Copy link
Member

although I didn't found any errors with that code.

I think issue #877 has several examples. Did you look at it?

Should I enable close quote by default?

If tests pass, why not? ;-)

@ccordoba12 ccordoba12 added this to the v4.0beta1 milestone Aug 22, 2017

def __init__(self, editor):
"""Initialize and add a reference to the editor."""
super(EditorExtensionsManager, self).__init__(editor)
self._extensions = {}

def append(self, extension):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add/remove are the antonyms, not append/remove

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think append is more list-pythonic, should I change it for add?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is, but append even your docsting uses ADD and not APPEND, so you can see why append is not really a good choice, independent of the structure used to store extensions (dict, list, set or whatever). So either we append/prepend or add/remove, insert/pop, but not a mix of those. So yes, please change to add

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@rlaverde rlaverde Aug 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't see your response because It was collapsed, I'm going to change it for add

"""
This module contains the close quotes editor extension
"""
"""This module contains the close quotes editor extension."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just use extensions folder? like spyder/widgets/sourcecode/extensions/closequotes.py

@ccordoba12
Copy link
Member

I agree with @goanpeca to use only the sourcecode/extensions for this directory instead of sourcecode/editorextensions.

@rlaverde
Copy link
Member Author

rlaverde commented Aug 23, 2017

I agree with @goanpeca to use only the sourcecode/extensions for this directory instead of sourcecode/editorextensions.

Yes, me too (at first I thought that editorextensions was more specific, anyway they are into the sourcecode folder, or the plugins/editor, after split-plugins, so that long name is redundant)

@rlaverde
Copy link
Member Author

I think issue #877 has several examples. Did you look at it?

I implement #877 (comment) although I change the behavior, It'll just keep the text selected allowing to add as many quotes as you like.

The parenthesis are managed by other logic, so integrate it with this extension could break some of their functionality

Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terrific job @rlaverde! I really like this!

@ccordoba12
Copy link
Member

I implement #877 (comment) although I change the behavior, It'll just keep the text selected allowing to add as many quotes as you like.

Ok, no problem. If more issues appear in the future, now we can fix and add tests for this quite easily.

@ccordoba12 ccordoba12 changed the title PR: Editor extensions PR: Add Editor extensions Aug 28, 2017
@ccordoba12 ccordoba12 merged commit 70a874f into spyder-ide:master Aug 28, 2017
@rlaverde rlaverde deleted the editor-extensions branch August 29, 2017 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants