-
-
Notifications
You must be signed in to change notification settings - Fork 252
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 support for copying code snippets #1455
base: main
Are you sure you want to change the base?
Conversation
e806d56
to
df786e5
Compare
d7a1b65
to
44b035b
Compare
I removed the Should I reintroduce the test with platform-specific expected code widths, or is it acceptable to leave it removed? |
@zulipbot add "PR needs review" |
2b8be1f
to
a012674
Compare
45201c8
to
fee1ed8
Compare
802ab9c
to
e7929db
Compare
Updated this PR :) |
ERROR: Label "PR needs review" already exists and was thus not added to this pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rsashank Thanks for taking this forward 👍
I know this was a followup on previous work, so some of my comments aren't necessarily directed at your approach.
That said, some commits likely benefit from co-author labelling, which you've handled previously.
I'm not sure where the 'Code snippet' term came from originally, but I wonder if we would be better served by using Code block
instead?
For now we may want to hold off on the last commit since
- Viewing Actions generally applies to the message itself, not one of the selected buttons
- We don't have copy for other actions
- This will be cleaner once we have Sushmey's category headings in place
I'd like us to remember to do some refactoring of the chunks of metadata we're passing around everywhere, that we'll add to here ;) Doing that first would simplify this PR, but would also delay it.
zulipterminal/ui_tools/buttons.py
Outdated
def copy_to_clipboard(self, *_: Any) -> None: | ||
self.controller.copy_to_clipboard(self.copy_code, "Code") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you changed this compared to the previous PR? ("code" -> "Code"?)
I think it would be clearer to change the clipboard method, since it's using the string passed as output.
If a message has multiple code blocks, it may also be useful to indicate which one is copied!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated this method to get copy_code only on demand as suggested in another comment. I've also made it indicate which code block has been copied.
Is that fine?
zulipterminal/ui_tools/buttons.py
Outdated
def get_code_from_snippet( | ||
self, snippet_list: List[Tuple[str, str]] | ||
) -> Tuple[List[Tuple[str, str]], str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following on some other comments, this function may not be necessary in the same way, but:
See my general comment re the naming we've ended up with here, but in regard to this method:
- This method returns two things, which the method name does not suggest is the case
- The returned items are described in the comment; fine details are good there, but the name should describe what it does as much as possible :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've revised this method to focus solely on extracting display_code
from snippet_list
.
zulipterminal/ui_tools/views.py
Outdated
display_code, copy_code = CodeSnippetButton.get_code_from_snippet( | ||
self, snippet_list | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There doesn't appear to be a change here compared to the previous PR, where I made a comment. I'll add some different notes here since I'm looking at it afresh.
Passing in a self
here which is completely different from the other self
(a different class & hierarchy) is quite a 'code smell' that something is going oddly. It's working OK since the method doesn't even use self
! We're using the function before we initialize a button to have an appropriate self
, if we needed one.
The other aspect is that we're using this method to get the raw code (copy_code
), and then only passing it into the button initializer - which the button could calculate itself once it has the full 'code snippet' data, and only on demand if it is copied.
Similarly, if the button had the code snippet data, it could provide the display_code, or even just the widths - so this logic could be retained inside the button itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it! That makes sense 👍
I've updated the copy_code to only be extracted from snippet_list on demand. Also updated this function to extract display_code from snippet_list once the Button is initialized.
zulipterminal/ui_tools/messages.py
Outdated
( | ||
content, | ||
self.message_links, | ||
self.code_snippets, | ||
self.time_mentions, | ||
) = self.transform_content(self.message["content"], self.model.server_url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be good to see some tests for what ends up in self.code_snippets
, though we don't seem to have tests for the others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm yet to work on adding these tests and was wondering if it would be alright moving this to a separate PR? I'll also include tests for the others as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test for self.code_snippets
. I'll add tests for the others in a separate PR 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made a PR for tests for the others :)
zulipterminal/ui_tools/messages.py
Outdated
if code_language != "Text only": | ||
metadata["code_snippets"].append((code_language, code_snippet)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have an example of this? There are no tests or comments to explain this.
Also, what happens for this when data-code-language
is absent?
I've seen cases where it looks like no language tag was applied and the code is not highlighted (what language should we show?)
However, also cases where the code is highlighted, and the source message has a language tag - but the current code also thinks there is no language present. This suggests another field was used previously?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible the language data was not available, or was tagged elsewhere, in a previous version of the Zulip markup. This was hinted at in zulip/zulip#11618.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you put text
after the 3 backticks (```), it causes the code_language to be "Text only"
. It seems they intended to avoid including that as a code snippet for some reason.
I'm looking into where the language data was previously tagged.
a7d74f6
to
7e27991
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, Sashank!
Ran it, and it works fine on Linux.
I noticed a few changes suggested by Neil, that are pending.
- Renaming: 'code blocks' instead of 'code snippets'. Which I noticed you've changed in the footer text, but other occurrences remain.
- Adding attributions
- The ambiguity about the languages part
Follow-up PR: metadata refactor
zulipterminal/ui_tools/views.py
Outdated
len(max(code_snippet_widgets[index].display_code, key=len)), | ||
) | ||
|
||
code = caption + str(snip[1] for snip in snippet_list) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: snip? Could a more descriptive variable name be used instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestions in mind? :)
a7d3191
to
4268b0b
Compare
Thanks for the detailed feedback @Niloth-p! I've updated the PR. I still need to figure out where the language data was tagged in the previous version of Zulip markup, but I believe everything else should be addressed now. :) |
59194bd
to
33bfce1
Compare
b5e97e4
to
dfb2e39
Compare
Adjusted clipboard_text by replacing \r\n with \n to accommodate carriage returns in WSL. Ensures consistent newline handling for comparison. This prevents unnecessary "text does not match" warnings for WSL users when copying texts containing newline characters.
This class inherits from urwid.Button and enables the creation of code block buttons that copy code to the clipboard when activated. Co-authored-by: kingjuno <[email protected]> Co-authored-by: yuktasarode <[email protected]>
Updated existing tests. New test added. Co-authored-by: kingjuno <[email protected]> Co-authored-by: yuktasarode <[email protected]>
c46598b
to
964c17a
Compare
Introduces support for copying code blocks in the message information popup. Makes use of the CodeBlockButton class and its methods. Tests added. Co-authored-by: kingjuno <[email protected]> Co-authored-by: yuktasarode <[email protected]> Fixes zulip#1123.
964c17a
to
d9cf3b8
Compare
What does this PR do, and why?
Introduces the ability to copy code snippets, building upon the groundwork laid by @yuktasarode, @kingjuno, and @neiljp in previous pull requests: #1134 #1353.
This PR incorporates feedback from earlier pull requests. It introduces the
k
keybinding for copying code from a Message Information Popup. This PR also resolves the previous issue of a flashing cursor on the button.Additionally, it includes a minor refactoring in copy_to_clipboard to address return carriage differences in WSL.
External discussion & connections
Copy code snippets #T1353 #T1134 (fix #T1123)
How did you test this?
Self-review checklist for each commit
CopyCodeSnippet.mp4