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

feat(conversation): enable copying of code blocks from messages #174

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AAClause
Copy link
Member

@AAClause AAClause commented Aug 30, 2024

Summary by CodeRabbit

  • New Features
    • Introduced functionality to copy code blocks from messages.
    • Added options in the context menu for copying the current code block and all code blocks in a message.
    • Implemented keyboard shortcuts for copying actions.

@AAClause AAClause added this to the v0.1a6 milestone Aug 30, 2024
@AAClause AAClause marked this pull request as ready for review October 5, 2024 22:56
Copy link
Contributor

coderabbitai bot commented Oct 5, 2024

Walkthrough

The changes in this pull request enhance the ConversationTab class within the basilisk/gui/conversation_tab.py file by introducing functionality for copying code blocks from messages. Two new methods, on_copy_current_code_block and on_copy_message_code_blocks, are added to facilitate copying the currently selected code block and all code blocks in a message, respectively. The context menu and keyboard shortcuts are updated to include these new actions, and an import statement is added for handling cursor position adjustments.

Changes

File Change Summary
basilisk/gui/conversation_tab.py - Added methods on_copy_current_code_block and on_copy_message_code_blocks for copying code blocks.
- Updated context menu to include new copy options.
- Modified on_messages_key_down to add key bindings for the new actions.
- Added import for adjust_utf16_position.

Possibly related PRs

  • feat: paste file from the clipboard  #129: The changes in this PR also modify the ConversationTab class in basilisk/gui/conversation_tab.py, adding new methods for clipboard interactions, which aligns with the main PR's focus on enhancing clipboard functionality for copying code blocks.

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 08062eb and c20c107.

📒 Files selected for processing (1)
  • basilisk/gui/conversation_tab.py (4 hunks)

Comment on lines +632 to +658
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))

Copy link
Contributor

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

Comment on lines +638 to +640
code_blocks = re.finditer(
r"```(?:\S+)?\n(.*?)\n```", content, re.DOTALL
)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

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)
Copy link
Contributor

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)
Copy link
Contributor

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.

Suggested change
log.debug(code_blocks)

@AAClause AAClause marked this pull request as draft October 6, 2024 09:19
@AAClause AAClause modified the milestones: v0.1a6, v0.1a7 Oct 6, 2024
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.

1 participant