-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat(conversation): enable copying of code blocks from messages #174
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThe changes in this pull request enhance the Changes
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
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.
def on_copy_current_code_block(self, event: wx.CommandEvent = None): | ||
cursor_pos = self.messages.GetInsertionPoint() | ||
self.message_segment_manager.absolute_position = cursor_pos | ||
start = self.message_segment_manager.start | ||
end = self.message_segment_manager.end | ||
content = self.messages.GetRange(start, end) | ||
code_blocks = re.finditer( | ||
r"```(?:\S+)?\n(.*?)\n```", content, re.DOTALL | ||
) | ||
log.debug(code_blocks) | ||
code_block = None | ||
adjusted_cursor_pos = adjust_utf16_position( | ||
content, cursor_pos, True | ||
) - adjust_utf16_position(content, start, True) | ||
log.debug(f"Adjusted Cursor position: {adjusted_cursor_pos}") | ||
for block in code_blocks: | ||
log.debug(f"Block: {block.start()} - {block.end()}") | ||
if block.start() <= adjusted_cursor_pos <= block.end(): | ||
code_block = block.group(1) | ||
log.debug(f"Code block: {code_block}") | ||
break | ||
if not code_block: | ||
wx.Bell() | ||
return | ||
with wx.TheClipboard as clipboard: | ||
clipboard.SetData(wx.TextDataObject(code_block)) | ||
|
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.
🛠️ Refactor suggestion
Consider refactoring to eliminate code duplication
The methods on_copy_current_code_block
and on_copy_message_code_blocks
share similar code for retrieving code blocks from the current message. To improve maintainability and reduce code duplication, consider refactoring the shared logic into a helper method.
Also applies to: 659-674
code_blocks = re.finditer( | ||
r"```(?:\S+)?\n(.*?)\n```", content, re.DOTALL | ||
) |
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.
Adjust the regex pattern to correctly match code blocks
The current regex pattern used to find code blocks may fail to match code blocks that don't have newlines immediately after the opening triple backticks or before the closing triple backticks. To handle code blocks more robustly, consider adjusting the regex pattern to allow optional whitespace.
Apply this diff to adjust the regex:
- code_blocks = re.finditer(
- r"```(?:\S+)?\n(.*?)\n```", content, re.DOTALL
- )
+ code_blocks = re.finditer(
+ r"```(?:\S+)?\s*\n(.*?)\s*```", content, re.DOTALL
+ )
Also applies to: 665-667
code_blocks = re.finditer( | ||
r"```(?:\S+)?\n(.*?)\n```", content, re.DOTALL | ||
) | ||
code_block = "\n".join(block.group(1) for block in code_blocks) |
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.
🧹 Nitpick (assertive)
Rename variable for clarity
The variable code_block
holds the concatenated content of all code blocks in the message. Renaming it to code_blocks_content
could improve code readability.
code_blocks = re.finditer( | ||
r"```(?:\S+)?\n(.*?)\n```", content, re.DOTALL | ||
) | ||
log.debug(code_blocks) |
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.
🧹 Nitpick (assertive)
Remove unnecessary logging of the iterator
The debug statement log.debug(code_blocks)
will log the iterator object, which may not provide meaningful information. Consider removing this statement or adjusting it to log useful details.
Apply this diff to remove the unnecessary debug statement:
- log.debug(code_blocks)
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
log.debug(code_blocks) |
Summary by CodeRabbit