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

buttons/messages/views: Add support for copying code snippets. #1353

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

yuktasarode
Copy link
Collaborator

@yuktasarode yuktasarode commented Mar 24, 2023

What does this PR do?
This PR aims to add support for copying code snippets and is based off on the work done by @kingjuno and @neiljp in the PR #1134. This PR mainly fixes the tests and linting issues and bring the PR to a merge-able state.

Tested?

  • Manually
  • Existing tests (adapted, if necessary)
  • New tests added (for any new behavior)
  • Passed linting & tests (each commit)

This may fix the core elements of #1123.

@zulipbot zulipbot added the size: XL [Automatic label added by zulipbot] label Mar 24, 2023
Copy link
Collaborator

@neiljp neiljp left a comment

Choose a reason for hiding this comment

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

@yuktasarode Thanks for tidying this up further! 👍

It's good to see this passing linting and tests :) It's also working well still, though I've not examined the functionality in great detail yet.

I've left some comments inline, but you may want to first rebase against main, since I recently merged a PR regarding how the 'Action' keys are shown and grouped, which affects this UI.

My note in the other PR (#1134 (comment)) mention a few other changes; I'm not sure if you're already looking at those aspects, but I've left some details below.

I'd suggest the next step to make this easier to work on and read would be to split this into multiple commits. You'll want to confirm how the code links together yourself, but my take would be that we're generating additional data via the message, which will need to go into a button, both of which are combined into the end result - so 3 commits might be a good starting point.

Moving the behavior into the CodeSnippetButton may be another good step, or at least inheriting via urwid.Button like the MessageLinkButton does in the first instance, since I believe it should achieve most of what the custom button does already?

My note in the other PR (#1134 (comment)) also notes regarding adding tests for each commit/change; if you wish to start on that, that's fine, but I expect it'll be a lot easier to keep track of when the commit size is smaller, and you can separate which code change/addition is related to which test change/addition.

Comment on lines 1692 to 1691
urwid.connect_signal(button, "click", self.copy_to_clipboard, copy_code)
display_attr = None if index % 2 else "popup_contrast"
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be cleaner if we could have this kind of behavior, and that in a few other methods below, defined in the CodeSnippetButton itself instead, like we do with other buttons - I can't see a reason not to do this.

Note how the create_link_button function is simpler compared to this one.

The CodeSnippetButton does very little that's unique right now - it's very close to being a very simple Button.

@@ -647,6 +647,40 @@ def handle_narrow_link(self) -> None:
self.controller.exit_popup()


class CodeSnippetButton(urwid.Text):
Copy link
Collaborator

Choose a reason for hiding this comment

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

As mentioned elsewhere, this works, but it would be good to migrate to a button with the specific functionality we want.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did trying inherting urwid.Button and Moving the behavior into the CodeSnippetButton but I could only move the method get_code_from_snippet() from views.py to buttons.py, since all other methods require the instance of controller to be taken into account. Kindly let me know anyother way to do this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right now, all the basic Button functionality currently in CodeSnippetButton except for get_code_from_snippet is as per the default in urwid.Button (you can compare yourself, to wimp.py in urwid).

Look at the initializer for MessageLinkButton - that is how we can set up a controller via the button. We may also want to store the code content (snippet information) in there, for example.

Right now you're using the class name to call a method in a class, and explicitly passing the self value. Normally that'd be a bad idea and often cause an error, but you're not using self in get_code_from_snippet here! Instead, we should be creating CodeSnippetButtons with the relevant data, then using their properties as we need them.

I understand you've inherited this PR, but this (and the UI, using urwid) is using OOP in Python, so you may need to learn more about this if you've not worked on this before.

@neiljp neiljp added missing feature: user A missing feature for all users, present in another Zulip client PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback labels Mar 25, 2023
@yuktasarode yuktasarode force-pushed the review-1134 branch 2 times, most recently from 86b195c to e94f337 Compare April 4, 2023 06:35
@neiljp
Copy link
Collaborator

neiljp commented Apr 4, 2023

@yuktasarode Is this ready for review? If so, please change the labels as per my message in the GSoC 2023 topic in zulip-terminal (ie. remove needing update and add needs review).

@yuktasarode
Copy link
Collaborator Author

@zulipbot add "PR needs review"

@zulipbot zulipbot added the PR needs review PR requires feedback to proceed label Apr 6, 2023
@yuktasarode
Copy link
Collaborator Author

@zulipbot remove "PR awaiting update"

@zulipbot zulipbot removed the PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback label Apr 6, 2023
@neiljp
Copy link
Collaborator

neiljp commented Apr 8, 2023

@yuktasarode Could you please rebase this after the recent changes we had in main, and further explore how you might simplify and initialize the snippet button like we do with the MessageLinkButton? (given my comment above)

You're welcome to take up another issue also, of course 👍

@neiljp neiljp added PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback and removed PR needs review PR requires feedback to proceed labels Apr 12, 2023
Made the button inhert urwid.button and added supporting methods for creating the code snippet button.
Inherited urwid.button class in create_code_snippet_buttons method.
Get the code snippets and copy it on button press.
Tests added for create_code_snippet_buttons.
Copy link
Collaborator

@neiljp neiljp left a comment

Choose a reason for hiding this comment

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

@yuktasarode You didn't note this for review, but I saw you had pushed. This looks much closer than before 🎉

Comment on lines +1682 to +1684
display_code, copy_code = CodeSnippetButton.get_code_from_snippet(
self, snippet_list
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Generally the button vs view separation looks much better, but this stands out. The button should be able to calculate this internally :)

Can you move the initialization of the button up here? It gets the display and copy_code, but that is associated with code related to the rendering. So you should be able to pass in the language/snippet and have a button function provide the data you need externally. It should be able to set the caption itself :)

super().__init__("")

self.update_widget(display_code, display_attr)
urwid.connect_signal(self, "click", self.copy_to_clipboard)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This makes it work for clicks, but not for the keypress. See eg. the StreamButton.

Comment on lines +678 to +680
# Set cursor position next to len(caption) to avoid the cursor.
icon = urwid.SelectableIcon(display_code, cursor_position=len(display_code) + 1)
self._w = urwid.AttrMap(icon, display_attr, focus_map="selected")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't appear to be working for this button. I see a flashing cursor on the button while testing.

@neiljp neiljp added PR replaced by another PR and removed PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback labels Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
missing feature: user A missing feature for all users, present in another Zulip client PR replaced by another PR size: XL [Automatic label added by zulipbot]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants